|
| 1 | +# Generated by HCW 0.1.0 on 2025 07 09 08:26:20 |
| 2 | +from packaging.version import Version |
| 3 | +from django.db import transaction |
| 4 | + |
| 5 | +from hope_flex_fields.models import FieldDefinition, FlexField |
| 6 | + |
| 7 | +from contextlib import suppress |
| 8 | +from django import forms |
| 9 | +from django.utils.text import slugify |
| 10 | + |
| 11 | +from hope_flex_fields.models import Fieldset |
| 12 | + |
| 13 | +from concurrency.utils import fqn |
| 14 | + |
| 15 | + |
| 16 | +from country_workspace.contrib.hope.constants import HOUSEHOLD_FIELDSET_NAME |
| 17 | + |
| 18 | +from hope_flex_fields.utils import get_kwargs_from_field_class, get_common_attrs |
| 19 | + |
| 20 | +_script_for_version = Version("0.1.0") |
| 21 | + |
| 22 | + |
| 23 | +attrs_default = lambda cls: get_kwargs_from_field_class(cls, get_common_attrs()) |
| 24 | + |
| 25 | + |
| 26 | +FIELDS = { |
| 27 | + "org_enumerator": { |
| 28 | + "forward": { |
| 29 | + "name": "HOPE org enumerator", |
| 30 | + "defaults": { |
| 31 | + "field_type": fqn(forms.ChoiceField), |
| 32 | + "attrs": { |
| 33 | + **attrs_default(forms.ChoiceField), |
| 34 | + "choices": [ |
| 35 | + ["", "None"], |
| 36 | + ["PARTNER", "Partner"], |
| 37 | + ["UNICEF", "UNICEF"], |
| 38 | + ], |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + "backward": { |
| 43 | + "name": "CharField", |
| 44 | + }, |
| 45 | + }, |
| 46 | + "registration_method": { |
| 47 | + "forward": { |
| 48 | + "name": "HOPE registration method", |
| 49 | + "defaults": { |
| 50 | + "field_type": fqn(forms.ChoiceField), |
| 51 | + "attrs": { |
| 52 | + **attrs_default(forms.ChoiceField), |
| 53 | + "choices": [ |
| 54 | + ["", "None"], |
| 55 | + ["COMMUNITY", "Community-level Registration"], |
| 56 | + ["HH_REGISTRATION", "Household Registration"], |
| 57 | + ], |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + "backward": { |
| 62 | + "name": "CharField", |
| 63 | + }, |
| 64 | + }, |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +def forward() -> None: |
| 69 | + with transaction.atomic(): |
| 70 | + fs, __ = Fieldset.objects.get_or_create(name=HOUSEHOLD_FIELDSET_NAME) |
| 71 | + for field_name, field_def in FIELDS.items(): |
| 72 | + field_def["forward"]["defaults"]["slug"] = slugify(field_def["forward"]["name"]) |
| 73 | + fd, __ = FieldDefinition.objects.update_or_create(**field_def["forward"]) |
| 74 | + FlexField.objects.update_or_create( |
| 75 | + name=field_name, |
| 76 | + fieldset=fs, |
| 77 | + defaults={ |
| 78 | + "definition": fd, |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def backward() -> None: |
| 84 | + with transaction.atomic(): |
| 85 | + for field_name, field_def in FIELDS.items(): |
| 86 | + fd = FieldDefinition.objects.get(name=field_def["backward"]["name"]) |
| 87 | + FlexField.objects.filter(name=field_name, fieldset__name=HOUSEHOLD_FIELDSET_NAME).update(definition=fd) |
| 88 | + with suppress(FieldDefinition.DoesNotExist): |
| 89 | + FieldDefinition.objects.get(name=field_def["forward"]["name"]).delete() |
| 90 | + |
| 91 | + |
| 92 | +class Scripts: |
| 93 | + requires = [] |
| 94 | + operations = [(forward, backward)] |
0 commit comments