2024-10-23 11:06:57 +02:00
|
|
|
|
import datetime
|
|
|
|
|
|
2017-09-21 10:58:19 +02:00
|
|
|
|
from django.db import models
|
2024-10-23 11:06:57 +02:00
|
|
|
|
from django.db.models import BooleanField, CharField, DateField, IntegerField, Q
|
2017-09-23 18:48:29 +02:00
|
|
|
|
from markdownx.models import MarkdownxField
|
2024-10-23 11:06:57 +02:00
|
|
|
|
from solo.models import SingletonModel
|
2017-09-21 10:58:19 +02:00
|
|
|
|
|
2017-09-21 11:34:40 +02:00
|
|
|
|
|
2017-09-22 18:07:38 +02:00
|
|
|
|
class SiteConfiguration(SingletonModel):
|
2017-09-23 18:48:29 +02:00
|
|
|
|
homepageText = MarkdownxField("Texte de la page d'accueil (Markdown)")
|
|
|
|
|
writearticleText = MarkdownxField("Texte de la page « écrire » (Markdown)")
|
2024-10-23 11:06:57 +02:00
|
|
|
|
email = CharField(
|
|
|
|
|
"Adresse de contact du BOcal", max_length=128, help_text="Attention au spam…"
|
|
|
|
|
)
|
|
|
|
|
specialPublisDescr = MarkdownxField(
|
|
|
|
|
"Texte de la page des " "publications spéciales (Markdown)"
|
|
|
|
|
)
|
2017-09-22 18:07:38 +02:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "Configuration du site"
|
|
|
|
|
|
2024-10-23 13:21:36 +02:00
|
|
|
|
class Meta: # pyright: ignore
|
2017-09-22 18:07:38 +02:00
|
|
|
|
verbose_name = "Configuration du site"
|
|
|
|
|
|
|
|
|
|
|
2017-09-21 11:34:40 +02:00
|
|
|
|
class Publication(models.Model):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
num = CharField("Numéro du BOcal", max_length=128)
|
|
|
|
|
url = CharField("Adresse sur le site", max_length=512)
|
2017-09-21 19:58:42 +02:00
|
|
|
|
# ^ This is not a URLField because we need internal URLS, eg `/static/blah`
|
|
|
|
|
|
2024-10-23 11:06:57 +02:00
|
|
|
|
date = DateField("Publication")
|
|
|
|
|
unknown_date = BooleanField(
|
|
|
|
|
"Date inconnue",
|
|
|
|
|
help_text=(
|
|
|
|
|
"La date de publication du BOcal "
|
|
|
|
|
"est inconnue parce qu'il est "
|
|
|
|
|
"trop vieux. La date indiquée ne "
|
|
|
|
|
"servira qu'à le ranger dans une "
|
|
|
|
|
"année et à ordonner les BOcals."
|
|
|
|
|
),
|
|
|
|
|
default=False,
|
|
|
|
|
)
|
|
|
|
|
is_special = BooleanField(
|
|
|
|
|
"Numéro spécial", help_text="Numéro du BOcal non-numéroté", default=False
|
|
|
|
|
)
|
2017-09-22 19:03:43 +02:00
|
|
|
|
in_year_view_anyway = BooleanField(
|
|
|
|
|
"Aussi dans l'année",
|
2024-10-23 11:06:57 +02:00
|
|
|
|
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)", max_length=512, blank=True)
|
|
|
|
|
custom_name = CharField(
|
|
|
|
|
"Nom customisé",
|
|
|
|
|
help_text="Vide pour laisser le numéro seulement",
|
|
|
|
|
max_length=128,
|
|
|
|
|
blank=True,
|
|
|
|
|
)
|
2017-09-21 11:34:40 +02:00
|
|
|
|
|
2017-09-24 18:41:31 +02:00
|
|
|
|
class NoPublicationYear(Exception):
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "No matching publication year."
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def numericPublicationYear(self):
|
|
|
|
|
startYear = self.date.year
|
|
|
|
|
if self.date.month < 8:
|
|
|
|
|
return startYear - 1
|
|
|
|
|
return startYear
|
|
|
|
|
|
|
|
|
|
def publicationYear(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
"""Fetch corresponding publication year
|
|
|
|
|
Raise `NoPublicationYear` if there is no such entry"""
|
2017-09-24 18:41:31 +02:00
|
|
|
|
startYear = self.numericPublicationYear
|
|
|
|
|
try:
|
|
|
|
|
return PublicationYear.objects.get(startYear=startYear)
|
|
|
|
|
except PublicationYear.DoesNotExist:
|
|
|
|
|
raise self.NoPublicationYear
|
|
|
|
|
|
|
|
|
|
def createPubYear(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
"""Creates the corresponding publication year if needed."""
|
|
|
|
|
if (
|
|
|
|
|
PublicationYear.objects.filter(
|
|
|
|
|
startYear=self.numericPublicationYear
|
|
|
|
|
).count()
|
|
|
|
|
) == 0:
|
|
|
|
|
pubYear = PublicationYear(startYear=self.numericPublicationYear, descr="")
|
2017-09-24 18:41:31 +02:00
|
|
|
|
pubYear.save()
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
2017-09-21 11:34:40 +02:00
|
|
|
|
def __str__(self):
|
|
|
|
|
if self.custom_name:
|
|
|
|
|
return self.custom_name
|
2017-09-23 16:34:37 +02:00
|
|
|
|
elif self.is_special:
|
|
|
|
|
return self.num
|
2024-10-23 11:06:57 +02:00
|
|
|
|
return "BOcal n°{}".format(self.num)
|
2017-09-21 20:21:35 +02:00
|
|
|
|
|
2017-09-24 19:10:20 +02:00
|
|
|
|
@staticmethod
|
|
|
|
|
def latest():
|
2024-10-23 11:06:57 +02:00
|
|
|
|
return Publication.objects.order_by("-date")[0]
|
2017-09-24 19:10:20 +02:00
|
|
|
|
|
2017-09-21 20:21:35 +02:00
|
|
|
|
class Meta:
|
2024-10-23 11:06:57 +02:00
|
|
|
|
ordering = ["date"]
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PublicationYear(models.Model):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
startYear = IntegerField(
|
|
|
|
|
"Année de début", help_text="Année scolaire à partir du 15/08", primary_key=True
|
|
|
|
|
)
|
2017-09-23 18:48:29 +02:00
|
|
|
|
descr = MarkdownxField("Accroche de l'année (Markdown)")
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
return "{}-{}".format(self.startYear, self.startYear + 1)
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
def beg(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
"""First day of this publication year (incl.)"""
|
2024-08-17 16:46:15 +02:00
|
|
|
|
return datetime.date(self.startYear, 8, 15)
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
def end(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
"""Last day of this publication year (excl.)"""
|
2024-08-17 16:46:15 +02:00
|
|
|
|
return datetime.date(self.startYear + 1, 8, 15)
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
def inYear(self, date):
|
|
|
|
|
return self.beg() <= date < self.end()
|
|
|
|
|
|
|
|
|
|
def publis(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
"""List of publications from this year"""
|
2017-09-22 15:15:05 +02:00
|
|
|
|
return Publication.objects.filter(
|
2017-09-24 19:10:20 +02:00
|
|
|
|
Q(is_special=False) | Q(in_year_view_anyway=True),
|
2017-09-22 15:15:05 +02:00
|
|
|
|
date__gte=self.beg(),
|
2024-10-23 11:06:57 +02:00
|
|
|
|
date__lt=self.end(),
|
|
|
|
|
)
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def url(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
return "/{}/".format(self)
|
2017-09-22 15:15:05 +02:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def prettyName(self):
|
2024-10-23 11:06:57 +02:00
|
|
|
|
return "{} – {}".format(self.startYear, self.startYear + 1)
|
2017-09-22 19:16:41 +02:00
|
|
|
|
|
|
|
|
|
class Meta:
|
2024-10-23 11:06:57 +02:00
|
|
|
|
ordering = ["-startYear"]
|