From 187ae4b2a1a4cc524bd87c95894a7d18966d3d15 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 18 Dec 2020 23:22:41 -0600 Subject: [PATCH] add custom link initializers --- initializers/custom_links.yml | 21 +++++++++++++++++++++ startup_scripts/280_custom_links.py | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 initializers/custom_links.yml create mode 100644 startup_scripts/280_custom_links.py diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml new file mode 100644 index 0000000..a18acb5 --- /dev/null +++ b/initializers/custom_links.yml @@ -0,0 +1,21 @@ +## Possible Choices: +## new_window: +## - True +## - False +## content_type_id: +## - device +## - site +## - any-other-content-type +## +## Examples: + +# - name: link_to_repo + # text: 'Link to docker repository' + # url: 'https://github.com/netbox-community/netbox-docker' + # new_window: False + # content_type: device +# - name: link_to_localhost + # text: 'Link to the users localhost' + # url: 'http://localhost' + # new_window: True + # content_type: device diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py new file mode 100644 index 0000000..6e8bb04 --- /dev/null +++ b/startup_scripts/280_custom_links.py @@ -0,0 +1,24 @@ +from django.contrib.contenttypes.models import ContentType +from extras.models import CustomLink +from startup_script_utils import load_yaml +import sys + + +custom_links = load_yaml('/opt/netbox/initializers/custom_links.yml') + +if custom_links is None: + sys.exit() + +def get_content_type_id(content_type_str): + for type in ContentType.objects.all(): + if type.name == content_type_str: + return type.id + +for link in custom_links: + content_type = link.pop('content_type') + link['content_type_id'] = get_content_type_id(content_type) + if link['content_type_id'] is None: + print("⚠️ Error determining content type id for user declared var: {0}".format(content_type)) + else: + CustomLink(**link).save() +