gestioCOF/gestion/migrations/0003_events.py
2017-05-14 13:02:06 +01:00

182 lines
7.7 KiB
Python

# -*- coding: utf-8 -*-
# Generated by Django 1.11b1 on 2017-03-18 20:02
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
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 = []
events = []
for event in OldEvent.objects.values():
locations.append(Location(name=event["location"]))
events.append(event)
Location.objects.bulk_create(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("auth", "Group").objects.get(name="cof_buro")
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'),
]
operations = [
migrations.CreateModel(
name='Location',
fields=[
(
'id',
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID'
)
),
(
'name',
models.CharField(max_length=200, verbose_name='Lieu')
),
],
options={
'verbose_name': 'lieu',
'verbose_name_plural': 'lieux',
},
),
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')),
('location', models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to='gestion.Location'
)),
('start_date', models.DateTimeField(blank=True, null=True, verbose_name='Date de début')),
('end_date', models.DateTimeField(blank=True, null=True, verbose_name='Date de fin')),
('description', models.TextField(blank=True, verbose_name='Description')),
('image', models.ImageField(blank=True, null=True, upload_to='imgs/events/', verbose_name='Image')),
('registration_open', models.BooleanField(default=True, verbose_name='Inscriptions ouvertes')),
('old', models.BooleanField(default=False, verbose_name='Archiver (événement fini)')),
('associations', models.ManyToManyField(related_name='events', to='auth.Group')),
],
options={
'verbose_name': 'Événement',
},
),
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='Champ')),
('fieldtype', models.CharField(choices=[('text', 'Texte long'), ('char', 'Texte court')], default='text', max_length=10, verbose_name='Type')),
('default', models.TextField(blank=True, verbose_name='Valeur par défaut')),
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='commentfields', to='gestion.Event')),
],
options={
'verbose_name': 'Champ',
},
),
migrations.CreateModel(
name='EventCommentValue',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.TextField(blank=True, null=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')),
],
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')),
],
options={
'verbose_name_plural': 'Choix',
'verbose_name': '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')),
('filledcomments', models.ManyToManyField(through='gestion.EventCommentValue', to='gestion.EventCommentField')),
('options', models.ManyToManyField(to='gestion.EventOptionChoice')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'Inscription',
},
),
migrations.AddField(
model_name='eventcommentvalue',
name='registration',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='gestion.EventRegistration'),
),
migrations.AlterUniqueTogether(
name='eventregistration',
unique_together=set([('user', 'event')]),
),
migrations.RunPython(import_events, restore_events),
]