poulpe/event/models.py
2017-07-15 18:14:15 +02:00

188 lines
5.9 KiB
Python

from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.core.validators import RegexValidator
from django.core.exceptions import FieldError
from django.db import models
from communication.models import Commentable, Notifying
class Event(Commentable):
title = models.CharField(
_("nom de l'évènement"),
max_length=200,
)
slug = models.SlugField(
_('identificateur'),
unique=True,
primary_key=True,
help_text=_("Seulement des lettres, des chiffres ou"
"les caractères '_' ou '-'."),
)
created_by = models.ForeignKey(
User,
related_name="created_events",
editable=False,
)
creation_date = models.DateTimeField(
_('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'),
blank=True
)
class Meta:
verbose_name = _("évènement")
verbose_name_plural = _("évènements")
def __str__(self):
return self.title
class Place(models.Model):
name = models.CharField(
_("nom du lieu"),
max_length=200,
)
description = models.TextField(blank=True)
class Meta:
verbose_name = _("lieu")
verbose_name_plural = _("lieux")
def __str__(self):
return self.name
class ActivityTag(models.Model):
name = models.CharField(
_("nom du tag"),
max_length=200,
)
is_public = models.BooleanField(
help_text=_("Sert à faire une distinction dans"
" l'affichage selon que cela soit"
" destiné au public ou à l'équipe"
" organisatrice"),
)
color_regex = RegexValidator(
regex=r'^#(?:[0-9a-fA-F]{3}){1,2}$',
message=_("La chaîne de caractère rentrée n'est pas"
" une couleur en hexadécimal."),
)
color = models.CharField(
_('couleur'),
max_length=7,
validators=[color_regex],
help_text=_("Rentrer une couleur en hexadécimal"),
)
class Meta:
verbose_name = _("tag")
verbose_name_plural = _("tags")
def __str__(self):
return self.name
class ActivityTemplate(Commentable):
title = models.CharField(
_("nom de l'activité"),
max_length=200,
blank=True,
null=True,
)
# FIXME: voir comment on traite l'héritage de `event`
event = models.ForeignKey(
Event,
related_name="activities",
blank=True,
null=True,
)
is_public = models.NullBooleanField(
blank=True,
)
has_perm = models.NullBooleanField(
blank=True,
)
min_perm = models.PositiveSmallIntegerField(
_('nombre minimum de permanents'),
blank=True,
null=True,
)
max_perm = models.PositiveSmallIntegerField(
_('nombre maximum de permanents'),
blank=True,
null=True,
)
description = models.TextField(
_('description'),
help_text=_("Public, Visible par tout le monde."),
blank=True,
null=True,
)
remarks = models.TextField(
_('remarques'),
help_text=_("Visible uniquement par les organisateurs"),
blank=True,
null=True,
)
tags = models.ManyToManyField(
ActivityTag,
related_name="activities",
blank=True,
)
place = models.ManyToManyField(
Place,
related_name="activities",
blank=True,
)
class Meta:
verbose_name = _("template activité")
verbose_name_plural = _("templates activité")
def __str__(self):
return self.title
class Activity(ActivityTemplate):
parent = models.ForeignKey(
ActivityTemplate,
related_name="children",
)
staff = models.ManyToManyField(
User,
related_name="in_perm_activities",
blank=True,
)
beginning = models.DateTimeField(_("heure de début"))
end = models.DateTimeField(_("heure de fin"))
def get_herited(self, attrname):
attr = super(Activity, self).__getattribute__(attrname)
if attrname in {"parent", "staff", "equipment"}:
raise FieldError(
_("%(attrname)s n'est pas un champ héritable"),
params={'attrname': attrname},
)
elif (attrname == 'place' or attrname == 'tags'):
if attr.exists():
return attr
else:
return self.parent.__getattribute__(attrname)
elif attr is None:
return self.parent.__getattribute__(attrname)
else:
return attr
class Meta:
verbose_name = _("activité")
verbose_name_plural = _("activités")
def __str__(self):
return self.get_herited('title')