2017-07-23 16:20:28 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2017-08-03 12:09:03 +02:00
|
|
|
from django.core.exceptions import FieldDoesNotExist, FieldError
|
2016-09-30 20:10:35 +02:00
|
|
|
from django.db import models
|
2017-08-03 12:09:03 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2017-07-17 17:47:34 +02:00
|
|
|
from communication.models import SubscriptionMixin
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-08-03 12:14:15 +02:00
|
|
|
from .validators import ColorValidator
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-08-03 12:14:15 +02:00
|
|
|
User = get_user_model()
|
2017-08-03 12:09:03 +02:00
|
|
|
|
|
|
|
|
2017-07-17 17:47:34 +02:00
|
|
|
class Event(SubscriptionMixin, models.Model):
|
2016-09-30 20:10:35 +02:00
|
|
|
title = models.CharField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("nom de l'évènement"),
|
|
|
|
max_length=200,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
slug = models.SlugField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("identificateur"),
|
|
|
|
unique=True,
|
|
|
|
help_text=_(
|
|
|
|
"Seulement des lettres, des chiffres ou les caractères '_' ou '-'."
|
|
|
|
),
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
created_by = models.ForeignKey(
|
2017-08-03 12:09:03 +02:00
|
|
|
User,
|
|
|
|
verbose_name=_("créé par"),
|
|
|
|
on_delete=models.SET_NULL,
|
|
|
|
related_name="created_events",
|
|
|
|
editable=False, null=True,
|
|
|
|
)
|
2017-07-26 13:15:41 +02:00
|
|
|
created_at = models.DateTimeField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("date de création"),
|
|
|
|
auto_now_add=True,
|
|
|
|
)
|
|
|
|
description = models.TextField(_("description"))
|
|
|
|
beginning_date = models.DateTimeField(_("date de début"))
|
|
|
|
ending_date = models.DateTimeField(_("date de fin"))
|
2016-09-30 20:10:35 +02:00
|
|
|
|
|
|
|
class Meta:
|
2016-10-13 11:51:41 +02:00
|
|
|
verbose_name = _("évènement")
|
|
|
|
verbose_name_plural = _("évènements")
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2016-10-03 00:31:21 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-07-21 16:18:19 +02:00
|
|
|
class EventSpecificMixin(models.Model):
|
|
|
|
"""Mixin allowing for event-specific models instances
|
|
|
|
or not (depending on whether the event field is null)"""
|
|
|
|
|
|
|
|
event = models.ForeignKey(
|
2017-08-03 12:09:03 +02:00
|
|
|
Event,
|
2017-07-21 16:18:19 +02:00
|
|
|
verbose_name=_("évènement"),
|
2017-08-03 12:09:03 +02:00
|
|
|
help_text=_(
|
|
|
|
"Si spécifié, l'instance du modèle est spécifique à l'évènement "
|
|
|
|
"en question."
|
|
|
|
),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
blank=True, null=True,
|
2017-07-21 16:18:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
2017-07-18 19:08:09 +02:00
|
|
|
class Place(EventSpecificMixin, models.Model):
|
2016-09-30 20:10:35 +02:00
|
|
|
name = models.CharField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("nom du lieu"),
|
|
|
|
max_length=200,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
description = models.TextField(blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
2016-10-13 11:51:41 +02:00
|
|
|
verbose_name = _("lieu")
|
|
|
|
verbose_name_plural = _("lieux")
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2016-10-03 00:31:21 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-07-18 19:08:09 +02:00
|
|
|
class ActivityTag(EventSpecificMixin, models.Model):
|
2016-09-30 20:10:35 +02:00
|
|
|
name = models.CharField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("nom du tag"),
|
|
|
|
max_length=200,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
is_public = models.BooleanField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("est public"),
|
|
|
|
help_text=_(
|
|
|
|
"Sert à faire une distinction dans l'affichage selon que le tag "
|
|
|
|
"soit destiné au public ou à l'organisation."
|
|
|
|
),
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
color = models.CharField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_('couleur'),
|
|
|
|
max_length=7,
|
|
|
|
validators=[ColorValidator],
|
|
|
|
help_text=_("Rentrer une couleur en hexadécimal (#XXX ou #XXXXXX)."),
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
|
|
|
|
class Meta:
|
2016-10-13 11:51:41 +02:00
|
|
|
verbose_name = _("tag")
|
|
|
|
verbose_name_plural = _("tags")
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2016-10-03 00:31:21 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-07-18 18:41:44 +02:00
|
|
|
class AbstractActivityTemplate(SubscriptionMixin, models.Model):
|
2016-09-30 20:10:35 +02:00
|
|
|
title = models.CharField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("nom de l'activité"),
|
|
|
|
max_length=200,
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
2016-10-13 11:51:41 +02:00
|
|
|
# FIXME: voir comment on traite l'héritage de `event`
|
2017-08-03 12:09:03 +02:00
|
|
|
event = models.ForeignKey(
|
|
|
|
Event,
|
|
|
|
verbose_name=_("évènement"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
editable=False,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
is_public = models.NullBooleanField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("est public"),
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
has_perm = models.NullBooleanField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_("inscription de permanents"),
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
min_perm = models.PositiveSmallIntegerField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_('nombre minimum de permanents'),
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
max_perm = models.PositiveSmallIntegerField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_('nombre maximum de permanents'),
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
description = models.TextField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_('description'),
|
|
|
|
help_text=_("Visible par tout le monde si l'événément est public."),
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
remarks = models.TextField(
|
2017-08-03 12:09:03 +02:00
|
|
|
_('remarques'),
|
|
|
|
help_text=_("Visible uniquement par les organisateurs."),
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
tags = models.ManyToManyField(
|
2017-08-03 12:09:03 +02:00
|
|
|
ActivityTag,
|
|
|
|
verbose_name=_('tags'),
|
|
|
|
blank=True,
|
|
|
|
)
|
|
|
|
places = models.ManyToManyField(
|
|
|
|
Place,
|
|
|
|
verbose_name=_('lieux'),
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-07-18 18:41:44 +02:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class ActivityTemplate(AbstractActivityTemplate):
|
2016-09-30 20:10:35 +02:00
|
|
|
class Meta:
|
2016-10-13 11:51:41 +02:00
|
|
|
verbose_name = _("template activité")
|
|
|
|
verbose_name_plural = _("templates activité")
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2016-10-03 00:31:21 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-07-18 18:41:44 +02:00
|
|
|
class Activity(AbstractActivityTemplate):
|
2016-09-30 20:10:35 +02:00
|
|
|
parent = models.ForeignKey(
|
2017-08-03 12:09:03 +02:00
|
|
|
ActivityTemplate,
|
|
|
|
verbose_name=_("template"),
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="children",
|
|
|
|
blank=True, null=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
staff = models.ManyToManyField(
|
2017-08-03 12:09:03 +02:00
|
|
|
User,
|
|
|
|
verbose_name=_("permanents"),
|
|
|
|
related_name="in_perm_activities",
|
|
|
|
blank=True,
|
|
|
|
)
|
2016-09-30 20:10:35 +02:00
|
|
|
|
2017-07-15 17:46:13 +02:00
|
|
|
beginning = models.DateTimeField(_("heure de début"))
|
|
|
|
end = models.DateTimeField(_("heure de fin"))
|
|
|
|
|
2016-10-13 11:51:41 +02:00
|
|
|
def get_herited(self, attrname):
|
2017-08-03 12:09:03 +02:00
|
|
|
try:
|
|
|
|
tpl_field = ActivityTemplate._meta.get_field(attrname)
|
|
|
|
except FieldDoesNotExist:
|
2016-10-21 18:49:46 +02:00
|
|
|
raise FieldError(
|
2017-08-03 12:09:03 +02:00
|
|
|
"%(attrname)s field can't be herited.",
|
|
|
|
params={'attrname': attrname},
|
|
|
|
)
|
|
|
|
|
|
|
|
value = getattr(self, attrname)
|
|
|
|
|
|
|
|
if tpl_field.many_to_many:
|
|
|
|
if value.exists():
|
|
|
|
return value
|
2016-10-13 11:51:41 +02:00
|
|
|
else:
|
2017-07-21 16:24:53 +02:00
|
|
|
return getattr(self.parent, attrname)
|
2017-08-03 12:09:03 +02:00
|
|
|
elif value is None:
|
2017-07-21 16:24:53 +02:00
|
|
|
return getattr(self.parent, attrname)
|
2016-09-30 20:10:35 +02:00
|
|
|
else:
|
2017-08-03 12:09:03 +02:00
|
|
|
return value
|
2016-10-13 11:51:41 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("activité")
|
|
|
|
verbose_name_plural = _("activités")
|
|
|
|
|
2016-10-13 16:26:35 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.get_herited('title')
|