Skip to content

Commit

Permalink
Update Custom Fields Initializer for Netbox 2.7
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cimnine committed Jan 20, 2020
1 parent 355f9d4 commit c001626
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
23 changes: 19 additions & 4 deletions initializers/custom_fields.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## Possible Choices:
## type:
## - text
## - integer
## - boolean
## - date
## - url
## - select
## filter_logic:
## - disabled
## - loose
## - exact
##
## Examples:

# text_field:
# type: text
# label: Custom Text
Expand All @@ -22,8 +37,8 @@
# weight: 10
# on_objects:
# - tenancy.models.Tenant
# selection_field:
# type: selection
# select_field:
# type: select
# label: Choose between items
# required: false
# filter_logic: exact
Expand All @@ -41,8 +56,8 @@
# weight: 50
# - value: Fourth Item
# weight: 40
# selection_field_auto_weight:
# type: selection
# select_field_auto_weight:
# type: select
# label: Choose between items
# required: false
# filter_logic: loose
Expand Down
18 changes: 1 addition & 17 deletions startup_scripts/020_custom_fields.py
Original file line number Diff line number Diff line change
@@ -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 ruamel.yaml import YAML
from pathlib import Path
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):
import importlib
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -42,12 +32,6 @@ def get_class_for_class_path(class_path):
if cf_details.get('description', 0):
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):
custom_field.label = cf_details['label']

Expand All @@ -58,7 +42,7 @@ def get_class_for_class_path(class_path):
custom_field.required = cf_details['required']

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):
custom_field.weight = cf_details['weight']
Expand Down

0 comments on commit c001626

Please sign in to comment.