Handle special publications differently

This commit is contained in:
Théophile Bastian 2017-09-22 19:03:43 +02:00
parent 3f43ccdc4e
commit 72202a4630
7 changed files with 75 additions and 14 deletions

View file

@ -6,6 +6,11 @@ from . import models
def sidebar_years(req): def sidebar_years(req):
avail_years = models.PublicationYear.objects.all() avail_years = models.PublicationYear.objects.all()
publi_years = [year for year in avail_years if len(year.publis()) > 0] publi_years = [year for year in avail_years if len(year.publis()) > 0]
num_special_publications = models.Publication.objects\
.filter(is_special=True).count()
return { return {
'publication_years': publi_years, 'publication_years': publi_years,
'has_special_publications': num_special_publications > 0,
} }

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-09-22 17:01
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('mainsite', '0003_siteconfiguration'),
]
operations = [
migrations.AddField(
model_name='publication',
name='in_year_view_anyway',
field=models.BooleanField(default=False, help_text="Si le numéro est spécial, l'afficher quand même dans la page de l'année correspondante.", verbose_name="Aussi dans l'année"),
),
]

View file

@ -32,6 +32,11 @@ class Publication(models.Model):
is_special = BooleanField('Numéro spécial', is_special = BooleanField('Numéro spécial',
help_text='Numéro du BOcal non-numéroté', help_text='Numéro du BOcal non-numéroté',
default=False) default=False)
in_year_view_anyway = BooleanField(
"Aussi dans l'année",
help_text=("Si le numéro est spécial, l'afficher quand même dans la "
"page de l'année correspondante."),
default=False)
descr = CharField('Description (optionnelle)', descr = CharField('Description (optionnelle)',
max_length=512, max_length=512,
blank=True) blank=True)

View file

@ -14,6 +14,10 @@
</ul> </ul>
<ul class="nav nav-sidebar"> <ul class="nav nav-sidebar">
{% if has_special_publications %}
<li><a href="{% url "special_publications" %}">Numéros spéciaux</a></li>
{% endif %}
{% for pubyear in publication_years %} {% for pubyear in publication_years %}
<li><a href="{{ pubyear.url }}">{{ pubyear.prettyName }}</a></li> <li><a href="{{ pubyear.url }}">{{ pubyear.prettyName }}</a></li>
{% endfor %} {% endfor %}

View file

@ -6,6 +6,8 @@ app_name = 'manisite'
urlpatterns = [ urlpatterns = [
url(r'^$', views.HomeView.as_view(), name='homepage'), url(r'^$', views.HomeView.as_view(), name='homepage'),
url(r'^ecrire$', views.WriteArticleView.as_view(), name='write_article'), url(r'^ecrire$', views.WriteArticleView.as_view(), name='write_article'),
url(r'^speciaux/', views.SpecialPublicationsView.as_view(),
name='special_publications'),
url(r'^(?P<year>\d{4})-(?P<nYear>\d{4})/', url(r'^(?P<year>\d{4})-(?P<nYear>\d{4})/',
views.YearView.as_view(), name='year_view'), views.YearView.as_view(), name='year_view'),
] ]

View file

@ -1,6 +1,7 @@
# from django.shortcuts import render # from django.shortcuts import render
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.http import Http404 from django.http import Http404
from django.db.models import Q
from datetime import date from datetime import date
@ -17,25 +18,49 @@ class WriteArticleView(TemplateView):
template_name = 'mainsite/write_article.html' template_name = 'mainsite/write_article.html'
class YearView(TemplateView): class PublicationListView(TemplateView):
''' Display a year worth of BOcals ''' ''' Display a list of publications (generic class).
template_name = 'mainsite/year_view.html'
def get_context_data(self, year, nYear, **kwargs): Reimplement `get_publications` (called with the url template args in a
context = super(YearView, self).get_context_data(**kwargs) place where you can Http404) in subclasses to get it working. '''
try: template_name = 'mainsite/publications_list_view.html'
year, nYear = int(year), int(nYear)
except ValueError: def get_context_data(self, **kwargs):
raise Http404 context = super(PublicationListView, self).get_context_data(**kwargs)
if year + 1 != nYear:
raise Http404 publications = self.get_publications(**kwargs)
publications = Publication.objects.filter(
date__gte=date(year, 8, 1),
date__lt=date(nYear, 8, 1))
if len(publications) == 0: if len(publications) == 0:
raise Http404 raise Http404
context['publications'] = publications context['publications'] = publications
return context 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