2021-02-08 04:59:33 -06:00
|
|
|
import sys
|
|
|
|
|
2020-12-18 23:22:41 -06:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from extras.models import CustomLink
|
2022-04-05 01:34:08 -05:00
|
|
|
from startup_script_utils import load_yaml, split_params
|
2020-12-18 23:22:41 -06:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
custom_links = load_yaml("/opt/netbox/initializers/custom_links.yml")
|
2020-12-18 23:22:41 -06:00
|
|
|
|
|
|
|
if custom_links is None:
|
2021-02-08 04:59:33 -06:00
|
|
|
sys.exit()
|
2020-12-18 23:22:41 -06:00
|
|
|
|
2021-02-08 03:35:31 -06:00
|
|
|
|
|
|
|
def get_content_type_id(content_type):
|
2021-02-08 04:59:33 -06:00
|
|
|
try:
|
|
|
|
return ContentType.objects.get(model=content_type).id
|
|
|
|
except ContentType.DoesNotExist:
|
|
|
|
pass
|
2021-02-08 03:35:31 -06:00
|
|
|
|
2020-12-18 23:22:41 -06:00
|
|
|
|
|
|
|
for link in custom_links:
|
2021-02-08 04:59:33 -06:00
|
|
|
content_type = link.pop("content_type")
|
|
|
|
link["content_type_id"] = get_content_type_id(content_type)
|
|
|
|
if link["content_type_id"] is None:
|
|
|
|
print(
|
|
|
|
"⚠️ Unable to create Custom Link '{0}': The content_type '{1}' is unknown".format(
|
2021-04-26 09:26:10 -05:00
|
|
|
link.get("name"), content_type
|
2021-02-08 04:59:33 -06:00
|
|
|
)
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
matching_params, defaults = split_params(link)
|
|
|
|
custom_link, created = CustomLink.objects.get_or_create(**matching_params, defaults=defaults)
|
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if created:
|
|
|
|
print("🔗 Created Custom Link '{0}'".format(custom_link.name))
|