Add PublicationYear, add years to sidebar

This commit is contained in:
Théophile Bastian 2017-09-22 15:15:05 +02:00
parent 31c4b046fc
commit 89c1bb63cd
6 changed files with 85 additions and 1 deletions

View file

@ -53,6 +53,7 @@ TEMPLATES = [
'django.template.context_processors.request', 'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'mainsite.context_processors.sidebar_years',
], ],
}, },
}, },

View file

@ -3,3 +3,4 @@ from . import models
admin.site.register(models.Publication) admin.site.register(models.Publication)
admin.site.register(models.PublicationYear)

View file

@ -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,
}

View file

@ -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']},
),
]

View file

@ -1,7 +1,11 @@
from django.db import models from django.db import models
from django.db.models import DateField, \ from django.db.models import DateField, \
CharField, \ CharField, \
BooleanField BooleanField, \
IntegerField, \
TextField
import datetime
class Publication(models.Model): class Publication(models.Model):
@ -28,3 +32,38 @@ class Publication(models.Model):
class Meta: class Meta:
ordering = ['date'] 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)

View file

@ -12,4 +12,10 @@
</a> </a>
</li> </li>
</ul> </ul>
<ul class="nav nav-sidebar">
{% for pubyear in publication_years %}
<li><a href="{{ pubyear.url }}">{{ pubyear.prettyName }}</a></li>
{% endfor %}
</ul>
</div> </div>