cmsmanage/paperwork/migrations/0006_department_alter_certificationdefinition_department.py

105 lines
3.6 KiB
Python
Raw Permalink Normal View History

# Generated by Django 4.1.3 on 2023-01-18 03:31
import django.core.validators
import django.db.models.deletion
from django.db import migrations, models
def link_departments(apps, schema_editor):
CertificationDefinition = apps.get_model("paperwork", "CertificationDefinition")
Department = apps.get_model("paperwork", "Department")
for certification_definition in CertificationDefinition.objects.all():
department, created = Department.objects.get_or_create(
name=certification_definition.old_department
)
certification_definition.department = department
certification_definition.save()
class Migration(migrations.Migration):
dependencies = [
("membershipworks", "0001_initial"),
("paperwork", "0005_certificationdefinition_mailing_list"),
]
operations = [
migrations.CreateModel(
name="Department",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"name",
models.CharField(
help_text="This will also be used to generate the mailing list name",
max_length=64,
validators=[
django.core.validators.RegexValidator("^[-_ A-Za-z0-9]*$")
],
),
),
(
"list_reply_to_address",
models.EmailField(blank=True, max_length=254),
),
(
"list_moderator_flag",
models.ForeignKey(
blank=True,
null=True,
db_constraint=False,
on_delete=django.db.models.deletion.PROTECT,
to="membershipworks.flag",
),
),
(
"parent",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.PROTECT,
related_name="children",
to="paperwork.department",
),
),
],
),
# rename the old field so that we can reuse the name
migrations.RenameField(
model_name="certificationdefinition",
old_name="department",
new_name="old_department",
),
# Create nullable foreign key to department
migrations.AddField(
model_name="certificationdefinition",
name="department",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="paperwork.department",
null=True,
),
),
# create new Departments and link them to Certification Definitions
migrations.RunPython(link_departments, atomic=True),
# make department not nullable
migrations.AlterField(
model_name="certificationdefinition",
name="department",
field=models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT, to="paperwork.department"
),
),
migrations.RemoveField(
model_name="certificationdefinition",
name="old_department",
),
]