www-bocal/mainsite/views.py

98 lines
2.7 KiB
Python
Raw Normal View History

from django.http import Http404, HttpResponse
2017-09-24 19:11:08 +02:00
from django.shortcuts import get_object_or_404, redirect
2017-09-21 14:00:10 +02:00
from django.views.generic import TemplateView
2017-09-21 19:38:49 +02:00
from mainsite.models import Publication, PublicationYear, SiteConfiguration
2017-09-21 10:58:19 +02:00
2017-09-21 14:00:10 +02:00
2019-01-19 17:28:24 +01:00
def robots_view(request):
"""Robots.txt view"""
2022-05-22 18:37:28 +02:00
body = "User-Agent: *\nDisallow: /\nAllow: /$\n"
2019-01-19 17:28:24 +01:00
return HttpResponse(body, content_type="text/plain")
class HomeView(TemplateView):
"""Website's homepage"""
2019-01-19 17:28:24 +01:00
template_name = "mainsite/homepage.html"
class WriteArticleView(TemplateView):
"""Tell the readers how they can contribute to the BOcal"""
2019-01-19 17:28:24 +01:00
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."""
2019-01-19 17:28:24 +01:00
template_name = "mainsite/publications_list_view.html"
def initView(self):
"""Cannot be __init__, we don't have **kwargs there"""
pass
def get_context_data(self, **kwargs):
self.initView(**kwargs)
context = super(PublicationListView, self).get_context_data(**kwargs)
context.update(self.additional_context(**kwargs))
publications = self.get_publications(**kwargs)
2017-09-21 14:00:10 +02:00
if len(publications) == 0:
raise Http404
2019-01-19 17:28:24 +01:00
context["publications"] = publications
return context
2017-09-21 19:38:49 +02:00
class YearView(PublicationListView):
"""Display a year worth of BOcals"""
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
self.year = year
2017-09-21 14:00:10 +02:00
self.pubYear = get_object_or_404(PublicationYear, startYear=year)
2017-09-21 14:00:10 +02:00
def additional_context(self, year, nYear):
return {
2019-01-19 17:28:24 +01:00
"intro_text": self.pubYear.descr,
"is_year_view": True,
"year_range": self.pubYear.prettyName,
}
def get_publications(self, year, nYear):
return self.pubYear.publis()
class SpecialPublicationsView(PublicationListView):
"""Display the list of special publications"""
def additional_context(self):
siteConf = SiteConfiguration.get_solo()
return {
2019-01-19 17:28:24 +01:00
"intro_text": siteConf.specialPublisDescr,
"is_year_view": False,
"list_title": "Numéros spéciaux",
}
def get_publications(self):
2019-01-19 17:28:24 +01:00
publications = Publication.objects.filter(is_special=True).order_by("-date")
return publications
2017-09-24 19:11:08 +02:00
def latestPublication(req):
"""Redirects to the latest standard publication"""
2017-09-24 19:11:08 +02:00
latestPubli = Publication.latest()
return redirect(latestPubli.url)