newmediamonitoring/polls/views/notice.py

111 lines
3.8 KiB
Python
Raw Normal View History

2020-09-03 04:03:19 +00:00
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
2020-09-24 06:51:39 +00:00
from django.apps import apps
2020-09-03 04:03:19 +00:00
import datetime
from polls.decorators import polls_login_required
from polls.models import Notice
2020-09-03 09:46:04 +00:00
from django.core.exceptions import ObjectDoesNotExist
2020-09-03 04:03:19 +00:00
2020-10-04 15:09:07 +00:00
2020-09-03 04:03:19 +00:00
@csrf_exempt
@polls_login_required
def notices(request):
2020-09-24 06:51:39 +00:00
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
2020-10-04 15:09:07 +00:00
notices = Notice.objects.filter(user__id=id, is_read=False)
2020-09-24 06:51:39 +00:00
results = []
for o in notices:
result = dict()
result['id'] = o.id
result['type'] = o.type
result['content'] = o.content
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result)
2020-10-04 15:09:07 +00:00
return JsonResponse({'status': 'success', 'message': results}, safe=False)
2020-09-24 06:51:39 +00:00
@csrf_exempt
@polls_login_required
def notice_top(request):
2020-09-03 04:03:19 +00:00
if request.method == 'POST':
return HttpResponse(status=405)
id = request.user.id
2020-10-04 15:09:07 +00:00
notices = Notice.objects.filter(user__id=id, is_read=False)[:10]
2020-09-03 04:03:19 +00:00
results = []
for o in notices:
result = dict()
result['id'] = o.id
2020-09-24 06:51:39 +00:00
result['type'] = o.type
2020-09-03 09:46:04 +00:00
result['content'] = o.content
2020-09-03 04:03:19 +00:00
result['added'] = o.added.strftime("%Y-%m-%d %H:%M:%S")
results.append(result)
2020-10-04 15:09:07 +00:00
return JsonResponse({'status': 'success', 'message': results}, safe=False)
2020-09-03 09:46:04 +00:00
@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'})
2020-09-24 06:51:39 +00:00
except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})
2020-09-30 08:02:29 +00:00
@csrf_exempt
@polls_login_required
def detail_notice(request, notice_id):
if request.method == 'GET':
return HttpResponse(status=405)
notice_id = request.POST.get('notice')
if not notice_id:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})
try:
notice = Notice.objects.get(id=notice_id)
results = dict()
results['id'] = notice.id
2020-10-04 15:09:07 +00:00
results['category'] = notice.type
results['content'] = notice.content
results['app'] = notice.app
results['model'] = notice.model
results['field'] = notice.field
results['record_id'] = notice.record_id
results['record_pass_value'] = notice.record_pass_value
results['record_reject_value'] = notice.record_reject_value
results['added'] = notice.added.strftime("%Y-%m-%d %H:%M:%S")
result['updated'] = notice.updated.strftime("%Y-%m-%d %H:%M:%S")
2020-09-30 08:02:29 +00:00
return JsonResponse({'status': 'success', 'message': results})
except ObjectDoesNotExist:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})
2020-09-24 06:51:39 +00:00
@csrf_exempt
@polls_login_required
def reply_notice(request):
if request.method == 'GET':
return HttpResponse(status=405)
id = request.user.id
notice_id = request.POST.get('notice_id')
if not notice_id:
return JsonResponse({'status': 'error', 'message': '通知ID错误'})
try:
notice = Notice.objects.get(id=notice_id)
ReplyModel = apps.get_model(notice.app, notice.model)
m = ReplyModel.objects.get(pk=notice.record_id)
2020-09-26 01:47:24 +00:00
setattr(m, notice.field, notice.record_value)
m.save(update_fields=[notice.field])
2020-09-24 06:51:39 +00:00
return JsonResponse({'status': 'success'})
2020-09-03 09:46:04 +00:00
except ObjectDoesNotExist:
2020-10-04 15:09:07 +00:00
return JsonResponse({'status': 'error', 'message': '通知ID错误'})