From e383fd42bdf9826979e3d3a17887ef252cd9f7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 14 Dec 2020 22:09:08 +0100 Subject: [PATCH] Fix custom fields initializer --- initializers/custom_fields.yml | 13 +++++++------ startup_scripts/000_users.py | 2 +- startup_scripts/020_custom_fields.py | 11 ++++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/initializers/custom_fields.yml b/initializers/custom_fields.yml index 98111fc..654fb69 100644 --- a/initializers/custom_fields.yml +++ b/initializers/custom_fields.yml @@ -52,7 +52,7 @@ # - Third Item # - Fifth Item # - Fourth Item -# select_field_auto_weight: +# select_field_legacy_format: # type: select # label: Choose between items # required: false @@ -61,11 +61,12 @@ # on_objects: # - dcim.models.Device # choices: -# - A -# - B -# - C -# - E -# - D like deprecated +# - value: A # this is the deprecated format. +# - value: B # we only use it for the tests. +# - value: C # please see above for the new format. +# - value: "D like deprecated" +# weight: 999 +# - value: E # boolean_field: # type: boolean # label: Yes Or No? diff --git a/startup_scripts/000_users.py b/startup_scripts/000_users.py index a801d85..ffd4bec 100644 --- a/startup_scripts/000_users.py +++ b/startup_scripts/000_users.py @@ -1,6 +1,6 @@ import sys -from django.contrib.auth.models import Group, User +from django.contrib.auth.models import User from startup_script_utils import load_yaml, set_permissions from users.models import Token diff --git a/startup_scripts/020_custom_fields.py b/startup_scripts/020_custom_fields.py index da93a92..7479a5e 100644 --- a/startup_scripts/020_custom_fields.py +++ b/startup_scripts/020_custom_fields.py @@ -45,12 +45,13 @@ for cf_name, cf_details in customfields.items(): if cf_details.get('choices', False): custom_field.choices = [] - for choice_detail in enumerate(cf_details.get('choices', [])): - if isinstance(choice_detail, str): - custom_field.choices.append(choice_detail) - else: # legacy mode - print(f"⚠️ Please migrate the 'choices' of '{cf_name}' to the new format, as 'weight' is no longer supported!") + for choice_detail in cf_details.get('choices', []): + if isinstance(choice_detail, dict) and 'value' in choice_detail: + # legacy mode + print(f"⚠️ Please migrate the choice '{choice_detail['value']}' of '{cf_name}' to the new format, as 'weight' is no longer supported!") custom_field.choices.append(choice_detail['value']) + else: + custom_field.choices.append(choice_detail) custom_field.save()