refactor message

This commit is contained in:
baoliang 2020-09-18 14:03:50 +08:00
parent c440f1f509
commit 4c9c1c6eeb
3 changed files with 110 additions and 23 deletions

View File

@ -37,7 +37,7 @@ class Notice(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
content = models.CharField('内容', max_length=256, null=False)
is_read = models.BooleanField('是否删除', default=False)
is_read = models.BooleanField('是否阅读', default=False)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
@ -52,6 +52,62 @@ class Notice(models.Model):
return self.content + ':' + self.user.username
class Task(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
created_by = models.ForeignKey(User, on_delete=models.NULL)
groups = models.ManyToManyField(Group)
content = models.TextField('内容', null=True, blank=True)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
ordering = ["-added"]
class TaskAddition(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
category = models.IntegerField(
'type', choices=MESSAGE_TYPE_CHOICES, default=0)
url = models.CharField('url', max_length=256, null=True, blank=True)
file = models.FileField(
upload_to='task/file/%Y/%m/%d/', null=True, blank=True)
picture = models.ImageField(
upload_to='task/image/%Y/%m/%d/', null=True, blank=True)
TASK_RECORD_STATUS_CHOICES = (
(0, '未完成'),
(1, '完成'),
)
class TaskRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.IntegerField(
'状态', choices=TASK_RECORD_STATUS_CHOICES, default=0)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
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)
content = models.TextField('内容', null=True, blank=True)
task = models.ForeignKey(Task, on_delete=models.CASCADE)
addition = models.ForeignKey(MessageAddition, on_delete=models.CASCADE)
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
MESSAGE_TYPE_CHOICES = (
(0, 'url'),
(1, 'file'),
@ -59,44 +115,38 @@ MESSAGE_TYPE_CHOICES = (
)
class Message(models.Model):
class MessageAddition(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
category = models.IntegerField(
'category', choices=MESSAGE_TYPE_CHOICES, default=0)
send_from = models.ForeignKey(User, on_delete=models.CASCADE)
send_to = models.ForeignKey(Group, on_delete=models.CASCADE)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
message = models.ForeignKey(Message, on_delete=models.CASCADE)
class Meta:
abstract = True
ordering = ["-added"]
def __str__(self):
return self.send_from + ':' + self.send_to
class URLMessage(Message):
class URLMessage(MessageAddition):
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):
class FileMessage(MessageAddition):
title = models.CharField('title', max_length=256, null=False)
file = models.FileField(
upload_to='message/file/%Y/%m/%d/', null=True, blank=True)
file = models.CharField('file', max_length=256, null=False)
class ImageMessage(Message):
file = models.FileField(
upload_to='resources/image/%Y/%m/%d/', null=True, blank=True)
class ImageMessage(MessageAddition):
title = models.CharField('title', max_length=256, null=False)
picture = models.CharField('picture', max_length=256, null=False)
class MessageRecord(models.Model):
pass
class ChatRecord(models.Model):
pass
class GroupRecord(models.Model):
id = models.UUIDField('id', primary_key=True, default=uuid.uuid4)
user = models.ForeignKey(User, on_delete=models.CASCADE)
group = models.ForeignKey(Group, on_delete=models.CASCADE)
enter_at = models.DateTimeField(auto_now=True)
leave_at = models.DateTimeField(auto_now=True)
added = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

View File

@ -94,6 +94,13 @@ def get_room_or_error(room_id, user):
raise ClientError("USER_HAS_TO_LOGIN")
def model_to_dict(o, fields):
result = dict()
for f in fields:
result[f] = o.f
return result
if __name__ == '__main__':
# sent_sms_code('13993199566')
og_title, og_description, og_url, og_image = parse(

30
polls/views/task.py Normal file
View File

@ -0,0 +1,30 @@
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.forms.models import model_to_dict
import datetime
from polls.decorators import polls_login_required
from polls.models import Task
from django.core.exceptions import ObjectDoesNotExist
@csrf_exempt
@polls_login_required
def tasks(request):
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
tasks = Task.objects.filter(created_by__id=id)
results = []
for o in tasks:
result = dict()
result['id'] = o.id
m = []
for g in o.groups:
n = model_to_dict(g, ['id', 'name'])
m.append(n)
result['groups'] = m
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result)
return JsonResponse(results, safe=False)