Apply suggestions from code review

This commit is contained in:
shamoon 2024-12-01 09:52:05 -08:00
parent 65bc65ac02
commit 7756d49e5c
2 changed files with 16 additions and 15 deletions

View File

@ -15,21 +15,22 @@ def migrate_customfield_selects(apps, schema_editor):
CustomField = apps.get_model("documents", "CustomField")
with transaction.atomic():
for custom_field in CustomField.objects.all():
if custom_field.data_type == "select": # CustomField.FieldDataType.SELECT
old_select_options = custom_field.extra_data["select_options"]
custom_field.extra_data["select_options"] = [
{"id": get_random_string(16), "label": value}
for value in old_select_options
]
custom_field.save()
for custom_field in CustomField.objects.filter(
data_type="select",
): # CustomField.FieldDataType.SELECT
old_select_options = custom_field.extra_data["select_options"]
custom_field.extra_data["select_options"] = [
{"id": get_random_string(16), "label": value}
for value in old_select_options
]
custom_field.save()
for instance in CustomFieldInstance.objects.filter(field=custom_field):
if instance.value_select:
instance.value_select = custom_field.extra_data[
"select_options"
][int(instance.value_select)]["id"]
instance.save()
for instance in CustomFieldInstance.objects.filter(field=custom_field):
if instance.value_select:
instance.value_select = custom_field.extra_data["select_options"][
int(instance.value_select)
]["id"]
instance.save()
def reverse_migrate_customfield_selects(apps, schema_editor):

View File

@ -552,7 +552,7 @@ class CustomFieldSerializer(serializers.ModelSerializer):
)
# labels are valid, generate ids if not present
for option in attrs["extra_data"]["select_options"]:
if "id" not in option or option["id"] is None:
if option.get("id") is None:
option["id"] = get_random_string(length=16)
elif (
"data_type" in attrs