newmediamonitoring1/polls/views/message.py

107 lines
3.8 KiB
Python

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, Task, TaskRecord
from dashboard.models import Group_user, Userprofile
@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)[:100]
results = []
filter_dict = dict()
for m in messages:
if m.type == 4 or m.type == 1:
task_id = m.task_id
if task_id:
if task_id in filter_dict:
c = filter_dict[task_id]
for o in messages:
result = dict()
t = o.type
if t == 4:
continue
result['id'] = str(o.id)
result['type'] = t
if t == 1:
t_id = o.task_id
task = Task.objects.get(id=t_id)
result['title'] = task.content
profile = Userprofile.objects.filter(user_id=o.send_from_id).first()
result['user_name'] = profile.name
result['organization'] = profile.organization.name
result['content'] = o.content
result['page_title'] = o.page_title
result['page_description'] = o.page_description
result['page_image'] = o.page_image
result['url'] = o.url
result['file_title'] = o.file_title
result['file'] = request.build_absolute_uri(
o.file.uri) if o.file else ''
result['image_title'] = o.image_title
result['picture'] = request.build_absolute_uri(
o.picture.uri) if o.picture else ''
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@polls_login_required
def send_text_message(request):
if request.method == 'GET':
return HttpResponse(status=405)
user = request.user
group_id = request.POST.get('group_id')
if not group_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
content = request.POST.get('content')
if not content:
return JsonResponse({'status': 'error', 'message': '内容不能为空'})
normalMessage = Message.objects.create(
type=0, send_from_id=user.id, send_to_id=group_id, content=content)
return JsonResponse({'status': 'success'})
@csrf_exempt
@polls_login_required
def is_read_message(request):
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
message_id = request.GET.get('message_id')
if not message_id:
return JsonResponse({'status': 'success', 'message': False}, safe=False)
is_exists = TaskRecord.objects.filter(
task__message__id=message_id, user_id=id).exists()
return JsonResponse({'status': 'success', 'message': is_exists}, safe=False)
@csrf_exempt
@polls_login_required
def read_message(request):
if request.method == 'GET':
return HttpResponse(status=405)
user = request.user
message_id = request.POST.get('message_id')
print(message_id)
if not message_id:
return JsonResponse({'status': 'success', 'message': False}, safe=False)
message = Message.objects.get(id=message_id)
group_id = message.send_to_id
is_exists = Group_user.objects.filter(group_id=group_id, user_id=user.id).exists()
if is_exists:
obj, created = TaskRecord.objects.get_or_create(user=user, task=message.task)
return JsonResponse({'status': 'success', 'message': True}, safe=False)