mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 09:28:38 -06:00
Misc cleanup
This commit is contained in:
parent
f664998d9b
commit
f7aa259995
@ -4,6 +4,9 @@ from django.conf import settings as django_settings
|
|||||||
|
|
||||||
|
|
||||||
def settings(request):
|
def settings(request):
|
||||||
|
"""
|
||||||
|
Expose Django settings in the template context. Example: {{ settings.DEBUG }}
|
||||||
|
"""
|
||||||
return {
|
return {
|
||||||
'settings': django_settings,
|
'settings': django_settings,
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,12 @@ from django.db import models
|
|||||||
|
|
||||||
from .forms import ColorSelect
|
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):
|
class NullableCharField(models.CharField):
|
||||||
@ -21,7 +26,7 @@ class NullableCharField(models.CharField):
|
|||||||
|
|
||||||
|
|
||||||
class ColorField(models.CharField):
|
class ColorField(models.CharField):
|
||||||
default_validators = [validate_color]
|
default_validators = [ColorValidator]
|
||||||
description = "A hexadecimal RGB color code"
|
description = "A hexadecimal RGB color code"
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -19,6 +19,9 @@ class NumericInFilter(django_filters.BaseInFilter, django_filters.NumberFilter):
|
|||||||
|
|
||||||
|
|
||||||
class NullableCharFieldFilter(django_filters.CharFilter):
|
class NullableCharFieldFilter(django_filters.CharFilter):
|
||||||
|
"""
|
||||||
|
Allow matching on null field values by passing a special string used to signify NULL.
|
||||||
|
"""
|
||||||
null_value = 'NULL'
|
null_value = 'NULL'
|
||||||
|
|
||||||
def filter(self, qs, value):
|
def filter(self, qs, value):
|
||||||
|
@ -154,6 +154,9 @@ def add_blank_choice(choices):
|
|||||||
#
|
#
|
||||||
|
|
||||||
class SmallTextarea(forms.Textarea):
|
class SmallTextarea(forms.Textarea):
|
||||||
|
"""
|
||||||
|
Subclass used for rendering a smaller textarea element.
|
||||||
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@ -169,6 +172,9 @@ class ColorSelect(forms.Select):
|
|||||||
|
|
||||||
|
|
||||||
class BulkEditNullBooleanSelect(forms.NullBooleanSelect):
|
class BulkEditNullBooleanSelect(forms.NullBooleanSelect):
|
||||||
|
"""
|
||||||
|
A Select widget for NullBooleanFields
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(BulkEditNullBooleanSelect, self).__init__(*args, **kwargs)
|
super(BulkEditNullBooleanSelect, self).__init__(*args, **kwargs)
|
||||||
@ -448,7 +454,9 @@ class ChainedModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
|||||||
|
|
||||||
|
|
||||||
class SlugField(forms.SlugField):
|
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):
|
def __init__(self, slug_source='name', *args, **kwargs):
|
||||||
label = kwargs.pop('label', "Slug")
|
label = kwargs.pop('label', "Slug")
|
||||||
help_text = kwargs.pop('help_text', "URL-friendly unique shorthand")
|
help_text = kwargs.pop('help_text', "URL-friendly unique shorthand")
|
||||||
@ -558,11 +566,15 @@ class JSONField(_JSONField):
|
|||||||
#
|
#
|
||||||
|
|
||||||
class BootstrapMixin(forms.BaseForm):
|
class BootstrapMixin(forms.BaseForm):
|
||||||
|
"""
|
||||||
|
Add the base Bootstrap CSS classes to form elements.
|
||||||
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(BootstrapMixin, self).__init__(*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():
|
for field_name, field in self.fields.items():
|
||||||
if field.widget.__class__ not in exempt_widgets:
|
if field.widget.__class__ not in exempt_widgets:
|
||||||
@ -632,14 +644,15 @@ class ComponentForm(BootstrapMixin, forms.Form):
|
|||||||
|
|
||||||
|
|
||||||
class BulkEditForm(forms.Form):
|
class BulkEditForm(forms.Form):
|
||||||
|
"""
|
||||||
|
Base form for editing multiple objects in bulk
|
||||||
|
"""
|
||||||
def __init__(self, model, parent_obj=None, *args, **kwargs):
|
def __init__(self, model, parent_obj=None, *args, **kwargs):
|
||||||
super(BulkEditForm, self).__init__(*args, **kwargs)
|
super(BulkEditForm, self).__init__(*args, **kwargs)
|
||||||
self.model = model
|
self.model = model
|
||||||
self.parent_obj = parent_obj
|
self.parent_obj = parent_obj
|
||||||
|
self.nullable_fields = []
|
||||||
|
|
||||||
# Copy any nullable fields defined in Meta
|
# Copy any nullable fields defined in Meta
|
||||||
if hasattr(self.Meta, 'nullable_fields'):
|
if hasattr(self.Meta, 'nullable_fields'):
|
||||||
self.nullable_fields = [field for field in self.Meta.nullable_fields]
|
self.nullable_fields = self.Meta.nullable_fields
|
||||||
else:
|
|
||||||
self.nullable_fields = []
|
|
||||||
|
@ -22,7 +22,9 @@ class BaseTable(tables.Table):
|
|||||||
|
|
||||||
|
|
||||||
class ToggleColumn(tables.CheckBoxColumn):
|
class ToggleColumn(tables.CheckBoxColumn):
|
||||||
|
"""
|
||||||
|
Extend CheckBoxColumn to add a "toggle all" checkbox in the column header.
|
||||||
|
"""
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
default = kwargs.pop('default', '')
|
default = kwargs.pop('default', '')
|
||||||
visible = kwargs.pop('visible', False)
|
visible = kwargs.pop('visible', False)
|
||||||
|
@ -5,7 +5,6 @@ class HttpStatusMixin(object):
|
|||||||
"""
|
"""
|
||||||
Custom mixin to provide more detail in the event of an unexpected HTTP response.
|
Custom mixin to provide more detail in the event of an unexpected HTTP response.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def assertHttpStatus(self, response, expected_status):
|
def assertHttpStatus(self, response, expected_status):
|
||||||
err_message = "Expected HTTP status {}; received {}: {}"
|
err_message = "Expected HTTP status {}; received {}: {}"
|
||||||
self.assertEqual(response.status_code, expected_status, err_message.format(
|
self.assertEqual(response.status_code, expected_status, err_message.format(
|
||||||
|
@ -9,7 +9,6 @@ class EnhancedURLValidator(URLValidator):
|
|||||||
"""
|
"""
|
||||||
Extends Django's built-in URLValidator to permit the use of hostnames with no domain extension.
|
Extends Django's built-in URLValidator to permit the use of hostnames with no domain extension.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class AnyURLScheme(object):
|
class AnyURLScheme(object):
|
||||||
"""
|
"""
|
||||||
A fake URL list which "contains" all scheme names abiding by the syntax defined in RFC 3986 section 3.1
|
A fake URL list which "contains" all scheme names abiding by the syntax defined in RFC 3986 section 3.1
|
||||||
|
Loading…
Reference in New Issue
Block a user