From 187c349fde945499edd1027fc2816984e076f6bb Mon Sep 17 00:00:00 2001 From: Grokzen Date: Wed, 18 Apr 2018 18:51:04 +0200 Subject: [PATCH 1/3] Add support for auto_weight counter on choices when loading selection field from custom_fields.yml This also fixes a bug when using the same value for 2 different custom fields breaks becuase it tries to create a new CustomFieldChoice object instead of reusing the one that already exists. --- initializers/custom_fields.yml | 15 +++++++++++++++ startup_scripts/20_custom_fields.py | 15 ++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/initializers/custom_fields.yml b/initializers/custom_fields.yml index ccde9a0..71c8b85 100644 --- a/initializers/custom_fields.yml +++ b/initializers/custom_fields.yml @@ -40,6 +40,21 @@ # weight: 50 # - value: Fourth Item # weight: 40 +# selection_field_auto_weight: +# type: selection +# label: Choose between items +# required: false +# filterable: true +# weight: 30 +# on_objects: +# - dcim.models.Device +# choices: +# - value: A +# - value: B +# - value: C +# - value: "D like deprecated" +# weight: 999 +# - value: E # boolean_field: # type: boolean # label: Yes Or No? diff --git a/startup_scripts/20_custom_fields.py b/startup_scripts/20_custom_fields.py index 5c40e37..3492baf 100644 --- a/startup_scripts/20_custom_fields.py +++ b/startup_scripts/20_custom_fields.py @@ -56,13 +56,14 @@ with open('/opt/netbox/initializers/custom_fields.yml', 'r') as stream: custom_field.save() - for choice_details in cf_details.get('choices', []): - choice = CustomFieldChoice.objects.create( + for idx, choice_details in enumerate(cf_details.get('choices', [])): + choice, created = CustomFieldChoice.objects.get_or_create( field=custom_field, - value=choice_details['value']) - - if choice_details.get('weight', 0): - choice.weight = choice_details['weight'] - choice.save() + value=choice_details['value'], + ) + # Add weight after initial creation to fix a bug if you use the same 'value' + # for multiple custom fields. + choice.weight = choice_details.get('weight', idx * 10) + choice.save() print("🔧 Created custom field", cf_name) From 170bf4e03df943be00fa5f89c979b683c3676fb6 Mon Sep 17 00:00:00 2001 From: Grokzen Date: Fri, 18 May 2018 21:24:31 +0200 Subject: [PATCH 2/3] Remove unused variable --- startup_scripts/20_custom_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/startup_scripts/20_custom_fields.py b/startup_scripts/20_custom_fields.py index 3492baf..494d425 100644 --- a/startup_scripts/20_custom_fields.py +++ b/startup_scripts/20_custom_fields.py @@ -57,7 +57,7 @@ with open('/opt/netbox/initializers/custom_fields.yml', 'r') as stream: custom_field.save() for idx, choice_details in enumerate(cf_details.get('choices', [])): - choice, created = CustomFieldChoice.objects.get_or_create( + choice, _ = CustomFieldChoice.objects.get_or_create( field=custom_field, value=choice_details['value'], ) From 5a1f6f6dd96dccad1acc8d848c8b5f5bdd844a8a Mon Sep 17 00:00:00 2001 From: Grokzen Date: Fri, 18 May 2018 21:33:50 +0200 Subject: [PATCH 3/3] Fix the default creation value for weight --- startup_scripts/20_custom_fields.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/startup_scripts/20_custom_fields.py b/startup_scripts/20_custom_fields.py index 494d425..3ddf97a 100644 --- a/startup_scripts/20_custom_fields.py +++ b/startup_scripts/20_custom_fields.py @@ -60,10 +60,7 @@ with open('/opt/netbox/initializers/custom_fields.yml', 'r') as stream: choice, _ = CustomFieldChoice.objects.get_or_create( field=custom_field, value=choice_details['value'], + defaults={'weight': idx * 10} ) - # Add weight after initial creation to fix a bug if you use the same 'value' - # for multiple custom fields. - choice.weight = choice_details.get('weight', idx * 10) - choice.save() print("🔧 Created custom field", cf_name)