2018-02-16 10:25:26 +01:00
|
|
|
from extras.constants import CF_TYPE_TEXT, CF_TYPE_INTEGER, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_URL, CF_TYPE_SELECT
|
|
|
|
from extras.models import CustomField, CustomFieldChoice
|
|
|
|
|
|
|
|
from ruamel.yaml import YAML
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
module_name, class_name = class_path.rsplit(".", 1)
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
clazz = getattr(module, class_name)
|
|
|
|
return ContentType.objects.get_for_model(clazz)
|
|
|
|
|
|
|
|
with open('/opt/netbox/initializers/custom_fields.yml', 'r') as stream:
|
|
|
|
yaml = YAML(typ='safe')
|
|
|
|
customfields = yaml.load(stream)
|
|
|
|
|
|
|
|
if customfields is not None:
|
|
|
|
for cf_name, cf_details in customfields.items():
|
|
|
|
custom_field, created = CustomField.objects.get_or_create(name = cf_name)
|
|
|
|
|
|
|
|
if created:
|
|
|
|
if cf_details.get('default', 0):
|
|
|
|
custom_field.default = cf_details['default']
|
|
|
|
|
|
|
|
if cf_details.get('description', 0):
|
|
|
|
custom_field.description = cf_details['description']
|
|
|
|
|
2018-04-18 19:04:29 +02:00
|
|
|
# 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
|
2018-02-16 10:25:26 +01:00
|
|
|
|
|
|
|
if cf_details.get('label', 0):
|
|
|
|
custom_field.label = cf_details['label']
|
|
|
|
|
|
|
|
for object_type in cf_details.get('on_objects', []):
|
|
|
|
custom_field.obj_type.add(get_class_for_class_path(object_type))
|
|
|
|
|
|
|
|
if cf_details.get('required', 0):
|
|
|
|
custom_field.required = cf_details['required']
|
|
|
|
|
|
|
|
if cf_details.get('type', 0):
|
|
|
|
custom_field.type = text_to_fields[cf_details['type']]
|
|
|
|
|
|
|
|
if cf_details.get('weight', 0):
|
|
|
|
custom_field.weight = cf_details['weight']
|
|
|
|
|
|
|
|
custom_field.save()
|
|
|
|
|
2018-04-18 18:51:04 +02:00
|
|
|
for idx, choice_details in enumerate(cf_details.get('choices', [])):
|
2018-05-18 21:24:31 +02:00
|
|
|
choice, _ = CustomFieldChoice.objects.get_or_create(
|
2018-02-16 10:25:26 +01:00
|
|
|
field=custom_field,
|
2018-04-18 18:51:04 +02:00
|
|
|
value=choice_details['value'],
|
2018-05-18 21:33:50 +02:00
|
|
|
defaults={'weight': idx * 10}
|
2018-04-18 18:51:04 +02:00
|
|
|
)
|
2018-02-16 10:25:26 +01:00
|
|
|
|
|
|
|
print("🔧 Created custom field", cf_name)
|