From edceafd88d35638f4ee1791cac17652cc47fd25d Mon Sep 17 00:00:00 2001 From: baoliang Date: Sat, 26 Sep 2020 13:47:36 +0800 Subject: [PATCH] add message --- polls/models.py | 50 +++++++++--------- polls/urls.py | 1 + polls/utils.py | 19 ------- polls/views/__init__.py | 3 +- .../views/__pycache__/__init__.cpython-38.pyc | Bin 992 -> 1037 bytes .../views/__pycache__/message.cpython-38.pyc | Bin 0 -> 1070 bytes polls/views/__pycache__/task.cpython-38.pyc | Bin 2504 -> 2502 bytes polls/views/message.py | 25 +++++++++ polls/views/task.py | 2 +- 9 files changed, 55 insertions(+), 45 deletions(-) create mode 100644 polls/views/__pycache__/message.cpython-38.pyc create mode 100644 polls/views/message.py diff --git a/polls/models.py b/polls/models.py index 4b0abb3..ba9e4a3 100644 --- a/polls/models.py +++ b/polls/models.py @@ -127,41 +127,43 @@ class TaskRecord(models.Model): updated = models.DateTimeField(auto_now=True) +MESSAGE_TYPE_CHOICES = ( + (0, 'text'), + (1, 'url'), + (2, 'file'), + (3, 'image') +) + + class Message(models.Model): id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) send_from = models.ForeignKey(User, on_delete=models.CASCADE) send_to = models.ForeignKey(Group, on_delete=models.CASCADE) task = models.ForeignKey(Task, on_delete=models.CASCADE) + category = models.IntegerField( + 'Type', choices=MESSAGE_TYPE_CHOICES, default=1) + #text + content = models.TextField('内容', null=True, blank=True) + #url + page_title = models.CharField('page_title', max_length=256, null=True, blank=True) + page_description = models.CharField('page_description', max_length=512, null=True, blank=True) + page_image = models.CharField('page_image', max_length=256, null=True, blank=True) + url = models.CharField('url', max_length=256, null=True, blank=True) + + #file + file_title = models.CharField('file_title', max_length=256, null=True, blank=True) + file = models.CharField('file', max_length=256, null=True, blank=True) + + #image + image_title = models.CharField('image_title', max_length=256, null=True, blank=True) + picture = models.CharField('picture', max_length=256, null=True, blank=True) + added = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) class Meta: ordering = ["-added"] - def __str__(self): - return self.send_from + ':' + self.send_to - - -class NormalMessage(Message): - content = models.TextField('内容', null=True, blank=True) - - -class URLMessage(Message): - title = models.CharField('title', max_length=256, null=False) - description = models.CharField('description', max_length=512, null=False) - image = models.CharField('image', max_length=256, null=True, blank=True) - url = models.CharField('url', max_length=256, null=True, blank=True) - - -class FileMessage(Message): - title = models.CharField('title', max_length=256, null=False) - file = models.CharField('file', max_length=256, null=False) - - -class ImageMessage(Message): - title = models.CharField('title', max_length=256, null=False) - picture = models.CharField('picture', max_length=256, null=False) - class GroupRecord(models.Model): id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) diff --git a/polls/urls.py b/polls/urls.py index 1090cb4..1679a8e 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -32,4 +32,5 @@ urlpatterns = [ path('groups/test-room/', views.room, name='polls_group_test_room'), path('compartments/list/', views.compartments, name='polls_compartments_list'), path('organizations/list/', views.organizations, name='polls_organizations_list'), + path('messages/list/last/', views.last_messages, name='polls_messages_list_last'), ] diff --git a/polls/utils.py b/polls/utils.py index 792a453..00b388e 100644 --- a/polls/utils.py +++ b/polls/utils.py @@ -6,7 +6,6 @@ from parsel import Selector from channels.db import database_sync_to_async from .exceptions import ClientError from django.conf import settings -from polls.models import FileMessage, ImageMessage, NormalMessage, URLMessage from itertools import chain @@ -96,24 +95,6 @@ def get_room_or_error(room_id, user): raise ClientError("USER_HAS_TO_LOGIN") -@database_sync_to_async -def build_message(type, user_id, group_id, task_id, payload): - if type == 1: - URLMessage.objects.create(send_from__id=user_id, send_to__id=group_id, - task__id=task_id, title=payload.title, description=payload.description, url=payload.url, image=payload.image) - - elif type == 1: - FileMessage.objects.create(send_from__id=user_id, send_to__id=group_id, - task__id=task_id, title=payload.title, file=payload.file) - - elif type == 2: - ImageMessage.objects.create(send_from__id=user_id, send_to__id=group_id, - task__id=task_id, title=payload.title, image=payload.image) - - else: - NormalMessage.objects.create(send_from__id=user_id, send_to__id=group_id, - category=0, task__id=task_id, content=payload.content) - def model_to_dict(instance, fields): opts = instance._meta diff --git a/polls/views/__init__.py b/polls/views/__init__.py index 7542549..a2f4202 100644 --- a/polls/views/__init__.py +++ b/polls/views/__init__.py @@ -6,4 +6,5 @@ from .monitor import monitor_statistics from .task import tasks, create_task, create_test_task from .group import groups, room from .compartment import compartments -from .organizations import organizations \ No newline at end of file +from .organizations import organizations +from .message import last_messages \ No newline at end of file diff --git a/polls/views/__pycache__/__init__.cpython-38.pyc b/polls/views/__pycache__/__init__.cpython-38.pyc index 007f6e2edf5781066afb31df0fa6f03a0fd7d637..8c474842a5fe388168a7e564408f1130daad5362 100644 GIT binary patch delta 124 zcmaFB-pj!o%FD~e00cD`^Wq~X^2#!vnW%k0kRgRDM>SV1N{x{rg*%u*lV{_Hd5j{O zjJJ4m5{paXb5o0p6Vp?R{WR4lPh>hTev2I{QX~&FY9&LF29PS!n4HO+EG7hG>M%+$ Q$}oyB@&K^_kmO+m0CsyGk^lez delta 79 zcmeC>c)-pZ%FD~e00c+na^lS<^2#zEn5cb#l`EJ*lY8Tzd5p|{nyQo6F`egJ$xx&Q dR92)mc?xr~hyakO!zjTh!zjYY1H=MA5&#ur5WN5Z diff --git a/polls/views/__pycache__/message.cpython-38.pyc b/polls/views/__pycache__/message.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a0213b45d07e4b1c4082aad69df5f91516e860f8 GIT binary patch literal 1070 zcmYjQJ8u**5VrSydwUO}2vN`h#WlAPbrd0y077s$JLn#k`w!TRvn7?`1vS&-#3j4KShs z?U0CczM~ah;cQ5}Lo|y2LRK`wCXlouRE`{zywSQopsE&?q9%E(M0F>DU$;>5bV@HN zuk{4v%~~*~rcBJrYAWgZqLh??eq;Y+P7A#&sG119cUdYua!vT2DwWTu=}m;>IlB|% z-moEld$nf z^wS|75??t0Sa)K)5BrBl%YEcx>_{fn7 z^k71Mi^41L^;CXJM*|aZs?P+GFe1o!3q_?ISk`^LcVyfd)iP>BMGLDk!D-1fmBz1S zS!v^vQW}qx1+Yp>6XbObMMw+YC^Vsc843n_r|j9AqID6<8r6bnWw5YgVEJE2J^`nH zlk^xWQRzu8SeaL|w3fm?t4g15J>N>XI4K!TD|*2xDRVBWQVXaD{OnYuUrKtR(siP7 z{nhkXuJrUSa~D#mM?jDp;}}Oc!F}wtX8>G+?p_O#NV7hDd z84PR-T-&aByjHi_$AlI_=1`Uz+tHQ2jf_aHsV+G+!?x0UUNW!_57&1~7lbm^zKiUDUfPe|mfV7Ok36+kM%xguV06b0xY_`c;2|^_ISJ5B*TD|{ E4<*|tT>t<8 literal 0 HcmV?d00001 diff --git a/polls/views/__pycache__/task.cpython-38.pyc b/polls/views/__pycache__/task.cpython-38.pyc index 490b041cee509716918476d0bc87a3ed118bce30..6cfdf279abcb8e1be56dfa5f15c3362b5fb54dd5 100644 GIT binary patch delta 50 zcmX>hd`y@(l$V!_0SMk-$ctCp$a{xXjHfs?FC{*$C_gtoGvyW+m{*d&nUU={GXRkp B5as{? delta 52 zcmX>md_tHvl$V!_0SNvd&x=>y$a{xXg10y|FC{*$C_gtoJ~QPOH<(+JAHVrO>v3iP Dx!@A6 diff --git a/polls/views/message.py b/polls/views/message.py new file mode 100644 index 0000000..5aab346 --- /dev/null +++ b/polls/views/message.py @@ -0,0 +1,25 @@ +from django.shortcuts import render +from django.http import HttpResponse, JsonResponse +from django.views.decorators.csrf import csrf_exempt +import datetime + +from polls.decorators import polls_login_required +from django.core.exceptions import ObjectDoesNotExist +from polls.models import Message, NormalMessage, URLMessage + + +@csrf_exempt +@polls_login_required +def last_messages(request): + if request.method == 'POST': + return HttpResponse(status=405) + id = request.user.id + group_id = request.GET.get('group_id') + messages = Message.objects.filter(send_to_id=group_id) + results = [] + for o in messages: + print(dir(o)) + result = dict() + result['id'] = str(o.id) + results.append(result) + return JsonResponse({'status': 'success', 'message': results}, safe=False) diff --git a/polls/views/task.py b/polls/views/task.py index 3748705..97e650e 100644 --- a/polls/views/task.py +++ b/polls/views/task.py @@ -63,7 +63,7 @@ def create_task(request): task=task, category=2, image=picture) for group in groups: - normalMessage = NormalMessage.objects.create(send_from__id=user.id, send_to__id=group, task=task, content=content) + normalMessage = NormalMessage.objects.create(send_from_id=user.id, send_to_id=group, task=task, content=content) # t = model_to_dict(task, ["id", "content"]) # gs = queryset_to_list(task.groups.all(), ["id"]) # channel_layer = get_channel_layer()