newmediamonitoring/management/views.py

467 lines
21 KiB
Python

import json
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
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
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):
user = User.objects.get(id=pk)
user.delete()
return HttpResponseRedirect('/management/user/management/')
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):
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)
print(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)
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})
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()
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)
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)
group.save()
messages.success(request, '添加成功')
else:
messages.error(request, '群组名不能为空')
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_admin_create(request, pk):
if request.method == 'POST':
user = request.POST.get("user")
print(user)
results = Group_admin.objects.filter(user_id=user, group_id=pk).count()
if results != 0:
messages.success(request, "该管理员已经存在")
return HttpResponseRedirect('/management/group/update/%s/' % (pk))
group_admin = Group_admin(user_id=user, group_id=pk)
group_admin.save()
messages.success(request, "添加成功")
return HttpResponseRedirect('/management/group/update/%s/' % (pk))
group = Group.objects.get(id=pk).name
user = User.objects.all()
return render(request, 'management/group-admin-create.html', {'usee': user, 'group': group, 'pk': pk})
def group_admin_delete(request, pk, group_pk):
group_admin = Group_admin.objects.get(id=pk)
group_admin.delete()
messages.success(request, '删除成功')
return HttpResponseRedirect('/management/group/update/%s/' % (group_pk))
def group_user_create(request, pk):
if request.method == 'POST':
user = request.POST.get('user')
results = Group_user.objects.filter(user_id=user, group_id=pk).count()
if results != 0:
messages.success(request, "该成员已经存在")
return HttpResponseRedirect('/management/group/update/%s/' % (pk))
group_user = Group_user(user_id=user, group_id=pk)
group_user.save()
messages.success(request, '添加成功')
return HttpResponseRedirect('/management/group/update/%s' % (pk))
group = Group.objects.get(id=pk).name
user = User.objects.all()
return render(request, 'management/group-user-create.html', {'usee': user, 'group': group, 'pk': pk})
def group_user_delete(request, pk, group_pk):
group_user = Group_user.objects.get(id=pk)
group_user.delete()
messages.success(request, '删除成功')
return HttpResponseRedirect('/management/group/update/%s/' % (group_pk))
def group_delete(request, pk):
group = Group.objects.get(id=pk)
group.delete()
messages.success(request, '删除成功')
return HttpResponseRedirect('/management/group/management/')
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):
organization = Organization.objects.get(id=pk)
organization.delete()
messages.success(request, '删除成功')
return HttpResponseRedirect('/management/organization/management/')
def organization_update(request, pk):
organization = Organization.objects.get(id=pk)
organizationtype = Organizationtype.objects.all()
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})
def organization_create(request):
organizationtype = Organizationtype.objects.all()
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)
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)
organization.save()
messages.success(request, '添加成功')
else:
messages.error(request, '单位名不能为空')
return HttpResponseRedirect('/management/organization/management/')
return render(request, 'management/organization-management-create.html', {'organizationtype': organizationtype})
def newmedia_management_create_menu(request):
return render(request, 'management/newmedia-management-create-menu.html')
def newmedia_management_create_weixin(request):
WEIXIN_STATUS_CHOICES = Weixin.WEIXIN_STATUS_CHOICES
weixin_status_choices_list = []
for w in WEIXIN_STATUS_CHOICES:
weixin_status_choices_list.append(list(w)[1])
organization = Organization.objects.all()
if request.method == 'POST':
code = request.POST.get('code')
weixinid = request.POST.get('weixinid')
alias = request.POST.get('alias')
image = request.FILES.get('image')
organization = request.POST.get('organization')
status = request.POST.get('status')
if code is not None:
if organization is not None:
if image is not None:
weixin = Weixin(code=code, weixinid=weixinid, alias=alias, image=image,
organization_id=organization, status=status)
weixin.save()
messages.success(request, '创建成功')
return HttpResponseRedirect('/management/newmedia/management/create/weixin/')
else:
weixin = Weixin(code=code, weixinid=weixinid, alias=alias, image='cover/weixin.PNG',
organization_id=organization, status=status)
weixin.save()
messages.success(request, '创建成功')
return HttpResponseRedirect('/management/newmedia/management/create/weixin/')
else:
messages.success(request, "请选择单位")
return render(request, 'management/newmedia-management-create-weixin.html',
{'weixin_status_choices_list': weixin_status_choices_list, "organization": organization})
else:
messages.success(request, "微信公众号不能为空")
return render(request, 'management/newmedia-management-create-weixin.html',
{'weixin_status_choices_list': weixin_status_choices_list, "organization": organization})
return render(request, 'management/newmedia-management-create-weixin.html',
{'weixin_status_choices_list': weixin_status_choices_list, "organization": organization})
def newmedia_management_edit_menu(request):
return render(request, 'management/newmedia-management-edit-menu.html')
def newmedia_management_edit_weixin(request):
weixin = Weixin.objects.all()
paginator = Paginator(weixin, 6)
page = int(request.GET.get('page', 1))
try:
weixin = paginator.page(page)
except PageNotAnInteger:
weixin = paginator.page(1)
except EmptyPage:
weixin = paginator.page(paginator.num_pages)
res = []
for w in weixin:
o = dict()
o['id'] = str(w.id)
o['image'] = w.image
o['code'] = w.code
o['weixinid'] = w.weixinid
o['organization'] = w.organization.name
o['organization_type'] = w.organization.organizationtype.organizationtype
o['administrativedivision'] = str(w.organization.province) + '-' + str(w.organization.cities) + '-' + str(
w.organization.district) + '-' + str(w.organization.town) + '-' + str(w.organization.village)
o['status'] = w.status
res.append(o)
return render(request, 'management/newmedia-management-edit-weixin.html', {'weixin': weixin,'res':res})