newmediamonitoring/polls/views/organizations.py

159 lines
5.9 KiB
Python

from polls.utils import send_tnps
from polls.models import Notice
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from dashboard.models import Area_code_2020, NewMedia, Organization, Userprofile
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from polls.decorators import polls_login_required
@csrf_exempt
@polls_login_required
def organizations(request):
category = request.GET.get('category', '1')
code = request.GET.get('code')
print(category, code)
if category == '1':
organizations = Organization.objects.exclude(
province=''
).filter(
cities='',
district=''
)
elif category == '2':
organizations = Organization.objects.exclude(
province=''
).filter(
cities=code,
district=''
)
elif category == '3':
organizations = Organization.objects.exclude(
province='',
cities='',
).filter(
district=code
)
results = []
for o in organizations:
result = dict()
result['id'] = o.id
result['name'] = o.name
result['province'] = o.province
result['cities'] = o.cities
result['district'] = o.district
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@polls_login_required
def organization_level(request):
profile = request.user.userprofile_set.first()
if not profile:
return JsonResponse({'status': 'error', 'message': '账户错误'})
organization = profile.organization
if not organization:
return JsonResponse({'status': 'error', 'message': '机构错误'})
level = organization.level.level
if not level:
return JsonResponse({'status': 'error', 'message': '权限错误'})
return JsonResponse({'status': 'success', 'message': {'level': level, 'province': organization.province, 'cities': organization.cities, 'district': organization.district}}, safe=False)
@csrf_exempt
@polls_login_required
def organization_districts(request):
code = request.GET.get('code', "620100000000")
districts = Area_code_2020.objects.filter(pcode=code)
results = []
for o in districts:
result = dict()
result['value'] = o.code
result['label'] = o.name
results.append(result)
return JsonResponse({'status': 'success', 'message': {'districts': results}}, safe=False)
@csrf_exempt
@polls_login_required
def create_organization(request):
if request.method == 'GET':
return HttpResponse(status=405)
profile = request.user.userprofile_set.first()
name = request.POST.get('name')
if not name:
return JsonResponse({'status': 'error', 'message': '名称不能为空'})
if Organization.objects.filter(name=name).exists():
return JsonResponse({'status': 'error', 'message': '名称已经存在'})
province = request.POST.get('province', '')
cities = request.POST.get('cities', '')
district = request.POST.get('district', '')
directly = ''
admin_status = 1
if (not cities) and (not district):
directly = ''
admin_status = 2
o = Organization.objects.create(name=name, province=province, cities=cities, district=district, directly=directly,
image='danweimoren.jpg', status='1', level_id='69be9ef4-b7b7-4049-a86e-7083bee40f0e', admin_status=admin_status)
if o.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', 'Organization', 'admin_status', o.id, '3', '0')
send_tnps(phones, '审核', content)
else:
admins = Userprofile.level2_admin(cities)
content = '%s创建了机构,请审核' % (profile,)
phones = []
for a in admins:
phones.append(a.user.username)
Notice.create_reply_notice(
a.user.id, content, 'dashboard', 'Organization', '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 delete_organization(request):
if request.method == 'GET':
return HttpResponse(status=405)
profile = request.user.userprofile_set.first()
id = request.POST.get('id')
if not id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
o = Organization.objects.get(id=id)
count1 = NewMedia.count(o.id)
count2 = Userprofile.count(o.id)
if count1 != 0 or count2 != 0:
return JsonResponse({'status': 'error', 'message': '机构下有新媒体或管理未注销,无法删除'})
if o.is_direct():
o.admin_status = 5
o.save()
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', 'Organization', 'admin_status', o.id, '3', '0')
send_tnps(phones, '审核', content)
else:
o.admin_status = 4
o.save()
admins = Userprofile.level2_admin(o.cities)
content = '%s注销了机构,请审核' % (profile,)
phones = []
for a in admins:
phones.append(a.user.username)
Notice.create_reply_notice(
a.user.id, content, 'dashboard', 'Organization', 'admin_status', o.id, '2', '0')
send_tnps(phones, '审核', content)
return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}})