2020-10-17 14:51:38 -05:00
|
|
|
import sys
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
from extras.models import CustomField
|
2020-02-05 08:31:01 -06:00
|
|
|
from startup_script_utils import load_yaml
|
2018-02-16 03:25:26 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
customfields = load_yaml('/opt/netbox/initializers/custom_fields.yml')
|
2018-12-19 07:25:58 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
if customfields is None:
|
|
|
|
sys.exit()
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for cf_name, cf_details in customfields.items():
|
|
|
|
custom_field, created = CustomField.objects.get_or_create(name = cf_name)
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
if created:
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('default', False):
|
2020-02-05 08:31:01 -06:00
|
|
|
custom_field.default = cf_details['default']
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('description', False):
|
2020-02-05 08:31:01 -06:00
|
|
|
custom_field.description = cf_details['description']
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('label', False):
|
2020-02-05 08:31:01 -06:00
|
|
|
custom_field.label = cf_details['label']
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for object_type in cf_details.get('on_objects', []):
|
2020-10-17 14:51:38 -05:00
|
|
|
custom_field.content_types.add(get_class_for_class_path(object_type))
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('required', False):
|
2020-02-05 08:31:01 -06:00
|
|
|
custom_field.required = cf_details['required']
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('type', False):
|
2020-02-05 08:31:01 -06:00
|
|
|
custom_field.type = cf_details['type']
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('weight', -1) >= 0:
|
2020-02-05 08:31:01 -06:00
|
|
|
custom_field.weight = cf_details['weight']
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-10-17 14:51:38 -05:00
|
|
|
if cf_details.get('choices', False):
|
|
|
|
custom_field.choices = []
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-12-14 15:09:08 -06:00
|
|
|
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!")
|
2020-10-17 14:51:38 -05:00
|
|
|
custom_field.choices.append(choice_detail['value'])
|
2020-12-14 15:09:08 -06:00
|
|
|
else:
|
|
|
|
custom_field.choices.append(choice_detail)
|
2020-10-17 14:51:38 -05:00
|
|
|
|
|
|
|
custom_field.save()
|
2018-02-16 03:25:26 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
print("🔧 Created custom field", cf_name)
|