newmediamonitoring/management/views.py

345 lines
15 KiB
Python
Raw Normal View History

2020-07-27 09:38:53 +00:00
import json
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
2020-07-27 09:38:53 +00:00
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from dashboard.models import Userprofile, Group, Organization, Level, Organizationtype, Area_code_2020, Weixin, Weibo, \
Toutiao, Qita, Group_type, Group_admin, Group_user
2020-07-27 09:38:53 +00:00
def user_management(request):
userpaginator = User.objects.all().order_by('-date_joined')
paginator = Paginator(userpaginator, 6)
page = int(request.GET.get('page', 1))
try:
userpaginator = paginator.page(page)
except PageNotAnInteger:
userpaginator = paginator.page(1)
except EmptyPage:
userpaginator = paginator.page(paginator.num_pages)
userallinfo = []
for u in userpaginator:
o = dict()
o['id'] = str(u.id)
o['image'] = u.userprofile_set.get(user_id=u.id).image.url
o['name'] = u.userprofile_set.get(user_id=u.id).name
o['phone'] = u.first_name
o['organization'] = u.userprofile_set.get(user_id=u.id).organization.name
o['type'] = u.userprofile_set.get(user_id=u.id).organization.organizationtype.organizationtype
organization_id = Userprofile.objects.get(user_id=u.id).organization_id
print(organization_id)
o['administrativedivision'] = str(str(Organization.objects.get(id=organization_id).province) + '-' + str(
Organization.objects.get(id=organization_id).cities) + '-' + str(
Organization.objects.get(id=organization_id).district) + '-' + str(
Organization.objects.get(id=organization_id).town) + '-' + str(
Organization.objects.get(id=organization_id).village)).replace('None', '')
userallinfo.append(o)
print(userallinfo)
return render(request, 'management/user-management.html',
{'userallinfo': userallinfo, 'userpaginator': userpaginator})
def user_delete(request, pk):
2020-07-27 09:38:53 +00:00
user = User.objects.get(id=pk)
user.delete()
return HttpResponseRedirect('/management/user/management/')
2020-07-27 09:38:53 +00:00
def group_management(request):
group = Group.objects.all().order_by('-created')
paginator = Paginator(group, 6)
page = int(request.GET.get('page', 1))
try:
group = paginator.page(page)
except PageNotAnInteger:
group = paginator.page(1)
except EmptyPage:
group = paginator.page(paginator.num_pages)
res = []
for g in group:
o = dict()
o['id'] = str(g.id)
o['image'] = g.image.url
o['name'] = g.name
o['type'] = g.type
o['admin_count'] = Group_admin.objects.filter(group_id=g.id).count()
o['user_count'] = Group_user.objects.filter(group_id=g.id).count()
o['status'] = g.status
res.append(o)
return render(request, 'management/group-management.html', {'res': res, 'group': group})
def group_update(request, pk):
2020-07-27 09:38:53 +00:00
group = Group.objects.get(id=pk)
GROUP_STATUS_CHOICES = Group.GROUP_STATUS_CHOICES
group_status_choices_list = []
for g in GROUP_STATUS_CHOICES:
group_status_choices_list.append(list(g)[1])
group_type = Group_type.objects.all()
group_admin = Group_admin.objects.filter(group_id=pk)
g_a_list = []
for g_a in group_admin:
o = dict()
o['id'] = str(g_a.id)
o['image'] = Userprofile.objects.get(user_id=g_a.user.id).image.url
o['username'] = g_a.user.username
o['name'] = Userprofile.objects.get(user_id=g_a.user.id).name
o['organization'] = Userprofile.objects.get(user_id=g_a.user.id).organization.name
o['administrativedivision'] = str(g_a.group.province)+'-'+str(g_a.group.cities)+'-'+str(g_a.group.district)+'-'+str(g_a.group.town)+'-'+str(g_a.group.village)
g_a_list.append(o)
group_user = Group_user.objects.filter(group_id=pk)
g_u_list = []
for g_u in group_user:
i = dict()
i['id'] = str(g_u.id)
i['image'] = Userprofile.objects.get(user_id=g_u.user.id).image.url
i['username'] = g_u.user.username
i['name'] = Userprofile.objects.get(user_id=g_u.user.id).name
i['organization'] = Userprofile.objects.get(user_id=g_u.user.id).organization.name
i['administrativedivision'] = str(g_u.group.province) + '-' + str(g_u.group.cities) + '-' + str(
g_u.group.district) + '-' + str(g_u.group.town) + '-' + str(g_u.group.village)
g_u_list.append(i)
2020-07-27 09:38:53 +00:00
if request.method == 'POST':
name = request.POST.get('name')
presentation = request.POST.get('presentation')
image = request.FILES.get('image')
type = request.POST.get('type')
status = request.POST.get('status')
province = request.POST.get('province')
print(str(province) + "66666666666666666666666666")
if province != '' and province.isdigit():
province_r = Area_code_2020.objects.get(code=province).name
else:
province_r = province
city = request.POST.get('city')
if city != '' and city.isdigit():
city_r = Area_code_2020.objects.get(code=city).name
else:
city_r = city
district = request.POST.get('district')
if district != '' and district.isdigit():
district_r = Area_code_2020.objects.get(code=district).name
else:
district_r = district
town = request.POST.get('town')
if town != '' and town.isdigit():
town_r = Area_code_2020.objects.get(code=town).name
else:
town_r = town
village = request.POST.get('village')
if village != '' and village.isdigit():
village_r = Area_code_2020.objects.get(code=village).name
else:
village_r = village
if image is not None:
Group.objects.filter(id=pk).update(name=name, presentation=presentation, type_id=type, status=status,
province=province_r, cities=city_r, district=district_r, town=town_r,
village=village_r)
g = Group.objects.get(id=pk)
g.image = image
g.save()
messages.success(request, '修改成功')
return HttpResponseRedirect('/management/group/management/')
else:
Group.objects.filter(id=pk).update(name=name, presentation=presentation, type_id=type, status=status,
province=province_r, cities=city_r, district=district_r, town=town_r,
village=village_r)
messages.success(request, '修改成功')
return HttpResponseRedirect('/management/group/management/')
return render(request, 'management/group-management-update.html',
{'group': group, 'group_status_choices_list': group_status_choices_list, 'group_type': group_type,
'g_a_list': g_a_list, 'g_u_list': g_u_list})
2020-07-27 09:38:53 +00:00
def group_create(request):
GROUP_STATUS_CHOICES = Group.GROUP_STATUS_CHOICES
group_status_choices_list = []
for g in GROUP_STATUS_CHOICES:
group_status_choices_list.append(list(g)[1])
group_type = Group_type.objects.all()
2020-07-27 09:38:53 +00:00
if request.method == 'POST':
name = request.POST.get('name')
presentation = request.POST.get('presentation')
image = request.FILES.get('image')
type = request.POST.get('type')
status = request.POST.get('status')
province = request.POST.get('province')
if province != '':
province_r = Area_code_2020.objects.get(code=province).name
else:
messages.error(request, '请选择行政区划')
return HttpResponseRedirect('/management/organization/create/')
city = request.POST.get('city')
if city != '':
city_r = Area_code_2020.objects.get(code=city).name
else:
city_r = city
district = request.POST.get('district')
if district != '':
district_r = Area_code_2020.objects.get(code=district).name
else:
district_r = district
town = request.POST.get('town')
if town != '':
town_r = Area_code_2020.objects.get(code=town).name
else:
town_r = town
village = request.POST.get('village')
if village != '':
village_r = Area_code_2020.objects.get(code=village).name
else:
village_r = village
print(name, presentation, image, type, status, province, city, district, town, village)
2020-07-27 09:38:53 +00:00
if name is not None:
group = Group(name=name, presentation=presentation, image=image, type_id=type, status=status,
province=province_r, cities=city_r, district=district_r, town=town_r, village=village_r)
2020-07-27 09:38:53 +00:00
group.save()
messages.success(request, '添加成功')
2020-07-27 09:38:53 +00:00
else:
messages.error(request, '群组名不能为空')
2020-07-27 09:38:53 +00:00
return HttpResponseRedirect('/management/group/management/')
return render(request, 'management/group-management-create.html',
{'group_status_choices_list': group_status_choices_list, 'group_type': group_type})
def group_delete(request, pk):
2020-07-27 09:38:53 +00:00
group = Group.objects.get(id=pk)
group.delete()
messages.success(request, '删除成功')
2020-07-27 09:38:53 +00:00
return HttpResponseRedirect('/management/group/management/')
2020-07-27 09:38:53 +00:00
def organization_management(request):
organization = Organization.objects.all().order_by('-created')
paginator = Paginator(organization, 6)
page = int(request.GET.get('page', 1))
try:
organization = paginator.page(page)
except PageNotAnInteger:
organization = paginator.page(1)
except EmptyPage:
organization = paginator.page(paginator.num_pages)
res = []
for i in organization:
o = dict()
o['id'] = str(i.id)
o['name'] = i.name
o['image'] = i.image.url
o['organizationtype'] = i.organizationtype.organizationtype
o['administrativedivision'] = str(i.province) + '-' + str(i.cities) + '-' + str(i.district) + '-' + str(
i.town) + '-' + str(i.village)
o['usercount'] = Userprofile.objects.filter(organization_id=i.id).count()
o['mediacount'] = Weixin.objects.filter(organization_id=i.id).count() + Weibo.objects.filter(
organization_id=i.id).count() + Toutiao.objects.filter(organization_id=i.id).count() + Qita.objects.filter(
organization_id=i.id).count()
res.append(o)
return render(request, 'management/organization-management.html', {"organization": organization, 'res': res})
def organization_delete(request, pk):
2020-07-27 09:38:53 +00:00
organization = Organization.objects.get(id=pk)
organization.delete()
messages.success(request, '删除成功')
return HttpResponseRedirect('/management/organization/management/')
def organization_update(request, pk):
2020-07-27 09:38:53 +00:00
organization = Organization.objects.get(id=pk)
organizationtype = Organizationtype.objects.all()
2020-07-27 09:38:53 +00:00
if request.method == 'POST':
name = request.POST.get('name')
image = request.FILES.get('image')
organizationtype = request.POST.get('organizationtype')
province = request.POST.get('province')
if province != '' and province.isdigit():
province_r = Area_code_2020.objects.get(code=province).name
else:
province_r = province
city = request.POST.get('city')
if city != '' and city.isdigit():
city_r = Area_code_2020.objects.get(code=city).name
else:
city_r = city
district = request.POST.get('district')
if district != '' and district.isdigit():
district_r = Area_code_2020.objects.get(code=district).name
else:
district_r = district
town = request.POST.get('town')
if town != '' and town.isdigit():
town_r = Area_code_2020.objects.get(code=town).name
else:
town_r = town
village = request.POST.get('village')
if village != '' and village.isdigit():
village_r = Area_code_2020.objects.get(code=village).name
else:
village_r = village
if name is not None:
if image is not None:
Organization.objects.filter(id=pk).update(name=name, organizationtype_id=organizationtype,
province=province_r, cities=city_r, district=district_r,
town=town_r, village=village_r)
o = Organization.objects.get(id=pk)
o.image = image
o.save()
messages.success(request, '修改成功')
return HttpResponseRedirect('/management/organization/management/')
else:
Organization.objects.filter(id=pk).update(name=name, organizationtype_id=organizationtype,
province=province_r, cities=city_r, district=district_r,
town=town_r, village=village_r)
messages.success(request, '修改成功')
return HttpResponseRedirect('/management/organization/management/')
return render(request, 'management/organization-management-update.html',
{'organization': organization, 'organizationtype': organizationtype})
2020-07-27 09:38:53 +00:00
def organization_create(request):
organizationtype = Organizationtype.objects.all()
2020-07-27 09:38:53 +00:00
if request.method == 'POST':
name = request.POST.get('name')
image = request.FILES.get('image')
organizationtype = request.POST.get('organizationtype')
province = request.POST.get('province')
if province != '':
province_r = Area_code_2020.objects.get(code=province).name
else:
messages.error(request, '请选择行政区划')
return HttpResponseRedirect('/management/organization/create/')
city = request.POST.get('city')
if city != '':
city_r = Area_code_2020.objects.get(code=city).name
else:
city_r = city
district = request.POST.get('district')
if district != '':
district_r = Area_code_2020.objects.get(code=district).name
else:
district_r = district
town = request.POST.get('town')
if town != '':
town_r = Area_code_2020.objects.get(code=town).name
else:
town_r = town
village = request.POST.get('village')
if village != '':
village_r = Area_code_2020.objects.get(code=village).name
else:
village_r = village
print(province, city)
2020-07-27 09:38:53 +00:00
if name is not None:
organization = Organization(name=name, image=image, organizationtype_id=organizationtype,
province=province_r, cities=city_r, district=district_r, town=town_r,
village=village_r)
2020-07-27 09:38:53 +00:00
organization.save()
messages.success(request, '添加成功')
2020-07-27 09:38:53 +00:00
else:
messages.error(request, '单位名不能为空')
2020-07-27 09:38:53 +00:00
return HttpResponseRedirect('/management/organization/management/')
return render(request, 'management/organization-management-create.html', {'organizationtype': organizationtype})