104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
# Generated by Django 3.0.8 on 2020-08-03 22:04
|
|
|
|
import django.db.models.deletion
|
|
from django.db import migrations, models
|
|
|
|
def forwards_migrate_data(apps, schema_editor):
|
|
EventRegistration = apps.get_model('events', 'EventRegistration')
|
|
TimeSlot = apps.get_model('events', 'TimeSlot')
|
|
Event = apps.get_model('events', 'Event')
|
|
|
|
grouped = {}
|
|
for event in Event.objects.all():
|
|
if event.note in grouped:
|
|
grouped[event.note].append(event)
|
|
else:
|
|
grouped[event.note] = [event]
|
|
|
|
for note, events in grouped.items():
|
|
event = events[0]
|
|
new_event = Event(date=event.date, capacity=event.capacity, note=event.note,
|
|
responsible_person=event.responsible_person)
|
|
new_event.save()
|
|
|
|
# Create a corresponding timeslot for every event with the same description
|
|
for event in events:
|
|
new_timeslot = TimeSlot(time=event.time, event=new_event)
|
|
new_timeslot.save()
|
|
|
|
# Connect registrations to the timeslots instead of the events.
|
|
for registration in EventRegistration.objects.filter(event_id=event.id).all():
|
|
registration.time_slot = new_timeslot
|
|
registration.save()
|
|
|
|
def forwards_remove_old_data(apps, schema_editor):
|
|
# Remove old events here so the cascade doesnt remove the registrations.
|
|
EventRegistration = apps.get_model('events', 'EventRegistration')
|
|
TimeSlot = apps.get_model('events', 'TimeSlot')
|
|
Event = apps.get_model('events', 'Event')
|
|
|
|
# Remove all events without timeslot
|
|
for event in Event.objects.exclude(id__in=TimeSlot.objects.values('event_id').all()):
|
|
event.delete()
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
('events', '0001_initial'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.CreateModel(
|
|
name='TimeSlot',
|
|
fields=[
|
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
|
('time', models.IntegerField(choices=[(0, 'voormiddag'), (1, 'namiddag'), (2, 'avond')], default=0)),
|
|
('event', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.Event'))
|
|
],
|
|
),
|
|
migrations.AddField(
|
|
model_name='eventregistration',
|
|
name='time_slot',
|
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='events.TimeSlot', null=True),
|
|
),
|
|
migrations.AddConstraint(
|
|
model_name='timeslot',
|
|
constraint=models.UniqueConstraint(fields=('time', 'event'), name='no_duplicate_timeslots'),
|
|
),
|
|
migrations.AddConstraint(
|
|
model_name='eventregistration',
|
|
constraint=models.UniqueConstraint(fields=('time_slot', 'user'), name='register_only_once_per_timeslot'),
|
|
),
|
|
|
|
migrations.RunPython(forwards_migrate_data, hints={'target_db': 'default'}),
|
|
|
|
migrations.RemoveField(
|
|
model_name='event',
|
|
name='time',
|
|
),
|
|
|
|
# Django bugs out when removing constraints that are made on fk's. We manualy fix it here 2 'AlterField' calls
|
|
# https://code.djangoproject.com/ticket/31335
|
|
migrations.AlterField(
|
|
model_name='eventregistration',
|
|
name='event',
|
|
field=models.ForeignKey(
|
|
db_constraint=False,
|
|
on_delete=django.db.models.deletion.CASCADE,
|
|
related_name='eventregistrations',
|
|
related_query_name='eventregistration',
|
|
to='events.Event',
|
|
),
|
|
),
|
|
migrations.RemoveConstraint(
|
|
model_name='eventregistration',
|
|
name='register_only_once',
|
|
),
|
|
migrations.RemoveField(
|
|
model_name='eventregistration',
|
|
name='event',
|
|
),
|
|
|
|
migrations.RunPython(forwards_remove_old_data, hints={'target_db': 'default'}),
|
|
]
|