2017-09-23 18:48:29 +02:00
|
|
|
from django.shortcuts import get_object_or_404
|
2017-09-21 14:00:10 +02:00
|
|
|
from django.views.generic import TemplateView
|
|
|
|
from django.http import Http404
|
2017-09-21 19:38:49 +02:00
|
|
|
|
2017-09-23 18:48:29 +02:00
|
|
|
from mainsite.models import Publication, PublicationYear
|
2017-09-21 10:58:19 +02:00
|
|
|
|
2017-09-21 14:00:10 +02:00
|
|
|
|
2017-09-22 13:44:36 +02:00
|
|
|
class HomeView(TemplateView):
|
|
|
|
''' Website's homepage '''
|
|
|
|
template_name = 'mainsite/homepage.html'
|
|
|
|
|
|
|
|
|
|
|
|
class WriteArticleView(TemplateView):
|
|
|
|
''' Tell the readers how they can contribute to the BOcal '''
|
|
|
|
template_name = 'mainsite/write_article.html'
|
|
|
|
|
|
|
|
|
2017-09-22 19:03:43 +02:00
|
|
|
class PublicationListView(TemplateView):
|
|
|
|
''' Display a list of publications (generic class).
|
|
|
|
|
|
|
|
Reimplement `get_publications` (called with the url template args in a
|
|
|
|
place where you can Http404) in subclasses to get it working. '''
|
|
|
|
|
|
|
|
template_name = 'mainsite/publications_list_view.html'
|
|
|
|
|
2017-09-23 18:48:29 +02:00
|
|
|
def initView(self):
|
|
|
|
''' Cannot be __init__, we don't have **kwargs there '''
|
|
|
|
pass
|
|
|
|
|
2017-09-22 19:03:43 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
2017-09-23 18:48:29 +02:00
|
|
|
self.initView(**kwargs)
|
|
|
|
|
2017-09-22 19:03:43 +02:00
|
|
|
context = super(PublicationListView, self).get_context_data(**kwargs)
|
2017-09-23 18:48:29 +02:00
|
|
|
context.update(self.additional_context(**kwargs))
|
2017-09-22 19:03:43 +02:00
|
|
|
|
|
|
|
publications = self.get_publications(**kwargs)
|
2017-09-21 14:00:10 +02:00
|
|
|
|
2017-09-22 19:03:43 +02:00
|
|
|
if len(publications) == 0:
|
|
|
|
raise Http404
|
|
|
|
context['publications'] = publications
|
|
|
|
|
|
|
|
return context
|
2017-09-21 19:38:49 +02:00
|
|
|
|
2017-09-22 19:03:43 +02:00
|
|
|
|
|
|
|
class YearView(PublicationListView):
|
|
|
|
''' Display a year worth of BOcals '''
|
2017-09-23 18:48:29 +02:00
|
|
|
|
|
|
|
def initView(self, year, nYear):
|
2017-09-21 19:38:49 +02:00
|
|
|
try:
|
|
|
|
year, nYear = int(year), int(nYear)
|
|
|
|
except ValueError:
|
|
|
|
raise Http404
|
2017-09-21 14:00:10 +02:00
|
|
|
if year + 1 != nYear:
|
|
|
|
raise Http404
|
2017-09-23 18:48:29 +02:00
|
|
|
self.year = year
|
2017-09-21 14:00:10 +02:00
|
|
|
|
2017-09-23 18:48:29 +02:00
|
|
|
self.pubYear = get_object_or_404(PublicationYear, startYear=year)
|
2017-09-21 14:00:10 +02:00
|
|
|
|
2017-09-23 18:48:29 +02:00
|
|
|
def additional_context(self, year, nYear):
|
|
|
|
return {
|
|
|
|
'intro_text': self.pubYear.descr,
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_publications(self, year, nYear):
|
|
|
|
return self.pubYear.publis()
|
2017-09-22 19:03:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SpecialPublicationsView(PublicationListView):
|
|
|
|
''' Display the list of special publications '''
|
|
|
|
def get_publications(self):
|
|
|
|
publications = Publication.objects\
|
|
|
|
.filter(is_special=True)\
|
|
|
|
.order_by('-date')
|
|
|
|
return publications
|