# from django.shortcuts import render from django.views.generic import TemplateView from django.http import Http404 from django.db.models import Q from datetime import date from mainsite.models import Publication 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' 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' def get_context_data(self, **kwargs): context = super(PublicationListView, self).get_context_data(**kwargs) publications = self.get_publications(**kwargs) if len(publications) == 0: raise Http404 context['publications'] = publications return context class YearView(PublicationListView): ''' Display a year worth of BOcals ''' def get_publications(self, year, nYear): try: year, nYear = int(year), int(nYear) except ValueError: raise Http404 if year + 1 != nYear: raise Http404 publications = Publication.objects.filter( Q(is_special=False) | Q(in_year_view_anyway=True), date__gte=date(year, 8, 1), date__lt=date(nYear, 8, 1)) return publications 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