Skip to content

Commit 618feff

Browse files
MajesticFalconcimnine
authored andcommitted
add error handling for webhook and custom links. fix initializer comments
1 parent a3cf645 commit 618feff

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

initializers/custom_links.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
##
1010
## Examples:
1111

12-
# - name: link_to_repo
13-
# text: 'Link to docker repository'
14-
# url: 'https://github.com/netbox-community/netbox-docker'
15-
# new_window: False
16-
# content_type: device
17-
# - name: link_to_localhost
18-
# text: 'Link to the users localhost'
19-
# url: 'http://localhost'
20-
# new_window: True
21-
# content_type: device
12+
# - name: link_to_repo
13+
# text: 'Link to docker repository'
14+
# url: 'https://github.com/netbox-community/netbox-docker'
15+
# new_window: False
16+
# content_type: device
17+
# - name: link_to_localhost
18+
# text: 'Link to the users localhost'
19+
# url: 'http://localhost'
20+
# new_window: True
21+
# content_type: device

startup_scripts/280_custom_links.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@
1010
sys.exit()
1111

1212
def get_content_type_id(content_type_str):
13-
for type in ContentType.objects.all():
14-
if type.name == content_type_str:
15-
return type.id
13+
try:
14+
id = ContentType.objects.get(model=content_type_str).id
15+
return id
16+
except ContentType.DoesNotExist:
17+
print(" Error determining content type id for user declared var: {0}".format(content_type_str))
1618

1719
for link in custom_links:
1820
content_type = link.pop('content_type')
1921
link['content_type_id'] = get_content_type_id(content_type)
20-
if link['content_type_id'] is None:
21-
print("⚠️ Error determining content type id for user declared var: {0}".format(content_type))
22-
else:
23-
CustomLink(**link).save()
22+
if link['content_type_id'] is not None:
23+
custom_link = CustomLink(**link)
24+
if not CustomLink.objects.filter(name=custom_link.name):
25+
custom_link.save()
26+
print(" Created Custom Link {0}".format(custom_link.name))
27+
else:
28+
print("⚠️ Skipping Custom Link {0}, already exists".format(custom_link.name))
2429

startup_scripts/290_webhooks.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,22 @@
1010
sys.exit()
1111

1212
def get_content_type_id(content_type_str):
13-
return ContentType.objects.get(model=content_type_str).id
13+
try:
14+
id = ContentType.objects.get(model=content_type_str).id
15+
return id
16+
except ContentType.DoesNotExist:
17+
print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str))
1418

1519
for hook in webhooks:
1620
obj_types = hook.pop('object_types')
1721
obj_type_ids = []
1822
for obj in obj_types:
1923
obj_type_ids.append(get_content_type_id(obj))
20-
if obj_type_ids is None:
21-
print("⚠️ Error determining content type id for user declared var: {0}".format(obj_type))
22-
else:
24+
if obj_type_ids is not None:
2325
webhook = Webhook(**hook)
2426
if not Webhook.objects.filter(name=webhook.name):
2527
webhook.save()
2628
webhook.content_types.set(obj_type_ids)
27-
print(" Created Webhook {0}".format(webhook.name))
29+
print("🖥️ Created Webhook {0}".format(webhook.name))
2830
else:
29-
print(" Skipping Webhook {0}, already exists".format(webhook.name))
31+
print("⚠️ Skipping Webhook {0}, already exists".format(webhook.name))

0 commit comments

Comments
 (0)