From 53d9084d6398a83c86882da16641096288096f61 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 18 Jul 2017 18:41:44 +0200 Subject: [PATCH 1/2] Correct Activity inheritance and get_herited() --- communication/migrations/0001_initial.py | 6 +-- equipment/migrations/0001_initial.py | 12 ++--- event/migrations/0001_initial.py | 65 ++++++++++++++++-------- event/models.py | 19 ++++--- 4 files changed, 64 insertions(+), 38 deletions(-) diff --git a/communication/migrations/0001_initial.py b/communication/migrations/0001_initial.py index f103630..17d5c27 100644 --- a/communication/migrations/0001_initial.py +++ b/communication/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.3 on 2017-07-18 15:12 +# Generated by Django 1.11.3 on 2017-07-18 16:10 from __future__ import unicode_literals from django.conf import settings @@ -12,9 +12,9 @@ class Migration(migrations.Migration): initial = True dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ('contenttypes', '0002_remove_content_type_name'), ('auth', '0008_alter_user_username_max_length'), + ('contenttypes', '0002_remove_content_type_name'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), ] operations = [ diff --git a/equipment/migrations/0001_initial.py b/equipment/migrations/0001_initial.py index 8ffae95..3ec60b7 100644 --- a/equipment/migrations/0001_initial.py +++ b/equipment/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.3 on 2017-07-18 15:12 +# Generated by Django 1.11.3 on 2017-07-18 16:10 from __future__ import unicode_literals from django.db import migrations, models @@ -24,8 +24,8 @@ class Migration(migrations.Migration): ('description', models.TextField(verbose_name='description')), ], options={ - 'verbose_name': 'matériel abstrait', 'verbose_name_plural': 'matériels abstraits', + 'verbose_name': 'matériel abstrait', }, ), migrations.CreateModel( @@ -37,8 +37,8 @@ class Migration(migrations.Migration): ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Activity')), ], options={ - 'verbose_name': 'attribution de matériel', 'verbose_name_plural': 'attributions de matériel', + 'verbose_name': 'attribution de matériel', }, ), migrations.CreateModel( @@ -51,8 +51,8 @@ class Migration(migrations.Migration): ('is_lost', models.BooleanField()), ], options={ - 'verbose_name': 'remarque sur matériel', 'verbose_name_plural': 'remarques sur le matériel', + 'verbose_name': 'remarque sur matériel', }, ), migrations.CreateModel( @@ -61,8 +61,8 @@ class Migration(migrations.Migration): ('abstractequipment_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='equipment.AbstractEquipment')), ], options={ - 'verbose_name': 'matériel permanent', 'verbose_name_plural': 'matériels permanents', + 'verbose_name': 'matériel permanent', }, bases=('equipment.abstractequipment',), ), @@ -73,8 +73,8 @@ class Migration(migrations.Migration): ('event', models.ForeignKey(help_text='Évènement pour lequel le matériel a été loué ou emprunté ou apporté', on_delete=django.db.models.deletion.CASCADE, related_name='specific_equipment', to='event.Event')), ], options={ - 'verbose_name': 'matériel temporaire', 'verbose_name_plural': 'matériels temporaires', + 'verbose_name': 'matériel temporaire', }, bases=('equipment.abstractequipment',), ), diff --git a/event/migrations/0001_initial.py b/event/migrations/0001_initial.py index ac25a16..b8d522b 100644 --- a/event/migrations/0001_initial.py +++ b/event/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.3 on 2017-07-18 15:12 +# Generated by Django 1.11.3 on 2017-07-18 16:10 from __future__ import unicode_literals from django.conf import settings @@ -17,6 +17,25 @@ class Migration(migrations.Migration): ] operations = [ + migrations.CreateModel( + name='Activity', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(blank=True, max_length=200, null=True, verbose_name="nom de l'activité")), + ('is_public', models.NullBooleanField()), + ('has_perm', models.NullBooleanField()), + ('min_perm', models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='nombre minimum de permanents')), + ('max_perm', models.PositiveSmallIntegerField(blank=True, null=True, verbose_name='nombre maximum de permanents')), + ('description', models.TextField(blank=True, help_text='Public, Visible par tout le monde.', null=True, verbose_name='description')), + ('remarks', models.TextField(blank=True, help_text='Visible uniquement par les organisateurs', null=True, verbose_name='remarques')), + ('beginning', models.DateTimeField(verbose_name='heure de début')), + ('end', models.DateTimeField(verbose_name='heure de fin')), + ], + options={ + 'verbose_name_plural': 'activités', + 'verbose_name': 'activité', + }, + ), migrations.CreateModel( name='ActivityTag', fields=[ @@ -26,8 +45,8 @@ class Migration(migrations.Migration): ('color', models.CharField(help_text='Rentrer une couleur en hexadécimal', max_length=7, validators=[django.core.validators.RegexValidator(message="La chaîne de caractère rentrée n'est pas une couleur en hexadécimal.", regex='^#(?:[0-9a-fA-F]{3}){1,2}$')], verbose_name='couleur')), ], options={ - 'verbose_name': 'tag', 'verbose_name_plural': 'tags', + 'verbose_name': 'tag', }, ), migrations.CreateModel( @@ -43,8 +62,8 @@ class Migration(migrations.Migration): ('remarks', models.TextField(blank=True, help_text='Visible uniquement par les organisateurs', null=True, verbose_name='remarques')), ], options={ - 'verbose_name': 'template activité', 'verbose_name_plural': 'templates activité', + 'verbose_name': 'template activité', }, ), migrations.CreateModel( @@ -56,12 +75,12 @@ class Migration(migrations.Migration): ('creation_date', models.DateTimeField(auto_now_add=True, verbose_name='date de création')), ('description', models.TextField(verbose_name='description')), ('beginning_date', models.DateTimeField(verbose_name='date de début')), - ('ending_date', models.DateTimeField(blank=True, null=True, verbose_name='date de fin')), + ('ending_date', models.DateTimeField(verbose_name='date de fin')), ('created_by', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='created_events', to=settings.AUTH_USER_MODEL)), ], options={ - 'verbose_name': 'évènement', 'verbose_name_plural': 'évènements', + 'verbose_name': 'évènement', }, ), migrations.CreateModel( @@ -72,46 +91,48 @@ class Migration(migrations.Migration): ('description', models.TextField(blank=True)), ], options={ - 'verbose_name': 'lieu', 'verbose_name_plural': 'lieux', + 'verbose_name': 'lieu', }, ), - migrations.CreateModel( - name='Activity', - fields=[ - ('activitytemplate_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='event.ActivityTemplate')), - ('beginning', models.DateTimeField(verbose_name='heure de début')), - ('end', models.DateTimeField(verbose_name='heure de fin')), - ], - options={ - 'verbose_name': 'activité', - 'verbose_name_plural': 'activités', - }, - bases=('event.activitytemplate',), - ), migrations.AddField( model_name='activitytemplate', name='event', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='activities', to='event.Event'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event'), ), migrations.AddField( model_name='activitytemplate', name='place', - field=models.ManyToManyField(blank=True, related_name='activities', to='event.Place'), + field=models.ManyToManyField(blank=True, to='event.Place'), ), migrations.AddField( model_name='activitytemplate', name='tags', - field=models.ManyToManyField(blank=True, related_name='activities', to='event.ActivityTag'), + field=models.ManyToManyField(blank=True, to='event.ActivityTag'), + ), + migrations.AddField( + model_name='activity', + name='event', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event'), ), migrations.AddField( model_name='activity', name='parent', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='children', to='event.ActivityTemplate'), ), + migrations.AddField( + model_name='activity', + name='place', + field=models.ManyToManyField(blank=True, to='event.Place'), + ), migrations.AddField( model_name='activity', name='staff', field=models.ManyToManyField(blank=True, related_name='in_perm_activities', to=settings.AUTH_USER_MODEL), ), + migrations.AddField( + model_name='activity', + name='tags', + field=models.ManyToManyField(blank=True, to='event.ActivityTag'), + ), ] diff --git a/event/models.py b/event/models.py index 9cee1ee..b9f5945 100644 --- a/event/models.py +++ b/event/models.py @@ -84,7 +84,7 @@ class ActivityTag(models.Model): return self.name -class ActivityTemplate(SubscriptionMixin, models.Model): +class AbstractActivityTemplate(SubscriptionMixin, models.Model): title = models.CharField( _("nom de l'activité"), max_length=200, @@ -94,7 +94,6 @@ class ActivityTemplate(SubscriptionMixin, models.Model): # FIXME: voir comment on traite l'héritage de `event` event = models.ForeignKey( Event, - related_name="activities", blank=True, null=True, ) @@ -128,15 +127,18 @@ class ActivityTemplate(SubscriptionMixin, models.Model): ) tags = models.ManyToManyField( ActivityTag, - related_name="activities", blank=True, ) place = models.ManyToManyField( Place, - related_name="activities", blank=True, ) + class Meta: + abstract = True + + +class ActivityTemplate(AbstractActivityTemplate): class Meta: verbose_name = _("template activité") verbose_name_plural = _("templates activité") @@ -145,7 +147,7 @@ class ActivityTemplate(SubscriptionMixin, models.Model): return self.title -class Activity(ActivityTemplate): +class Activity(AbstractActivityTemplate): parent = models.ForeignKey( ActivityTemplate, related_name="children", @@ -160,13 +162,16 @@ class Activity(ActivityTemplate): end = models.DateTimeField(_("heure de fin")) def get_herited(self, attrname): + inherited_fields = [f.name for f in ActivityTemplate._meta.get_fields()] + m2m_fields = [f.name for f in ActivityTemplate._meta.get_fields() + if f.many_to_many] attr = super(Activity, self).__getattribute__(attrname) - if attrname in {"parent", "staff", "equipment"}: + if attrname not in inherited_fields: raise FieldError( _("%(attrname)s n'est pas un champ héritable"), params={'attrname': attrname}, ) - elif (attrname == 'place' or attrname == 'tags'): + elif attrname in m2m_fields: if attr.exists(): return attr else: From 670a9d45da782dc3db83da866da421411c77a2e0 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 18 Jul 2017 19:08:09 +0200 Subject: [PATCH 2/2] Use EventSpecificMixin for event-specific models --- communication/migrations/0001_initial.py | 4 +- equipment/migrations/0001_initial.py | 50 ++++++------------------ equipment/models.py | 32 ++++----------- event/migrations/0001_initial.py | 12 ++++-- event/models.py | 11 ++---- event/tests.py | 3 +- shared/models.py | 18 ++++++++- 7 files changed, 53 insertions(+), 77 deletions(-) diff --git a/communication/migrations/0001_initial.py b/communication/migrations/0001_initial.py index 17d5c27..afcbd10 100644 --- a/communication/migrations/0001_initial.py +++ b/communication/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.3 on 2017-07-18 16:10 +# Generated by Django 1.11.3 on 2017-07-18 17:05 from __future__ import unicode_literals from django.conf import settings @@ -13,8 +13,8 @@ class Migration(migrations.Migration): dependencies = [ ('auth', '0008_alter_user_username_max_length'), - ('contenttypes', '0002_remove_content_type_name'), migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('contenttypes', '0002_remove_content_type_name'), ] operations = [ diff --git a/equipment/migrations/0001_initial.py b/equipment/migrations/0001_initial.py index 3ec60b7..c6a34b4 100644 --- a/equipment/migrations/0001_initial.py +++ b/equipment/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.3 on 2017-07-18 16:10 +# Generated by Django 1.11.3 on 2017-07-18 17:05 from __future__ import unicode_literals from django.db import migrations, models @@ -16,7 +16,7 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='AbstractEquipment', + name='Equipment', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=200, verbose_name='nom du matériel')), @@ -24,8 +24,8 @@ class Migration(migrations.Migration): ('description', models.TextField(verbose_name='description')), ], options={ - 'verbose_name_plural': 'matériels abstraits', - 'verbose_name': 'matériel abstrait', + 'verbose_name_plural': 'matériels permanents', + 'verbose_name': 'matériel permanent', }, ), migrations.CreateModel( @@ -35,6 +35,7 @@ class Migration(migrations.Migration): ('amount', models.PositiveSmallIntegerField(verbose_name='quantité attribuée')), ('remarks', models.TextField(verbose_name="remarques concernant l'attribution")), ('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Activity')), + ('equipment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='equipment.Equipment')), ], options={ 'verbose_name_plural': 'attributions de matériel', @@ -49,48 +50,21 @@ class Migration(migrations.Migration): ('amount', models.PositiveSmallIntegerField(verbose_name='quantité concernée')), ('is_broken', models.BooleanField()), ('is_lost', models.BooleanField()), + ('equipment', models.ForeignKey(help_text='Matériel concerné par la remarque', on_delete=django.db.models.deletion.CASCADE, related_name='remarks', to='equipment.Equipment')), ], options={ 'verbose_name_plural': 'remarques sur le matériel', 'verbose_name': 'remarque sur matériel', }, ), - migrations.CreateModel( - name='Equipment', - fields=[ - ('abstractequipment_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='equipment.AbstractEquipment')), - ], - options={ - 'verbose_name_plural': 'matériels permanents', - 'verbose_name': 'matériel permanent', - }, - bases=('equipment.abstractequipment',), - ), - migrations.CreateModel( - name='TemporaryEquipment', - fields=[ - ('abstractequipment_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='equipment.AbstractEquipment')), - ('event', models.ForeignKey(help_text='Évènement pour lequel le matériel a été loué ou emprunté ou apporté', on_delete=django.db.models.deletion.CASCADE, related_name='specific_equipment', to='event.Event')), - ], - options={ - 'verbose_name_plural': 'matériels temporaires', - 'verbose_name': 'matériel temporaire', - }, - bases=('equipment.abstractequipment',), - ), migrations.AddField( - model_name='equipmentremark', - name='equipment', - field=models.ForeignKey(help_text='Matériel concerné par la remarque', on_delete=django.db.models.deletion.CASCADE, related_name='remarks', to='equipment.AbstractEquipment'), - ), - migrations.AddField( - model_name='equipmentattribution', - name='equipment', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='equipment.AbstractEquipment'), - ), - migrations.AddField( - model_name='abstractequipment', + model_name='equipment', name='activities', field=models.ManyToManyField(related_name='equipment', through='equipment.EquipmentAttribution', to='event.Activity'), ), + migrations.AddField( + model_name='equipment', + name='event', + field=models.ForeignKey(blank=True, help_text="Si spécifié, l'instance du modèleest spécifique à l'évènement en question", null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event', verbose_name='évènement'), + ), ] diff --git a/equipment/models.py b/equipment/models.py index 9fd3287..84dd205 100644 --- a/equipment/models.py +++ b/equipment/models.py @@ -1,9 +1,10 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -from event.models import Event, Activity +from event.models import Activity +from shared.models import EventSpecificMixin -class AbstractEquipment(models.Model): +class Equipment(EventSpecificMixin, models.Model): name = models.CharField( _("nom du matériel"), max_length=200, @@ -17,34 +18,15 @@ class AbstractEquipment(models.Model): ) class Meta: - verbose_name = _("matériel abstrait") - verbose_name_plural = _("matériels abstraits") + verbose_name = _("matériel permanent") + verbose_name_plural = _("matériels permanents") def __str__(self): return self.name -class Equipment(AbstractEquipment): - class Meta: - verbose_name = _("matériel permanent") - verbose_name_plural = _("matériels permanents") - - -class TemporaryEquipment(AbstractEquipment): - event = models.ForeignKey( - Event, - related_name="specific_equipment", - help_text=_("Évènement pour lequel le matériel " - "a été loué ou emprunté ou apporté"), - ) - - class Meta: - verbose_name = _("matériel temporaire") - verbose_name_plural = _("matériels temporaires") - - class EquipmentAttribution(models.Model): - equipment = models.ForeignKey(AbstractEquipment) + equipment = models.ForeignKey(Equipment) activity = models.ForeignKey(Activity) amount = models.PositiveSmallIntegerField(_("quantité attribuée")) remarks = models.TextField(_("remarques concernant l'attribution")) @@ -62,7 +44,7 @@ class EquipmentAttribution(models.Model): class EquipmentRemark(models.Model): remark = models.TextField(_("remarque sur le matériel")) equipment = models.ForeignKey( - AbstractEquipment, + Equipment, related_name="remarks", help_text=_("Matériel concerné par la remarque"), ) diff --git a/event/migrations/0001_initial.py b/event/migrations/0001_initial.py index b8d522b..38949c0 100644 --- a/event/migrations/0001_initial.py +++ b/event/migrations/0001_initial.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.3 on 2017-07-18 16:10 +# Generated by Django 1.11.3 on 2017-07-18 17:05 from __future__ import unicode_literals from django.conf import settings @@ -89,6 +89,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField(max_length=200, verbose_name='nom du lieu')), ('description', models.TextField(blank=True)), + ('event', models.ForeignKey(blank=True, help_text="Si spécifié, l'instance du modèleest spécifique à l'évènement en question", null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event', verbose_name='évènement')), ], options={ 'verbose_name_plural': 'lieux', @@ -98,7 +99,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='activitytemplate', name='event', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Event'), ), migrations.AddField( model_name='activitytemplate', @@ -110,10 +111,15 @@ class Migration(migrations.Migration): name='tags', field=models.ManyToManyField(blank=True, to='event.ActivityTag'), ), + migrations.AddField( + model_name='activitytag', + name='event', + field=models.ForeignKey(blank=True, help_text="Si spécifié, l'instance du modèleest spécifique à l'évènement en question", null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event', verbose_name='évènement'), + ), migrations.AddField( model_name='activity', name='event', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='event.Event'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Event'), ), migrations.AddField( model_name='activity', diff --git a/event/models.py b/event/models.py index b9f5945..fca94c2 100644 --- a/event/models.py +++ b/event/models.py @@ -4,6 +4,7 @@ from django.core.validators import RegexValidator from django.core.exceptions import FieldError from django.db import models from communication.models import SubscriptionMixin +from shared.models import EventSpecificMixin class Event(SubscriptionMixin, models.Model): @@ -38,7 +39,7 @@ class Event(SubscriptionMixin, models.Model): return self.title -class Place(models.Model): +class Place(EventSpecificMixin, models.Model): name = models.CharField( _("nom du lieu"), max_length=200, @@ -53,7 +54,7 @@ class Place(models.Model): return self.name -class ActivityTag(models.Model): +class ActivityTag(EventSpecificMixin, models.Model): name = models.CharField( _("nom du tag"), max_length=200, @@ -92,11 +93,7 @@ class AbstractActivityTemplate(SubscriptionMixin, models.Model): null=True, ) # FIXME: voir comment on traite l'héritage de `event` - event = models.ForeignKey( - Event, - blank=True, - null=True, - ) + event = models.ForeignKey(Event) is_public = models.NullBooleanField( blank=True, ) diff --git a/event/tests.py b/event/tests.py index 7185054..f94b5ab 100644 --- a/event/tests.py +++ b/event/tests.py @@ -43,9 +43,10 @@ class ActivityInheritanceTest(TestCase): ) def setUp(self): - self.template_act = ActivityTemplate.objects.create() + self.template_act = ActivityTemplate.objects.create(event=self.event) self.real_act = Activity.objects.create( parent=self.template_act, + event=self.event, beginning=timezone.now() + timedelta(days=30), end=timezone.now() diff --git a/shared/models.py b/shared/models.py index 71a8362..0e55ab0 100644 --- a/shared/models.py +++ b/shared/models.py @@ -1,3 +1,19 @@ from django.db import models +from django.utils.translation import ugettext_lazy as _ -# Create your models here. + +class EventSpecificMixin(models.Model): + """Mixin allowing for event-specific models instances + or not (depending on whether the event field is null)""" + + event = models.ForeignKey( + 'event.Event', + verbose_name=_("évènement"), + help_text=_("Si spécifié, l'instance du modèle" + "est spécifique à l'évènement en question"), + blank=True, + null=True + ) + + class Meta: + abstract = True