add crud for organization

This commit is contained in:
baoliang 2021-04-24 16:50:15 +08:00
parent 9af96aad1d
commit bfe9ad0fba
8 changed files with 153 additions and 23 deletions

View File

@ -205,6 +205,10 @@ class Userprofile(models.Model):
def level1_admin(cls):
return Userprofile.objects.filter(Q(organization__level__level=1) | Q(organization__level__level=9))
@classmethod
def count(cls, organization_id):
return Userprofile.objects.filter(organization_id=organization_id).exclude(admin_status=0).count()
def __str__(self):
if self.is_level1():
return '省级管理员%s' % (self.name,)
@ -254,6 +258,20 @@ class NewMedia(models.Model):
abstract = True
ordering = ["-created"]
@classmethod
def count(cls, organization_id):
t1 = Weixin.objects.exclude(status=0).filter(
organization_id=organization_id).count()
t2 = Weibo.objects.exclude(status=0).filter(
organization_id=organization_id).count()
t3 = Toutiao.objects.exclude(status=0).filter(
organization_id=organization_id).count()
t4 = Douyin.objects.exclude(status=0).filter(
organization_id=organization_id).count()
t5 = Qita.objects.exclude(status=0).filter(
organization_id=organization_id).count()
return t1 + t2 + t3 + t4 + t5
@classmethod
def category_one_count(cls):
t1 = Weixin.objects.exclude(

View File

@ -32,6 +32,8 @@ urlpatterns = [
path('medias/admin/my/', views.my_media_admins, name='polls_my_media_admins'),
path('medias/admin/change/', views.media_admin_change,
name='polls_media_admin_change'),
path('medias/admin/add/', views.media_admin_add,
name='polls_media_admin_add'),
path('medias/admin/delete/', views.media_admin_delete,
name='polls_media_admin_delete'),
path('medias/<str:type>/<str:media_id>/',
@ -75,6 +77,8 @@ urlpatterns = [
name="polls_organization_districts"),
path('organizations/create/', views.create_organization,
name='polls_add_organization'),
path('organizations/delete/', views.delete_organization,
name='polls_delete_organization'),
path('messages/list/last/', views.last_messages,
name='polls_messages_list_last'),
path('messages/send/text/', views.send_text_message,

View File

@ -1,11 +1,11 @@
from .user import index, status_500, status_401, polls_login, send_code, register_step_one, register_step_two, password_recover_step_one, password_recover_step_two, reset_password, upload_profile
from .notice import notices, notice_top, read_notice, pass_notice, reject_notice, detail_notice, unread_notice_count
from .media import medias, media_admins, my_medias, create_media, update_media, media_detail, delete_media, media_admin_change, my_media_admins, media_admin_delete
from .media import medias, media_admins, my_medias, create_media, update_media, media_detail, delete_media, media_admin_change, my_media_admins, media_admin_delete, media_admin_add
from .news import news_list, news_top, news_detail
from .monitor import monitor_statistics, monitor_result, media_statistics, city_ranking, monitor_unqualified_result, monitor_unqualified_index
from .task import tasks, create_task, create_test_task, get_task
from .group import groups, room, is_level1_or_leve2, group_member, group_manager, group_enter, group_leave, pick_groups
from .compartment import compartments
from .organizations import organizations, organization_level, organization_districts, create_organization
from .organizations import organizations, organization_level, organization_districts, create_organization, delete_organization
from .message import last_messages, send_text_message, is_read_message, read_message
from .download import download, app_intro, has_update

View File

@ -4,7 +4,7 @@ from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator
from datetime import datetime
from dashboard.models import Douyin, NewMedia, Qita, Toutiao, Userprofile, Weibo, Weixin
from dashboard.models import Douyin, NewMedia, Organization, Qita, Toutiao, Userprofile, Weibo, Weixin
from polls.decorators import polls_login_required
from polls.models import Notice
from django.contrib.auth import get_user_model
@ -31,7 +31,7 @@ def media_admins(request):
results = []
for p in profiles:
result = dict()
result['id'] = p.id
result['id'] = p.user.id
result['name'] = p.name
result['phone'] = p.user.username
result['adminStatus'] = p.admin_status
@ -64,7 +64,7 @@ def my_media_admins(request):
results = []
for p in profiles:
result = dict()
result['id'] = p.id
result['id'] = p.user.id
result['name'] = p.name
result['phone'] = p.user.username
result['adminStatus'] = p.admin_status
@ -81,6 +81,7 @@ def create_media(request):
return HttpResponse(status=405)
f = request.FILES.get('file')
organization_id = request.POST.get('organization_id', o.id)
organization = Organization.objects.get(id=organization_id)
category = request.POST.get('category')
if not category:
return JsonResponse({'status': 'error', 'message': '类型错误'})
@ -96,7 +97,7 @@ def create_media(request):
attention = request.POST.get('attention', '')
remark = request.POST.get('remark', '')
model_name = ''
status = 2 if o.is_direct() else 1
status = 2 if organization.is_direct() else 1
if category == 'weixin':
model_name = 'Weixin'
@ -120,7 +121,7 @@ def create_media(request):
instance = Qita(code=code, qitaid=media_id, alias=alias,
image=f, status=status, organization_id=organization_id, attention=attention, remark=remark)
instance.save()
if o.is_direct():
if organization.is_direct():
admins = Userprofile.level1_admin()
content = '%s创建了新媒体,请审核' % (profile,)
phones = []
@ -138,7 +139,7 @@ def create_media(request):
Notice.create_reply_notice(
a.user.id, content, 'dashboard', model_name, 'status', instance.id, '2', '0')
send_tnps(phones, '审核', content)
return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': organization_id}}, safe=False)
return JsonResponse({'status': 'success', 'message': {'title': organization.name, 'organizationId': organization_id}}, safe=False)
@csrf_exempt
@ -162,19 +163,24 @@ def update_media(request):
if category == 'weixin':
instance = Weixin.objects.get(pk=media_id)
result['media_id'] = instance.weixinid
result['type'] = '微信'
elif category == 'weibo':
instance = Weibo.objects.get(pk=media_id)
result['media_id'] = instance.weiboid
result['type'] = '微博'
elif category == 'toutiao':
instance = Toutiao.objects.get(pk=media_id)
result['media_id'] = instance.toutiaoid
result['type'] = '头条'
elif category == 'douyin':
instance = Douyin.objects.get(pk=media_id)
result['media_id'] = instance.douyinid
result['type'] = '抖音'
else:
instance = Qita.objects.get(pk=media_id)
result['media_id'] = instance.qitaid
result['type'] = instance.type
setattr(instance, name, value)
instance.save(update_fields=[name])
admins = []
@ -183,13 +189,13 @@ def update_media(request):
u['id'] = admin.user.id
u['name'] = admin.name
u['phone'] = admin.user.username
u['adminStatus'] = admin.admin_status
admins.append(u)
result['id'] = instance.id
result['code'] = instance.code
result['alias'] = instance.alias
result['status'] = instance.status
result['type'] = instance.type
result['attention'] = instance.attention
result['remark'] = instance.remark
result['organization'] = instance.organization.name
@ -248,7 +254,6 @@ def delete_media(request):
if request.method == 'GET':
return HttpResponse(status=405)
profile = request.user.userprofile_set.first()
o = profile.organization
category = request.POST.get('category')
if not category:
return JsonResponse({'status': 'error', 'message': '参数错误'})
@ -276,7 +281,7 @@ def delete_media(request):
model_name = 'Qita'
instance = Qita.objects.get(pk=media_id)
result['media_id'] = instance.qitaid
if o.is_direct():
if instance.organization.is_direct():
instance.status = 5
instance.save()
managers = Userprofile.level1_admin()
@ -298,7 +303,7 @@ def delete_media(request):
Notice.create_reply_notice(
a.user.id, content, 'dashboard', model_name, 'status', instance.id, '5', '3')
send_tnps(phones, '审核', content)
return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}}, safe=False)
return JsonResponse({'status': 'success', 'message': {'title': instance.organization.name, 'organizationId': instance.organization.id}}, safe=False)
@csrf_exempt
@ -307,6 +312,7 @@ def media_admin_change(request):
if request.method == 'GET':
return HttpResponse(status=405)
id = request.POST.get('id')
print(id)
if not id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
name = request.POST.get('name')
@ -328,8 +334,9 @@ def media_admin_change(request):
p = Userprofile.objects.create(
user=obj, name=name, organization=o, admin_status=2)
else:
obj.admin_status = 2
obj.save()
p = Userprofile.objects.filter(user=obj).first()
p.admin_status = 2
p.save()
profile.admin_status = 5
profile.save()
managers = Userprofile.level1_admin()
@ -348,8 +355,9 @@ def media_admin_change(request):
p = Userprofile.objects.create(
user=obj, name=name, organization=o, admin_status=1)
else:
obj.admin_status = 1
obj.save()
p = Userprofile.objects.filter(user=obj).first()
p.admin_status = 2
p.save()
profile.admin_status = 4
profile.save()
managers = Userprofile.level2_admin(o.cities)
@ -363,6 +371,67 @@ def media_admin_change(request):
return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}})
@csrf_exempt
@polls_login_required
def media_admin_add(request):
if request.method == 'GET':
return HttpResponse(status=405)
organization_id = request.POST.get('organization_id')
if not organization_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
name = request.POST.get('name')
if not name:
return JsonResponse({'status': 'error', 'message': '姓名不能为空'})
phone = request.POST.get('phone')
if not phone:
return JsonResponse({'status': 'error', 'message': '手机号不能为空'})
u = request.user
operator = Userprofile.objects.filter(user=u).first()
o = Organization.objects.get(id=organization_id)
UserModel = get_user_model()
if o.is_direct(): # 如果是省直
obj, created = UserModel.objects.get_or_create(
username=phone, last_login=datetime.now())
if created:
p = Userprofile.objects.create(
user=obj, name=name, organization=o, admin_status=2)
else:
p = Userprofile.objects.filter(user=obj).first()
p.admin_status = 2
p.save()
managers = Userprofile.level1_admin()
content = '%s申请添加管理员,请审核' % (operator,)
phones = []
for a in managers:
phones.append(a.user.username)
Notice.create_reply_notice(
a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '3', '0')
send_tnps(phones, '审核', content)
return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}})
else:
obj, created = UserModel.objects.get_or_create(
username=phone, last_login=datetime.now())
if created:
p = Userprofile.objects.create(
user=obj, name=name, organization=o, admin_status=1)
else:
p = Userprofile.objects.filter(user=obj).first()
p.admin_status = 1
p.save()
managers = Userprofile.level2_admin(o.cities)
content = '%s申请添加管理员,请审核' % (operator,)
phones = []
for a in managers:
phones.append(a.user.username)
Notice.create_reply_notice(
a.user.id, content, 'dashboard', 'Userprofile', 'admin_status', o.id, '2', '0')
send_tnps(phones, '审核', content)
return JsonResponse({'status': 'success', 'message': {'title': o.name, 'organizationId': o.id}})
@csrf_exempt
@polls_login_required
def media_admin_delete(request):

View File

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