From 10f29d8264f2d9ffbaa233ded2b5b136d4c7f6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Thu, 21 Sep 2017 14:00:10 +0200 Subject: [PATCH] Add a simple view for a year --- mainsite/urls.py | 7 +++++++ mainsite/views.py | 25 +++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 mainsite/urls.py diff --git a/mainsite/urls.py b/mainsite/urls.py new file mode 100644 index 0000000..a41c5cb --- /dev/null +++ b/mainsite/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url +from .views import YearView + +urlpatterns = [ + url(r'^(?P\d{4})-(?P\d{4})/', + YearView.as_view(), name='year_view'), +] diff --git a/mainsite/views.py b/mainsite/views.py index 91ea44a..a7a83e4 100644 --- a/mainsite/views.py +++ b/mainsite/views.py @@ -1,3 +1,24 @@ -from django.shortcuts import render +# from django.shortcuts import render +from django.views.generic import TemplateView +from mainsite.models import Publication +from django.http import Http404 +from datetime import datetime -# Create your views here. + +class YearView(TemplateView): + ''' Display a year worth of BOcals ''' + template_name = 'mainsite/year_view.html' + + def get_context_data(self, year, nYear, **kwargs): + context = super(YearView, self).get_context_data(**kwargs) + if year + 1 != nYear: + raise Http404 + + publications = Publication.objects.filter( + date__ge=datetime(year, 8, 1), + date__lt=datetime(nYear, 8, 1)) + if len(publications) == 0: + raise Http404 + context['publications'] = publications + + return context