Fix YearView filters and types

This commit is contained in:
Théophile Bastian 2017-09-21 19:38:49 +02:00
parent b2c5d5c3c6
commit 0c34bc8f3e

View file

@ -1,8 +1,10 @@
# from django.shortcuts import render # from django.shortcuts import render
from django.views.generic import TemplateView from django.views.generic import TemplateView
from mainsite.models import Publication
from django.http import Http404 from django.http import Http404
from datetime import datetime
from datetime import date
from mainsite.models import Publication
class YearView(TemplateView): class YearView(TemplateView):
@ -11,12 +13,17 @@ class YearView(TemplateView):
def get_context_data(self, year, nYear, **kwargs): def get_context_data(self, year, nYear, **kwargs):
context = super(YearView, self).get_context_data(**kwargs) context = super(YearView, self).get_context_data(**kwargs)
try:
year, nYear = int(year), int(nYear)
except ValueError:
raise Http404
if year + 1 != nYear: if year + 1 != nYear:
raise Http404 raise Http404
publications = Publication.objects.filter( publications = Publication.objects.filter(
date__ge=datetime(year, 8, 1), date__gte=date(year, 8, 1),
date__lt=datetime(nYear, 8, 1)) date__lt=date(nYear, 8, 1))
if len(publications) == 0: if len(publications) == 0:
raise Http404 raise Http404
context['publications'] = publications context['publications'] = publications