newmediamonitoring/polls/views/message.py

92 lines
3.3 KiB
Python
Raw Normal View History

2020-09-26 05:47:36 +00:00
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
2020-10-10 14:44:15 +00:00
from polls.models import Message, TaskRecord
2020-12-05 04:46:36 +00:00
from dashboard.models import Group_user, Userprofile
2020-09-26 05:47:36 +00:00
@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')
2020-10-17 16:00:23 +00:00
messages = Message.objects.filter(send_to_id=group_id)[:100]
2020-09-26 05:47:36 +00:00
results = []
for o in messages:
result = dict()
result['id'] = str(o.id)
2020-09-26 07:34:36 +00:00
result['type'] = o.type
2020-09-26 08:19:27 +00:00
profile = Userprofile.objects.filter(user_id=o.send_from_id).first()
result['user_name'] = profile.name
2020-10-22 13:52:48 +00:00
result['organization'] = profile.organization.name
2020-09-26 07:34:36 +00:00
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
2020-09-26 08:19:27 +00:00
result['file'] = request.build_absolute_uri(
o.file.uri) if o.file else ''
2020-09-26 07:34:36 +00:00
result['image_title'] = o.image_title
2020-09-26 08:19:27 +00:00
result['picture'] = request.build_absolute_uri(
o.picture.uri) if o.picture else ''
2020-09-26 07:34:36 +00:00
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
2020-09-26 05:47:36 +00:00
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
2020-09-26 07:34:36 +00:00
@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'})
2020-10-10 14:44:15 +00:00
@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).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')
if not message_id:
return JsonResponse({'status': 'success', 'message': False}, safe=False)
message = Message.objects.get(id=message_id)
2020-12-05 04:46:36 +00:00
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)
2020-10-10 14:44:15 +00:00
return JsonResponse({'status': 'success', 'message': True}, safe=False)