mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-17 17:56:29 +00:00
* Create RenderMixin, and unify template_code rendering and exporting * Join migrations * Add DEFAULT_MIME_TE constant * Move RenderMixin to extras.models.mixins, Rename RenderMixin to RenderTemplateMixin * Add render_jinja2 to __all__ * Rename ConfigTemplateFilterForm rendering FieldSet * ConfigTemplate lint * Simplify ExportTemplate get_context * Fix table order, and add fields for translations * Update Serializers * Update forms, tables, graphQL, API * Add extra tests for ConfigTemplate and ExportTemplate * Documentation update * Fix typo * Misc cleanup * Clean up template layouts --------- Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
105 lines
3.0 KiB
Python
105 lines
3.0 KiB
Python
import importlib
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
from django.db import models
|
|
from taggit.managers import _TaggableManager
|
|
|
|
from netbox.context import current_request
|
|
from .validators import CustomValidator
|
|
|
|
__all__ = (
|
|
'image_upload',
|
|
'is_report',
|
|
'is_script',
|
|
'is_taggable',
|
|
'run_validators',
|
|
)
|
|
|
|
|
|
def filename_from_model(model: models.Model) -> str:
|
|
"""Standardises how we generate filenames from model class for exports"""
|
|
base = model._meta.verbose_name_plural.lower().replace(' ', '_')
|
|
return f'netbox_{base}'
|
|
|
|
|
|
def filename_from_object(context: dict) -> str:
|
|
"""Standardises how we generate filenames from model class for exports"""
|
|
if 'device' in context:
|
|
base = f"{context['device'].name or 'config'}"
|
|
elif 'virtualmachine' in context:
|
|
base = f"{context['virtualmachine'].name or 'config'}"
|
|
else:
|
|
base = 'config'
|
|
return base
|
|
|
|
|
|
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
|
|
|
|
|
|
def image_upload(instance, filename):
|
|
"""
|
|
Return a path for uploading image attachments.
|
|
"""
|
|
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', 'webp']:
|
|
filename = '.'.join([instance.name, extension])
|
|
elif instance.name:
|
|
filename = instance.name
|
|
|
|
return '{}{}_{}_{}'.format(path, instance.object_type.name, instance.object_id, filename)
|
|
|
|
|
|
def is_script(obj):
|
|
"""
|
|
Returns True if the object is a Script or Report.
|
|
"""
|
|
from .reports import Report
|
|
from .scripts import Script
|
|
try:
|
|
return (issubclass(obj, Report) and obj != Report) or (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
|
|
|
|
|
|
def run_validators(instance, validators):
|
|
"""
|
|
Run the provided iterable of CustomValidators for the instance.
|
|
"""
|
|
request = current_request.get()
|
|
for validator in validators:
|
|
|
|
# Loading a validator class by dotted path
|
|
if type(validator) is str:
|
|
module, cls = validator.rsplit('.', 1)
|
|
validator = getattr(importlib.import_module(module), cls)()
|
|
|
|
# Constructing a new instance on the fly from a ruleset
|
|
elif type(validator) is dict:
|
|
validator = CustomValidator(validator)
|
|
|
|
elif not issubclass(validator.__class__, CustomValidator):
|
|
raise ImproperlyConfigured(f"Invalid value for custom validator: {validator}")
|
|
|
|
validator(instance, request)
|