www-bocal/mainsite/views.py

87 lines
2.3 KiB
Python

from django.http import Http404, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.views.generic import ListView, TemplateView
from mainsite.models import Publication, PublicationYear, SiteConfiguration
def robots_view(request):
"""Robots.txt view"""
body = "User-Agent: *\nDisallow: /\nAllow: /$\n"
return HttpResponse(body, content_type="text/plain")
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(ListView):
"""
Display a list of publications (generic class).
"""
model = Publication
context_object_name = "publications"
template_name = "mainsite/publications_list_view.html"
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
if len(ctx["publications"]) == 0:
raise Http404
return ctx
class YearView(PublicationListView):
"""Display a year worth of BOcals"""
def get_queryset(self):
year: int = self.kwargs.get("year")
nYear: int = self.kwargs.get("nYear")
if nYear != year + 1:
raise Http404
self.publication_year = get_object_or_404(PublicationYear, startYear=year)
return self.publication_year.publis()
def get_context_data(self, **kwargs):
return super().get_context_data(
intro_text=self.publication_year.descr,
is_year_view=True,
year_range=self.publication_year.prettyName,
**kwargs
)
class SpecialPublicationsView(PublicationListView):
"""Display the list of special publications"""
ordering = "-date"
def get_queryset(self):
return super().get_queryset().filter(is_special=True)
def get_context_data(self, **kwargs):
return super().get_context_data(
intro_text=SiteConfiguration.get_solo().specialPublisDescr,
is_year_view=False,
list_title="Numéros spéciaux",
**kwargs
)
def latestPublication(req):
"""Redirects to the latest standard publication"""
latestPubli = Publication.latest()
return redirect(latestPubli.url)