gestioCOF/events/models.py
Martin Pépin d5e9d09044 Events are configurable
This commit mostly reproduces the structure of gestioncof's events,
renames some stuff and adds a generic export view.
2020-05-08 16:14:04 +02:00

118 lines
4.1 KiB
Python

from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _
User = get_user_model()
class Event(models.Model):
title = models.CharField(_("titre"), max_length=200)
location = models.CharField(_("lieu"), max_length=200)
start_date = models.DateTimeField(_("date de début"), blank=True, null=True)
end_date = models.DateTimeField(_("date de fin"), blank=True, null=True)
description = models.TextField(_("description"), blank=True)
image = models.ImageField(
_("image"), blank=True, null=True, upload_to="imgs/events/"
)
registration_open = models.BooleanField(_("inscriptions ouvertes"), default=True)
old = models.BooleanField(_("archiver (événement fini)"), default=False)
subscribers = models.ManyToManyField(
User, through="Registration", verbose_name=_("inscrit⋅e⋅s")
)
class Meta:
verbose_name = _("événement")
verbose_name_plural = _("événements")
def __str__(self):
return self.title
class Option(models.Model):
"""Event options to be selected by participants at registration.
The possible choices are instances of `OptionChoice` (see below). A typical example
is when the participants have the choice between different meal types (e.g. vegan /
vegetarian / no pork / with meat). In this case, the "meal type" is an `Option` and
the three alternatives are `OptionChoice`s.
"""
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name="options")
name = models.CharField(_("option d'événement"), max_length=200)
multi_choices = models.BooleanField(_("choix multiples"), default=False)
class Meta:
verbose_name = _("option d'événement")
verbose_name_plural = _("options d'événement")
def __str__(self):
return self.name
class OptionChoice(models.Model):
"""A possible choice for an event option (see Option)."""
option = models.ForeignKey(Option, on_delete=models.CASCADE, related_name="choices")
choice = models.CharField(_("choix"), max_length=200)
class Meta:
verbose_name = _("choix d'option d'événement")
verbose_name_plural = _("choix d'option d'événement")
def __str__(self):
return self.choice
class ExtraField(models.Model):
"""Extra event field, for event creators.
Extra text field that can be added by event creators to the event registration form.
Typical examples are "remarks" fields (of type LONGTEXT) or more specific questions
such as "emergency contact".
"""
LONGTEXT = "longtext"
SHORTTEXT = "shorttext"
FIELD_TYPE = [
(SHORTTEXT, _("texte court (une ligne)")),
(LONGTEXT, _("texte long (plusieurs lignes)")),
]
event = models.ForeignKey(
Event, on_delete=models.CASCADE, related_name="extra_fields"
)
name = models.CharField(_("champ d'événement supplémentaire"), max_length=200)
field_type = models.CharField(_("type de champ"), max_length=9, choices=FIELD_TYPE)
class ExtraFieldContent(models.Model):
field = models.ForeignKey(ExtraField, on_delete=models.CASCADE)
registration = models.ForeignKey(
"Registration", on_delete=models.CASCADE, related_name="extra_info"
)
content = models.TextField(_("contenu du champ"))
class Meta:
verbose_name = _("contenu d'un champ événement supplémentaire")
verbose_name_plural = _("contenus d'un champ événement supplémentaire")
def __str__(self):
max_length = 50
if len(self.content) > max_length:
return self.content[: max_length - 1] + ""
else:
return self.content
class Registration(models.Model):
event = models.ForeignKey(Event, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
options_choices = models.ManyToManyField(OptionChoice)
class Meta:
verbose_name = _("inscription à un événement")
verbose_name_plural = _("inscriptions à un événement")
def __str__(self):
return "inscription de {} à {}".format(self.user, self.event)