mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Merge branch 'develop' into develop-2.3
This commit is contained in:
@@ -42,7 +42,7 @@ class NullableModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
||||
"""
|
||||
iterator = forms.models.ModelChoiceIterator
|
||||
|
||||
def __init__(self, null_value=0, null_label='None', *args, **kwargs):
|
||||
def __init__(self, null_value=0, null_label='-- None --', *args, **kwargs):
|
||||
self.null_value = null_value
|
||||
self.null_label = null_label
|
||||
super(NullableModelMultipleChoiceField, self).__init__(*args, **kwargs)
|
||||
|
||||
@@ -407,11 +407,25 @@ class SlugField(forms.SlugField):
|
||||
self.widget.attrs['slug-source'] = slug_source
|
||||
|
||||
|
||||
class FilterChoiceFieldMixin(object):
|
||||
iterator = forms.models.ModelChoiceIterator
|
||||
class FilterChoiceIterator(forms.models.ModelChoiceIterator):
|
||||
|
||||
def __init__(self, null_option=None, *args, **kwargs):
|
||||
self.null_option = null_option
|
||||
def __iter__(self):
|
||||
# Filter on "empty" choice using FILTERS_NULL_CHOICE_VALUE (instead of an empty string)
|
||||
if self.field.null_label is not None:
|
||||
yield (settings.FILTERS_NULL_CHOICE_VALUE, self.field.null_label)
|
||||
queryset = self.queryset.all()
|
||||
# Can't use iterator() when queryset uses prefetch_related()
|
||||
if not queryset._prefetch_related_lookups:
|
||||
queryset = queryset.iterator()
|
||||
for obj in queryset:
|
||||
yield self.choice(obj)
|
||||
|
||||
|
||||
class FilterChoiceFieldMixin(object):
|
||||
iterator = FilterChoiceIterator
|
||||
|
||||
def __init__(self, null_label=None, *args, **kwargs):
|
||||
self.null_label = null_label
|
||||
if 'required' not in kwargs:
|
||||
kwargs['required'] = False
|
||||
if 'widget' not in kwargs:
|
||||
@@ -424,15 +438,6 @@ class FilterChoiceFieldMixin(object):
|
||||
return '{} ({})'.format(label, obj.filter_count)
|
||||
return label
|
||||
|
||||
def _get_choices(self):
|
||||
if hasattr(self, '_choices'):
|
||||
return self._choices
|
||||
if self.null_option is not None:
|
||||
return itertools.chain([self.null_option], self.iterator(self))
|
||||
return self.iterator(self)
|
||||
|
||||
choices = property(_get_choices, forms.ChoiceField._set_choices)
|
||||
|
||||
|
||||
class FilterChoiceField(FilterChoiceFieldMixin, forms.ModelMultipleChoiceField):
|
||||
pass
|
||||
|
||||
@@ -4,7 +4,7 @@ import sys
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import ProgrammingError
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse
|
||||
|
||||
@@ -61,6 +61,10 @@ class ExceptionHandlingMiddleware(object):
|
||||
if settings.DEBUG:
|
||||
return
|
||||
|
||||
# Ignore Http404s (defer to Django's built-in 404 handling)
|
||||
if isinstance(exception, Http404):
|
||||
return
|
||||
|
||||
# Determine the type of exception
|
||||
if isinstance(exception, ProgrammingError):
|
||||
template_name = 'exceptions/programming_error.html'
|
||||
|
||||
@@ -309,8 +309,14 @@ class BulkCreateView(View):
|
||||
|
||||
def get(self, request):
|
||||
|
||||
# Set initial values for visible form fields from query args
|
||||
initial = {}
|
||||
for field in getattr(self.model_form._meta, 'fields', []):
|
||||
if request.GET.get(field):
|
||||
initial[field] = request.GET[field]
|
||||
|
||||
form = self.form()
|
||||
model_form = self.model_form()
|
||||
model_form = self.model_form(initial=initial)
|
||||
|
||||
return render(request, self.template_name, {
|
||||
'obj_type': self.model_form._meta.model._meta.verbose_name,
|
||||
|
||||
Reference in New Issue
Block a user