From 0ff2f40832a99919a6dff521e41b40c63f003efa Mon Sep 17 00:00:00 2001 From: Qwann Date: Mon, 20 Aug 2018 17:46:41 +0200 Subject: [PATCH] cleaning activity --- event/migrations/0002_auto_20180820_1529.py | 32 ++++++ event/models.py | 109 +++++++++++++++++++- 2 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 event/migrations/0002_auto_20180820_1529.py diff --git a/event/migrations/0002_auto_20180820_1529.py b/event/migrations/0002_auto_20180820_1529.py new file mode 100644 index 0000000..6b89d38 --- /dev/null +++ b/event/migrations/0002_auto_20180820_1529.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.11 on 2018-08-20 15:29 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('event', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='activitytemplate', + name='name', + field=models.CharField(default='change_me!', help_text='Ne sera pas affiché', max_length=200, verbose_name='Nom du template'), + preserve_default=False, + ), + migrations.AlterField( + model_name='activity', + name='event', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Event', verbose_name='évènement'), + ), + migrations.AlterField( + model_name='activitytemplate', + name='event', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Event', verbose_name='évènement'), + ), + ] diff --git a/event/models.py b/event/models.py index c58b17f..a014dc0 100644 --- a/event/models.py +++ b/event/models.py @@ -1,5 +1,5 @@ from django.contrib.auth import get_user_model -from django.core.exceptions import FieldDoesNotExist, FieldError +from django.core.exceptions import FieldDoesNotExist, FieldError, ValidationError from django.db import models from django.utils.translation import ugettext_lazy as _ @@ -123,7 +123,6 @@ class AbstractActivityTemplate(SubscriptionMixin, models.Model): Event, verbose_name=_("évènement"), on_delete=models.CASCADE, - editable=False, ) is_public = models.NullBooleanField( _("est public"), @@ -167,12 +166,34 @@ class AbstractActivityTemplate(SubscriptionMixin, models.Model): class ActivityTemplate(AbstractActivityTemplate): + name = models.CharField( + _("Nom du template"), + max_length=200, + help_text=_("Ne sera pas affiché"), + ) + class Meta: verbose_name = _("template activité") verbose_name_plural = _("templates activité") def __str__(self): - return self.title + return self.name + + def clean(self): + errors = [] + + # On clean les nombre de permanents + if not self.has_perm: + self.max_perm = None + self.min_perm = None + else: + if self.min_perm > self.max_perm: + errors.append(ValidationError( + _("Nombres de permanents incompatibles"), + code='wrong-nb-perm', + )) + if errors != []: + raise ValidationError(errors) class Activity(AbstractActivityTemplate): @@ -193,6 +214,86 @@ class Activity(AbstractActivityTemplate): beginning = models.DateTimeField(_("heure de début")) end = models.DateTimeField(_("heure de fin")) + def clean(self): + errors = [] + + # On clean les nombre de permanents + if not self.get_herited('has_perm'): + self.max_perm = None + self.min_perm = None + else: + if self.get_herited('min_perm') > self.get_herited('max_perm'): + errors.append(ValidationError( + _("Nombres de permanents incompatibles"), + code='wrong-nb-perm', + )) + + # On valide l'héritage + for f in self._meta.get_fields(): + try: + # On réccupère le field du parent + attrname = f.name + tpl_field = ActivityTemplate._meta.get_field(attrname) + # Peut-être que ce n'est pas un field + # concerné par l'héritage + except FieldDoesNotExist: + continue + + # Y'a certains champs dont on se moque + if attrname in ['id', 'staff', 'tags', ]: + continue + + # C'est plus compliqué que ça pour les nb_perm + if attrname in ['max_perm', 'min_perm', ]: + if not self.get_herited('has_perm'): + continue + + + # On a un Many to Many, on lit différement + if tpl_field.many_to_many: + pass + # # On a pas spécifié + # if not value.exists(): + # # On a pas de parent + # if self.parent is None: + # errors.append(ValidationError( + # _("N'hérite pas d'un template, spécifier le champs : %(attr)s"), + # code='bad-overriding', + # params={'attr': f.verbose_name}, + # )) + # else: + # pvalue = getattr(self.parent, attrname) + # # On a un parent qui ne dit rien + # if not pvalue.exists(): + # errors.append(ValidationError( + # _("Champs non précisé chez le parent, spécifier : %(attr)s"), + # code='bad-overriding', + # params={'attr': f.verbose_name}, + # )) + else: + value = getattr(self, attrname) + # On a pas spécifié + if value is None: + # On a pas de parent + if self.parent is None: + errors.append(ValidationError( + _("N'hérite pas d'un template, spécifier le champs : %(attr)s"), + code='bad-overriding', + params={'attr': f.verbose_name}, + )) + else: + pvalue = getattr(self.parent, attrname) + # On a un parent qui ne dit rien + if pvalue is None: + errors.append(ValidationError( + _("Champs non précisé chez le parent, spécifier : %(attr)s"), + code='bad-overriding', + params={'attr': f.verbose_name}, + )) + if errors != []: + raise ValidationError(errors) + + def get_herited(self, attrname): try: tpl_field = ActivityTemplate._meta.get_field(attrname) @@ -219,4 +320,4 @@ class Activity(AbstractActivityTemplate): verbose_name_plural = _("activités") def __str__(self): - return self.get_herited('title') \ No newline at end of file + return self.get_herited('title')