Use EventSpecificMixin for event-specific models

This commit is contained in:
Ludovic Stephan 2017-07-18 19:08:09 +02:00
parent 53d9084d63
commit 670a9d45da
7 changed files with 53 additions and 77 deletions

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- 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 __future__ import unicode_literals
from django.conf import settings from django.conf import settings
@ -13,8 +13,8 @@ class Migration(migrations.Migration):
dependencies = [ dependencies = [
('auth', '0008_alter_user_username_max_length'), ('auth', '0008_alter_user_username_max_length'),
('contenttypes', '0002_remove_content_type_name'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL), migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0002_remove_content_type_name'),
] ]
operations = [ operations = [

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- 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 __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
@ -16,7 +16,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='AbstractEquipment', name='Equipment',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('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')), ('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')), ('description', models.TextField(verbose_name='description')),
], ],
options={ options={
'verbose_name_plural': 'matériels abstraits', 'verbose_name_plural': 'matériels permanents',
'verbose_name': 'matériel abstrait', 'verbose_name': 'matériel permanent',
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
@ -35,6 +35,7 @@ class Migration(migrations.Migration):
('amount', models.PositiveSmallIntegerField(verbose_name='quantité attribuée')), ('amount', models.PositiveSmallIntegerField(verbose_name='quantité attribuée')),
('remarks', models.TextField(verbose_name="remarques concernant l'attribution")), ('remarks', models.TextField(verbose_name="remarques concernant l'attribution")),
('activity', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='event.Activity')), ('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={ options={
'verbose_name_plural': 'attributions de matériel', 'verbose_name_plural': 'attributions de matériel',
@ -49,48 +50,21 @@ class Migration(migrations.Migration):
('amount', models.PositiveSmallIntegerField(verbose_name='quantité concernée')), ('amount', models.PositiveSmallIntegerField(verbose_name='quantité concernée')),
('is_broken', models.BooleanField()), ('is_broken', models.BooleanField()),
('is_lost', 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={ options={
'verbose_name_plural': 'remarques sur le matériel', 'verbose_name_plural': 'remarques sur le matériel',
'verbose_name': 'remarque sur 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( migrations.AddField(
model_name='equipmentremark', model_name='equipment',
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',
name='activities', name='activities',
field=models.ManyToManyField(related_name='equipment', through='equipment.EquipmentAttribution', to='event.Activity'), 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'),
),
] ]

View file

@ -1,9 +1,10 @@
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ 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( name = models.CharField(
_("nom du matériel"), _("nom du matériel"),
max_length=200, max_length=200,
@ -17,34 +18,15 @@ class AbstractEquipment(models.Model):
) )
class Meta: class Meta:
verbose_name = _("matériel abstrait") verbose_name = _("matériel permanent")
verbose_name_plural = _("matériels abstraits") verbose_name_plural = _("matériels permanents")
def __str__(self): def __str__(self):
return self.name 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): class EquipmentAttribution(models.Model):
equipment = models.ForeignKey(AbstractEquipment) equipment = models.ForeignKey(Equipment)
activity = models.ForeignKey(Activity) activity = models.ForeignKey(Activity)
amount = models.PositiveSmallIntegerField(_("quantité attribuée")) amount = models.PositiveSmallIntegerField(_("quantité attribuée"))
remarks = models.TextField(_("remarques concernant l'attribution")) remarks = models.TextField(_("remarques concernant l'attribution"))
@ -62,7 +44,7 @@ class EquipmentAttribution(models.Model):
class EquipmentRemark(models.Model): class EquipmentRemark(models.Model):
remark = models.TextField(_("remarque sur le matériel")) remark = models.TextField(_("remarque sur le matériel"))
equipment = models.ForeignKey( equipment = models.ForeignKey(
AbstractEquipment, Equipment,
related_name="remarks", related_name="remarks",
help_text=_("Matériel concerné par la remarque"), help_text=_("Matériel concerné par la remarque"),
) )

View file

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- 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 __future__ import unicode_literals
from django.conf import settings 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')), ('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')), ('name', models.CharField(max_length=200, verbose_name='nom du lieu')),
('description', models.TextField(blank=True)), ('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={ options={
'verbose_name_plural': 'lieux', 'verbose_name_plural': 'lieux',
@ -98,7 +99,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='activitytemplate', model_name='activitytemplate',
name='event', 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( migrations.AddField(
model_name='activitytemplate', model_name='activitytemplate',
@ -110,10 +111,15 @@ class Migration(migrations.Migration):
name='tags', name='tags',
field=models.ManyToManyField(blank=True, to='event.ActivityTag'), 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( migrations.AddField(
model_name='activity', model_name='activity',
name='event', 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( migrations.AddField(
model_name='activity', model_name='activity',

View file

@ -4,6 +4,7 @@ from django.core.validators import RegexValidator
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django.db import models from django.db import models
from communication.models import SubscriptionMixin from communication.models import SubscriptionMixin
from shared.models import EventSpecificMixin
class Event(SubscriptionMixin, models.Model): class Event(SubscriptionMixin, models.Model):
@ -38,7 +39,7 @@ class Event(SubscriptionMixin, models.Model):
return self.title return self.title
class Place(models.Model): class Place(EventSpecificMixin, models.Model):
name = models.CharField( name = models.CharField(
_("nom du lieu"), _("nom du lieu"),
max_length=200, max_length=200,
@ -53,7 +54,7 @@ class Place(models.Model):
return self.name return self.name
class ActivityTag(models.Model): class ActivityTag(EventSpecificMixin, models.Model):
name = models.CharField( name = models.CharField(
_("nom du tag"), _("nom du tag"),
max_length=200, max_length=200,
@ -92,11 +93,7 @@ class AbstractActivityTemplate(SubscriptionMixin, models.Model):
null=True, null=True,
) )
# FIXME: voir comment on traite l'héritage de `event` # FIXME: voir comment on traite l'héritage de `event`
event = models.ForeignKey( event = models.ForeignKey(Event)
Event,
blank=True,
null=True,
)
is_public = models.NullBooleanField( is_public = models.NullBooleanField(
blank=True, blank=True,
) )

View file

@ -43,9 +43,10 @@ class ActivityInheritanceTest(TestCase):
) )
def setUp(self): def setUp(self):
self.template_act = ActivityTemplate.objects.create() self.template_act = ActivityTemplate.objects.create(event=self.event)
self.real_act = Activity.objects.create( self.real_act = Activity.objects.create(
parent=self.template_act, parent=self.template_act,
event=self.event,
beginning=timezone.now() beginning=timezone.now()
+ timedelta(days=30), + timedelta(days=30),
end=timezone.now() end=timezone.now()

View file

@ -1,3 +1,19 @@
from django.db import models 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