kpsul/gestion/migrations/0003_association_and_events.py

290 lines
13 KiB
Python
Raw Normal View History

2017-07-26 20:24:49 +02:00
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-07-26 18:29
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
def create_apps(apps, schema_editor):
Association = apps.get_model("gestion", "Association")
Group = apps.get_model("auth", "Group")
cof_m = Group.objects.get(name="cof_members")
cof_b = Group.objects.get(name="cof_buro")
bds_m = Group.objects.get(name="bds_members")
bds_b = Group.objects.get(name="bds_buro")
Association.objects.bulk_create([
2017-08-06 20:56:11 +02:00
Association(id=1, name="COF", staff_group=cof_b, members_group=cof_m),
Association(id=2, name="BDS", staff_group=bds_b, members_group=bds_m),
2017-07-26 20:24:49 +02:00
])
def import_events(apps, schema_editor):
# Fetching the models that have be moved from cof to gestion
model_names = [
"Event", "EventCommentField", "EventCommentValue", "EventOption",
"EventOptionChoice", "EventRegistration"
]
models = [
(apps.get_model("cof", name), apps.get_model("gestion", name))
for name in model_names
]
# The old Event.location field becomes a table: we need to create an entry
# in this table for each value of the old `location` field.
OldEvent, NewEvent = models[0]
Location = apps.get_model("gestion", "Location")
locations = set() # A set to prevent duplicate entries
events = []
for event in OldEvent.objects.values():
locations.add(event["location"])
events.append(event)
Location.objects.bulk_create([Location(name=name) for name in locations])
map_loc = {
loc.name: loc
for loc in Location.objects.all()
}
for event in events:
event["location"] = map_loc[event["location"]]
NewEvent.objects.bulk_create([
NewEvent(**event)
for event in events
])
# Do not forget to link all the existing event to the COF group
cof = apps.get_model("gestion", "Association").objects.get(name="COF")
for event in NewEvent.objects.all():
event.associations.add(cof)
# Migrating the other models is straigtforward
for OldModel, NewModel in models[1:]:
NewModel.objects.bulk_create([
NewModel(**values)
for values in OldModel.objects.values()
])
def restore_events(apps, schema_editor):
# TODO?
raise NotImplementedError
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('auth', '0008_alter_user_username_max_length'),
('gestion', '0002_club_support'),
("cof", "0009_generic_profiles"),
]
operations = [
migrations.CreateModel(
name='Association',
fields=[
2017-08-06 20:56:11 +02:00
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30, verbose_name="nom de l'association")),
('members_group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='member_group_of', to='auth.Group', verbose_name='groupe des membres')),
('staff_group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='staff_group_of', to='auth.Group', verbose_name='groupe des membres du bureau')),
2017-07-26 20:24:49 +02:00
],
options={
'verbose_name': 'association',
'verbose_name_plural': 'associations',
},
),
migrations.CreateModel(
name='Event',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200, verbose_name='titre')),
('start_date', models.DateTimeField(blank=True, null=True, verbose_name="début de l'événement")),
('end_date', models.DateTimeField(blank=True, null=True, verbose_name="fin de l'événement")),
('description', models.TextField(blank=True, verbose_name='description')),
('image', models.ImageField(blank=True, null=True, upload_to='public/imgs/events/', verbose_name='image')),
2017-08-06 20:56:11 +02:00
('registration_open', models.NullBooleanField(default=True, help_text="Indéfini signifie « l'inscription n'est pas requise\xa0»", verbose_name='les inscriptions sont ouvertes')),
2017-07-26 20:24:49 +02:00
('old', models.BooleanField(default=False, verbose_name='archiver (événement fini)')),
('associations', models.ManyToManyField(related_name='events', to='gestion.Association', verbose_name='associations')),
],
options={
'verbose_name': 'événement',
'verbose_name_plural': 'événements',
},
),
migrations.CreateModel(
name='EventCommentField',
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 champ')),
('fieldtype', models.CharField(choices=[('text', 'Texte long'), ('char', 'Texte court')], default='text', max_length=10, verbose_name='type de champ')),
('default', models.TextField(blank=True, verbose_name='valeur par défaut')),
('ordering', models.IntegerField(default=False, help_text='plus petit en premier, ordre alphabétique sur le nom si ambiguïté', verbose_name='ordre des champs')),
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='commentfields', to='gestion.Event', verbose_name='événement')),
],
options={
'verbose_name': 'champ',
'verbose_name_plural': 'champs',
},
),
migrations.CreateModel(
name='EventCommentValue',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField(blank=True, verbose_name='contenu')),
('commentfield', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='values', to='gestion.EventCommentField')),
],
),
migrations.CreateModel(
name='EventOption',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=200, verbose_name='option')),
('multi_choices', models.BooleanField(default=False, verbose_name='choix multiples')),
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='options', to='gestion.Event', verbose_name='événement')),
],
options={
'verbose_name': 'option des événements',
'verbose_name_plural': 'options des événements',
},
),
migrations.CreateModel(
name='EventOptionChoice',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.CharField(max_length=200, verbose_name='Valeur')),
('event_option', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='choices', to='gestion.EventOption', verbose_name='événement')),
],
options={
'verbose_name': 'choix',
'verbose_name_plural': 'choix',
},
),
migrations.CreateModel(
name='EventRegistration',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('paid', models.BooleanField(default=False, verbose_name='a payé')),
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gestion.Event', verbose_name='événement')),
('filledcomments', models.ManyToManyField(through='gestion.EventCommentValue', to='gestion.EventCommentField', verbose_name='commentaires')),
('options', models.ManyToManyField(to='gestion.EventOptionChoice', verbose_name='choix')),
2017-08-06 20:56:11 +02:00
('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL, verbose_name='utilisateur')),
2017-07-26 20:24:49 +02:00
],
options={
'verbose_name': 'inscription',
'verbose_name_plural': 'inscriptions',
},
),
migrations.CreateModel(
name='Location',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
2017-08-06 20:56:11 +02:00
('name', models.CharField(max_length=200, verbose_name='nom du lieu')),
2017-07-26 20:24:49 +02:00
],
options={
'verbose_name': 'lieu',
'verbose_name_plural': 'lieux',
},
),
migrations.AlterModelOptions(
name='profile',
options={'verbose_name': 'profil', 'verbose_name_plural': 'profils'},
),
migrations.RemoveField(
model_name='club',
name='associations',
),
migrations.AlterField(
model_name='club',
name='cotisation_frequency',
field=models.CharField(blank=True, choices=[('ANN', 'Annuel'), ('SEM', 'Semestriel'), ('COU', 'Au cours')], default='ANN', max_length=3, verbose_name='fréquence de la cotisation'),
),
migrations.AlterField(
model_name='club',
name='description',
field=models.TextField(blank=True, verbose_name='description'),
),
migrations.AlterField(
model_name='club',
name='members',
field=models.ManyToManyField(blank=True, related_name='in_clubs', through='gestion.ClubUser', to=settings.AUTH_USER_MODEL, verbose_name='membres du club'),
),
migrations.AlterField(
model_name='club',
name='name',
field=models.CharField(max_length=200, unique=True, verbose_name='nom'),
),
migrations.AlterField(
model_name='club',
name='price',
field=models.DecimalField(blank=True, decimal_places=2, default=0, max_digits=5, verbose_name='cotisation (€)'),
),
migrations.AlterField(
model_name='clubuser',
name='has_paid',
field=models.BooleanField(verbose_name='a payé sa cotisation'),
),
migrations.AlterField(
model_name='clubuser',
name='is_respo',
field=models.BooleanField(verbose_name='est responsable du club'),
),
migrations.AlterField(
model_name='profile',
name='comments',
field=models.TextField(blank=True, verbose_name="commentaires visibles par l'utilisateur"),
),
migrations.AlterField(
model_name='profile',
name='departement',
field=models.CharField(blank=True, max_length=50, verbose_name='département'),
),
migrations.AlterField(
model_name='profile',
name='login_clipper',
field=models.CharField(blank=True, max_length=8, verbose_name='login clipper'),
),
migrations.AlterField(
model_name='profile',
name='occupation',
field=models.CharField(choices=[('exterieur', 'Extérieur'), ('1A', '1A'), ('2A', '2A'), ('3A', '3A'), ('4A', '4A'), ('archicube', 'Archicube'), ('doctorant', 'Doctorant'), ('CST', 'CST')], default='1A', max_length=9, verbose_name='occupation'),
),
migrations.AlterField(
model_name='profile',
name='phone',
field=models.CharField(blank=True, max_length=20, verbose_name='téléphone'),
),
migrations.AlterField(
model_name='profile',
name='user',
field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL, verbose_name='utilisateur'),
),
migrations.AddField(
model_name='eventcommentvalue',
name='registration',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='gestion.EventRegistration'),
),
migrations.AddField(
model_name='event',
name='location',
field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='gestion.Location', verbose_name='lieux'),
),
migrations.AddField(
model_name='club',
name='association',
2017-08-06 20:56:11 +02:00
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, related_name='clubs', to='gestion.Association', verbose_name='association'),
2017-07-26 20:24:49 +02:00
preserve_default=False,
),
migrations.AlterUniqueTogether(
name='eventregistration',
unique_together=set([('user', 'event')]),
),
migrations.AlterUniqueTogether(
name='eventcommentvalue',
unique_together=set([('commentfield', 'registration')]),
),
migrations.RunPython(create_apps, migrations.RunPython.noop),
migrations.RunPython(import_events, restore_events),
]