From 89c1bb63cd45ae5689b1bd4f65d9e995813a1bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Fri, 22 Sep 2017 15:15:05 +0200 Subject: [PATCH] Add PublicationYear, add years to sidebar --- bocal/settings_base.py | 1 + mainsite/admin.py | 1 + mainsite/context_processors.py | 11 +++++ .../migrations/0002_auto_20170922_1438.py | 26 ++++++++++++ mainsite/models.py | 41 ++++++++++++++++++- mainsite/templates/sidebar.html | 6 +++ 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 mainsite/context_processors.py create mode 100644 mainsite/migrations/0002_auto_20170922_1438.py diff --git a/bocal/settings_base.py b/bocal/settings_base.py index 3e37efa..6f220f7 100644 --- a/bocal/settings_base.py +++ b/bocal/settings_base.py @@ -53,6 +53,7 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'mainsite.context_processors.sidebar_years', ], }, }, diff --git a/mainsite/admin.py b/mainsite/admin.py index 9d4fa1e..4d241cb 100644 --- a/mainsite/admin.py +++ b/mainsite/admin.py @@ -3,3 +3,4 @@ from . import models admin.site.register(models.Publication) +admin.site.register(models.PublicationYear) diff --git a/mainsite/context_processors.py b/mainsite/context_processors.py new file mode 100644 index 0000000..c1d7057 --- /dev/null +++ b/mainsite/context_processors.py @@ -0,0 +1,11 @@ +""" Context processors """ + +from . import models + + +def sidebar_years(req): + avail_years = models.PublicationYear.objects.all() + publi_years = [year for year in avail_years if len(year.publis()) > 0] + return { + 'publication_years': publi_years, + } diff --git a/mainsite/migrations/0002_auto_20170922_1438.py b/mainsite/migrations/0002_auto_20170922_1438.py new file mode 100644 index 0000000..bf6dbb7 --- /dev/null +++ b/mainsite/migrations/0002_auto_20170922_1438.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2017-09-22 12:38 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('mainsite', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='PublicationYear', + fields=[ + ('startYear', models.IntegerField(help_text='Année scolaire à partir du 1/08', primary_key=True, serialize=False, verbose_name='Année de début')), + ('descr', models.TextField(verbose_name="Accroche de l'année")), + ], + ), + migrations.AlterModelOptions( + name='publication', + options={'ordering': ['date']}, + ), + ] diff --git a/mainsite/models.py b/mainsite/models.py index 86930d6..f0e5a98 100644 --- a/mainsite/models.py +++ b/mainsite/models.py @@ -1,7 +1,11 @@ from django.db import models from django.db.models import DateField, \ CharField, \ - BooleanField + BooleanField, \ + IntegerField, \ + TextField + +import datetime class Publication(models.Model): @@ -28,3 +32,38 @@ class Publication(models.Model): class Meta: ordering = ['date'] + + +class PublicationYear(models.Model): + startYear = IntegerField('Année de début', + help_text='Année scolaire à partir du 1/08', + primary_key=True) + descr = TextField('Accroche de l\'année') + + def __str__(self): + return '{}-{}'.format(self.startYear, self.startYear+1) + + def beg(self): + ''' First day of this publication year (incl.) ''' + return datetime.date(self.startYear, 8, 1) + + def end(self): + ''' Last day of this publication year (excl.) ''' + return datetime.date(self.startYear + 1, 8, 1) + + def inYear(self, date): + return self.beg() <= date < self.end() + + def publis(self): + ''' List of publications from this year ''' + return Publication.objects.filter( + date__gte=self.beg(), + date__lt=self.end()) + + @property + def url(self): + return '/{}/'.format(self) + + @property + def prettyName(self): + return '{} – {}'.format(self.startYear, self.startYear+1) diff --git a/mainsite/templates/sidebar.html b/mainsite/templates/sidebar.html index ae9a022..dc52cdd 100644 --- a/mainsite/templates/sidebar.html +++ b/mainsite/templates/sidebar.html @@ -12,4 +12,10 @@ + +