You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

widgets.py 17KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. """
  2. Form Widget classes specific to the Django admin site.
  3. """
  4. import copy
  5. import json
  6. from django import forms
  7. from django.conf import settings
  8. from django.core.exceptions import ValidationError
  9. from django.core.validators import URLValidator
  10. from django.db.models.deletion import CASCADE
  11. from django.urls import reverse
  12. from django.urls.exceptions import NoReverseMatch
  13. from django.utils.html import smart_urlquote
  14. from django.utils.safestring import mark_safe
  15. from django.utils.text import Truncator
  16. from django.utils.translation import get_language, gettext as _
  17. class FilteredSelectMultiple(forms.SelectMultiple):
  18. """
  19. A SelectMultiple with a JavaScript filter interface.
  20. Note that the resulting JavaScript assumes that the jsi18n
  21. catalog has been loaded in the page
  22. """
  23. @property
  24. def media(self):
  25. extra = '' if settings.DEBUG else '.min'
  26. js = [
  27. 'vendor/jquery/jquery%s.js' % extra,
  28. 'jquery.init.js',
  29. 'core.js',
  30. 'SelectBox.js',
  31. 'SelectFilter2.js',
  32. ]
  33. return forms.Media(js=["admin/js/%s" % path for path in js])
  34. def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
  35. self.verbose_name = verbose_name
  36. self.is_stacked = is_stacked
  37. super().__init__(attrs, choices)
  38. def get_context(self, name, value, attrs):
  39. context = super().get_context(name, value, attrs)
  40. context['widget']['attrs']['class'] = 'selectfilter'
  41. if self.is_stacked:
  42. context['widget']['attrs']['class'] += 'stacked'
  43. context['widget']['attrs']['data-field-name'] = self.verbose_name
  44. context['widget']['attrs']['data-is-stacked'] = int(self.is_stacked)
  45. return context
  46. class AdminDateWidget(forms.DateInput):
  47. class Media:
  48. js = [
  49. 'admin/js/calendar.js',
  50. 'admin/js/admin/DateTimeShortcuts.js',
  51. ]
  52. def __init__(self, attrs=None, format=None):
  53. attrs = {'class': 'vDateField', 'size': '10', **(attrs or {})}
  54. super().__init__(attrs=attrs, format=format)
  55. class AdminTimeWidget(forms.TimeInput):
  56. class Media:
  57. js = [
  58. 'admin/js/calendar.js',
  59. 'admin/js/admin/DateTimeShortcuts.js',
  60. ]
  61. def __init__(self, attrs=None, format=None):
  62. attrs = {'class': 'vTimeField', 'size': '8', **(attrs or {})}
  63. super().__init__(attrs=attrs, format=format)
  64. class AdminSplitDateTime(forms.SplitDateTimeWidget):
  65. """
  66. A SplitDateTime Widget that has some admin-specific styling.
  67. """
  68. template_name = 'admin/widgets/split_datetime.html'
  69. def __init__(self, attrs=None):
  70. widgets = [AdminDateWidget, AdminTimeWidget]
  71. # Note that we're calling MultiWidget, not SplitDateTimeWidget, because
  72. # we want to define widgets.
  73. forms.MultiWidget.__init__(self, widgets, attrs)
  74. def get_context(self, name, value, attrs):
  75. context = super().get_context(name, value, attrs)
  76. context['date_label'] = _('Date:')
  77. context['time_label'] = _('Time:')
  78. return context
  79. class AdminRadioSelect(forms.RadioSelect):
  80. template_name = 'admin/widgets/radio.html'
  81. class AdminFileWidget(forms.ClearableFileInput):
  82. template_name = 'admin/widgets/clearable_file_input.html'
  83. def url_params_from_lookup_dict(lookups):
  84. """
  85. Convert the type of lookups specified in a ForeignKey limit_choices_to
  86. attribute to a dictionary of query parameters
  87. """
  88. params = {}
  89. if lookups and hasattr(lookups, 'items'):
  90. for k, v in lookups.items():
  91. if callable(v):
  92. v = v()
  93. if isinstance(v, (tuple, list)):
  94. v = ','.join(str(x) for x in v)
  95. elif isinstance(v, bool):
  96. v = ('0', '1')[v]
  97. else:
  98. v = str(v)
  99. params[k] = v
  100. return params
  101. class ForeignKeyRawIdWidget(forms.TextInput):
  102. """
  103. A Widget for displaying ForeignKeys in the "raw_id" interface rather than
  104. in a <select> box.
  105. """
  106. template_name = 'admin/widgets/foreign_key_raw_id.html'
  107. def __init__(self, rel, admin_site, attrs=None, using=None):
  108. self.rel = rel
  109. self.admin_site = admin_site
  110. self.db = using
  111. super().__init__(attrs)
  112. def get_context(self, name, value, attrs):
  113. context = super().get_context(name, value, attrs)
  114. rel_to = self.rel.model
  115. if rel_to in self.admin_site._registry:
  116. # The related object is registered with the same AdminSite
  117. related_url = reverse(
  118. 'admin:%s_%s_changelist' % (
  119. rel_to._meta.app_label,
  120. rel_to._meta.model_name,
  121. ),
  122. current_app=self.admin_site.name,
  123. )
  124. params = self.url_parameters()
  125. if params:
  126. related_url += '?' + '&amp;'.join('%s=%s' % (k, v) for k, v in params.items())
  127. context['related_url'] = mark_safe(related_url)
  128. context['link_title'] = _('Lookup')
  129. # The JavaScript code looks for this class.
  130. context['widget']['attrs'].setdefault('class', 'vForeignKeyRawIdAdminField')
  131. else:
  132. context['related_url'] = None
  133. if context['widget']['value']:
  134. context['link_label'], context['link_url'] = self.label_and_url_for_value(value)
  135. else:
  136. context['link_label'] = None
  137. return context
  138. def base_url_parameters(self):
  139. limit_choices_to = self.rel.limit_choices_to
  140. if callable(limit_choices_to):
  141. limit_choices_to = limit_choices_to()
  142. return url_params_from_lookup_dict(limit_choices_to)
  143. def url_parameters(self):
  144. from django.contrib.admin.views.main import TO_FIELD_VAR
  145. params = self.base_url_parameters()
  146. params.update({TO_FIELD_VAR: self.rel.get_related_field().name})
  147. return params
  148. def label_and_url_for_value(self, value):
  149. key = self.rel.get_related_field().name
  150. try:
  151. obj = self.rel.model._default_manager.using(self.db).get(**{key: value})
  152. except (ValueError, self.rel.model.DoesNotExist, ValidationError):
  153. return '', ''
  154. try:
  155. url = reverse(
  156. '%s:%s_%s_change' % (
  157. self.admin_site.name,
  158. obj._meta.app_label,
  159. obj._meta.object_name.lower(),
  160. ),
  161. args=(obj.pk,)
  162. )
  163. except NoReverseMatch:
  164. url = '' # Admin not registered for target model.
  165. return Truncator(obj).words(14), url
  166. class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
  167. """
  168. A Widget for displaying ManyToMany ids in the "raw_id" interface rather than
  169. in a <select multiple> box.
  170. """
  171. template_name = 'admin/widgets/many_to_many_raw_id.html'
  172. def get_context(self, name, value, attrs):
  173. context = super().get_context(name, value, attrs)
  174. if self.rel.model in self.admin_site._registry:
  175. # The related object is registered with the same AdminSite
  176. context['widget']['attrs']['class'] = 'vManyToManyRawIdAdminField'
  177. return context
  178. def url_parameters(self):
  179. return self.base_url_parameters()
  180. def label_and_url_for_value(self, value):
  181. return '', ''
  182. def value_from_datadict(self, data, files, name):
  183. value = data.get(name)
  184. if value:
  185. return value.split(',')
  186. def format_value(self, value):
  187. return ','.join(str(v) for v in value) if value else ''
  188. class RelatedFieldWidgetWrapper(forms.Widget):
  189. """
  190. This class is a wrapper to a given widget to add the add icon for the
  191. admin interface.
  192. """
  193. template_name = 'admin/widgets/related_widget_wrapper.html'
  194. def __init__(self, widget, rel, admin_site, can_add_related=None,
  195. can_change_related=False, can_delete_related=False,
  196. can_view_related=False):
  197. self.needs_multipart_form = widget.needs_multipart_form
  198. self.attrs = widget.attrs
  199. self.choices = widget.choices
  200. self.widget = widget
  201. self.rel = rel
  202. # Backwards compatible check for whether a user can add related
  203. # objects.
  204. if can_add_related is None:
  205. can_add_related = rel.model in admin_site._registry
  206. self.can_add_related = can_add_related
  207. # XXX: The UX does not support multiple selected values.
  208. multiple = getattr(widget, 'allow_multiple_selected', False)
  209. self.can_change_related = not multiple and can_change_related
  210. # XXX: The deletion UX can be confusing when dealing with cascading deletion.
  211. cascade = getattr(rel, 'on_delete', None) is CASCADE
  212. self.can_delete_related = not multiple and not cascade and can_delete_related
  213. self.can_view_related = not multiple and can_view_related
  214. # so we can check if the related object is registered with this AdminSite
  215. self.admin_site = admin_site
  216. def __deepcopy__(self, memo):
  217. obj = copy.copy(self)
  218. obj.widget = copy.deepcopy(self.widget, memo)
  219. obj.attrs = self.widget.attrs
  220. memo[id(self)] = obj
  221. return obj
  222. @property
  223. def is_hidden(self):
  224. return self.widget.is_hidden
  225. @property
  226. def media(self):
  227. return self.widget.media
  228. def get_related_url(self, info, action, *args):
  229. return reverse("admin:%s_%s_%s" % (info + (action,)),
  230. current_app=self.admin_site.name, args=args)
  231. def get_context(self, name, value, attrs):
  232. from django.contrib.admin.views.main import IS_POPUP_VAR, TO_FIELD_VAR
  233. rel_opts = self.rel.model._meta
  234. info = (rel_opts.app_label, rel_opts.model_name)
  235. self.widget.choices = self.choices
  236. url_params = '&'.join("%s=%s" % param for param in [
  237. (TO_FIELD_VAR, self.rel.get_related_field().name),
  238. (IS_POPUP_VAR, 1),
  239. ])
  240. context = {
  241. 'rendered_widget': self.widget.render(name, value, attrs),
  242. 'is_hidden': self.is_hidden,
  243. 'name': name,
  244. 'url_params': url_params,
  245. 'model': rel_opts.verbose_name,
  246. 'can_add_related': self.can_add_related,
  247. 'can_change_related': self.can_change_related,
  248. 'can_delete_related': self.can_delete_related,
  249. 'can_view_related': self.can_view_related,
  250. }
  251. if self.can_add_related:
  252. context['add_related_url'] = self.get_related_url(info, 'add')
  253. if self.can_delete_related:
  254. context['delete_related_template_url'] = self.get_related_url(info, 'delete', '__fk__')
  255. if self.can_view_related or self.can_change_related:
  256. context['change_related_template_url'] = self.get_related_url(info, 'change', '__fk__')
  257. return context
  258. def value_from_datadict(self, data, files, name):
  259. return self.widget.value_from_datadict(data, files, name)
  260. def value_omitted_from_data(self, data, files, name):
  261. return self.widget.value_omitted_from_data(data, files, name)
  262. def id_for_label(self, id_):
  263. return self.widget.id_for_label(id_)
  264. class AdminTextareaWidget(forms.Textarea):
  265. def __init__(self, attrs=None):
  266. super().__init__(attrs={'class': 'vLargeTextField', **(attrs or {})})
  267. class AdminTextInputWidget(forms.TextInput):
  268. def __init__(self, attrs=None):
  269. super().__init__(attrs={'class': 'vTextField', **(attrs or {})})
  270. class AdminEmailInputWidget(forms.EmailInput):
  271. def __init__(self, attrs=None):
  272. super().__init__(attrs={'class': 'vTextField', **(attrs or {})})
  273. class AdminURLFieldWidget(forms.URLInput):
  274. template_name = 'admin/widgets/url.html'
  275. def __init__(self, attrs=None, validator_class=URLValidator):
  276. super().__init__(attrs={'class': 'vURLField', **(attrs or {})})
  277. self.validator = validator_class()
  278. def get_context(self, name, value, attrs):
  279. try:
  280. self.validator(value if value else '')
  281. url_valid = True
  282. except ValidationError:
  283. url_valid = False
  284. context = super().get_context(name, value, attrs)
  285. context['current_label'] = _('Currently:')
  286. context['change_label'] = _('Change:')
  287. context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
  288. context['url_valid'] = url_valid
  289. return context
  290. class AdminIntegerFieldWidget(forms.NumberInput):
  291. class_name = 'vIntegerField'
  292. def __init__(self, attrs=None):
  293. super().__init__(attrs={'class': self.class_name, **(attrs or {})})
  294. class AdminBigIntegerFieldWidget(AdminIntegerFieldWidget):
  295. class_name = 'vBigIntegerField'
  296. class AdminUUIDInputWidget(forms.TextInput):
  297. def __init__(self, attrs=None):
  298. super().__init__(attrs={'class': 'vUUIDField', **(attrs or {})})
  299. # Mapping of lowercase language codes [returned by Django's get_language()] to
  300. # language codes supported by select2.
  301. # See django/contrib/admin/static/admin/js/vendor/select2/i18n/*
  302. SELECT2_TRANSLATIONS = {x.lower(): x for x in [
  303. 'ar', 'az', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'et',
  304. 'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hi', 'hr', 'hu', 'id', 'is',
  305. 'it', 'ja', 'km', 'ko', 'lt', 'lv', 'mk', 'ms', 'nb', 'nl', 'pl',
  306. 'pt-BR', 'pt', 'ro', 'ru', 'sk', 'sr-Cyrl', 'sr', 'sv', 'th',
  307. 'tr', 'uk', 'vi',
  308. ]}
  309. SELECT2_TRANSLATIONS.update({'zh-hans': 'zh-CN', 'zh-hant': 'zh-TW'})
  310. class AutocompleteMixin:
  311. """
  312. Select widget mixin that loads options from AutocompleteJsonView via AJAX.
  313. Renders the necessary data attributes for select2 and adds the static form
  314. media.
  315. """
  316. url_name = '%s:%s_%s_autocomplete'
  317. def __init__(self, rel, admin_site, attrs=None, choices=(), using=None):
  318. self.rel = rel
  319. self.admin_site = admin_site
  320. self.db = using
  321. self.choices = choices
  322. self.attrs = {} if attrs is None else attrs.copy()
  323. def get_url(self):
  324. model = self.rel.model
  325. return reverse(self.url_name % (self.admin_site.name, model._meta.app_label, model._meta.model_name))
  326. def build_attrs(self, base_attrs, extra_attrs=None):
  327. """
  328. Set select2's AJAX attributes.
  329. Attributes can be set using the html5 data attribute.
  330. Nested attributes require a double dash as per
  331. https://select2.org/configuration/data-attributes#nested-subkey-options
  332. """
  333. attrs = super().build_attrs(base_attrs, extra_attrs=extra_attrs)
  334. attrs.setdefault('class', '')
  335. attrs.update({
  336. 'data-ajax--cache': 'true',
  337. 'data-ajax--type': 'GET',
  338. 'data-ajax--url': self.get_url(),
  339. 'data-theme': 'admin-autocomplete',
  340. 'data-allow-clear': json.dumps(not self.is_required),
  341. 'data-placeholder': '', # Allows clearing of the input.
  342. 'class': attrs['class'] + (' ' if attrs['class'] else '') + 'admin-autocomplete',
  343. })
  344. return attrs
  345. def optgroups(self, name, value, attr=None):
  346. """Return selected options based on the ModelChoiceIterator."""
  347. default = (None, [], 0)
  348. groups = [default]
  349. has_selected = False
  350. selected_choices = {
  351. str(v) for v in value
  352. if str(v) not in self.choices.field.empty_values
  353. }
  354. if not self.is_required and not self.allow_multiple_selected:
  355. default[1].append(self.create_option(name, '', '', False, 0))
  356. choices = (
  357. (obj.pk, self.choices.field.label_from_instance(obj))
  358. for obj in self.choices.queryset.using(self.db).filter(pk__in=selected_choices)
  359. )
  360. for option_value, option_label in choices:
  361. selected = (
  362. str(option_value) in value and
  363. (has_selected is False or self.allow_multiple_selected)
  364. )
  365. has_selected |= selected
  366. index = len(default[1])
  367. subgroup = default[1]
  368. subgroup.append(self.create_option(name, option_value, option_label, selected_choices, index))
  369. return groups
  370. @property
  371. def media(self):
  372. extra = '' if settings.DEBUG else '.min'
  373. i18n_name = SELECT2_TRANSLATIONS.get(get_language())
  374. i18n_file = ('admin/js/vendor/select2/i18n/%s.js' % i18n_name,) if i18n_name else ()
  375. return forms.Media(
  376. js=(
  377. 'admin/js/vendor/jquery/jquery%s.js' % extra,
  378. 'admin/js/vendor/select2/select2.full%s.js' % extra,
  379. ) + i18n_file + (
  380. 'admin/js/jquery.init.js',
  381. 'admin/js/autocomplete.js',
  382. ),
  383. css={
  384. 'screen': (
  385. 'admin/css/vendor/select2/select2%s.css' % extra,
  386. 'admin/css/autocomplete.css',
  387. ),
  388. },
  389. )
  390. class AutocompleteSelect(AutocompleteMixin, forms.Select):
  391. pass
  392. class AutocompleteSelectMultiple(AutocompleteMixin, forms.SelectMultiple):
  393. pass