newmediamonitoring1/polls/views/media.py

163 lines
5.9 KiB
Python

from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator
from dashboard.models import NewMedia, Weixin, Weibo, Toutiao, Douyin, Qita
from polls.decorators import polls_login_required
@csrf_exempt
@polls_login_required
def medias(request):
if request.method == 'POST':
return HttpResponse(status=405)
organization_id = request.GET.get('organization_id')
new_medias = NewMedia.media_list(organization_id)
return JsonResponse({'status': 'success', 'message': new_medias}, safe=False)
@csrf_exempt
@polls_login_required
def my_medias(request):
if request.method == 'POST':
return HttpResponse(status=405)
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)
@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)
category = request.POST.get('category')
if not category:
return JsonResponse({'status': 'error', 'message': '类型错误'})
code = request.POST.get('code')
if not code:
return JsonResponse({'status': 'error', 'message': '公众号名称不能为空'})
media_id = request.POST.get('media_id')
if not media_id:
return JsonResponse({'status': 'error', 'message': '公众号ID不能为空'})
alias = request.POST.get('alias')
if not alias:
return JsonResponse({'status': 'error', 'message': '别名不能为空'})
if category == 'weixin':
instance = Weixin(code=code, weixinid=media_id, alias=alias,
image=request.FILES['file'], status='0', organization=o)
elif category == 'weibo':
instance = Weibo(code=code, weiboid=media_id, alias=alias,
image=request.FILES['file'], status='0', organization=o)
elif category == 'toutiao':
instance = Toutiao(code=code, toutiaoid=media_id, alias=alias,
image=request.FILES['file'], status='0', organization=o)
elif category == 'douyin':
instance = Douyin(code=code, douyinid=media_id, alias=alias,
image=request.FILES['file'], status='0', organization=o)
else:
type = request.POST.get('type')
instance = Qita(code=code, qitaid=media_id, alias=alias,
image=request.FILES['file'], status='0', organization=o)
instance.save()
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')
if not name:
return JsonResponse({'status': 'error', 'message': '值不能为空'})
result = dict()
if category == 'weixin':
instance = Weixin.objects.get(pk=media_id)
result['media_id'] = instance.weixinid
elif category == 'weibo':
instance = Weibo.objects.get(pk=media_id)
result['media_id'] = instance.weiboid
elif category == 'toutiao':
instance = Toutiao.objects.get(pk=media_id)
result['media_id'] = instance.toutiaoid
elif category == 'douyin':
instance = Douyin.objects.get(pk=media_id)
result['media_id'] = instance.douyinid
else:
instance = Qita.objects.get(pk=media_id)
result['media_id'] = instance.qitaid
setattr(instance, name, value)
instance.save(update_fields=[name])
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
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)