newmediamonitoring1/polls/views/media.py

259 lines
9.5 KiB
Python
Raw Normal View History

2020-09-03 09:46:04 +00:00
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
2020-09-04 14:42:41 +00:00
from django.core.paginator import Paginator
2020-09-26 01:47:24 +00:00
from dashboard.models import Douyin, NewMedia, Qita, Toutiao, Userprofile, Weibo, Weixin
2020-09-03 09:46:04 +00:00
from polls.decorators import polls_login_required
2020-09-26 01:47:24 +00:00
from polls.models import Notice
2020-09-03 09:46:04 +00:00
2020-09-04 14:42:41 +00:00
@csrf_exempt
@polls_login_required
def medias(request):
if request.method == 'POST':
return HttpResponse(status=405)
2020-09-22 03:48:12 +00:00
organization_id = request.GET.get('organization_id')
new_medias = NewMedia.media_list(organization_id)
return JsonResponse({'status': 'success', 'message': new_medias}, safe=False)
2020-09-04 14:42:41 +00:00
2020-09-03 09:46:04 +00:00
@csrf_exempt
@polls_login_required
2020-09-23 09:47:05 +00:00
def my_medias(request):
if request.method == 'POST':
2020-09-03 09:46:04 +00:00
return HttpResponse(status=405)
2020-09-23 09:47:05 +00:00
user = request.user
profile = user.userprofile_set.first()
organization_id = profile.organization.id
new_medias = NewMedia.media_list(organization_id)
return JsonResponse({'status': 'success', 'message': new_medias}, safe=False)
2020-09-22 07:39:33 +00:00
@csrf_exempt
@polls_login_required
def create_media(request):
profile = request.user.userprofile_set.first()
o = profile.organization
if request.method == 'GET':
return HttpResponse(status=405)
2020-09-26 01:47:24 +00:00
f = request.FILES.get('file')
2020-09-22 07:39:33 +00:00
category = request.POST.get('category')
if not category:
return JsonResponse({'status': 'error', 'message': '类型错误'})
code = request.POST.get('code')
if not code:
2020-09-26 01:47:24 +00:00
return JsonResponse({'status': 'error', 'message': '名称不能为空'})
2020-09-22 07:39:33 +00:00
media_id = request.POST.get('media_id')
if not media_id:
2020-09-26 01:47:24 +00:00
return JsonResponse({'status': 'error', 'message': '监测ID不能为空'})
2020-09-22 07:39:33 +00:00
alias = request.POST.get('alias')
if not alias:
return JsonResponse({'status': 'error', 'message': '别名不能为空'})
2020-09-26 01:47:24 +00:00
attention = request.POST.get('attention', '')
remark = request.POST.get('remark', '')
model_name = ''
2020-10-09 12:09:15 +00:00
status = 2 if o.directly == '省直部门' else 1
2020-09-22 07:39:33 +00:00
if category == 'weixin':
2020-09-26 01:47:24 +00:00
model_name = 'Weixin'
2020-09-22 07:39:33 +00:00
instance = Weixin(code=code, weixinid=media_id, alias=alias,
2020-10-09 12:09:15 +00:00
image=f, status=status, organization=o, attention=attention, remark=remark)
2020-09-22 07:39:33 +00:00
elif category == 'weibo':
2020-09-26 01:47:24 +00:00
model_name = 'Weibo'
2020-09-22 07:39:33 +00:00
instance = Weibo(code=code, weiboid=media_id, alias=alias,
2020-10-09 12:09:15 +00:00
image=f, status=status, organization=o, attention=attention, remark=remark)
2020-09-22 07:39:33 +00:00
elif category == 'toutiao':
2020-09-26 01:47:24 +00:00
model_name = 'Toutiao'
2020-09-22 07:39:33 +00:00
instance = Toutiao(code=code, toutiaoid=media_id, alias=alias,
2020-10-09 12:09:15 +00:00
image=f, status=status, organization=o, attention=attention, remark=remark)
2020-09-22 07:39:33 +00:00
elif category == 'douyin':
2020-09-26 01:47:24 +00:00
model_name = 'Douyin'
2020-09-22 07:39:33 +00:00
instance = Douyin(code=code, douyinid=media_id, alias=alias,
2020-10-09 12:09:15 +00:00
image=f, status=status, organization=o, attention=attention, remark=remark)
2020-09-22 07:39:33 +00:00
else:
2020-09-26 01:47:24 +00:00
model_name = 'Qita'
2020-09-22 07:39:33 +00:00
type = request.POST.get('type')
instance = Qita(code=code, qitaid=media_id, alias=alias,
2020-10-09 12:09:15 +00:00
image=f, status=status, organization=o, attention=attention, remark=remark)
2020-09-22 07:39:33 +00:00
instance.save()
2020-10-09 12:09:15 +00:00
if o.directly == '省直部门':
admins = Userprofile.level1_admin()
for a in admins:
2020-10-21 13:37:19 +00:00
content = '%s创建了新媒体,请审核' % (profile,)
2020-10-09 12:09:15 +00:00
Notice.create_reply_notice(
a.user.id, content, 'dashboard', model_name, 'status', instance.id, '3', '0')
else:
admins = Userprofile.level2_admin(instance.organization.cities)
for a in admins:
2020-10-21 13:37:19 +00:00
content = '%s创建了新媒体,请审核' % (profile,)
2020-10-09 12:09:15 +00:00
Notice.create_reply_notice(
a.user.id, content, 'dashboard', model_name, 'status', instance.id, '2', '0')
2020-09-26 01:47:24 +00:00
2020-09-22 07:39:33 +00:00
return JsonResponse({'status': 'success'})
@csrf_exempt
@polls_login_required
def update_media(request):
if request.method == 'GET':
return HttpResponse(status=405)
category = request.POST.get('category')
if not category:
return JsonResponse({'status': 'error', 'message': '参数错误'})
media_id = request.POST.get('media_id')
if not media_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
name = request.POST.get('name')
if not name:
return JsonResponse({'status': 'error', 'message': '名称不能为空'})
value = request.POST.get('value')
2020-10-08 07:12:48 +00:00
if not value:
2020-09-22 07:39:33 +00:00
return JsonResponse({'status': 'error', 'message': '值不能为空'})
2020-09-23 09:47:05 +00:00
result = dict()
2020-09-22 07:39:33 +00:00
if category == 'weixin':
instance = Weixin.objects.get(pk=media_id)
2020-09-23 09:47:05 +00:00
result['media_id'] = instance.weixinid
2020-09-22 07:39:33 +00:00
elif category == 'weibo':
instance = Weibo.objects.get(pk=media_id)
2020-09-23 09:47:05 +00:00
result['media_id'] = instance.weiboid
2020-09-22 07:39:33 +00:00
elif category == 'toutiao':
instance = Toutiao.objects.get(pk=media_id)
2020-09-23 09:47:05 +00:00
result['media_id'] = instance.toutiaoid
2020-09-22 07:39:33 +00:00
elif category == 'douyin':
instance = Douyin.objects.get(pk=media_id)
2020-09-23 09:47:05 +00:00
result['media_id'] = instance.douyinid
2020-09-22 07:39:33 +00:00
else:
instance = Qita.objects.get(pk=media_id)
2020-09-23 09:47:05 +00:00
result['media_id'] = instance.qitaid
2020-09-22 07:39:33 +00:00
setattr(instance, name, value)
instance.save(update_fields=[name])
2020-09-23 09:47:05 +00:00
admins = []
for admin in instance.organization.userprofile_set.all():
u = dict()
u['id'] = admin.user.id
u['name'] = admin.name
u['phone'] = admin.user.username
admins.append(u)
result['id'] = instance.id
result['code'] = instance.code
result['alias'] = instance.alias
result['status'] = instance.status
result['attention'] = instance.attention
result['remark'] = instance.remark
result['organization'] = instance.organization.name
result['admin'] = admins
return JsonResponse({'status': 'success', 'message': result})
@csrf_exempt
@polls_login_required
def media_detail(request, type, media_id):
result = dict()
if type == 'weixin':
media = Weixin.objects.get(pk=media_id)
result['media_id'] = media.weixinid
elif type == 'weibo':
media = Weibo.objects.get(pk=media_id)
result['media_id'] = media.weiboid
elif type == 'toutiao':
media = Toutiao.objects.get(pk=media_id)
result['media_id'] = media.toutiaoid
elif type == 'douyin':
media = Douyin.objects.get(pk=media_id)
result['media_id'] = media.douyinid
else:
media = Qita.objects.get(pk=media_id)
result['media_id'] = media.qitaid
2020-10-08 07:12:48 +00:00
2020-09-23 09:47:05 +00:00
admins = []
for admin in media.organization.userprofile_set.all():
u = dict()
u['id'] = admin.user.id
u['name'] = admin.name
u['phone'] = admin.user.username
admins.append(u)
result['id'] = media.id
result['code'] = media.code
result['alias'] = media.alias
result['status'] = media.status
result['attention'] = media.attention
result['remark'] = media.remark
result['organization'] = media.organization.name
result['admin'] = admins
return JsonResponse({'status': 'success', 'message': result}, safe=False)
2020-10-08 07:12:48 +00:00
@csrf_exempt
@polls_login_required
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': '参数错误'})
media_id = request.POST.get('media_id')
if not media_id:
return JsonResponse({'status': 'error', 'message': '参数错误'})
result = dict()
if category == 'weixin':
model_name = 'Weixin'
instance = Weixin.objects.get(pk=media_id)
result['media_id'] = instance.weixinid
elif category == 'weibo':
model_name = 'Weibo'
instance = Weibo.objects.get(pk=media_id)
result['media_id'] = instance.weiboid
elif category == 'toutiao':
model_name = 'Toutiao'
instance = Toutiao.objects.get(pk=media_id)
result['media_id'] = instance.toutiaoid
elif category == 'douyin':
model_name = 'Douyin'
instance = Douyin.objects.get(pk=media_id)
result['media_id'] = instance.douyinid
else:
model_name = 'Qita'
instance = Qita.objects.get(pk=media_id)
result['media_id'] = instance.qitaid
2020-10-09 12:09:15 +00:00
2020-10-21 13:37:19 +00:00
if o.is_direct():
2020-10-09 12:09:15 +00:00
instance.status = 5
instance.save()
managers = Userprofile.level1_admin()
for a in managers:
2020-10-21 13:37:19 +00:00
content = '%s申请注销新媒体,请审核' % (profile,)
2020-10-09 12:09:15 +00:00
Notice.create_reply_notice(
a.user.id, content, 'dashboard', model_name, 'status', instance.id, '0', '3')
else:
instance.status = 4
instance.save()
managers = Userprofile.level2_admin(instance.organization.cities)
for a in managers:
2020-10-21 13:37:19 +00:00
content = '%s申请注销新媒体,请审核' % (profile,)
2020-10-09 12:09:15 +00:00
Notice.create_reply_notice(
a.user.id, content, 'dashboard', model_name, 'status', instance.id, '5', '3')
2020-10-08 07:12:48 +00:00
admins = []
for admin in instance.organization.userprofile_set.all():
u = dict()
u['id'] = admin.user.id
u['name'] = admin.name
u['phone'] = admin.user.username
admins.append(u)
result['id'] = instance.id
result['code'] = instance.code
result['alias'] = instance.alias
result['status'] = instance.status
result['attention'] = instance.attention
result['remark'] = instance.remark
result['organization'] = instance.organization.name
result['admin'] = admins
return JsonResponse({'status': 'success', 'message': result})