diff --git a/netbox/utilities/context_processors.py b/netbox/utilities/context_processors.py index 58c8641ec..dab35e982 100644 --- a/netbox/utilities/context_processors.py +++ b/netbox/utilities/context_processors.py @@ -4,6 +4,9 @@ from django.conf import settings as django_settings def settings(request): + """ + Expose Django settings in the template context. Example: {{ settings.DEBUG }} + """ return { 'settings': django_settings, } diff --git a/netbox/utilities/fields.py b/netbox/utilities/fields.py index d0a0c2180..34f59fe16 100644 --- a/netbox/utilities/fields.py +++ b/netbox/utilities/fields.py @@ -5,7 +5,12 @@ from django.db import models from .forms import ColorSelect -validate_color = RegexValidator('^[0-9a-f]{6}$', 'Enter a valid hexadecimal RGB color code.', 'invalid') + +ColorValidator = RegexValidator( + regex='^[0-9a-f]{6}$', + message='Enter a valid hexadecimal RGB color code.', + code='invalid' +) class NullableCharField(models.CharField): @@ -21,7 +26,7 @@ class NullableCharField(models.CharField): class ColorField(models.CharField): - default_validators = [validate_color] + default_validators = [ColorValidator] description = "A hexadecimal RGB color code" def __init__(self, *args, **kwargs): diff --git a/netbox/utilities/filters.py b/netbox/utilities/filters.py index 3e403e676..90cdcd9fc 100644 --- a/netbox/utilities/filters.py +++ b/netbox/utilities/filters.py @@ -19,6 +19,9 @@ class NumericInFilter(django_filters.BaseInFilter, django_filters.NumberFilter): class NullableCharFieldFilter(django_filters.CharFilter): + """ + Allow matching on null field values by passing a special string used to signify NULL. + """ null_value = 'NULL' def filter(self, qs, value): diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 9a4284054..1e6e3c0c4 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -154,6 +154,9 @@ def add_blank_choice(choices): # class SmallTextarea(forms.Textarea): + """ + Subclass used for rendering a smaller textarea element. + """ pass @@ -169,6 +172,9 @@ class ColorSelect(forms.Select): class BulkEditNullBooleanSelect(forms.NullBooleanSelect): + """ + A Select widget for NullBooleanFields + """ def __init__(self, *args, **kwargs): super(BulkEditNullBooleanSelect, self).__init__(*args, **kwargs) @@ -448,7 +454,9 @@ class ChainedModelMultipleChoiceField(forms.ModelMultipleChoiceField): class SlugField(forms.SlugField): - + """ + Extend the built-in SlugField to automatically populate from a field called `name` unless otherwise specified. + """ def __init__(self, slug_source='name', *args, **kwargs): label = kwargs.pop('label', "Slug") help_text = kwargs.pop('help_text', "URL-friendly unique shorthand") @@ -558,11 +566,15 @@ class JSONField(_JSONField): # class BootstrapMixin(forms.BaseForm): - + """ + Add the base Bootstrap CSS classes to form elements. + """ def __init__(self, *args, **kwargs): super(BootstrapMixin, self).__init__(*args, **kwargs) - exempt_widgets = [forms.CheckboxInput, forms.ClearableFileInput, forms.FileInput, forms.RadioSelect] + exempt_widgets = [ + forms.CheckboxInput, forms.ClearableFileInput, forms.FileInput, forms.RadioSelect + ] for field_name, field in self.fields.items(): if field.widget.__class__ not in exempt_widgets: @@ -632,14 +644,15 @@ class ComponentForm(BootstrapMixin, forms.Form): class BulkEditForm(forms.Form): - + """ + Base form for editing multiple objects in bulk + """ def __init__(self, model, parent_obj=None, *args, **kwargs): super(BulkEditForm, self).__init__(*args, **kwargs) self.model = model self.parent_obj = parent_obj + self.nullable_fields = [] # Copy any nullable fields defined in Meta if hasattr(self.Meta, 'nullable_fields'): - self.nullable_fields = [field for field in self.Meta.nullable_fields] - else: - self.nullable_fields = [] + self.nullable_fields = self.Meta.nullable_fields diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index a9f1044d6..e531b5e32 100644 --- a/netbox/utilities/tables.py +++ b/netbox/utilities/tables.py @@ -22,7 +22,9 @@ class BaseTable(tables.Table): class ToggleColumn(tables.CheckBoxColumn): - + """ + Extend CheckBoxColumn to add a "toggle all" checkbox in the column header. + """ def __init__(self, *args, **kwargs): default = kwargs.pop('default', '') visible = kwargs.pop('visible', False) diff --git a/netbox/utilities/testing.py b/netbox/utilities/testing.py index d40202842..fa38166e0 100644 --- a/netbox/utilities/testing.py +++ b/netbox/utilities/testing.py @@ -5,7 +5,6 @@ class HttpStatusMixin(object): """ Custom mixin to provide more detail in the event of an unexpected HTTP response. """ - def assertHttpStatus(self, response, expected_status): err_message = "Expected HTTP status {}; received {}: {}" self.assertEqual(response.status_code, expected_status, err_message.format( diff --git a/netbox/utilities/validators.py b/netbox/utilities/validators.py index dcdb9bc6d..102e368a5 100644 --- a/netbox/utilities/validators.py +++ b/netbox/utilities/validators.py @@ -9,7 +9,6 @@ class EnhancedURLValidator(URLValidator): """ Extends Django's built-in URLValidator to permit the use of hostnames with no domain extension. """ - class AnyURLScheme(object): """ A fake URL list which "contains" all scheme names abiding by the syntax defined in RFC 3986 section 3.1