konova/konova/migrations/0005_auto_20220216_0856.py
mpeltriaux a6f7e605e6 Migrations + Cleanup
* adds needed migrations
* refactors forms.py (700+ lines) in main konova app
    * splits into forms/ and forms/modals and single class/topic-files for better maintainability and overview
* fixes bug in main konova app migration which could occur if a certain compensation migration did not run before
2022-08-15 10:50:01 +02:00

42 lines
1.4 KiB
Python

# Generated by Django 3.1.3 on 2022-02-16 07:56
from django.db import migrations, transaction
from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_AFTER_STATE_BIOTOPES_ID
def migrate_biotopes_from_974_to_654(apps, schema_editor):
KonovaCode = apps.get_model("codelist", "KonovaCode")
CompensationState = apps.get_model("compensation", "CompensationState")
all_states = CompensationState.objects.all()
with transaction.atomic():
for state in all_states:
new_biotope_code = KonovaCode.objects.get(
code_lists__in=[CODELIST_BIOTOPES_ID],
is_selectable=True,
is_archived=False,
short_name=state.biotope_type.short_name,
)
state.biotope_type = new_biotope_code
state.save()
all_states = CompensationState.objects.all()
after_state_list_elements = all_states.filter(
biotope_type__code_lists__in=[CODELIST_AFTER_STATE_BIOTOPES_ID]
)
if after_state_list_elements.count() > 0:
raise Exception("Still states with wrong codelist entries!")
class Migration(migrations.Migration):
dependencies = [
('konova', '0004_auto_20220209_0839'),
('compensation', '0002_auto_20220114_0936'),
]
operations = [
migrations.RunPython(migrate_biotopes_from_974_to_654),
]