newmediamonitoring/polls/views/message.py

59 lines
2.1 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
from dashboard.models import 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)
results = []
for o in messages:
result = dict()
result['id'] = str(o.id)
result['type'] = o.type
profile = Userprofile.objects.filter(user_id=o.send_from_id).first()
result['user_name'] = profile.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'})