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.http import Http404, HttpResponse
|
||||||
from django.shortcuts import get_object_or_404, redirect
|
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
|
from mainsite.models import Publication, PublicationYear, SiteConfiguration
|
||||||
|
|
||||||
|
@ -23,72 +23,62 @@ class WriteArticleView(TemplateView):
|
||||||
template_name = "mainsite/write_article.html"
|
template_name = "mainsite/write_article.html"
|
||||||
|
|
||||||
|
|
||||||
class PublicationListView(TemplateView):
|
class PublicationListView(ListView):
|
||||||
"""Display a list of publications (generic class).
|
"""
|
||||||
|
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."""
|
|
||||||
|
|
||||||
|
model = Publication
|
||||||
|
context_object_name = "publications"
|
||||||
template_name = "mainsite/publications_list_view.html"
|
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):
|
def get_context_data(self, **kwargs):
|
||||||
self.initView(**kwargs)
|
ctx = super().get_context_data(**kwargs)
|
||||||
|
|
||||||
context = super(PublicationListView, self).get_context_data(**kwargs)
|
if len(ctx["publications"]) == 0:
|
||||||
context.update(self.additional_context(**kwargs))
|
|
||||||
|
|
||||||
publications = self.get_publications(**kwargs)
|
|
||||||
|
|
||||||
if len(publications) == 0:
|
|
||||||
raise Http404
|
raise Http404
|
||||||
context["publications"] = publications
|
|
||||||
|
|
||||||
return context
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
class YearView(PublicationListView):
|
class YearView(PublicationListView):
|
||||||
"""Display a year worth of BOcals"""
|
"""Display a year worth of BOcals"""
|
||||||
|
|
||||||
def initView(self, year, nYear):
|
def get_queryset(self):
|
||||||
try:
|
year: int = self.kwargs.get("year")
|
||||||
year, nYear = int(year), int(nYear)
|
nYear: int = self.kwargs.get("nYear")
|
||||||
except ValueError:
|
|
||||||
|
if nYear != year + 1:
|
||||||
raise Http404
|
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 self.publication_year.publis()
|
||||||
return {
|
|
||||||
"intro_text": self.pubYear.descr,
|
|
||||||
"is_year_view": True,
|
|
||||||
"year_range": self.pubYear.prettyName,
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_publications(self, year, nYear):
|
def get_context_data(self, **kwargs):
|
||||||
return self.pubYear.publis()
|
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):
|
class SpecialPublicationsView(PublicationListView):
|
||||||
"""Display the list of special publications"""
|
"""Display the list of special publications"""
|
||||||
|
|
||||||
def additional_context(self):
|
ordering = "-date"
|
||||||
siteConf = SiteConfiguration.get_solo()
|
|
||||||
return {
|
|
||||||
"intro_text": siteConf.specialPublisDescr,
|
|
||||||
"is_year_view": False,
|
|
||||||
"list_title": "Numéros spéciaux",
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_publications(self):
|
def get_queryset(self):
|
||||||
publications = Publication.objects.filter(is_special=True).order_by("-date")
|
return super().get_queryset().filter(is_special=True)
|
||||||
return publications
|
|
||||||
|
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):
|
def latestPublication(req):
|
||||||
|
|
Loading…
Reference in a new issue