Files
breccia-mapper/people/migrations/0051_refactor_hardcoded_questions.py
James Graham 81598ea624 refactor: allow admin config of static questions
Text and visibility set in admin panel are now respected everywhere
2021-03-19 15:36:09 +00:00

82 lines
2.6 KiB
Python

# Generated by Django 2.2.10 on 2021-03-19 14:37
from django.db import migrations, models
from django.db.models import F
def forward(apps, schema_editor):
"""Move `text` field to `hardcoded_field`."""
models = [
'OrganisationQuestion',
'OrganisationRelationshipQuestion',
'PersonQuestion',
'RelationshipQuestion',
]
models = map(lambda m: apps.get_model('people', m), models)
for model in models:
model.objects.filter(is_hardcoded=True).update(
hardcoded_field=F('text'))
def backward(apps, schema_editor):
"""Move `hardcoded_field` to `text` field."""
models = [
'OrganisationQuestion',
'OrganisationRelationshipQuestion',
'PersonQuestion',
'RelationshipQuestion',
]
models = map(lambda m: apps.get_model('people', m), models)
for model in models:
model.objects.exclude(hardcoded_field='').update(
text=F('hardcoded_field'), is_hardcoded=True)
class Migration(migrations.Migration):
dependencies = [
('people', '0050_relationship_remove_timestamps'),
]
operations = [
migrations.AddField(
model_name='organisationquestion',
name='hardcoded_field',
field=models.CharField(blank=True, help_text='Which hardcoded field does this question represent?', max_length=255),
),
migrations.AddField(
model_name='organisationrelationshipquestion',
name='hardcoded_field',
field=models.CharField(blank=True, help_text='Which hardcoded field does this question represent?', max_length=255),
),
migrations.AddField(
model_name='personquestion',
name='hardcoded_field',
field=models.CharField(blank=True, help_text='Which hardcoded field does this question represent?', max_length=255),
),
migrations.AddField(
model_name='relationshipquestion',
name='hardcoded_field',
field=models.CharField(blank=True, help_text='Which hardcoded field does this question represent?', max_length=255),
),
migrations.RunPython(forward, backward),
migrations.RemoveField(
model_name='organisationquestion',
name='is_hardcoded',
),
migrations.RemoveField(
model_name='organisationrelationshipquestion',
name='is_hardcoded',
),
migrations.RemoveField(
model_name='personquestion',
name='is_hardcoded',
),
migrations.RemoveField(
model_name='relationshipquestion',
name='is_hardcoded',
),
]