beesgospel/beesgospel/models.py

147 lines
5.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.contrib.auth.models import AbstractUser, UserManager
from django.db import models
from django.template.defaultfilters import linebreaksbr
from django.utils.safestring import SafeString
class CustomUserManager(UserManager):
def create_superuser(self, **kwargs):
return super().create_superuser("foo", **kwargs)
class User(AbstractUser):
username = None
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
objects = CustomUserManager()
class Meta:
constraints = [
models.UniqueConstraint(name="unique_user_email", fields=["email"]),
]
def __init__(self, *args, username=None, **kwargs):
super().__init__(*args, **kwargs)
class Membre(models.Model):
nom = models.CharField("Nom", max_length=40)
prenom = models.CharField("Prénom", max_length=40)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
fonction = models.CharField("Fonction", max_length=100, blank=True)
avatar = models.ImageField("Avatar", upload_to="avatars", blank=True)
rue = models.CharField("Rue", max_length=80, blank=True)
npa = models.CharField("NPA", max_length=5, blank=True)
localite = models.CharField("Localité", max_length=40, blank=True)
tel1 = models.CharField("Tél. 1", max_length=20, blank=True)
tel2 = models.CharField("Tél. 2", max_length=20, blank=True)
courriel = models.EmailField("Courriel", blank=True)
date_naissance = models.DateField("Date de naissance", null=True, blank=True)
annee_entree = models.PositiveSmallIntegerField("Année entrée", null=True, blank=True)
def __str__(self):
return f"{self.nom} {self.prenom}"
class Agenda(models.Model):
class Statuts(models.TextChoices):
PUBLIC = "public", "Événement public"
PRIVE = "prive", "Événement privé"
REPETITION = "repet", "Répétition"
titre = models.CharField("Titre", max_length=150)
lieu = models.CharField("Lieu", max_length=80, blank=True)
date_heure = models.DateTimeField("Date/heure")
infos = models.TextField("Informations", blank=True)
infos_internes = models.TextField("Informations internes", blank=True)
statut = models.CharField("Statut", max_length=10, choices=Statuts, help_text=(
"Un évènement privé ou une répétition ne sont visibles que pour les membres de "
"lassociation, tandis qu'un évènement public est visible de tous."
))
class Meta:
verbose_name = "Agenda"
verbose_name_plural = "Agenda"
def __str__(self):
return f"{self.titre} {self.date_heure}"
class Document(models.Model):
class Categories(models.TextChoices):
VIDEO = "video", "Vidéo"
AUDIO = "audio", "Audio"
PHOTOS = "photos", "Photos"
DOCUMENT = "doc", "Document"
fichier = models.FileField("Fichier", upload_to="documents", blank=True)
url = models.URLField("URL", blank=True)
quand = models.DateField("Date")
titre = models.CharField("Titre", max_length=150)
infos = models.TextField("Infos", blank=True)
categorie = models.CharField("Catégorie", max_length=30, choices=Categories)
prive = models.BooleanField(
"Privé", default=False, help_text=(
"Un document privé ne peut être consulté que par les membres de "
"l'association, tandis qu'un document public est visible de tous."
)
)
def __str__(self):
return f"{self.titre} {self.quand}"
def print_infos(self):
if self.infos.startswith("<"):
return SafeString(self.infos)
return linebreaksbr(self.infos)
def get_url(self):
if self.url:
return self.url
elif self.fichier:
return self.fichier.url
class Chant(models.Model):
class StatutChoices(models.TextChoices):
ACTIF = "actif", "Actif"
PREP = "prep", "En préparation"
INACTIF = "inactif", "Inactif"
titre = models.CharField("Titre", max_length=100)
particularite = models.CharField("Particularité", max_length=100, blank=True)
statut = models.CharField("Statut", max_length=10, choices=StatutChoices, default="actif")
def __str__(self):
return self.titre
class ChantDoc(models.Model):
chant = models.ForeignKey(Chant, on_delete=models.CASCADE)
fichier = models.FileField("Fichier", upload_to="chants", blank=True)
lien = models.URLField("Lien", blank=True)
titre = models.CharField("Titre", max_length=200)
def __str__(self):
return f"Document {self.titre} pour le chant {self.chant}"
@property
def fichier_son(self):
return self.fichier and self.fichier.name.endswith((".m4a", ".mp3", ".wav"))
class ConcertItem(models.Model):
concert = models.ForeignKey(Agenda, on_delete=models.CASCADE, related_name="programme")
chant = models.ForeignKey(Chant, on_delete=models.CASCADE, blank=True, null=True)
item = models.CharField(max_length=250, blank=True)
ordre = models.SmallIntegerField(default=0)
remarque = models.TextField("Remarque", blank=True)
class Meta:
ordering = ["ordre"]
def __str__(self):
if self.chant:
return f"Chant {self.chant} pour le concert {self.concert}"
return f"{self.item} pour le concert {self.concert}"