Correct Activity inheritance and get_herited()

This commit is contained in:
Ludovic Stephan 2017-07-18 18:41:44 +02:00
parent 5b005571d5
commit 53d9084d63
4 changed files with 64 additions and 38 deletions

View file

@ -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 = [

View file

@ -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',),
),

View file

@ -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'),
),
]

View file

@ -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: