ernestophone.ens.fr/partitions/models.py

113 lines
3.2 KiB
Python
Raw Normal View History

2015-03-17 19:03:51 +01:00
import os
2017-10-13 15:01:17 +02:00
from django.conf import settings
2022-02-05 16:18:00 +01:00
from django.contrib import admin
2017-10-13 15:01:17 +02:00
from django.db import models
from django.db.models.functions import Lower
from django.utils.translation import gettext_lazy as _
2017-10-13 15:01:17 +02:00
2015-03-17 19:03:51 +01:00
2018-01-04 20:20:30 +01:00
class Category(models.Model):
name = models.CharField(max_length=127)
order = models.IntegerField(verbose_name=_("ordre"))
2021-04-29 00:27:33 +02:00
nom_en = models.CharField(max_length=100, null=True, blank=True)
2018-01-04 20:20:30 +01:00
def __str__(self):
return self.name
class Meta:
verbose_name = _("Categorie")
verbose_name_plural = _("Categories")
2021-04-29 00:27:33 +02:00
ordering = ("order",)
2015-03-17 19:03:51 +01:00
class Partition(models.Model):
nom = models.CharField(max_length=100)
part = models.FileField(upload_to="partitions/")
2021-04-29 00:27:33 +02:00
morceau = models.ForeignKey("PartitionSet", on_delete=models.CASCADE)
2015-03-17 19:03:51 +01:00
def __str__(self):
return self.nom
def delete(self, *args, **kwargs):
2017-10-13 15:01:17 +02:00
os.remove(os.path.join(settings.MEDIA_ROOT, self.part.name))
2015-03-17 19:03:51 +01:00
super(Partition, self).delete(*args, **kwargs)
class Meta:
2022-01-18 15:36:04 +01:00
verbose_name = _("Partition")
verbose_name_plural = _("Partitions")
2021-04-29 00:27:33 +02:00
ordering = (Lower("nom"),)
2015-03-17 19:03:51 +01:00
class PartitionSet(models.Model):
nom = models.CharField(max_length=100)
auteur = models.CharField(max_length=100)
2018-01-04 20:20:30 +01:00
category = models.ForeignKey(
2021-04-29 00:27:33 +02:00
Category, on_delete=models.PROTECT, verbose_name=_("Type de partition")
)
download_unlogged = models.CharField(
_("Téléchargeable non connecté ?"),
default="n",
choices=[("n", _("Non")), ("o", _("Oui"))],
max_length=1,
)
infos = models.TextField(_("Infos utiles"), null=False, blank=True, default="")
infos_en = models.TextField(
"Infos utiles en anglais", null=False, blank=True, default=""
2018-01-04 20:20:30 +01:00
)
2021-04-29 00:27:33 +02:00
url = models.URLField(
_("Url d'une video youtube"),
null=True,
blank=True,
help_text=_(
"Dans Youtube cliquer sur partager puis importer pour récuperer la bonne adresse"
),
)
2015-03-17 19:03:51 +01:00
def __str__(self):
2021-04-29 00:27:33 +02:00
return "%s - %s [%s]" % (self.nom, self.auteur, self.category)
class Meta:
2021-04-29 00:27:33 +02:00
verbose_name = _("Morceau")
verbose_name_plural = _("Morceaux")
2022-02-05 16:18:00 +01:00
ordering = (
"category",
Lower("nom"),
)
2022-01-09 01:18:50 +01:00
from datetime import date as ddate
from datetime import timedelta
class SetList(models.Model):
"""
Modèle qui stocke les setlists de répétition (date et morceaux)
"""
2022-01-09 01:18:50 +01:00
date = models.DateField(_("Date de la répétition"))
2022-01-09 01:18:50 +01:00
is_current = models.CharField(
verbose_name=_(
"Afficher le programme de répétition (les répétition vieilles de plus d'une semaine ne sont pas affiché d'office)"
2022-01-09 01:18:50 +01:00
),
max_length=1,
choices=(("y", "Oui"), ("n", "Non")),
default="y",
blank=False,
)
morceaux = models.ManyToManyField(
"PartitionSet",
verbose_name=_(
"Morceaux de la répétition (ctrl ou cmd pour en selectionner plusieurs)"
2022-01-09 01:18:50 +01:00
),
)
def __str__(self):
return "%s - (%s)" % (
self.date,
", ".join(self.morceaux.all().values_list("nom", flat=True)),
)
def is_visible(self):
return self.is_current == "y" and self.date > ddate.today() - timedelta(days=7)