feat(mainsite/views): Simplify the list views
This commit is contained in:
parent
0c35609496
commit
3681bed05d
1 changed files with 35 additions and 45 deletions
|
@ -1,6 +1,6 @@
|
|||
from django.http import Http404, HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.views.generic import TemplateView
|
||||
from django.views.generic import ListView, TemplateView
|
||||
|
||||
from mainsite.models import Publication, PublicationYear, SiteConfiguration
|
||||
|
||||
|
@ -23,72 +23,62 @@ class WriteArticleView(TemplateView):
|
|||
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."""
|
||||
class PublicationListView(ListView):
|
||||
"""
|
||||
Display a list of publications (generic class).
|
||||
"""
|
||||
|
||||
model = Publication
|
||||
context_object_name = "publications"
|
||||
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)
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
||||
context = super(PublicationListView, self).get_context_data(**kwargs)
|
||||
context.update(self.additional_context(**kwargs))
|
||||
|
||||
publications = self.get_publications(**kwargs)
|
||||
|
||||
if len(publications) == 0:
|
||||
if len(ctx["publications"]) == 0:
|
||||
raise Http404
|
||||
context["publications"] = publications
|
||||
|
||||
return context
|
||||
return ctx
|
||||
|
||||
|
||||
class YearView(PublicationListView):
|
||||
"""Display a year worth of BOcals"""
|
||||
|
||||
def initView(self, year, nYear):
|
||||
try:
|
||||
year, nYear = int(year), int(nYear)
|
||||
except ValueError:
|
||||
def get_queryset(self):
|
||||
year: int = self.kwargs.get("year")
|
||||
nYear: int = self.kwargs.get("nYear")
|
||||
|
||||
if nYear != year + 1:
|
||||
raise Http404
|
||||
if year + 1 != nYear:
|
||||
raise Http404
|
||||
self.year = year
|
||||
|
||||
self.pubYear = get_object_or_404(PublicationYear, startYear=year)
|
||||
self.publication_year = get_object_or_404(PublicationYear, startYear=year)
|
||||
|
||||
def additional_context(self, year, nYear):
|
||||
return {
|
||||
"intro_text": self.pubYear.descr,
|
||||
"is_year_view": True,
|
||||
"year_range": self.pubYear.prettyName,
|
||||
}
|
||||
return self.publication_year.publis()
|
||||
|
||||
def get_publications(self, year, nYear):
|
||||
return self.pubYear.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"""
|
||||
|
||||
def additional_context(self):
|
||||
siteConf = SiteConfiguration.get_solo()
|
||||
return {
|
||||
"intro_text": siteConf.specialPublisDescr,
|
||||
"is_year_view": False,
|
||||
"list_title": "Numéros spéciaux",
|
||||
}
|
||||
ordering = "-date"
|
||||
|
||||
def get_publications(self):
|
||||
publications = Publication.objects.filter(is_special=True).order_by("-date")
|
||||
return publications
|
||||
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):
|
||||
|
|
Loading…
Reference in a new issue