2008-07-19 07:54:34 +08:00
|
|
|
"""
|
|
|
|
Form Widget classes specific to the Django admin site.
|
|
|
|
"""
|
2011-03-28 09:40:43 +08:00
|
|
|
import copy
|
2017-05-10 20:48:57 +08:00
|
|
|
import json
|
2012-06-08 00:08:47 +08:00
|
|
|
|
2008-07-19 09:22:26 +08:00
|
|
|
from django import forms
|
2017-07-20 23:06:30 +08:00
|
|
|
from django.conf import settings
|
2018-02-01 02:43:05 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
2013-11-18 06:26:20 +08:00
|
|
|
from django.db.models.deletion import CASCADE
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2016-01-28 08:43:04 +08:00
|
|
|
from django.urls.exceptions import NoReverseMatch
|
2016-12-28 06:00:56 +08:00
|
|
|
from django.utils.html import smart_urlquote
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2011-07-14 21:47:10 +08:00
|
|
|
from django.utils.text import Truncator
|
2017-05-10 20:48:57 +08:00
|
|
|
from django.utils.translation import get_language, gettext as _
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class FilteredSelectMultiple(forms.SelectMultiple):
|
|
|
|
"""
|
|
|
|
A SelectMultiple with a JavaScript filter interface.
|
|
|
|
|
2008-09-01 01:20:43 +08:00
|
|
|
Note that the resulting JavaScript assumes that the jsi18n
|
|
|
|
catalog has been loaded in the page
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2011-08-11 22:07:39 +08:00
|
|
|
@property
|
|
|
|
def media(self):
|
2017-07-20 23:06:30 +08:00
|
|
|
extra = '' if settings.DEBUG else '.min'
|
|
|
|
js = [
|
|
|
|
'vendor/jquery/jquery%s.js' % extra,
|
|
|
|
'jquery.init.js',
|
|
|
|
'core.js',
|
|
|
|
'SelectBox.js',
|
|
|
|
'SelectFilter2.js',
|
|
|
|
]
|
2015-11-07 19:24:38 +08:00
|
|
|
return forms.Media(js=["admin/js/%s" % path for path in js])
|
2008-09-01 01:20:43 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
|
|
|
|
self.verbose_name = verbose_name
|
|
|
|
self.is_stacked = is_stacked
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(attrs, choices)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2017-03-21 22:15:34 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context(name, value, attrs)
|
2016-12-28 06:00:56 +08:00
|
|
|
context['widget']['attrs']['class'] = 'selectfilter'
|
2011-08-11 22:07:39 +08:00
|
|
|
if self.is_stacked:
|
2016-12-28 06:00:56 +08:00
|
|
|
context['widget']['attrs']['class'] += 'stacked'
|
|
|
|
context['widget']['attrs']['data-field-name'] = self.verbose_name
|
|
|
|
context['widget']['attrs']['data-is-stacked'] = int(self.is_stacked)
|
|
|
|
return context
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
class AdminDateWidget(forms.DateInput):
|
2018-06-25 18:24:40 +08:00
|
|
|
class Media:
|
2017-07-20 23:06:30 +08:00
|
|
|
js = [
|
2018-06-25 18:24:40 +08:00
|
|
|
'admin/js/calendar.js',
|
|
|
|
'admin/js/admin/DateTimeShortcuts.js',
|
2017-07-20 23:06:30 +08:00
|
|
|
]
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2011-11-20 19:12:37 +08:00
|
|
|
def __init__(self, attrs=None, format=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
attrs = {'class': 'vDateField', 'size': '10', **(attrs or {})}
|
|
|
|
super().__init__(attrs=attrs, format=format)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
class AdminTimeWidget(forms.TimeInput):
|
2018-06-25 18:24:40 +08:00
|
|
|
class Media:
|
2017-07-20 23:06:30 +08:00
|
|
|
js = [
|
2018-06-25 18:24:40 +08:00
|
|
|
'admin/js/calendar.js',
|
|
|
|
'admin/js/admin/DateTimeShortcuts.js',
|
2017-07-20 23:06:30 +08:00
|
|
|
]
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2011-11-20 19:12:37 +08:00
|
|
|
def __init__(self, attrs=None, format=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
attrs = {'class': 'vTimeField', 'size': '8', **(attrs or {})}
|
|
|
|
super().__init__(attrs=attrs, format=format)
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AdminSplitDateTime(forms.SplitDateTimeWidget):
|
|
|
|
"""
|
|
|
|
A SplitDateTime Widget that has some admin-specific styling.
|
|
|
|
"""
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/split_datetime.html'
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, attrs=None):
|
|
|
|
widgets = [AdminDateWidget, AdminTimeWidget]
|
|
|
|
# Note that we're calling MultiWidget, not SplitDateTimeWidget, because
|
|
|
|
# we want to define widgets.
|
|
|
|
forms.MultiWidget.__init__(self, widgets, attrs)
|
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context(name, value, attrs)
|
2016-12-28 06:00:56 +08:00
|
|
|
context['date_label'] = _('Date:')
|
|
|
|
context['time_label'] = _('Time:')
|
|
|
|
return context
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AdminRadioSelect(forms.RadioSelect):
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/radio.html'
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2010-10-01 10:02:58 +08:00
|
|
|
class AdminFileWidget(forms.ClearableFileInput):
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/clearable_file_input.html'
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2011-01-28 22:08:25 +08:00
|
|
|
def url_params_from_lookup_dict(lookups):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Convert the type of lookups specified in a ForeignKey limit_choices_to
|
2011-01-28 22:08:25 +08:00
|
|
|
attribute to a dictionary of query parameters
|
|
|
|
"""
|
|
|
|
params = {}
|
|
|
|
if lookups and hasattr(lookups, 'items'):
|
|
|
|
for k, v in lookups.items():
|
2013-08-07 00:38:31 +08:00
|
|
|
if callable(v):
|
|
|
|
v = v()
|
2011-04-22 20:03:50 +08:00
|
|
|
if isinstance(v, (tuple, list)):
|
2013-08-30 07:20:00 +08:00
|
|
|
v = ','.join(str(x) for x in v)
|
2011-01-28 22:08:42 +08:00
|
|
|
elif isinstance(v, bool):
|
|
|
|
v = ('0', '1')[v]
|
2011-01-28 22:08:25 +08:00
|
|
|
else:
|
2016-12-29 23:27:49 +08:00
|
|
|
v = str(v)
|
2017-09-05 21:47:11 +08:00
|
|
|
params[k] = v
|
2011-01-28 22:08:25 +08:00
|
|
|
return params
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class ForeignKeyRawIdWidget(forms.TextInput):
|
|
|
|
"""
|
|
|
|
A Widget for displaying ForeignKeys in the "raw_id" interface rather than
|
|
|
|
in a <select> box.
|
|
|
|
"""
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/foreign_key_raw_id.html'
|
|
|
|
|
2011-08-02 07:38:11 +08:00
|
|
|
def __init__(self, rel, admin_site, attrs=None, using=None):
|
2008-07-19 07:54:34 +08:00
|
|
|
self.rel = rel
|
2011-08-02 07:38:11 +08:00
|
|
|
self.admin_site = admin_site
|
2009-12-22 23:18:51 +08:00
|
|
|
self.db = using
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(attrs)
|
2008-07-19 07:54:34 +08:00
|
|
|
|
2017-03-21 22:15:34 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context(name, value, attrs)
|
2015-02-26 22:19:17 +08:00
|
|
|
rel_to = self.rel.model
|
2011-08-02 07:38:11 +08:00
|
|
|
if rel_to in self.admin_site._registry:
|
|
|
|
# The related object is registered with the same AdminSite
|
2013-12-13 04:23:24 +08:00
|
|
|
related_url = reverse(
|
|
|
|
'admin:%s_%s_changelist' % (
|
|
|
|
rel_to._meta.app_label,
|
|
|
|
rel_to._meta.model_name,
|
|
|
|
),
|
|
|
|
current_app=self.admin_site.name,
|
|
|
|
)
|
2011-08-02 07:38:11 +08:00
|
|
|
|
|
|
|
params = self.url_parameters()
|
|
|
|
if params:
|
2017-11-18 04:38:29 +08:00
|
|
|
related_url += '?' + '&'.join('%s=%s' % (k, v) for k, v in params.items())
|
2016-12-28 06:00:56 +08:00
|
|
|
context['related_url'] = mark_safe(related_url)
|
|
|
|
context['link_title'] = _('Lookup')
|
|
|
|
# The JavaScript code looks for this class.
|
|
|
|
context['widget']['attrs'].setdefault('class', 'vForeignKeyRawIdAdminField')
|
2017-08-25 17:47:59 +08:00
|
|
|
else:
|
|
|
|
context['related_url'] = None
|
2016-12-28 06:00:56 +08:00
|
|
|
if context['widget']['value']:
|
|
|
|
context['link_label'], context['link_url'] = self.label_and_url_for_value(value)
|
2017-08-25 17:47:59 +08:00
|
|
|
else:
|
|
|
|
context['link_label'] = None
|
2016-12-28 06:00:56 +08:00
|
|
|
return context
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2008-09-02 14:10:14 +08:00
|
|
|
def base_url_parameters(self):
|
2014-02-02 03:23:31 +08:00
|
|
|
limit_choices_to = self.rel.limit_choices_to
|
|
|
|
if callable(limit_choices_to):
|
|
|
|
limit_choices_to = limit_choices_to()
|
|
|
|
return url_params_from_lookup_dict(limit_choices_to)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2008-09-02 14:10:14 +08:00
|
|
|
def url_parameters(self):
|
|
|
|
from django.contrib.admin.views.main import TO_FIELD_VAR
|
|
|
|
params = self.base_url_parameters()
|
|
|
|
params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
|
|
|
|
return params
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
def label_and_url_for_value(self, value):
|
2008-09-02 06:43:38 +08:00
|
|
|
key = self.rel.get_related_field().name
|
2010-03-02 10:28:49 +08:00
|
|
|
try:
|
2015-02-26 22:19:17 +08:00
|
|
|
obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
|
2018-02-01 02:43:05 +08:00
|
|
|
except (ValueError, self.rel.model.DoesNotExist, ValidationError):
|
2016-12-28 06:00:56 +08:00
|
|
|
return '', ''
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2016-01-28 08:43:04 +08:00
|
|
|
try:
|
2016-12-28 06:00:56 +08:00
|
|
|
url = reverse(
|
2016-01-28 08:43:04 +08:00
|
|
|
'%s:%s_%s_change' % (
|
|
|
|
self.admin_site.name,
|
|
|
|
obj._meta.app_label,
|
|
|
|
obj._meta.object_name.lower(),
|
|
|
|
),
|
|
|
|
args=(obj.pk,)
|
|
|
|
)
|
|
|
|
except NoReverseMatch:
|
2016-12-28 06:00:56 +08:00
|
|
|
url = '' # Admin not registered for target model.
|
2016-01-28 08:43:04 +08:00
|
|
|
|
2018-08-21 21:28:51 +08:00
|
|
|
return Truncator(obj).words(14), url
|
2016-01-28 08:43:04 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
|
|
|
"""
|
|
|
|
A Widget for displaying ManyToMany ids in the "raw_id" interface rather than
|
|
|
|
in a <select multiple> box.
|
|
|
|
"""
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/many_to_many_raw_id.html'
|
|
|
|
|
2017-03-21 22:15:34 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context(name, value, attrs)
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.rel.model in self.admin_site._registry:
|
2011-08-02 07:38:11 +08:00
|
|
|
# The related object is registered with the same AdminSite
|
2016-12-28 06:00:56 +08:00
|
|
|
context['widget']['attrs']['class'] = 'vManyToManyRawIdAdminField'
|
|
|
|
return context
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2008-09-02 14:10:14 +08:00
|
|
|
def url_parameters(self):
|
|
|
|
return self.base_url_parameters()
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
def label_and_url_for_value(self, value):
|
|
|
|
return '', ''
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
2010-10-19 15:14:38 +08:00
|
|
|
value = data.get(name)
|
2008-07-19 07:54:34 +08:00
|
|
|
if value:
|
2010-10-19 15:14:38 +08:00
|
|
|
return value.split(',')
|
2008-09-02 06:43:38 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
def format_value(self, value):
|
2017-04-22 01:52:26 +08:00
|
|
|
return ','.join(str(v) for v in value) if value else ''
|
2016-12-28 06:00:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
class RelatedFieldWidgetWrapper(forms.Widget):
|
|
|
|
"""
|
|
|
|
This class is a wrapper to a given widget to add the add icon for the
|
|
|
|
admin interface.
|
|
|
|
"""
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/related_widget_wrapper.html'
|
2013-11-18 06:26:20 +08:00
|
|
|
|
|
|
|
def __init__(self, widget, rel, admin_site, can_add_related=None,
|
2018-05-02 16:39:12 +08:00
|
|
|
can_change_related=False, can_delete_related=False,
|
|
|
|
can_view_related=False):
|
2008-07-19 07:54:34 +08:00
|
|
|
self.needs_multipart_form = widget.needs_multipart_form
|
|
|
|
self.attrs = widget.attrs
|
|
|
|
self.choices = widget.choices
|
|
|
|
self.widget = widget
|
|
|
|
self.rel = rel
|
2010-09-11 00:56:36 +08:00
|
|
|
# Backwards compatible check for whether a user can add related
|
|
|
|
# objects.
|
|
|
|
if can_add_related is None:
|
2015-02-26 22:19:17 +08:00
|
|
|
can_add_related = rel.model in admin_site._registry
|
2010-09-11 00:56:36 +08:00
|
|
|
self.can_add_related = can_add_related
|
2013-11-18 06:26:20 +08:00
|
|
|
# XXX: The UX does not support multiple selected values.
|
|
|
|
multiple = getattr(widget, 'allow_multiple_selected', False)
|
|
|
|
self.can_change_related = not multiple and can_change_related
|
|
|
|
# XXX: The deletion UX can be confusing when dealing with cascading deletion.
|
|
|
|
cascade = getattr(rel, 'on_delete', None) is CASCADE
|
|
|
|
self.can_delete_related = not multiple and not cascade and can_delete_related
|
2018-05-02 16:39:12 +08:00
|
|
|
self.can_view_related = not multiple and can_view_related
|
2008-07-19 07:54:34 +08:00
|
|
|
# so we can check if the related object is registered with this AdminSite
|
|
|
|
self.admin_site = admin_site
|
|
|
|
|
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
obj = copy.copy(self)
|
|
|
|
obj.widget = copy.deepcopy(self.widget, memo)
|
|
|
|
obj.attrs = self.widget.attrs
|
|
|
|
memo[id(self)] = obj
|
|
|
|
return obj
|
|
|
|
|
2014-02-26 23:03:51 +08:00
|
|
|
@property
|
|
|
|
def is_hidden(self):
|
|
|
|
return self.widget.is_hidden
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
@property
|
|
|
|
def media(self):
|
2015-02-22 20:03:28 +08:00
|
|
|
return self.widget.media
|
2013-11-18 06:26:20 +08:00
|
|
|
|
|
|
|
def get_related_url(self, info, action, *args):
|
|
|
|
return reverse("admin:%s_%s_%s" % (info + (action,)),
|
|
|
|
current_app=self.admin_site.name, args=args)
|
2008-09-01 01:20:43 +08:00
|
|
|
|
2017-03-21 22:15:34 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2013-11-18 06:26:20 +08:00
|
|
|
from django.contrib.admin.views.main import IS_POPUP_VAR, TO_FIELD_VAR
|
2015-02-26 22:19:17 +08:00
|
|
|
rel_opts = self.rel.model._meta
|
2013-11-18 06:26:20 +08:00
|
|
|
info = (rel_opts.app_label, rel_opts.model_name)
|
2017-02-20 21:48:03 +08:00
|
|
|
self.widget.choices = self.choices
|
2013-11-18 06:26:20 +08:00
|
|
|
url_params = '&'.join("%s=%s" % param for param in [
|
|
|
|
(TO_FIELD_VAR, self.rel.get_related_field().name),
|
|
|
|
(IS_POPUP_VAR, 1),
|
|
|
|
])
|
2017-02-20 21:48:03 +08:00
|
|
|
context = {
|
|
|
|
'rendered_widget': self.widget.render(name, value, attrs),
|
2018-11-20 02:11:04 +08:00
|
|
|
'is_hidden': self.is_hidden,
|
2017-02-20 21:48:03 +08:00
|
|
|
'name': name,
|
|
|
|
'url_params': url_params,
|
|
|
|
'model': rel_opts.verbose_name,
|
2018-05-02 16:39:12 +08:00
|
|
|
'can_add_related': self.can_add_related,
|
|
|
|
'can_change_related': self.can_change_related,
|
|
|
|
'can_delete_related': self.can_delete_related,
|
|
|
|
'can_view_related': self.can_view_related,
|
2017-02-20 21:48:03 +08:00
|
|
|
}
|
2010-09-11 00:56:36 +08:00
|
|
|
if self.can_add_related:
|
2018-05-02 16:39:12 +08:00
|
|
|
context['add_related_url'] = self.get_related_url(info, 'add')
|
2013-11-18 06:26:20 +08:00
|
|
|
if self.can_delete_related:
|
2018-05-02 16:39:12 +08:00
|
|
|
context['delete_related_template_url'] = self.get_related_url(info, 'delete', '__fk__')
|
|
|
|
if self.can_view_related or self.can_change_related:
|
|
|
|
context['change_related_template_url'] = self.get_related_url(info, 'change', '__fk__')
|
2016-12-28 06:00:56 +08:00
|
|
|
return context
|
2008-07-19 07:54:34 +08:00
|
|
|
|
|
|
|
def value_from_datadict(self, data, files, name):
|
|
|
|
return self.widget.value_from_datadict(data, files, name)
|
|
|
|
|
2017-03-08 02:56:29 +08:00
|
|
|
def value_omitted_from_data(self, data, files, name):
|
|
|
|
return self.widget.value_omitted_from_data(data, files, name)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def id_for_label(self, id_):
|
|
|
|
return self.widget.id_for_label(id_)
|
2008-08-09 03:54:34 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-08-09 03:54:34 +08:00
|
|
|
class AdminTextareaWidget(forms.Textarea):
|
2008-08-09 05:27:03 +08:00
|
|
|
def __init__(self, attrs=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
super().__init__(attrs={'class': 'vLargeTextField', **(attrs or {})})
|
2008-08-09 03:54:34 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-08-09 03:54:34 +08:00
|
|
|
class AdminTextInputWidget(forms.TextInput):
|
2008-08-09 05:27:03 +08:00
|
|
|
def __init__(self, attrs=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
super().__init__(attrs={'class': 'vTextField', **(attrs or {})})
|
2008-08-09 03:54:34 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2013-07-26 19:22:30 +08:00
|
|
|
class AdminEmailInputWidget(forms.EmailInput):
|
|
|
|
def __init__(self, attrs=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
super().__init__(attrs={'class': 'vTextField', **(attrs or {})})
|
2013-07-26 19:22:30 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2013-07-26 19:22:30 +08:00
|
|
|
class AdminURLFieldWidget(forms.URLInput):
|
2016-12-28 06:00:56 +08:00
|
|
|
template_name = 'admin/widgets/url.html'
|
|
|
|
|
2008-08-09 05:27:03 +08:00
|
|
|
def __init__(self, attrs=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
super().__init__(attrs={'class': 'vURLField', **(attrs or {})})
|
2008-08-09 03:54:34 +08:00
|
|
|
|
2016-12-28 06:00:56 +08:00
|
|
|
def get_context(self, name, value, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
context = super().get_context(name, value, attrs)
|
2016-12-28 06:00:56 +08:00
|
|
|
context['current_label'] = _('Currently:')
|
|
|
|
context['change_label'] = _('Change:')
|
2017-03-09 22:42:47 +08:00
|
|
|
context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
|
2016-12-28 06:00:56 +08:00
|
|
|
return context
|
|
|
|
|
2012-07-07 20:24:50 +08:00
|
|
|
|
2016-09-08 23:58:42 +08:00
|
|
|
class AdminIntegerFieldWidget(forms.NumberInput):
|
2012-06-05 19:28:32 +08:00
|
|
|
class_name = 'vIntegerField'
|
|
|
|
|
2008-08-09 05:27:03 +08:00
|
|
|
def __init__(self, attrs=None):
|
2017-12-11 20:08:45 +08:00
|
|
|
super().__init__(attrs={'class': self.class_name, **(attrs or {})})
|
2008-08-29 04:58:10 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2012-06-05 19:28:32 +08:00
|
|
|
class AdminBigIntegerFieldWidget(AdminIntegerFieldWidget):
|
|
|
|
class_name = 'vBigIntegerField'
|
2017-05-10 20:48:57 +08:00
|
|
|
|
|
|
|
|
2018-08-18 22:16:22 +08:00
|
|
|
class AdminUUIDInputWidget(forms.TextInput):
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super().__init__(attrs={'class': 'vUUIDField', **(attrs or {})})
|
|
|
|
|
|
|
|
|
2018-09-25 22:30:18 +08:00
|
|
|
# Mapping of lowercase language codes [returned by Django's get_language()] to
|
|
|
|
# language codes supported by select2.
|
2017-05-10 20:48:57 +08:00
|
|
|
# See django/contrib/admin/static/admin/js/vendor/select2/i18n/*
|
|
|
|
SELECT2_TRANSLATIONS = {x.lower(): x for x in [
|
|
|
|
'ar', 'az', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et',
|
|
|
|
'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hi', 'hr', 'hu', 'id', 'is',
|
|
|
|
'it', 'ja', 'km', 'ko', 'lt', 'lv', 'mk', 'ms', 'nb', 'nl', 'pl',
|
|
|
|
'pt-BR', 'pt', 'ro', 'ru', 'sk', 'sr-Cyrl', 'sr', 'sv', 'th',
|
2018-03-13 00:50:43 +08:00
|
|
|
'tr', 'uk', 'vi',
|
2017-05-10 20:48:57 +08:00
|
|
|
]}
|
2018-03-13 00:50:43 +08:00
|
|
|
SELECT2_TRANSLATIONS.update({'zh-hans': 'zh-CN', 'zh-hant': 'zh-TW'})
|
2017-05-10 20:48:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AutocompleteMixin:
|
|
|
|
"""
|
|
|
|
Select widget mixin that loads options from AutocompleteJsonView via AJAX.
|
|
|
|
|
|
|
|
Renders the necessary data attributes for select2 and adds the static form
|
|
|
|
media.
|
|
|
|
"""
|
2017-12-02 03:00:00 +08:00
|
|
|
url_name = '%s:%s_%s_autocomplete'
|
2017-05-10 20:48:57 +08:00
|
|
|
|
2017-12-02 03:00:00 +08:00
|
|
|
def __init__(self, rel, admin_site, attrs=None, choices=(), using=None):
|
2017-05-10 20:48:57 +08:00
|
|
|
self.rel = rel
|
2017-12-02 03:00:00 +08:00
|
|
|
self.admin_site = admin_site
|
2017-05-10 20:48:57 +08:00
|
|
|
self.db = using
|
|
|
|
self.choices = choices
|
2018-03-04 02:35:09 +08:00
|
|
|
self.attrs = {} if attrs is None else attrs.copy()
|
2017-05-10 20:48:57 +08:00
|
|
|
|
|
|
|
def get_url(self):
|
|
|
|
model = self.rel.model
|
2017-12-02 03:00:00 +08:00
|
|
|
return reverse(self.url_name % (self.admin_site.name, model._meta.app_label, model._meta.model_name))
|
2017-05-10 20:48:57 +08:00
|
|
|
|
|
|
|
def build_attrs(self, base_attrs, extra_attrs=None):
|
|
|
|
"""
|
|
|
|
Set select2's AJAX attributes.
|
|
|
|
|
|
|
|
Attributes can be set using the html5 data attribute.
|
|
|
|
Nested attributes require a double dash as per
|
|
|
|
https://select2.org/configuration/data-attributes#nested-subkey-options
|
|
|
|
"""
|
|
|
|
attrs = super().build_attrs(base_attrs, extra_attrs=extra_attrs)
|
|
|
|
attrs.setdefault('class', '')
|
|
|
|
attrs.update({
|
|
|
|
'data-ajax--cache': 'true',
|
|
|
|
'data-ajax--type': 'GET',
|
|
|
|
'data-ajax--url': self.get_url(),
|
|
|
|
'data-theme': 'admin-autocomplete',
|
|
|
|
'data-allow-clear': json.dumps(not self.is_required),
|
|
|
|
'data-placeholder': '', # Allows clearing of the input.
|
2018-03-15 22:55:34 +08:00
|
|
|
'class': attrs['class'] + (' ' if attrs['class'] else '') + 'admin-autocomplete',
|
2017-05-10 20:48:57 +08:00
|
|
|
})
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
def optgroups(self, name, value, attr=None):
|
|
|
|
"""Return selected options based on the ModelChoiceIterator."""
|
|
|
|
default = (None, [], 0)
|
|
|
|
groups = [default]
|
|
|
|
has_selected = False
|
|
|
|
selected_choices = {
|
|
|
|
str(v) for v in value
|
|
|
|
if str(v) not in self.choices.field.empty_values
|
|
|
|
}
|
|
|
|
if not self.is_required and not self.allow_multiple_selected:
|
|
|
|
default[1].append(self.create_option(name, '', '', False, 0))
|
|
|
|
choices = (
|
|
|
|
(obj.pk, self.choices.field.label_from_instance(obj))
|
|
|
|
for obj in self.choices.queryset.using(self.db).filter(pk__in=selected_choices)
|
|
|
|
)
|
|
|
|
for option_value, option_label in choices:
|
|
|
|
selected = (
|
|
|
|
str(option_value) in value and
|
|
|
|
(has_selected is False or self.allow_multiple_selected)
|
|
|
|
)
|
2018-01-12 22:05:16 +08:00
|
|
|
has_selected |= selected
|
2017-05-10 20:48:57 +08:00
|
|
|
index = len(default[1])
|
|
|
|
subgroup = default[1]
|
|
|
|
subgroup.append(self.create_option(name, option_value, option_label, selected_choices, index))
|
|
|
|
return groups
|
|
|
|
|
|
|
|
@property
|
|
|
|
def media(self):
|
|
|
|
extra = '' if settings.DEBUG else '.min'
|
|
|
|
i18n_name = SELECT2_TRANSLATIONS.get(get_language())
|
|
|
|
i18n_file = ('admin/js/vendor/select2/i18n/%s.js' % i18n_name,) if i18n_name else ()
|
|
|
|
return forms.Media(
|
|
|
|
js=(
|
|
|
|
'admin/js/vendor/jquery/jquery%s.js' % extra,
|
|
|
|
'admin/js/vendor/select2/select2.full%s.js' % extra,
|
|
|
|
) + i18n_file + (
|
|
|
|
'admin/js/jquery.init.js',
|
|
|
|
'admin/js/autocomplete.js',
|
|
|
|
),
|
|
|
|
css={
|
|
|
|
'screen': (
|
|
|
|
'admin/css/vendor/select2/select2%s.css' % extra,
|
|
|
|
'admin/css/autocomplete.css',
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class AutocompleteSelect(AutocompleteMixin, forms.Select):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class AutocompleteSelectMultiple(AutocompleteMixin, forms.SelectMultiple):
|
|
|
|
pass
|