2019-12-11 15:26:47 -05:00
|
|
|
from taggit.managers import _TaggableManager
|
|
|
|
|
2022-11-02 15:18:07 -04:00
|
|
|
from netbox.registry import registry
|
2020-03-14 03:03:22 -04:00
|
|
|
|
2019-12-11 15:26:47 -05:00
|
|
|
|
|
|
|
def is_taggable(obj):
|
|
|
|
"""
|
|
|
|
Return True if the instance can have Tags assigned to it; False otherwise.
|
|
|
|
"""
|
|
|
|
if hasattr(obj, 'tags'):
|
|
|
|
if issubclass(obj.tags.__class__, _TaggableManager):
|
|
|
|
return True
|
|
|
|
return False
|
2020-03-14 03:03:22 -04:00
|
|
|
|
|
|
|
|
2020-05-07 16:59:27 -04:00
|
|
|
def image_upload(instance, filename):
|
|
|
|
"""
|
2023-02-19 13:58:01 -05:00
|
|
|
Return a path for uploading image attachments.
|
2020-05-07 16:59:27 -04:00
|
|
|
"""
|
|
|
|
path = 'image-attachments/'
|
|
|
|
|
|
|
|
# Rename the file to the provided name, if any. Attempt to preserve the file extension.
|
|
|
|
extension = filename.rsplit('.')[-1].lower()
|
|
|
|
if instance.name and extension in ['bmp', 'gif', 'jpeg', 'jpg', 'png']:
|
|
|
|
filename = '.'.join([instance.name, extension])
|
|
|
|
elif instance.name:
|
|
|
|
filename = instance.name
|
|
|
|
|
|
|
|
return '{}{}_{}_{}'.format(path, instance.content_type.name, instance.object_id, filename)
|
|
|
|
|
|
|
|
|
2022-01-19 14:46:50 -05:00
|
|
|
def register_features(model, features):
|
2023-02-19 13:58:01 -05:00
|
|
|
"""
|
|
|
|
Register model features in the application registry.
|
|
|
|
"""
|
|
|
|
app_label, model_name = model._meta.label_lower.split('.')
|
2022-01-19 14:46:50 -05:00
|
|
|
for feature in features:
|
2023-02-19 13:58:01 -05:00
|
|
|
try:
|
|
|
|
registry['model_features'][feature][app_label].add(model_name)
|
|
|
|
except KeyError:
|
|
|
|
raise KeyError(
|
|
|
|
f"{feature} is not a valid model feature! Valid keys are: {registry['model_features'].keys()}"
|
|
|
|
)
|
2023-03-24 21:00:36 -04:00
|
|
|
|
2023-11-03 10:32:59 -04:00
|
|
|
# Register public models
|
|
|
|
if not getattr(model, '_netbox_private', False):
|
|
|
|
registry['models'][app_label].add(model_name)
|
|
|
|
|
2023-03-24 21:00:36 -04:00
|
|
|
|
|
|
|
def is_script(obj):
|
|
|
|
"""
|
|
|
|
Returns True if the object is a Script.
|
|
|
|
"""
|
|
|
|
from .scripts import Script
|
|
|
|
try:
|
|
|
|
return issubclass(obj, Script) and obj != Script
|
|
|
|
except TypeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def is_report(obj):
|
|
|
|
"""
|
|
|
|
Returns True if the given object is a Report.
|
|
|
|
"""
|
|
|
|
from .reports import Report
|
|
|
|
try:
|
|
|
|
return issubclass(obj, Report) and obj != Report
|
|
|
|
except TypeError:
|
|
|
|
return False
|