newmediamonitoring1/polls/views/notice.py

42 lines
1.3 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 polls.models import Notice
from django.core.exceptions import ObjectDoesNotExist
@csrf_exempt
@polls_login_required
def notices(request):
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
notices = Notice.objects.filter(user__id=id,is_read=False)[:10]
results = []
for o in notices:
result = dict()
result['id'] = o.id
result['content'] = o.content
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result)
return JsonResponse(results, safe=False)
@csrf_exempt
@polls_login_required
def read_notice(request):
if request.method == 'GET':
return HttpResponse(status=405)
id = request.user.id
notice_id = request.POST.get('notice')
if not notice_id:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})
try:
notice = Notice.objects.get(id=notice_id)
notice.is_read = True
notice.save()
return JsonResponse({'status': 'success'})
except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})