import json from polls.utils import send_tnps from polls.models import Notice from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.auth.hashers import make_password from django.contrib.auth.models import User from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.db.models import Q 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, News, Douyin @login_required def user_management(request): user_count = User.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district userpaginator = None if level == 1: userpaginator = Userprofile.objects.filter(organization__province=province).order_by('-created') elif level == 2: userpaginator = Userprofile.objects.filter(organization__province=province, organization__cities=cities).order_by('-created') elif level == 3: userpaginator = Userprofile.objects.filter(organization__province=province, organization__cities=cities, organization__district=district).order_by('-created') elif level == 9: userpaginator = Userprofile.objects.all().order_by('-created') userallinfo = [] if userpaginator is not None: 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) for u in userpaginator: o = dict() o['id'] = str(u.id) o['user_id'] = str(u.user_id) if u.image: o['image'] = u.image o['name'] = u.name o['phone'] = u.user.username o['organization'] = u.organization.name organization_id = u.organization.id if Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities and Organization.objects.get( id=organization_id).district and Organization.objects.get( id=organization_id).town and Organization.objects.get(id=organization_id).village: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).district)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).town)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).village))).replace( 'None', '') elif Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities and Organization.objects.get( id=organization_id).district and Organization.objects.get(id=organization_id).town: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).district)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).town))).replace('None', '') elif Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities and Organization.objects.get(id=organization_id).district: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).district))).replace( 'None', '') elif Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities))).replace( 'None', '') elif Organization.objects.get(id=organization_id).province: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province))).replace( 'None', '') userallinfo.append(o) return render(request, 'management/user-management.html', {'userallinfo': userallinfo, 'userpaginator': userpaginator, 'user_count': user_count}) @login_required def user_delete(request, pk): user = User.objects.get(id=pk) user.delete() return HttpResponseRedirect('/management/user/management/') @login_required def user_update(request, pk): user = User.objects.get(id=pk) userprofile = Userprofile.objects.get(user_id=user.id) organization = Organization.objects.filter(status='1') if request.method == 'POST': o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id name = request.POST.get('name') email = request.POST.get('email') username = request.POST.get('username') image = request.FILES.get('image') passwordold = request.POST.get('passwordold') passwordnew = request.POST.get('passwordnew') confirm_password = request.POST.get('confirm_password') if len(passwordold) != 0 and len(passwordnew) != 0 and len(confirm_password) != 0: if passwordnew == confirm_password: user.email = email user.username = username user.password = make_password(passwordnew) userprofile.organization_id = o_id userprofile.name = name if image is not None: userprofile.image = image user.save() userprofile.save() messages.success(request, "修改成功") return HttpResponseRedirect("/") else: messages.error(request, '两次输入密码不一致') return HttpResponseRedirect('/management/user/update/%s/' % (pk)) else: user.email = email user.username = username userprofile.organization_id = o_id userprofile.name = name if image is not None: userprofile.image = image user.save() userprofile.save() messages.success(request, "修改成功") return HttpResponseRedirect("/index") return render(request, 'management/user-management-update.html', {'usee': user, 'userprofile': userprofile, 'organization': organization}) @login_required def group_management_init(request): user = request.user level = Userprofile.objects.get(user_id=user.id).organization.level.level init_count = Group.objects.filter(user_id=user.id).count() # 创建的矩阵 group_initer = Group.objects.filter(user_id=user.id).order_by('-created') paginator = Paginator(group_initer, 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_g_i = [] for r_g_i in group: o1 = dict() o1['id'] = str(r_g_i.id) o1['image'] = r_g_i.image.url o1['name'] = r_g_i.name o1['type'] = r_g_i.type o1['admin_count'] = Group_admin.objects.filter(group_id=r_g_i.id).count() o1['user_count'] = Group_user.objects.filter(group_id=r_g_i.id).count() o1['status'] = r_g_i.status res_g_i.append(o1) return render(request, 'management/group-management-init.html', {'group': group, 'res_g_i': res_g_i, 'init_count': init_count, 'level': level}) @login_required def group_management_admin(request): # 管理的矩阵 user = request.user level = Userprofile.objects.get(user_id=user.id).organization.level.level admin_count = Group_admin.objects.filter(user_id=user.id).count() group_admin_list = [] for g_a in Group_admin.objects.filter(user_id=user.id).order_by('-created'): group_admin = Group.objects.get(id=g_a.group_id) group_admin_list.append(group_admin) paginator = Paginator(group_admin_list, 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_g_a = [] for r_g_a in group: o2 = dict() o2['id'] = str(r_g_a.id) o2['image'] = r_g_a.image.url o2['name'] = r_g_a.name o2['type'] = r_g_a.type o2['admin_count'] = Group_admin.objects.filter(group_id=r_g_a.id).count() o2['user_count'] = Group_user.objects.filter(group_id=r_g_a.id).count() o2['status'] = r_g_a.status res_g_a.append(o2) return render(request, 'management/group-management-admin.html', {'group': group, 'res_g_a': res_g_a, 'admin_count': admin_count, 'level': level}) @login_required def group_management_user(request): # 加入的矩阵 user = request.user level = Userprofile.objects.get(user_id=user.id).organization.level.level user_count = Group_user.objects.filter(user_id=user.id).count() group_user_list = [] for g_u in Group_user.objects.filter(user_id=user.id).order_by('-created'): group_user = Group.objects.get(id=g_u.group_id) group_user_list.append(group_user) paginator = Paginator(group_user_list, 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_g_u = [] for r_g_u in group: o3 = dict() o3['id'] = str(r_g_u.id) o3['image'] = r_g_u.image.url o3['name'] = r_g_u.name o3['type'] = r_g_u.type o3['admin_count'] = Group_admin.objects.filter(group_id=r_g_u.id).count() o3['user_count'] = Group_user.objects.filter(group_id=r_g_u.id).count() o3['status'] = r_g_u.status res_g_u.append(o3) return render(request, 'management/group-management-user.html', {'group': group, 'res_g_u': res_g_u, 'user_count': user_count, 'level': level}) @login_required def group_management_superuser(request): user = request.user level = Userprofile.objects.get(user_id=user.id).organization.level.level count = Group.objects.all().count() 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_g_i = [] for r_g_i in group: o1 = dict() o1['id'] = str(r_g_i.id) o1['image'] = r_g_i.image.url o1['name'] = r_g_i.name o1['type'] = r_g_i.type o1['admin_count'] = Group_admin.objects.filter(group_id=r_g_i.id).count() o1['user_count'] = Group_user.objects.filter(group_id=r_g_i.id).count() o1['status'] = r_g_i.status res_g_i.append(o1) return render(request, 'management/group-management-superuser.html', {'group': group, 'res_g_i': res_g_i, 'count': count, 'level': level}) @login_required def group_update(request, pk): group = Group.objects.get(id=pk) p = '' c = '' d = '' t = '' v = '' if group.province != '' and group.province != 'None': p = Area_code_2020.objects.get(code=group.province).name if group.cities and group.cities != 'None': c = Area_code_2020.objects.get(code=group.cities).name if group.district and group.district != 'None': d = Area_code_2020.objects.get(code=group.district).name if group.town and group.town != 'None': t = Area_code_2020.objects.get(code=group.town).name if group.village and group.village != 'None': v = Area_code_2020.objects.get(code=group.village).name group_status_choices_list = [] for g in range(2): group_status_choices_list.append(g) GROUP_CLASSIFY_CHOICES = Group.GROUP_CLASSIFY_CHOICES g_c_c_res = [] for g_c_c in GROUP_CLASSIFY_CHOICES: g_c_c_res.append([list(g_c_c)[0], list(g_c_c)[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) if Userprofile.objects.get(user_id=g_a.user.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 admin_p = Userprofile.objects.get(user_id=g_a.user_id).organization.province admin_c = Userprofile.objects.get(user_id=g_a.user_id).organization.cities admin_d = Userprofile.objects.get(user_id=g_a.user_id).organization.district admin_t = Userprofile.objects.get(user_id=g_a.user_id).organization.town admin_v = Userprofile.objects.get(user_id=g_a.user_id).organization.village if admin_p and admin_p != 'None' and admin_c and admin_c != 'None' and admin_d and admin_d != 'None' and admin_t and admin_t != 'None' and admin_v and admin_v != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) + '-' + str( Area_code_2020.objects.get(code=admin_d).name) + '-' + str( Area_code_2020.objects.get(code=admin_t).name) + '-' + str( Area_code_2020.objects.get(code=admin_v).name) elif admin_p and admin_p != 'None' and admin_c and admin_c != 'None' and admin_d and admin_d != 'None' and admin_t and admin_t != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) + '-' + str( Area_code_2020.objects.get(code=admin_d).name) + '-' + str( Area_code_2020.objects.get(code=admin_t).name) elif admin_p and admin_p != 'None' and admin_c and admin_c != 'None' and admin_d and admin_d != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) + '-' + str( Area_code_2020.objects.get(code=admin_d).name) elif admin_p and admin_p != 'None' and admin_c and admin_c != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) elif admin_p and admin_p != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=str(admin_p)).name) g_a_list.append(o) group_user = Group_user.objects.filter(group_id=pk) paginator = Paginator(group_user, 6) page = int(request.GET.get('page', 1)) try: group_user = paginator.page(page) except PageNotAnInteger: group_user = paginator.page(1) except EmptyPage: group_user = paginator.page(paginator.num_pages) 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 user_p = Userprofile.objects.get(user_id=g_u.user_id).organization.province user_c = Userprofile.objects.get(user_id=g_u.user_id).organization.cities user_d = Userprofile.objects.get(user_id=g_u.user_id).organization.district user_t = Userprofile.objects.get(user_id=g_u.user_id).organization.town user_v = Userprofile.objects.get(user_id=g_u.user_id).organization.village if user_p and user_p != 'None' and user_c and user_c != 'None' and user_d and user_d != 'None' and user_t and user_t != 'None' and user_v and user_v != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) + '-' + str( Area_code_2020.objects.get(code=user_d).name) + '-' + str( Area_code_2020.objects.get(code=user_t).name) + '-' + str( Area_code_2020.objects.get(code=user_v).name) elif user_p and user_p != 'None' and user_c and user_c != 'None' and user_d and user_d != 'None' and user_t and user_t != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) + '-' + str( Area_code_2020.objects.get(code=user_d).name) + '-' + str( Area_code_2020.objects.get(code=user_t).name) elif user_p and user_p != 'None' and user_c and user_c != 'None' and user_d and user_d != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) + '-' + str( Area_code_2020.objects.get(code=user_d).name) elif user_p and user_p != 'None' and user_c and user_c != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) elif user_p and user_p != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=str(user_p)).name) 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') classify = request.POST.get('classify') if province != '' and province.isdigit(): province_r = province else: province_r = province city = request.POST.get('city') if city != '' and city.isdigit(): city_r = city else: city_r = city district = request.POST.get('district') if district != '' and district.isdigit(): district_r = district else: district_r = district town = request.POST.get('town') if town != '' and town.isdigit(): town_r = town else: town_r = town village = request.POST.get('village') if village != '' and village.isdigit(): village_r = village 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, classify=classify) g = Group.objects.get(id=pk) g.image = image g.save() messages.success(request, '修改成功') return HttpResponseRedirect('/management/group/management/init/') 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, classify=classify) messages.success(request, '修改成功') return HttpResponseRedirect('/management/group/management/init/') 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, 'g_c_c_res': g_c_c_res, 'p': p, 'c': c, 'd': d, 't': t, 'v': v,'page':group_user}) @login_required def group_create(request): user = request.user group_status_choices_list = [] for g in range(2): group_status_choices_list.append(g) group_type = Group_type.objects.all() GROUP_CLASSIFY_CHOICES = Group.GROUP_CLASSIFY_CHOICES g_c_c_res = [] for g_c_c in GROUP_CLASSIFY_CHOICES: g_c_c_res.append([list(g_c_c)[0], list(g_c_c)[1]]) if request.method == 'POST': name = request.POST.get('name') presentation = request.POST.get('presentation') image = None if not request.FILES.get('image'): image = 'group.jpg' else: image = request.FILES.get('image') type = request.POST.get('type') status = request.POST.get('status') province = request.POST.get('province') classify = request.POST.get('classify') if province != '': province_r = province else: messages.error(request, '请选择行政区划') return HttpResponseRedirect('/management/organization/create/') city = request.POST.get('city') if city != '': city_r = city else: city_r = city district = request.POST.get('district') if district != '': district_r = district else: district_r = district town = request.POST.get('town') if town != '': town_r = town else: town_r = town village = request.POST.get('village') if village != '': village_r = village else: village_r = 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, user_id=user.id, classify=classify) group.save() p = '' c = '' d = '' t = '' v = '' if group.province: p = Area_code_2020.objects.get(code=group.province).name if group.cities: c = Area_code_2020.objects.get(code=group.cities).name if group.district: d = Area_code_2020.objects.get(code=group.district).name if group.town: t = Area_code_2020.objects.get(code=group.town).name if group.village: v = Area_code_2020.objects.get(code=group.village).name messages.success(request, '添加成功') return render(request, 'management/add-group-admin-and-user.html', {'group': group, 'p': p, 'c': c, 'd': d, 't': t, 'v': v}) 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, 'g_c_c_res': g_c_c_res}) @login_required def get_user(request): keywords = request.GET.get('keyword') if len(keywords) != 0: userprofile = Userprofile.objects.filter(Q(name__contains=keywords))[:10] topten_list = [] for u in userprofile: d = dict() d['user_id'] = str(u.user_id) d['name'] = u.name d['phone'] = u.user.username d['o'] = u.organization.name topten_list.append(d) return render(request, 'management/user_results_by_keywords.html', {'topten_list': topten_list}) @login_required def group_admin_create(request, pk): if request.method == 'POST': user_id = None if not request.POST.get('user_name'): messages.error(request, '请选择用户') else: user_name = request.POST.get('user_name') res = User.objects.filter(username=user_name).count() if res > 0: user_id = User.objects.get(username=user_name).id else: messages.error(request, '您填写的用户不存在') results = Group_admin.objects.filter(user_id=user_id, group_id=pk).count() if results != 0: messages.success(request, "该管理员已经存在") return HttpResponseRedirect('/management/group/update/%s/' % (pk)) group_admin = Group_admin(user_id=user_id, group_id=pk) group_admin.save() messages.success(request, "添加成功") return HttpResponseRedirect('/management/group/update/%s/' % (pk)) group = Group.objects.get(id=pk).name return render(request, 'management/group-admin-create.html', {'group': group, 'pk': pk}) @login_required 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)) @login_required def group_user_create(request, pk): if request.method == 'POST': user_id = None if not request.POST.get('user_name'): messages.error(request, '请选择用户') else: user_name = request.POST.get('user_name') res = User.objects.filter(username=user_name).count() if res > 0: user_id = User.objects.get(username=user_name).id else: messages.error(request, '您填写的用户不存在') results = Group_user.objects.filter(user_id=user_id, group_id=pk).count() if results != 0: messages.success(request, "该成员已经存在") return HttpResponseRedirect('/management/group/update/%s/' % (pk)) group_user = Group_user(user_id=user_id, group_id=pk) group_user.save() messages.success(request, '添加成功') return HttpResponseRedirect('/management/group/update/%s' % (pk)) group = Group.objects.get(id=pk).name return render(request, 'management/group-user-create.html', {'group': group, 'pk': pk}) @login_required 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)) @login_required def group_delete(request, pk): group = Group.objects.get(id=pk) group.delete() messages.success(request, '删除成功') return HttpResponseRedirect('/management/group/management/') @login_required def organization_management(request): organization_count = Organization.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district organization = None res = [] if level == 1: organization = Organization.objects.filter(province=province).order_by('-created') elif level == 2: organization = Organization.objects.filter(province=province, cities=cities).order_by('-created') elif level == 3: organization = Organization.objects.filter(province=province, cities=cities, district=district).order_by( '-created') elif level == 9: organization = Organization.objects.all().order_by('-created') if organization is not None: 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) for i in organization: o = dict() o['id'] = str(i.id) o['name'] = i.name o['image'] = i.image.url if i.province and i.cities and i.district and i.town and i.village: o['administrativedivision'] = str(Area_code_2020.objects.get(code=i.province).name) + '-' + str( Area_code_2020.objects.get(code=i.cities).name) + '-' + str( Area_code_2020.objects.get(code=i.district).name) + '-' + str( Area_code_2020.objects.get(code=i.town).name) + '-' + str( Area_code_2020.objects.get(code=i.village).name) elif i.province and i.cities and i.district and i.town: o['administrativedivision'] = str(Area_code_2020.objects.get(code=i.province).name) + '-' + str( Area_code_2020.objects.get(code=i.cities).name) + '-' + str( Area_code_2020.objects.get(code=i.district).name) + '-' + str( Area_code_2020.objects.get(code=i.town).name) elif i.province and i.cities and i.district: o['administrativedivision'] = str(Area_code_2020.objects.get(code=i.province).name) + '-' + str( Area_code_2020.objects.get(code=i.cities).name) + '-' + str( Area_code_2020.objects.get(code=i.district).name) elif i.province and i.cities: print(i.cities) o['administrativedivision'] = str(Area_code_2020.objects.get(code=i.province).name) + '-' + str( Area_code_2020.objects.get(code=i.cities).name) elif i.province: o['administrativedivision'] = str(Area_code_2020.objects.get(code=i.province).name) 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, 'organization_count': organization_count}) @login_required def organization_delete(request, pk): organization = Organization.objects.get(id=pk) organization.delete() messages.success(request, '删除成功') return HttpResponseRedirect('/management/organization/management/') @login_required def organization_update(request, pk): organization = Organization.objects.get(id=pk) p = '' c = '' d = '' t = '' v = '' if organization.province: p = Area_code_2020.objects.get(code=organization.province).name if organization.cities: c = Area_code_2020.objects.get(code=organization.cities).name if organization.district: d = Area_code_2020.objects.get(code=organization.district).name if organization.town: t = Area_code_2020.objects.get(code=organization.town).name if organization.village: v = Area_code_2020.objects.get(code=organization.village).name level = Level.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') level_id = request.POST.get('level') if province: province = Area_code_2020.objects.get(name=province).code city = request.POST.get('city') if city: city = Area_code_2020.objects.get(name=city).code district = request.POST.get('district') if district: district = Area_code_2020.objects.get(name=district).code town = request.POST.get('town') if town: town = Area_code_2020.objects.get(name=town).code village = request.POST.get('village') if village: village = Area_code_2020.objects.get(name=village).code if name is not None: if image is not None: Organization.objects.filter(id=pk).update(name=name, province=province, cities=city, district=district, town=town, village=village, level_id=level_id) 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, province=province, cities=city, district=district, town=town, village=village, level_id=level_id) messages.success(request, '修改成功') return HttpResponseRedirect('/management/organization/management/') return render(request, 'management/organization-management-update.html', {'organization': organization, 'level': level, 'p': p, 'c': c, 'd': d, 't': t, 'v': v}) def organization_create(request): user = request.user # l = Userprofile.objects.get(user_id=user.id).organization.level.level # if user.is_superuser: # level = Level.objects.filter(level__in=[1, 2, 3, 5, 0]) # elif l == '1': # level = Level.objects.filter(level__in=[2, 3, 5, 0]) # elif l == '2': # level = Level.objects.filter(level__in=[3, 5, 0]) # elif l == '3': # level = Level.objects.filter(level__in=[5, 0]) # else: # level = Level.objects.filter(level__in=[0]) if request.method == 'POST': name = request.POST.get('name') image = None if request.FILES.get('image'): image = request.FILES.get('image') else: image = 'danweimoren.jpg' # organizationtype = request.POST.get('organizationtype') # level_id = request.POST.get('level_id') province = request.POST.get('province') if province != '': province = province else: messages.error(request, '请选择行政区划') return HttpResponseRedirect('/management/organization/create/') city = request.POST.get('city') district = request.POST.get('district') town = request.POST.get('town') village = request.POST.get('village') results = Organization.objects.filter(name=name).count() if results > 0: messages.error(request, '您注册的主体已经存在') return HttpResponseRedirect('/management/organization/create/') if name is not None: organization = Organization(name=name, image=image, province=province, cities=city, district=district, town=town, village=village, level_id='f517fd75-8127-4599-ad6e-72687da63bdd', status='0') organization.save() messages.success(request, '添加成功') return HttpResponseRedirect('/management/organization/create/') else: messages.error(request, '单位名不能为空') return HttpResponseRedirect('/management/organization/create/') return render(request, 'management/organization-management-create.html') @login_required def newmedia_management_create_menu(request): return render(request, 'management/newmedia-management-create-menu.html') @login_required def newmedia_management_create_weixin(request): weixin_status_choices_list = [] for w in range(2): weixin_status_choices_list.append(w) organization = Organization.objects.filter(status='1').order_by('-name') if request.method == 'POST': code = request.POST.get('code') weixinid = request.POST.get('weixinid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') 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=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) weixin.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/weixin/') else: weixin = Weixin(code=code, weixinid=weixinid, alias=alias, image='weixin.png', organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) 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}) @login_required def newmedia_management_create_weibo(request): weibo_status_choices_list = [] for w in range(2): weibo_status_choices_list.append(w) organization = Organization.objects.filter(status='1').order_by('-name') if request.method == 'POST': code = request.POST.get('code') weiboid = request.POST.get('weiboid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: weibo = Weibo(code=code, weiboid=weiboid, alias=alias, image=image, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) weibo.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/weibo/') else: weibo = Weibo(code=code, weiboid=weiboid, alias=alias, image='weibo.png', organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) weibo.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/weibo/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-create-weibo.html', {'weibo_status_choices_list': weibo_status_choices_list, "organization": organization}) else: messages.success(request, "微博号不能为空") return render(request, 'management/newmedia-management-create-weibo.html', {'weixin_status_choices_list': weibo_status_choices_list, "organization": organization}) return render(request, 'management/newmedia-management-create-weibo.html', {'weibo_status_choices_list': weibo_status_choices_list, "organization": organization}) @login_required def newmedia_management_create_toutiao(request): toutiao_status_choices_list = [] for w in range(2): toutiao_status_choices_list.append(w) organization = Organization.objects.filter(status='1').order_by('-name') if request.method == 'POST': code = request.POST.get('code') toutiaoid = request.POST.get('toutiaoid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: toutiao = Toutiao(code=code, toutiaoid=toutiaoid, alias=alias, image=image, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) toutiao.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/toutiao/') else: toutiao = Toutiao(code=code, toutiaoid=toutiaoid, alias=alias, image='toutiao.png', organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) toutiao.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/toutiao/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-create-toutiao.html', {'toutiao_status_choices_list': toutiao_status_choices_list, "organization": organization}) else: messages.success(request, "头条号不能为空") return render(request, 'management/newmedia-management-create-toutiao.html', {'toutiao_status_choices_list': toutiao_status_choices_list, "organization": organization}) return render(request, 'management/newmedia-management-create-toutiao.html', {'toutiao_status_choices_list': toutiao_status_choices_list, "organization": organization}) @login_required def newmedia_management_create_douyin(request): douyin_status_choices_list = [] for w in range(2): douyin_status_choices_list.append(w) organization = Organization.objects.filter(status='1').order_by('-name') if request.method == 'POST': code = request.POST.get('code') douyinid = request.POST.get('douyinid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: douyin = Douyin(code=code, douyinid=douyinid, alias=alias, image=image, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) douyin.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/douyin/') else: douyin = Douyin(code=code, douyinid=douyinid, alias=alias, image='douyin.png', organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) douyin.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/douyin/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-create-douyin.html', {'douyin_status_choices_list': douyin_status_choices_list, "organization": organization}) else: messages.success(request, "抖音号不能为空") return render(request, 'management/newmedia-management-create-douyin.html', {'douyin_status_choices_list': douyin_status_choices_list, "organization": organization}) return render(request, 'management/newmedia-management-create-douyin.html', {'douyin_status_choices_list': douyin_status_choices_list, "organization": organization}) @login_required def newmedia_management_create_qita(request): qita_status_choices_list = [] for w in range(2): qita_status_choices_list.append(w) organization = Organization.objects.filter(status='1').order_by('-name') if request.method == 'POST': type = request.POST.get('type') name = request.POST.get('name') qitaid = request.POST.get('qitaid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if name is not None: if organization is not None: if image is not None: qita = Qita(type=type, name=name, qitaid=qitaid, alias=alias, image=image, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) qita.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/qita/') else: qita = Qita(type=type, name=name, qitaid=qitaid, alias=alias, image='qita.png', organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) qita.save() messages.success(request, '创建成功') return HttpResponseRedirect('/management/newmedia/management/create/qita/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-create-qita.html', {'qita_status_choices_list': qita_status_choices_list, "organization": organization}) else: messages.success(request, "新媒体名称不能为空") return render(request, 'management/newmedia-management-create-qita.html', {'qita_status_choices_list': qita_status_choices_list, "organization": organization}) return render(request, 'management/newmedia-management-create-qita.html', {'qita_status_choices_list': qita_status_choices_list, "organization": organization}) @login_required def newmedia_management_edit_menu(request): return render(request, 'management/newmedia-management-edit-menu.html') @login_required def newmedia_management_edit_weixin(request): weixin_count = Weixin.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district weixin = None res = [] if level == 1: weixin = Weixin.objects.filter(organization__province=province).order_by('-created') elif level == 2: weixin = Weixin.objects.filter(organization__province=province, organization__cities=cities).order_by( '-created') elif level == 3: weixin = Weixin.objects.filter(organization__province=province, organization__cities=cities, organization__district=district).order_by( '-created') elif level == 9: weixin = Weixin.objects.all().order_by('-created') if weixin is not None: 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) for w in weixin: o = dict() o['id'] = str(w.id) if w.image: o['image'] = w.image o['code'] = w.code o['weixinid'] = w.weixinid o['organization'] = w.organization.name # o['organization_type'] = w.organization.organizationtype.organizationtype if len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0 and len(w.organization.village) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.village).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) elif len(w.organization.province) > 0: o['administrativedivision'] = str(Area_code_2020.objects.get(code=w.organization.province).name) o['status'] = w.status res.append(o) return render(request, 'management/newmedia-management-edit-weixin.html', {'weixin': weixin, 'res': res, 'weixin_count': weixin_count}) @login_required def newmedia_management_edit_weibo(request): weibo_count = Weibo.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district weibo = None res = [] if level == 1: weibo = Weibo.objects.filter(organization__province=province).order_by('-created') elif level == 2: weibo = Weibo.objects.filter(organization__province=province, organization__cities=cities).order_by('-created') elif level == 3: weibo = Weibo.objects.filter(organization__province=province, organization__cities=cities, organization__district=district).order_by('-created') elif level == 9: weibo = Weibo.objects.all().order_by('-created') if weibo is not None: paginator = Paginator(weibo, 6) page = int(request.GET.get('page', 1)) try: weibo = paginator.page(page) except PageNotAnInteger: weibo = paginator.page(1) except EmptyPage: weibo = paginator.page(paginator.num_pages) for w in weibo: o = dict() o['id'] = str(w.id) o['image'] = w.image o['code'] = w.code o['weiboid'] = w.weiboid o['organization'] = w.organization.name # o['organization_type'] = w.organization.organizationtype.organizationtype if len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0 and len(w.organization.village) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.village).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) elif len(w.organization.province) > 0: o['administrativedivision'] = str(Area_code_2020.objects.get(code=w.organization.province).name) o['status'] = w.status res.append(o) return render(request, 'management/newmedia-management-edit-weibo.html', {'weibo': weibo, 'res': res, 'weibo_count': weibo_count}) @login_required def newmedia_management_edit_toutiao(request): toutiao_count = Toutiao.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district toutiao = None res = [] if level == 1: toutiao = Toutiao.objects.filter(organization__province=province).order_by('-created') elif level == 2: toutiao = Toutiao.objects.filter(organization__province=province, organization__cities=cities).order_by( '-created') elif level == 3: toutiao = Toutiao.objects.filter(organization__province=province, organization__cities=cities, organization__district=district).order_by( '-created') elif level == 9: toutiao = Toutiao.objects.all().order_by('-created') if toutiao is not None: paginator = Paginator(toutiao, 6) page = int(request.GET.get('page', 1)) try: toutiao = paginator.page(page) except PageNotAnInteger: toutiao = paginator.page(1) except EmptyPage: toutiao = paginator.page(paginator.num_pages) for w in toutiao: o = dict() o['id'] = str(w.id) o['image'] = w.image o['code'] = w.code o['toutiaoid'] = w.toutiaoid o['organization'] = w.organization.name # o['organization_type'] = w.organization.organizationtype.organizationtype if len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0 and len(w.organization.village) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.village).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) elif len(w.organization.province) > 0: o['administrativedivision'] = str(Area_code_2020.objects.get(code=w.organization.province).name) o['status'] = w.status res.append(o) return render(request, 'management/newmedia-management-edit-toutiao.html', {'toutiao': toutiao, 'res': res, 'toutiao_count': toutiao_count}) @login_required def newmedia_management_edit_douyin(request): douyin_count = Douyin.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district douyin = None res = [] if level == 1: douyin = Douyin.objects.filter(organization__province=province).order_by('-created') elif level == 2: douyin = Douyin.objects.filter(organization__province=province, organization__cities=cities).order_by( '-created') elif level == 3: douyin = Douyin.objects.filter(organization__province=province, organization__cities=cities, organization__district=district).order_by( '-created') elif level == 9: douyin = Douyin.objects.all().order_by('-created') if douyin is not None: paginator = Paginator(douyin, 6) page = int(request.GET.get('page', 1)) try: douyin = paginator.page(page) except PageNotAnInteger: douyin = paginator.page(1) except EmptyPage: douyin = paginator.page(paginator.num_pages) for w in douyin: o = dict() o['id'] = str(w.id) o['image'] = w.image o['code'] = w.code o['douyinid'] = w.douyinid o['organization'] = w.organization.name # o['organization_type'] = w.organization.organizationtype.organizationtype if len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0 and len(w.organization.village) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.village).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) elif len(w.organization.province) > 0: o['administrativedivision'] = str(Area_code_2020.objects.get(code=w.organization.province).name) o['status'] = w.status res.append(o) return render(request, 'management/newmedia-management-edit-douyin.html', {'douyin': douyin, 'res': res, 'douyin_count': douyin_count}) @login_required def newmedia_management_edit_qita(request): qita_count = Qita.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district qita = None res = [] if level == 1: qita = Qita.objects.filter(organization__province=province).order_by('-created') elif level == 2: qita = Qita.objects.filter(organization__province=province, organization__cities=cities).order_by('-created') elif level == 3: qita = Qita.objects.filter(organization__province=province, organization__cities=cities, organization__district=district).order_by('-created') elif level == 9: qita = Qita.objects.all().order_by('-created') if qita is not None: paginator = Paginator(qita, 6) page = int(request.GET.get('page', 1)) try: qita = paginator.page(page) except PageNotAnInteger: qita = paginator.page(1) except EmptyPage: qita = paginator.page(paginator.num_pages) for w in qita: o = dict() o['id'] = str(w.id) o['image'] = w.image o['type'] = w.type o['name'] = w.code o['qitaid'] = w.qitaid o['organization'] = w.organization.name # o['organization_type'] = w.organization.organizationtype.organizationtype if len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0 and len(w.organization.village) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.village).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0 and len(w.organization.town) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.town).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0 and len( w.organization.district) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.district).name) elif len(w.organization.province) > 0 and len(w.organization.cities) > 0: o['administrativedivision'] = str( Area_code_2020.objects.get(code=w.organization.province).name) + '-' + str( Area_code_2020.objects.get(code=w.organization.cities).name) elif len(w.organization.province) > 0: o['administrativedivision'] = str(Area_code_2020.objects.get(code=w.organization.province).name) o['status'] = w.status res.append(o) return render(request, 'management/newmedia-management-edit-qita.html', {'qita': qita, 'res': res, 'qita_count': qita_count}) @login_required def newmedia_management_update_weixin(request, pk): weixin_status_choices_list = [] for w in range(2): weixin_status_choices_list.append(w) weixin = Weixin.objects.get(id=pk) organization = Organization.objects.filter(status='1') if request.method == 'POST': code = request.POST.get('code') weixinid = request.POST.get('weixinid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: Weixin.objects.filter(id=pk).update(code=code, weixinid=weixinid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) weixin.image = image weixin.save() messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/weixin/') else: Weixin.objects.filter(id=pk).update(code=code, weixinid=weixinid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/weixin/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-update-weixin.html', {'weixin': weixin, 'organization': organization, 'weixin_status_choices_list': weixin_status_choices_list}) else: messages.success(request, "微信公众号不能为空") return render(request, 'management/newmedia-management-update-weixin.html', {'weixin': weixin, 'organization': organization, 'weixin_status_choices_list': weixin_status_choices_list}) return render(request, 'management/newmedia-management-update-weixin.html', {'weixin': weixin, 'organization': organization, 'weixin_status_choices_list': weixin_status_choices_list}) @login_required def newmedia_management_update_weibo(request, pk): weibo_status_choices_list = [] for w in range(2): weibo_status_choices_list.append(w) weibo = Weibo.objects.get(id=pk) organization = Organization.objects.filter(status='1') if request.method == 'POST': code = request.POST.get('code') weiboid = request.POST.get('weiboid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: Weibo.objects.filter(id=pk).update(code=code, weiboid=weiboid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) weibo.image = image weibo.save() messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/weibo/') else: Weibo.objects.filter(id=pk).update(code=code, weiboid=weiboid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/weibo/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-update-weibo.html', {'weibo': weibo, 'organization': organization, 'weibo_status_choices_list': weibo_status_choices_list}) else: messages.success(request, "微博号不能为空") return render(request, 'management/newmedia-management-update-weibo.html', {'weibo': weibo, 'organization': organization, 'weibo_status_choices_list': weibo_status_choices_list}) return render(request, 'management/newmedia-management-update-weibo.html', {'weibo': weibo, 'organization': organization, 'weibo_status_choices_list': weibo_status_choices_list}) @login_required def newmedia_management_update_toutiao(request, pk): toutiao_status_choices_list = [] for w in range(2): toutiao_status_choices_list.append(w) toutiao = Toutiao.objects.get(id=pk) organization = Organization.objects.filter(status='1') if request.method == 'POST': code = request.POST.get('code') toutiaoid = request.POST.get('toutiaoid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: Toutiao.objects.filter(id=pk).update(code=code, toutiaoid=toutiaoid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) toutiao.image = image toutiao.save() messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/toutiao/') else: Toutiao.objects.filter(id=pk).update(code=code, toutiaoid=toutiaoid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/toutiao/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-update-toutiao.html', {'toutiao': toutiao, 'organization': organization, 'toutiao_status_choices_list': toutiao_status_choices_list}) else: messages.success(request, "头条号不能为空") return render(request, 'management/newmedia-management-update-toutiao.html', {'toutiao': toutiao, 'organization': organization, 'toutiao_status_choices_list': toutiao_status_choices_list}) return render(request, 'management/newmedia-management-update-toutiao.html', {'toutiao': toutiao, 'organization': organization, 'toutiao_status_choices_list': toutiao_status_choices_list}) @login_required def newmedia_management_update_douyin(request, pk): douyin_status_choices_list = [] for w in range(2): douyin_status_choices_list.append(w) douyin = Douyin.objects.get(id=pk) organization = Organization.objects.filter(status='1') if request.method == 'POST': code = request.POST.get('code') douyinid = request.POST.get('douyinid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if code is not None: if organization is not None: if image is not None: Douyin.objects.filter(id=pk).update(code=code, douyinid=douyinid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) douyin.image = image douyin.save() messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/douyin/') else: Douyin.objects.filter(id=pk).update(code=code, douyinid=douyinid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/douyin/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-update-douyin.html', {'douyin': douyin, 'organization': organization, 'douyin_status_choices_list': douyin_status_choices_list}) else: messages.success(request, "抖音号不能为空") return render(request, 'management/newmedia-management-update-douyin.html', {'douyin': douyin, 'organization': organization, 'douyin_status_choices_list': douyin_status_choices_list}) return render(request, 'management/newmedia-management-update-douyin.html', {'douyin': douyin, 'organization': organization, 'douyin_status_choices_list': douyin_status_choices_list}) @login_required def newmedia_management_update_qita(request, pk): qita_status_choices_list = [] for w in range(2): qita_status_choices_list.append(w) organization = Organization.objects.filter(status='1') qita = Qita.objects.get(id=pk) if request.method == 'POST': type = request.POST.get('type') name = request.POST.get('name') qitaid = request.POST.get('qitaid') alias = request.POST.get('alias') image = request.FILES.get('image') o = request.POST.get('organization') o_id = Organization.objects.get(name=o).id status = request.POST.get('status') identificationcode = request.POST.get('identificationcode') function = request.POST.get('function') articleurl = request.POST.get('articleurl') if name is not None: if organization is not None: if image is not None: Qita.objects.filter(id=pk).update(type=type, code=name, qitaid=qitaid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) qita.image = image qita.save() messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/qita/') else: Qita.objects.filter(id=pk).update(type=type, code=name, qitaid=qitaid, alias=alias, organization_id=o_id, status=status, identificationcode=identificationcode, function=function, articleurl=articleurl) messages.success(request, '修改成功') return HttpResponseRedirect('/management/newmedia/management/edit/qita/') else: messages.success(request, "请选择单位") return render(request, 'management/newmedia-management-update-qita.html', {'qita': qita, 'organization': organization, 'qita_status_choices_list': qita_status_choices_list}) else: messages.success(request, "新媒体名称不能为空") return render(request, 'management/newmedia-management-update-qita.html', {'qita': qita, 'organization': organization, 'qita_status_choices_list': qita_status_choices_list}) return render(request, 'management/newmedia-management-update-qita.html', {'qita': qita, 'organization': organization, 'qita_status_choices_list': qita_status_choices_list}) @login_required def newmedia_management_delete_weixin(request, pk): weixin = Weixin.objects.get(id=pk) weixin.delete() messages.success(request, "删除成功") return HttpResponseRedirect('/management/newmedia/management/edit/weixin/') @login_required def newmedia_management_delete_weibo(request, pk): weibo = Weibo.objects.get(id=pk) weibo.delete() messages.success(request, "删除成功") return HttpResponseRedirect('/management/newmedia/management/edit/weibo/') @login_required def newmedia_management_delete_toutiao(request, pk): toutiao = Toutiao.objects.get(id=pk) toutiao.delete() messages.success(request, "删除成功") return HttpResponseRedirect('/management/newmedia/management/edit/toutiao/') @login_required def newmedia_management_delete_douyin(request, pk): douyin = Douyin.objects.get(id=pk) douyin.delete() messages.success(request, "删除成功") return HttpResponseRedirect('/management/newmedia/management/edit/douyin/') @login_required def newmedia_management_delete_qita(request, pk): qita = Qita.objects.get(id=pk) qita.delete() messages.success(request, "删除成功") return HttpResponseRedirect('/management/newmedia/management/edit/qita/') @login_required def new_management(request): news_count = News.objects.all().count() news = News.objects.all() paginator = Paginator(news, 6) page = int(request.GET.get('page', 1)) try: news = paginator.page(page) except PageNotAnInteger: news = paginator.page(1) except EmptyPage: news = paginator.page(paginator.num_pages) news_list = [] for n in news: o = dict() o['id'] = str(n.id) o['title'] = n.title o['date'] = n.date o['author'] = n.author if n.type == '0': o['type'] = '政策依据' elif n.type == '1': o['type'] = '基层动态' elif n.type == '2': o['type'] = '外省动态' elif n.type == '3': o['type'] = '监测通报' elif n.type == '4': o['type'] = '舆情热点' elif n.type == '5': o['type'] = '通知' elif n.type == '6': o['type'] = '重点新闻' news_list.append(o) return render(request, 'management/news-management.html', {'news': news_list, 'new': news, 'news_count': news_count}) @login_required def news_management_create(request): if request.method == 'POST': type = request.POST.get('type') title = request.POST.get('title') author = request.POST.get('author') date = request.POST.get('date') content = request.POST.get('content') source = request.POST.get('source') image = request.FILES.get('image') t = None if type == '政策依据': t = '0' elif type == "基层动态": t = '1' elif type == '外省动态': t = '2' elif type == '监测通报': t = '3' elif type == '舆情热点': t = '4' elif type == '通知': t = '5' elif type == '重点新闻': t = '6' news = News(type=t, title=title, author=author, date=date, content=content, source=source, image=image) news.save() if t == '5': profiles = Userprofile.objects.filter(status=1) content = '管理员发布了通知,请查收消息' phones = [] for p in profiles: phones.append(p.user.username) Notice.create_news_notice(p.user_id, content, news.id) send_tnps(phones, '通知', content) messages.success(request, '添加成功!') return HttpResponseRedirect('/management/news/management/create/') type = News.NEWMEDIA_NEWS_CHOICES results = [] for i in type: o = dict() o['choices'] = list(i)[1] results.append(o) return render(request, 'management/news-management-create.html', {'type': results}) @login_required def news_management_update(request, pk): news = News.objects.get(id=pk) type = News.NEWMEDIA_NEWS_CHOICES results = [] for i in type: o = dict() o['choices'] = list(i)[1] results.append(o) if request.method == 'POST': type = request.POST.get('type') title = request.POST.get('title') author = request.POST.get('author') content = request.POST.get('content') source = request.POST.get('source') t = None if type == '政策依据': t = '0' elif type == "基层动态": t = '1' elif type == '外省动态': t = '2' elif type == '监测通报': t = '3' elif type == '舆情热点': t = '4' elif type == '通知': t = '5' elif type == '重点新闻': t = '6' if request.FILES.get('image'): image = request.FILES.get('image') n = News.objects.get(id=pk) if request.POST.get('date'): date = request.POST.get('date') News.objects.filter(id=pk).update(type=t, title=title, author=author, date=date, content=content, source=source) n.image = image n.save() else: News.objects.filter(id=pk).update(type=t, title=title, author=author, content=content, source=source) n.image = image n.save() else: News.objects.filter(id=pk).update(type=t, title=title, author=author, content=content, source=source) messages.success(request, '编辑成功') return HttpResponseRedirect('/management/news/management/') return render(request, 'management/news-management-update.html', {'news': news, 'type': results}) @login_required def news_management_detail(request, pk): news = News.objects.get(id=pk) return render(request, 'management/news-management-detail.html', {'news': news}) @login_required def news_management_delete(request, pk): news = News.objects.get(id=pk) news.delete() messages.success(request, '删除成功') return HttpResponseRedirect('/management/news/management/') @login_required def newmedia_detail_weixin(request, pk): weixin = Weixin.objects.all().order_by('-created') return render(request, 'management/newmedia-detail-weixin.html', {'weixin': weixin}) @login_required def group_user_search(request,pk): keyword = request.POST.get('keyword') group = Group.objects.get(id=pk) p = '' c = '' d = '' t = '' v = '' if group.province != '' and group.province != 'None': p = Area_code_2020.objects.get(code=group.province).name if group.cities and group.cities != 'None': c = Area_code_2020.objects.get(code=group.cities).name if group.district and group.district != 'None': d = Area_code_2020.objects.get(code=group.district).name if group.town and group.town != 'None': t = Area_code_2020.objects.get(code=group.town).name if group.village and group.village != 'None': v = Area_code_2020.objects.get(code=group.village).name group_status_choices_list = [] for g in range(2): group_status_choices_list.append(g) GROUP_CLASSIFY_CHOICES = Group.GROUP_CLASSIFY_CHOICES g_c_c_res = [] for g_c_c in GROUP_CLASSIFY_CHOICES: g_c_c_res.append([list(g_c_c)[0], list(g_c_c)[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) if Userprofile.objects.get(user_id=g_a.user.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 admin_p = Userprofile.objects.get(user_id=g_a.user_id).organization.province admin_c = Userprofile.objects.get(user_id=g_a.user_id).organization.cities admin_d = Userprofile.objects.get(user_id=g_a.user_id).organization.district admin_t = Userprofile.objects.get(user_id=g_a.user_id).organization.town admin_v = Userprofile.objects.get(user_id=g_a.user_id).organization.village if admin_p and admin_p != 'None' and admin_c and admin_c != 'None' and admin_d and admin_d != 'None' and admin_t and admin_t != 'None' and admin_v and admin_v != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) + '-' + str( Area_code_2020.objects.get(code=admin_d).name) + '-' + str( Area_code_2020.objects.get(code=admin_t).name) + '-' + str( Area_code_2020.objects.get(code=admin_v).name) elif admin_p and admin_p != 'None' and admin_c and admin_c != 'None' and admin_d and admin_d != 'None' and admin_t and admin_t != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) + '-' + str( Area_code_2020.objects.get(code=admin_d).name) + '-' + str( Area_code_2020.objects.get(code=admin_t).name) elif admin_p and admin_p != 'None' and admin_c and admin_c != 'None' and admin_d and admin_d != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) + '-' + str( Area_code_2020.objects.get(code=admin_d).name) elif admin_p and admin_p != 'None' and admin_c and admin_c != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=admin_p).name) + '-' + str( Area_code_2020.objects.get(code=admin_c).name) elif admin_p and admin_p != 'None': o['administrativedivision'] = str(Area_code_2020.objects.get(code=str(admin_p)).name) g_a_list.append(o) group_user = Group_user.objects.filter(group_id=pk,user__userprofile__name__contains=keyword) paginator = Paginator(group_user, 6) page = int(request.GET.get('page', 1)) try: group_user = paginator.page(page) except PageNotAnInteger: group_user = paginator.page(1) except EmptyPage: group_user = paginator.page(paginator.num_pages) 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 user_p = Userprofile.objects.get(user_id=g_u.user_id).organization.province user_c = Userprofile.objects.get(user_id=g_u.user_id).organization.cities user_d = Userprofile.objects.get(user_id=g_u.user_id).organization.district user_t = Userprofile.objects.get(user_id=g_u.user_id).organization.town user_v = Userprofile.objects.get(user_id=g_u.user_id).organization.village if user_p and user_p != 'None' and user_c and user_c != 'None' and user_d and user_d != 'None' and user_t and user_t != 'None' and user_v and user_v != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) + '-' + str( Area_code_2020.objects.get(code=user_d).name) + '-' + str( Area_code_2020.objects.get(code=user_t).name) + '-' + str( Area_code_2020.objects.get(code=user_v).name) elif user_p and user_p != 'None' and user_c and user_c != 'None' and user_d and user_d != 'None' and user_t and user_t != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) + '-' + str( Area_code_2020.objects.get(code=user_d).name) + '-' + str( Area_code_2020.objects.get(code=user_t).name) elif user_p and user_p != 'None' and user_c and user_c != 'None' and user_d and user_d != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) + '-' + str( Area_code_2020.objects.get(code=user_d).name) elif user_p and user_p != 'None' and user_c and user_c != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=user_p).name) + '-' + str( Area_code_2020.objects.get(code=user_c).name) elif user_p and user_p != 'None': i['administrativedivision'] = str(Area_code_2020.objects.get(code=str(user_p)).name) g_u_list.append(i) 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, 'g_c_c_res': g_c_c_res, 'p': p, 'c': c, 'd': d, 't': t, 'v': v, 'page': group_user})