from django.shortcuts import render from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from django.apps import apps import datetime from polls.decorators import polls_login_required from polls.models import Notice from django.core.exceptions import ObjectDoesNotExist from dashboard.models import Userprofile @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) results = [] for o in notices: result = dict() result['id'] = o.id result['type'] = o.type result['content'] = o.content result['isRead'] = o.is_read 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 notice_top(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['type'] = o.type result['content'] = o.content result['isRead'] = o.is_read 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 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错误'}) @csrf_exempt @polls_login_required def detail_notice(request, notice_id): if request.method == 'POST': return HttpResponse(status=405) print(notice_id, (not notice_id)) if not notice_id: return JsonResponse({'status': 'error', 'message': '通知ID错误'}) try: notice = Notice.objects.get(id=notice_id) results = dict() results['id'] = notice.id results['category'] = notice.type results['content'] = notice.content if notice.type == 1: ReplyModel = apps.get_model(notice.app, notice.model) m = ReplyModel.objects.get(pk=notice.record_id) detail = dict() detail['id'] = m.id detail['code'] = m.code if notice.model == 'Weixin': detail['type'] = 'weixin' elif notice.model == 'Weibo': detail['type'] = 'weibo' elif notice.model == 'Toutiao': detail['type'] = 'Toutiao' elif notice.model == 'Douyin': detail['type'] = 'douyin' else: detail['type'] = 'qita' detail['status'] = m.status results['media'] = detail else: results['groupId'] = notice.group_id results['added'] = notice.added.strftime("%Y-%m-%d %H:%M:%S") results['updated'] = notice.updated.strftime("%Y-%m-%d %H:%M:%S") notice.is_read = True notice.save() return JsonResponse({'status': 'success', 'message': results}) except ObjectDoesNotExist: return JsonResponse({'status': 'error', 'message': '通知ID错误'}) @csrf_exempt @polls_login_required def pass_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) setattr(m, notice.field, notice.record_pass_value) m.save(update_fields=[notice.field]) notice.is_read = True notice.save() pass_value = notice.record_pass_value if pass_value == '2': admins = Userprofile.level1_admin() for a in admins: Notice.create_reply_notice(a.user.id, notice.content, notice.app, notice.model, notice.field, notice.record_id, '3', '0') if pass_value == '5': admins = Userprofile.level1_admin() for a in admins: Notice.create_reply_notice(a.user.id, notice.content, notice.app, notice.model, notice.field, notice.record_id, '0', '3') return JsonResponse({'status': 'success', 'message': '申请已同意'}) except ObjectDoesNotExist: return JsonResponse({'status': 'error', 'message': '通知ID错误'}) @csrf_exempt @polls_login_required def reject_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) setattr(m, notice.field, notice.record_reject_value) m.save(update_fields=[notice.field]) notice.is_read = True notice.save() return JsonResponse({'status': 'success', 'message': '申请已拒绝'}) except ObjectDoesNotExist: return JsonResponse({'status': 'error', 'message': '通知ID错误'}) @csrf_exempt @polls_login_required def unread_notice_count(request): if request.method == 'POST': return HttpResponse(status=405) user_id = request.user.id count = Notice.objects.filter(user_id=user_id, is_read=False).count() return JsonResponse({'status': 'success', 'message': count})