add message

This commit is contained in:
baoliang 2020-09-26 13:47:36 +08:00
parent 3a5fb97c43
commit edceafd88d
9 changed files with 55 additions and 45 deletions

View File

@ -127,41 +127,43 @@ class TaskRecord(models.Model):
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
MESSAGE_TYPE_CHOICES = (
(0, 'text'),
(1, 'url'),
(2, 'file'),
(3, 'image')
)
class Message(models.Model): class Message(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
send_from = models.ForeignKey(User, on_delete=models.CASCADE) send_from = models.ForeignKey(User, on_delete=models.CASCADE)
send_to = models.ForeignKey(Group, on_delete=models.CASCADE) send_to = models.ForeignKey(Group, on_delete=models.CASCADE)
task = models.ForeignKey(Task, 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) added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
class Meta: class Meta:
ordering = ["-added"] 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): class GroupRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)

View File

@ -32,4 +32,5 @@ urlpatterns = [
path('groups/test-room/', views.room, name='polls_group_test_room'), path('groups/test-room/', views.room, name='polls_group_test_room'),
path('compartments/list/', views.compartments, name='polls_compartments_list'), path('compartments/list/', views.compartments, name='polls_compartments_list'),
path('organizations/list/', views.organizations, name='polls_organizations_list'), path('organizations/list/', views.organizations, name='polls_organizations_list'),
path('messages/list/last/', views.last_messages, name='polls_messages_list_last'),
] ]

View File

@ -6,7 +6,6 @@ from parsel import Selector
from channels.db import database_sync_to_async from channels.db import database_sync_to_async
from .exceptions import ClientError from .exceptions import ClientError
from django.conf import settings from django.conf import settings
from polls.models import FileMessage, ImageMessage, NormalMessage, URLMessage
from itertools import chain from itertools import chain
@ -96,24 +95,6 @@ def get_room_or_error(room_id, user):
raise ClientError("USER_HAS_TO_LOGIN") 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): def model_to_dict(instance, fields):
opts = instance._meta opts = instance._meta

View File

@ -6,4 +6,5 @@ from .monitor import monitor_statistics
from .task import tasks, create_task, create_test_task from .task import tasks, create_task, create_test_task
from .group import groups, room from .group import groups, room
from .compartment import compartments from .compartment import compartments
from .organizations import organizations from .organizations import organizations
from .message import last_messages

Binary file not shown.

25
polls/views/message.py Normal file
View File

@ -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)

View File

@ -63,7 +63,7 @@ def create_task(request):
task=task, category=2, image=picture) task=task, category=2, image=picture)
for group in groups: 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"]) # t = model_to_dict(task, ["id", "content"])
# gs = queryset_to_list(task.groups.all(), ["id"]) # gs = queryset_to_list(task.groups.all(), ["id"])
# channel_layer = get_channel_layer() # channel_layer = get_channel_layer()