add message

This commit is contained in:
baoliang 2020-10-11 01:42:12 +08:00
parent 07188d510a
commit 0150bf4f8d
4 changed files with 19 additions and 11 deletions

View File

@ -98,6 +98,17 @@ class Task(models.Model):
def record(self): def record(self):
return TaskRecord.objects.filter(task=self).count() return TaskRecord.objects.filter(task=self).count()
def add_task_message(self, user_id):
for group in self.groups.all():
Message.objects.create(
type=4, send_from_id=user_id, send_to_id=group.id, task=self, content=self.content)
def add_url_message(self, user_id, title, description, url, image):
for group in self.groups.all():
Message.objects.create(
type=1, send_from_id=user_id, send_to_id=group.id, task=self, page_title=title, page_description=description, page_image=image, url=url)
class Meta: class Meta:
ordering = ["-added"] ordering = ["-added"]
@ -180,7 +191,7 @@ class Message(models.Model):
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
class Meta: class Meta:
ordering = ["-added"] ordering = ["added"]
class GroupRecord(models.Model): class GroupRecord(models.Model):

View File

@ -9,7 +9,7 @@ from polls.decorators import polls_login_required
from polls.models import Message, Notice, Task, TaskAddition from polls.models import Message, Notice, Task, TaskAddition
from polls.tasks import process_task from polls.tasks import process_task
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from polls.utils import model_to_dict, queryset_to_list from polls.utils import model_to_dict, queryset_to_list, parse
@csrf_exempt @csrf_exempt
@ -47,27 +47,24 @@ def create_task(request):
groups = request.POST.getlist('groups', []) groups = request.POST.getlist('groups', [])
task = Task.objects.create(created_by=user, content=content) task = Task.objects.create(created_by=user, content=content)
task.add_groups(groups) task.add_groups(groups)
task.add_task_message(user.id)
url = request.POST.get('url') url = request.POST.get('url')
print(groups, content, url) # print(groups, content, url)
file = request.FILES.get('file') file = request.FILES.get('file')
picture = request.FILES.get('picture') picture = request.FILES.get('picture')
if url: if url:
urlAddtion = TaskAddition.objects.create( urlAddtion = TaskAddition.objects.create(
task=task, category=0, url=url) task=task, category=0, url=url)
og_title, og_description, og_url, og_image = parse(url)
print(og_title, og_description, og_url, og_image)
task.add_url_message(
user.id, og_title, og_description, og_url, og_image)
if file: if file:
fileAddtion = TaskAddition.objects.create( fileAddtion = TaskAddition.objects.create(
task=task, category=1, file=file) task=task, category=1, file=file)
if picture: if picture:
pictureAddtion = TaskAddition.objects.create( pictureAddtion = TaskAddition.objects.create(
task=task, category=2, image=picture) task=task, category=2, image=picture)
for group in groups:
normalMessage = Message.objects.create(
type=4, send_from_id=user.id, send_to_id=group, task=task, content=content)
content = '用户%s发布了任务,请查收消息' % (profile.name,)
Notice.create_normal_notice(user.id, content)
return JsonResponse({'status': 'success'}) return JsonResponse({'status': 'success'})