Update Custom Fields Initializer for Netbox 2.7

The custom field database model has changed in Netbox 2.7.
Therefore the initializer code, that was used before, broke.
As a user, you will need to update your custom_fields.yml file as
follows:

- type must be lowercase
  - the `selection` type was changed to `select`
- the filter_logic must be lower case

This is to achieve compatibility with the naming schema that
Netbox uses internally. It allows us to become forward compatible
in case Netbox ever introduces a new type for custom fields.

See the diff of this commit for further information how this is meant.
This commit is contained in:
Christian Mäder 2020-01-17 15:22:16 +01:00
parent 355f9d4cf7
commit c001626b85
2 changed files with 20 additions and 21 deletions

View File

@ -1,3 +1,18 @@
## Possible Choices:
## type:
## - text
## - integer
## - boolean
## - date
## - url
## - select
## filter_logic:
## - disabled
## - loose
## - exact
##
## Examples:
# text_field: # text_field:
# type: text # type: text
# label: Custom Text # label: Custom Text
@ -22,8 +37,8 @@
# weight: 10 # weight: 10
# on_objects: # on_objects:
# - tenancy.models.Tenant # - tenancy.models.Tenant
# selection_field: # select_field:
# type: selection # type: select
# label: Choose between items # label: Choose between items
# required: false # required: false
# filter_logic: exact # filter_logic: exact
@ -41,8 +56,8 @@
# weight: 50 # weight: 50
# - value: Fourth Item # - value: Fourth Item
# weight: 40 # weight: 40
# selection_field_auto_weight: # select_field_auto_weight:
# type: selection # type: select
# label: Choose between items # label: Choose between items
# required: false # required: false
# filter_logic: loose # filter_logic: loose

View File

@ -1,19 +1,9 @@
from extras.constants import CF_TYPE_TEXT, CF_TYPE_INTEGER, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_URL, CF_TYPE_SELECT, CF_FILTER_CHOICES
from extras.models import CustomField, CustomFieldChoice from extras.models import CustomField, CustomFieldChoice
from ruamel.yaml import YAML from ruamel.yaml import YAML
from pathlib import Path from pathlib import Path
import sys import sys
text_to_fields = {
'boolean': CF_TYPE_BOOLEAN,
'date': CF_TYPE_DATE,
'integer': CF_TYPE_INTEGER,
'selection': CF_TYPE_SELECT,
'text': CF_TYPE_TEXT,
'url': CF_TYPE_URL,
}
def get_class_for_class_path(class_path): def get_class_for_class_path(class_path):
import importlib import importlib
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -42,12 +32,6 @@ with file.open('r') as stream:
if cf_details.get('description', 0): if cf_details.get('description', 0):
custom_field.description = cf_details['description'] custom_field.description = cf_details['description']
# If no filter_logic is specified then it will default to 'Loose'
if cf_details.get('filter_logic', 0):
for choice_id, choice_text in CF_FILTER_CHOICES:
if choice_text.lower() == cf_details['filter_logic']:
custom_field.filter_logic = choice_id
if cf_details.get('label', 0): if cf_details.get('label', 0):
custom_field.label = cf_details['label'] custom_field.label = cf_details['label']
@ -58,7 +42,7 @@ with file.open('r') as stream:
custom_field.required = cf_details['required'] custom_field.required = cf_details['required']
if cf_details.get('type', 0): if cf_details.get('type', 0):
custom_field.type = text_to_fields[cf_details['type']] custom_field.type = cf_details['type']
if cf_details.get('weight', 0): if cf_details.get('weight', 0):
custom_field.weight = cf_details['weight'] custom_field.weight = cf_details['weight']