from polls.utils import send_tnps from django.http import HttpResponse, JsonResponse from django.views.decorators.csrf import csrf_exempt from django.core.paginator import Paginator from datetime import datetime from dashboard.models import Douyin, NewMedia, Organization, Qita, Toutiao, Userprofile, Weibo, Weixin from polls.decorators import polls_login_required from polls.models import Notice from django.contrib.auth import get_user_model @csrf_exempt @polls_login_required def medias(request): if request.method == 'POST': return HttpResponse(status=405) organization_id = request.GET.get('organization_id') new_medias = NewMedia.media_list(organization_id) return JsonResponse({'status': 'success', 'message': new_medias}, safe=False) @csrf_exempt @polls_login_required def media_admins(request): if request.method == 'POST': return HttpResponse(status=405) organization_id = request.GET.get('organization_id') profiles = Userprofile.objects.filter( organization_id=organization_id).exclude(admin_status=0) results = [] for p in profiles: result = dict() result['id'] = p.user.id result['name'] = p.name result['phone'] = p.user.username result['adminStatus'] = p.admin_status results.append(result) return JsonResponse({'status': 'success', 'message': results}, safe=False) @csrf_exempt @polls_login_required def my_medias(request): if request.method == 'POST': return HttpResponse(status=405) user = request.user profile = user.userprofile_set.first() organization_id = profile.organization.id new_medias = NewMedia.media_list(organization_id) return JsonResponse({'status': 'success', 'message': new_medias}, safe=False) @csrf_exempt @polls_login_required def my_media_admins(request): if request.method == 'POST': return HttpResponse(status=405) user = request.user profile = user.userprofile_set.first() organization_id = profile.organization.id profiles = Userprofile.objects.filter( organization_id=organization_id).exclude(admin_status=0) results = [] for p in profiles: result = dict() result['id'] = p.user.id result['name'] = p.name result['phone'] = p.user.username result['adminStatus'] = p.admin_status results.append(result) return JsonResponse({'status': 'success', 'message': results}, safe=False) @csrf_exempt @polls_login_required def create_media(request): profile = request.user.userprofile_set.first() o = profile.organization if request.method == 'GET': return HttpResponse(status=405) f = request.FILES.get('file') organization_id = request.POST.get('organization_id', o.id) organization = Organization.objects.get(id=organization_id) category = request.POST.get('category') if not category: return JsonResponse({'status': 'error', 'message': '类型错误'}) code = request.POST.get('code') if not code: return JsonResponse({'status': 'error', 'message': '名称不能为空'}) media_id = request.POST.get('media_id') if not media_id: return JsonResponse({'status': 'error', 'message': '监测ID不能为空'}) alias = request.POST.get('alias') if not alias: return JsonResponse({'status': 'error', 'message': '别名不能为空'}) attention = request.POST.get('attention', '') remark = request.POST.get('remark', '') model_name = '' status = 2 if organization.is_direct() else 1 if category == 'weixin': model_name = 'Weixin' instance = Weixin(code=code, weixinid=media_id, alias=alias, image=f, status=status, organization_id=organization_id, attention=attention, remark=remark) elif category == 'weibo': model_name = 'Weibo' instance = Weibo(code=code, weiboid=media_id, alias=alias, image=f, status=status, organization_id=organization_id, attention=attention, remark=remark) elif category == 'toutiao': model_name = 'Toutiao' instance = Toutiao(code=code, toutiaoid=media_id, alias=alias, image=f, status=status, organization_id=organization_id, attention=attention, remark=remark) elif category == 'douyin': model_name = 'Douyin' instance = Douyin(code=code, douyinid=media_id, alias=alias, image=f, status=status, organization_id=organization_id, attention=attention, remark=remark) else: model_name = 'Qita' type = request.POST.get('type') instance = Qita(code=code, qitaid=media_id, alias=alias, image=f, status=status, organization_id=organization_id, attention=attention, remark=remark) instance.save() if organization.is_direct(): admins = Userprofile.level1_admin() content = '%s创建了新媒体,请审核' % (profile,) phones = [] for a in admins: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', model_name, 'status', instance.id, '3', '0') send_tnps(phones, '审核', content) else: admins = Userprofile.level2_admin(instance.organization.cities) content = '%s创建了新媒体,请审核' % (profile,) phones = [] for a in admins: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', model_name, 'status', instance.id, '2', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': organization.name, 'organizationId': organization_id}}, safe=False) @csrf_exempt @polls_login_required def update_media(request): if request.method == 'GET': return HttpResponse(status=405) category = request.POST.get('category') if not category: return JsonResponse({'status': 'error', 'message': '参数错误'}) media_id = request.POST.get('media_id') if not media_id: return JsonResponse({'status': 'error', 'message': '参数错误'}) name = request.POST.get('name') if not name: return JsonResponse({'status': 'error', 'message': '名称不能为空'}) value = request.POST.get('value') if not value: return JsonResponse({'status': 'error', 'message': '值不能为空'}) result = dict() if category == 'weixin': instance = Weixin.objects.get(pk=media_id) result['media_id'] = instance.weixinid result['type'] = '微信' elif category == 'weibo': instance = Weibo.objects.get(pk=media_id) result['media_id'] = instance.weiboid result['type'] = '微博' elif category == 'toutiao': instance = Toutiao.objects.get(pk=media_id) result['media_id'] = instance.toutiaoid result['type'] = '头条' elif category == 'douyin': instance = Douyin.objects.get(pk=media_id) result['media_id'] = instance.douyinid result['type'] = '抖音' else: instance = Qita.objects.get(pk=media_id) result['media_id'] = instance.qitaid result['type'] = instance.type setattr(instance, name, value) instance.save(update_fields=[name]) admins = [] for admin in instance.organization.userprofile_set.all(): u = dict() u['id'] = admin.user.id u['name'] = admin.name u['phone'] = admin.user.username u['adminStatus'] = admin.admin_status admins.append(u) result['id'] = instance.id result['code'] = instance.code result['alias'] = instance.alias result['status'] = instance.status result['attention'] = instance.attention result['remark'] = instance.remark result['organization'] = instance.organization.name result['admin'] = admins return JsonResponse({'status': 'success', 'message': result}) @csrf_exempt @polls_login_required def media_detail(request, type, media_id): result = dict() if type == 'weixin': media = Weixin.objects.get(pk=media_id) result['media_id'] = media.weixinid result['type'] = '微信', elif type == 'weibo': media = Weibo.objects.get(pk=media_id) result['media_id'] = media.weiboid result['type'] = '微博', elif type == 'toutiao': media = Toutiao.objects.get(pk=media_id) result['media_id'] = media.toutiaoid result['type'] = '头条', elif type == 'douyin': media = Douyin.objects.get(pk=media_id) result['media_id'] = media.douyinid result['type'] = '抖音', else: media = Qita.objects.get(pk=media_id) result['media_id'] = media.qitaid result['type'] = media.type, admins = [] for admin in media.organization.userprofile_set.exclude(admin_status=0): u = dict() u['id'] = admin.user.id u['name'] = admin.name u['phone'] = admin.user.username u['adminStatus'] = admin.admin_status admins.append(u) result['id'] = media.id result['code'] = media.code result['alias'] = media.alias result['status'] = media.status result['attention'] = media.attention result['remark'] = media.remark result['organization'] = media.organization.name result['admin'] = admins return JsonResponse({'status': 'success', 'message': result}, safe=False) @csrf_exempt @polls_login_required def delete_media(request): if request.method == 'GET': return HttpResponse(status=405) profile = request.user.userprofile_set.first() category = request.POST.get('category') if not category: return JsonResponse({'status': 'error', 'message': '参数错误'}) media_id = request.POST.get('media_id') if not media_id: return JsonResponse({'status': 'error', 'message': '参数错误'}) result = dict() if category == 'weixin': model_name = 'Weixin' instance = Weixin.objects.get(pk=media_id) result['media_id'] = instance.weixinid elif category == 'weibo': model_name = 'Weibo' instance = Weibo.objects.get(pk=media_id) result['media_id'] = instance.weiboid elif category == 'toutiao': model_name = 'Toutiao' instance = Toutiao.objects.get(pk=media_id) result['media_id'] = instance.toutiaoid elif category == 'douyin': model_name = 'Douyin' instance = Douyin.objects.get(pk=media_id) result['media_id'] = instance.douyinid else: model_name = 'Qita' instance = Qita.objects.get(pk=media_id) result['media_id'] = instance.qitaid if instance.organization.is_direct(): instance.status = 5 instance.save() managers = Userprofile.level1_admin() content = '%s申请注销新媒体,请审核' % (profile,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', model_name, 'status', instance.id, '0', '3') send_tnps(phones, '审核', content) else: instance.status = 4 instance.save() managers = Userprofile.level2_admin(instance.organization.cities) content = '%s申请注销新媒体,请审核' % (profile,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', model_name, 'status', instance.id, '5', '3') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': instance.organization.name, 'organizationId': instance.organization.id}}, safe=False) @csrf_exempt @polls_login_required def media_admin_change(request): if request.method == 'GET': return HttpResponse(status=405) id = request.POST.get('id') if not id: return JsonResponse({'status': 'error', 'message': '参数错误'}) name = request.POST.get('name') if not name: return JsonResponse({'status': 'error', 'message': '姓名不能为空'}) phone = request.POST.get('phone') if not phone: return JsonResponse({'status': 'error', 'message': '手机号不能为空'}) u = request.user operator = Userprofile.objects.filter(user=u).first() UserModel = get_user_model() changing_user = UserModel.objects.get(id=id) profile = changing_user.userprofile_set.first() o = profile.organization if o.is_direct(): # 如果是省直 if changing_user.username == phone: # 修改名字 profile.name = name profile.save() else: if UserModel.objects.filter(username=phone).exists(): return JsonResponse({'status': 'error', 'message': '手机号已存在'}) obj = UserModel.objects.create( username=phone, last_login=datetime.now()) p = Userprofile.objects.create( user=obj, name=name, organization=o, admin_status=2) profile.admin_status = 5 profile.save() managers = Userprofile.level1_admin() content = '%s申请变更管理员,请审核' % (operator,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '3', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}) else: if changing_user.username == phone: # 修改名字 profile.name = name profile.save() else: if UserModel.objects.filter(username=phone).exists(): return JsonResponse({'status': 'error', 'message': '手机号已存在'}) obj = UserModel.objects.create( username=phone, last_login=datetime.now()) p = Userprofile.objects.create( user=obj, name=name, organization=o, admin_status=1) profile.admin_status = 4 profile.save() managers = Userprofile.level2_admin(o.cities) content = '%s申请变更管理员,请审核' % (operator,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '2', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}) @csrf_exempt @polls_login_required def media_admin_add(request): if request.method == 'GET': return HttpResponse(status=405) organization_id = request.POST.get('organization_id') if not organization_id: return JsonResponse({'status': 'error', 'message': '参数错误'}) name = request.POST.get('name') if not name: return JsonResponse({'status': 'error', 'message': '姓名不能为空'}) phone = request.POST.get('phone') if not phone: return JsonResponse({'status': 'error', 'message': '手机号不能为空'}) u = request.user operator = Userprofile.objects.filter(user=u).first() o = Organization.objects.get(id=organization_id) UserModel = get_user_model() if o.is_direct(): # 如果是省直 if UserModel.objects.filter(username=phone).exists(): return JsonResponse({'status': 'error', 'message': '手机号已存在'}) obj = UserModel.objects.create( username=phone, last_login=datetime.now()) p = Userprofile.objects.create( user=obj, name=name, organization=o, admin_status=2) managers = Userprofile.level1_admin() content = '%s申请添加管理员,请审核' % (operator,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '3', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}) else: if UserModel.objects.filter(username=phone).exists(): return JsonResponse({'status': 'error', 'message': '手机号已存在'}) obj = UserModel.objects.create( username=phone, last_login=datetime.now()) p = Userprofile.objects.create( user=obj, name=name, organization=o, admin_status=1) managers = Userprofile.level2_admin(o.cities) content = '%s申请添加管理员,请审核' % (operator,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '2', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}) @csrf_exempt @polls_login_required def media_admin_delete(request): if request.method == 'GET': return HttpResponse(status=405) id = request.POST.get('id') if not id: return JsonResponse({'status': 'error', 'message': '参数错误'}) u = request.user operator = Userprofile.objects.filter(user=u).first() profile = Userprofile.objects.filter(user_id=id).first() o = profile.organization UserModel = get_user_model() if o.is_direct(): # 如果是省直 profile.admin_status = 5 profile.save() managers = Userprofile.level1_admin() content = '%s申请删除管理员,请审核' % (operator,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '3', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}, safe=False) else: profile.admin_status = 4 profile.save() managers = Userprofile.level2_admin(o.cities) content = '%s申请删除管理员,请审核' % (operator,) phones = [] for a in managers: phones.append(a.user.username) Notice.create_reply_notice( a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '2', '0') send_tnps(phones, '审核', content) return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}, safe=False)