Misc cleanup

This commit is contained in:
Jeremy Stretch 2018-07-30 14:00:37 -04:00
parent f664998d9b
commit f7aa259995
7 changed files with 36 additions and 12 deletions

View File

@ -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,
}

View File

@ -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):

View File

@ -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):

View File

@ -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

View File

@ -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)

View File

@ -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(

View File

@ -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