ernestophone.ens.fr/partitions/migrations/0002_category.py

93 lines
3.1 KiB
Python
Raw Permalink Normal View History

2018-01-04 20:20:30 +01:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import django.db.models.deletion
2021-04-29 00:27:33 +02:00
from django.db import migrations, models
2018-01-04 20:20:30 +01:00
def create_categories(apps, schema_editor):
# Insert the previously hardcoded categories in the database
Category = apps.get_model("partitions", "Category")
2021-04-29 00:27:33 +02:00
Category.objects.bulk_create(
[
Category(name="Partitions actives", order=1),
Category(name="Partitions optionnelles", order=2),
Category(name="Partitions à venir", order=3),
Category(name="Archives", order=4),
]
)
2018-01-04 20:20:30 +01:00
def set_new_categories(apps, schema_editor):
Category = apps.get_model("partitions", "Category")
PartitionSet = apps.get_model("partitions", "PartitionSet")
assoc_list = Category.objects.order_by("order")
mapping = {
"active": assoc_list[0],
"incoming": assoc_list[2],
"old": assoc_list[3],
2021-04-29 00:27:33 +02:00
"optional": assoc_list[1],
2018-01-04 20:20:30 +01:00
}
for par_set in PartitionSet.objects.all():
par_set.category = mapping[par_set.category_old]
par_set.save()
def reset_old_categories(apps, schema_editor):
Category = apps.get_model("partitions", "Category")
PartitionSet = apps.get_model("partition", "PartitionSet")
assoc_list = Category.values_list("order", "id").order_by("order")
mapping = {
assoc_list[0][1]: "active",
assoc_list[2][1]: "incoming",
assoc_list[3][1]: "old",
2021-04-29 00:27:33 +02:00
assoc_list[1][1]: "optional",
2018-01-04 20:20:30 +01:00
}
for par_set in PartitionSet.objects.all():
par_set.category_old = mapping[par_set.category]
par_set.save()
class Migration(migrations.Migration):
dependencies = [
2021-04-29 00:27:33 +02:00
("partitions", "0001_initial"),
2018-01-04 20:20:30 +01:00
]
operations = [
migrations.CreateModel(
2021-04-29 00:27:33 +02:00
name="Category",
2018-01-04 20:20:30 +01:00
fields=[
2021-04-29 00:27:33 +02:00
(
"id",
models.AutoField(
verbose_name="ID",
primary_key=True,
serialize=False,
auto_created=True,
),
),
("name", models.CharField(max_length=127)),
("order", models.IntegerField(verbose_name="order")),
2018-01-04 20:20:30 +01:00
],
2021-04-29 00:27:33 +02:00
options={"verbose_name": "catégorie", "verbose_name_plural": "catégories"},
2018-01-04 20:20:30 +01:00
),
migrations.RunPython(create_categories, migrations.RunPython.noop),
2021-04-29 00:27:33 +02:00
migrations.RenameField(
model_name="partitionset", old_name="category", new_name="category_old"
),
2018-01-04 20:20:30 +01:00
migrations.AddField(
2021-04-29 00:27:33 +02:00
model_name="partitionset",
name="category",
2018-01-04 20:20:30 +01:00
field=models.ForeignKey(
2021-04-29 00:27:33 +02:00
verbose_name="Type de partition",
to="partitions.Category",
2018-01-04 20:20:30 +01:00
on_delete=django.db.models.deletion.PROTECT,
2021-04-29 00:27:33 +02:00
default=1, # Dummy, will be set by the RunPython operation
2018-01-04 20:20:30 +01:00
),
preserve_default=False,
),
migrations.RunPython(set_new_categories, migrations.RunPython.noop),
migrations.RemoveField(model_name="partitionset", name="category_old"),
]