newmediamonitoring/polls/views/news.py

48 lines
1.6 KiB
Python

from django.shortcuts import render
from dashboard.models import News
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from polls.decorators import polls_login_required
@csrf_exempt
@polls_login_required
def news_list(request):
category = request.GET.get('category', '1')
news_list = News.objects.filter(type=category).order_by('-date')
results = []
for o in news_list:
result = dict()
result['id'] = o.id
result['title'] = o.title
result['author'] = o.author
result['source'] = o.source
result['image'] = request.build_absolute_uri(o.image.url) if o.image else ''
result['date'] = o.date.strftime("%Y-%m-%d")
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
@csrf_exempt
@polls_login_required
def news_top(request):
category = request.GET.get('category', '3')
news_list = News.objects.filter(type=category)[:5]
results = []
for o in news_list:
result = dict()
result['id'] = o.id
result['title'] = o.title
result['author'] = o.author
result['source'] = o.source
result['image'] = request.build_absolute_uri(o.image.url) if o.image else ''
result['date'] = o.date.strftime("%Y-%m-%d")
results.append(result)
return JsonResponse({'status': 'success', 'message': results}, safe=False)
def news_detail(request, news_id):
news = News.objects.get(pk=news_id)
return render(request, 'polls/news_detail.html', {'news': news})