gestioCOF/gestion/models.py

308 lines
7.9 KiB
Python
Raw Normal View History

from django.contrib.auth.models import User, Group
from django.db import models
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
from django.utils.translation import ugettext_lazy as _
from cof.petits_cours_models import choices_length
# ---
# User management
# ---
OCCUPATION_CHOICES = (
('exterieur', _("Extérieur")),
('1A', _("1A")),
('2A', _("2A")),
('3A', _("3A")),
('4A', _("4A")),
('archicube', _("Archicube")),
('doctorant', _("Doctorant")),
('CST', _("CST")),
)
class Profile(models.Model):
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
related_name="profile",
verbose_name=_("utilisateur"),
)
login_clipper = models.CharField(
_("login clipper"),
max_length=8,
blank=True,
)
phone = models.CharField(_("téléphone"), max_length=20, blank=True)
occupation = models.CharField(_("occupation"),
default="1A",
choices=OCCUPATION_CHOICES,
max_length=choices_length(
OCCUPATION_CHOICES))
departement = models.CharField(_("département"), max_length=50,
blank=True)
comments = models.TextField(
_("commentaires visibles par l'utilisateur"), blank=True
)
class Meta:
verbose_name = _("profil")
verbose_name_plural = _("profils")
def __str__(self):
return self.user.username
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.get_or_create(user=instance)
@receiver(post_delete, sender=Profile)
def post_delete_user(sender, instance, *args, **kwargs):
instance.user.delete()
# ---
# Clubs
# ---
class Club(models.Model):
ANNUAL = "ANN"
SEMESTER = "SEM"
COURSE = "COU"
COTISATION_FREQUENCY_CHOICES = [
(ANNUAL, _("Annuel")),
(SEMESTER, _("Semestriel")),
(COURSE, _("Au cours"))
]
associations = models.ManyToManyField(Group, related_name="clubs")
name = models.CharField(_("nom"), max_length=200, unique=True)
description = models.TextField("description", blank=True)
members = models.ManyToManyField(
User,
through="ClubUser",
related_name="in_clubs",
blank=True,
verbose_name=_("membres du club"),
)
price = models.DecimalField(
_("cotisation (€)"),
decimal_places=2,
max_digits=5,
blank=True,
default=0
)
cotisation_frequency = models.CharField(
_("fréquence de la cotisation"),
default=ANNUAL,
choices=COTISATION_FREQUENCY_CHOICES,
max_length=3,
blank=True
)
def __str__(self):
template = (
self.price
and _("%(name)s (%(price).2f€ / %(cotisation_frequency)s)")
or _("%(name)s")
)
return template % vars(self)
class ClubUser(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
club = models.ForeignKey(Club, on_delete=models.CASCADE)
is_respo = models.BooleanField(_("est responsable du club"))
has_paid = models.BooleanField(_("a payé sa cotisation"))
# ---
# Events
# ---
class Location(models.Model):
name = models.CharField(_("nom du lieux"), max_length=200)
class Meta:
verbose_name = _("lieu")
verbose_name_plural = _("lieux")
def __str__(self):
return self.name
class Event(models.Model):
associations = models.ManyToManyField(
Group,
related_name="events",
verbose_name=_("associations"),
)
title = models.CharField(_("titre"), max_length=200)
location = models.ForeignKey(
Location,
on_delete=models.PROTECT,
verbose_name=_("lieux"),
)
start_date = models.DateTimeField(
_("début de l'événement"),
blank=True, null=True,
)
end_date = models.DateTimeField(
_("fin de l'événement"),
blank=True, null=True,
)
description = models.TextField(_("description"), blank=True)
image = models.ImageField(
_("image"),
blank=True, null=True,
upload_to="public/imgs/events/",
)
registration_open = models.BooleanField(
_("les inscriptions sont ouvertes"),
default=True
)
old = models.BooleanField(_("archiver (événement fini)"), default=False)
class Meta:
verbose_name = _("événement")
verbose_name_plural = _("événements")
def __str__(self):
return self.title
class EventCommentField(models.Model):
TEXT = "text"
CHAR = "char"
FIELD_TYPE = [
(TEXT, _("Texte long")),
(CHAR, _("Texte court")),
]
event = models.ForeignKey(
Event,
on_delete=models.CASCADE,
related_name="commentfields",
verbose_name=_("événement"),
)
name = models.CharField(_("nom du champ"), max_length=200)
fieldtype = models.CharField(
_("type de champ"),
max_length=10,
choices=FIELD_TYPE, default=TEXT,
)
default = models.TextField(_("valeur par défaut"), blank=True)
ordering = models.IntegerField(
_("ordre des champs"),
help_text=_(
"plus petit en premier, ordre alphabétique sur le nom si "
"ambiguïté"
),
)
class Meta:
verbose_name = _("champ")
verbose_name_plural = _("champs")
def __str__(self):
return self.name
class EventCommentValue(models.Model):
commentfield = models.ForeignKey(
EventCommentField,
on_delete=models.CASCADE,
related_name="values"
)
registration = models.ForeignKey(
"EventRegistration",
on_delete=models.CASCADE,
related_name="comments"
)
content = models.TextField(_("contenu"), blank=True)
class Meta:
unique_together = ("commentfield", "registration")
def __str__(self):
return (
_("Commentaire de %(field_name)s")
% {"field_name": self.commentfield}
)
class EventOption(models.Model):
event = models.ForeignKey(
Event,
on_delete=models.CASCADE,
related_name="options",
verbose_name=_("événement"),
)
name = models.CharField(_("option"), max_length=200)
multi_choices = models.BooleanField(_("choix multiples"), default=False)
class Meta:
verbose_name = _("option des événements")
verbose_name_plural = _("options des événements")
def __str__(self):
return self.name
class EventOptionChoice(models.Model):
event_option = models.ForeignKey(
EventOption,
on_delete=models.CASCADE,
related_name="choices",
verbose_name=_("événement"),
)
value = models.CharField("Valeur", max_length=200)
class Meta:
verbose_name = _("choix")
verbose_name_plural = _("choix")
def __str__(self):
return self.value
class EventRegistration(models.Model):
user = models.ForeignKey(
User,
on_delete=models.CASCADE,
verbose_name=_("utilisateur"),
)
event = models.ForeignKey(
Event,
on_delete=models.CASCADE,
verbose_name=_("événement"),
)
options = models.ManyToManyField(
EventOptionChoice,
verbose_name=_("choix"),
)
filledcomments = models.ManyToManyField(
EventCommentField,
through=EventCommentValue,
verbose_name=_("commentaires"),
)
paid = models.BooleanField(_("a payé"), default=False)
class Meta:
verbose_name = _("inscription")
verbose_name_plural = _("inscriptions")
unique_together = ("user", "event")
def __str__(self):
return (
_("Inscription de %(user)s à %(event)s")
% {"user": self.user, "event": self.event}
)