Skip to content
Snippets Groups Projects
Unverified Commit 13327f01 authored by Niklas Mohrin's avatar Niklas Mohrin
Browse files

Update `Evaluation.state` values in log entries too

parent 1e936a76
No related branches found
No related tags found
No related merge requests found
from django.db import migrations
import django_fsm
# as defined in the Evaluation model
......@@ -24,28 +23,52 @@ CONVERSION = {
"reviewed": State.REVIEWED,
"published": State.PUBLISHED,
}
REV_CONVERSION = {val: key for key, val in CONVERSION.items()}
def str_to_int(apps, _schema_editor):
def model_str_to_int(apps, _schema_editor):
Evaluation = apps.get_model("evaluation", "Evaluation")
for string_type, int_type in CONVERSION.items():
Evaluation.objects.filter(state=string_type).update(int_state=int_type)
def int_to_str(apps, _schema_editor):
def model_int_to_str(apps, _schema_editor):
Evaluation = apps.get_model("evaluation", "Evaluation")
for string_type, int_type in CONVERSION.items():
Evaluation.objects.filter(int_state=int_type).update(state=string_type)
def logentries_str_to_int(apps, _schema_editor):
LogEntry = apps.get_model("evaluation", "LogEntry")
for entry in LogEntry.objects.filter(content_type__app_label="evaluation", content_type__model="evaluation"):
if "state" in entry.data:
for key in entry.data["state"]:
entry.data["state"][key] = [
CONVERSION[val] if val in CONVERSION else val for val in entry.data["state"][key]
]
entry.save()
def logentries_int_to_str(apps, _schema_editor):
LogEntry = apps.get_model("evaluation", "LogEntry")
for entry in LogEntry.objects.filter(content_type__app_label="evaluation", content_type__model="evaluation"):
if "state" in entry.data:
for key in entry.data["state"]:
entry.data["state"][key] = [
REV_CONVERSION[val] if val in REV_CONVERSION else val for val in entry.data["state"][key]
]
entry.save()
class Migration(migrations.Migration):
dependencies = [
('evaluation', '0122_prepare_evaluation_state_fsm_int'),
("evaluation", "0122_prepare_evaluation_state_fsm_int"),
]
operations = [
migrations.RunPython(str_to_int, int_to_str),
migrations.RunPython(model_str_to_int, model_int_to_str),
migrations.RunPython(logentries_str_to_int, logentries_int_to_str),
migrations.RemoveField(
model_name="evaluation",
name="state",
......
import importlib
from django.db import migrations
original_migration_module = importlib.import_module("evap.evaluation.migrations.0123_evaluation_state_fsm_int")
class Migration(migrations.Migration):
"""
Initially, we forgot to migrate logentries in 0123, so this migration cleans this up.
Note that 0123 is now modified to also take care of the logentries in the first place.
"""
dependencies = [
("evaluation", "0126_add_textanswer_review_email_template"),
]
operations = [
migrations.RunPython(original_migration_module.logentries_str_to_int, migrations.RunPython.noop),
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment