newmediamonitoring/polls/views/media.py

77 lines
2.8 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 Weixin, Weibo, Toutiao, Qita
from polls.decorators import polls_login_required
@csrf_exempt
@polls_login_required
def medias(request):
if request.method == 'POST':
return HttpResponse(status=405)
category = request.POST.get('category', 'weixin')
if category == 'weixin':
q = Weixin.objects.filter(status=1)
elif category == 'weibo':
q = Weibo.objects.filter(status=1)
elif category == 'toutiao':
q = Toutiao.objects.filter(status=1)
else:
q = Qita.objects.filter(status=1)
results = []
for o in q:
result = dict()
result['id'] = o.id
result['code'] = o.code
if category == 'weixin':
result['media_id'] = o.weixinid
elif category == 'weibo':
result['media_id'] = o.weixinid
elif category == 'toutiao':
result['media_id'] = o.weixinid
else:
result['media_id'] = o.weixinid
result['alias'] = o.alias
result['thumbnail'] = request.build_absolute_uri(o.image.url)
results.append(result)
return JsonResponse(results, safe=False)
@csrf_exempt
@polls_login_required
def create_media(request):
profile = request.user.userprofile_set.first()
o = profile.organization
print(profile)
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=1, organization=o)
elif category == 'weibo':
instance = Weibo(code=code, weixinid=media_id, alias=alias,
image=request.FILES['file'], status=1, organization=o)
elif category == 'toutiao':
instance = Toutiao(code=code, weixinid=media_id, alias=alias,
image=request.FILES['file'], status=1, organization=o)
else:
instance = Qita(code=code, weixinid=media_id, alias=alias,
image=request.FILES['file'], status=1, organization=o)
instance.save()
return JsonResponse({'status': 'success'})