Use a separate models for events' locations
This commit is contained in:
parent
18ee33e1e0
commit
8a751e5c85
2 changed files with 64 additions and 10 deletions
|
@ -18,21 +18,42 @@ def import_events(apps, schema_editor):
|
|||
for name in model_names
|
||||
]
|
||||
|
||||
# Moving the data into the new table
|
||||
for OldModel, NewModel in models:
|
||||
# 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()
|
||||
])
|
||||
|
||||
# Linking all the existing event to the COF group
|
||||
cof = apps.get_model("auth", "Group").objects.get(name="cof_buro")
|
||||
_, NewEvent = models[0]
|
||||
for event in NewEvent.objects.all():
|
||||
event.associations.add(cof)
|
||||
|
||||
|
||||
def restore_events(apps, schema_editor):
|
||||
# TODO?
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
|
@ -45,12 +66,37 @@ class Migration(migrations.Migration):
|
|||
]
|
||||
|
||||
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.CharField(max_length=200, verbose_name='Lieu')),
|
||||
('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')),
|
||||
|
|
|
@ -118,10 +118,18 @@ class ClubUser(models.Model):
|
|||
# Events
|
||||
# ---
|
||||
|
||||
class Location(models.Model):
|
||||
name = models.CharField("Lieu", max_length=200)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "lieu"
|
||||
verbose_name_plural = "lieux"
|
||||
|
||||
|
||||
class Event(models.Model):
|
||||
associations = models.ManyToManyField(Group, related_name="events")
|
||||
title = models.CharField("Titre", max_length=200)
|
||||
location = models.CharField("Lieu", max_length=200)
|
||||
location = models.ForeignKey(Location, on_delete=models.PROTECT)
|
||||
start_date = models.DateTimeField("Date de début", blank=True, null=True)
|
||||
end_date = models.DateTimeField("Date de fin", blank=True, null=True)
|
||||
description = models.TextField("Description", blank=True)
|
||||
|
|
Loading…
Reference in a new issue