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.
 
 
 
 

373 lines
15 KiB

  1. from __future__ import unicode_literals
  2. import warnings
  3. from django import forms
  4. from django.conf import settings
  5. from django.contrib.admin.templatetags.admin_static import static
  6. from django.contrib.admin.utils import (
  7. display_for_field, flatten_fieldsets, help_text_for_field, label_for_field,
  8. lookup_field,
  9. )
  10. from django.core.exceptions import ObjectDoesNotExist
  11. from django.db.models.fields.related import ManyToManyRel
  12. from django.forms.utils import flatatt
  13. from django.template.defaultfilters import capfirst, linebreaksbr
  14. from django.utils import six
  15. from django.utils.deprecation import (
  16. RemovedInDjango20Warning, RemovedInDjango110Warning,
  17. )
  18. from django.utils.encoding import force_text, smart_text
  19. from django.utils.functional import cached_property
  20. from django.utils.html import conditional_escape, format_html
  21. from django.utils.safestring import mark_safe
  22. from django.utils.translation import ugettext_lazy as _
  23. ACTION_CHECKBOX_NAME = '_selected_action'
  24. class ActionForm(forms.Form):
  25. action = forms.ChoiceField(label=_('Action:'))
  26. select_across = forms.BooleanField(label='', required=False, initial=0,
  27. widget=forms.HiddenInput({'class': 'select-across'}))
  28. checkbox = forms.CheckboxInput({'class': 'action-select'}, lambda value: False)
  29. class AdminForm(object):
  30. def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
  31. self.form, self.fieldsets = form, fieldsets
  32. self.prepopulated_fields = [{
  33. 'field': form[field_name],
  34. 'dependencies': [form[f] for f in dependencies]
  35. } for field_name, dependencies in prepopulated_fields.items()]
  36. self.model_admin = model_admin
  37. if readonly_fields is None:
  38. readonly_fields = ()
  39. self.readonly_fields = readonly_fields
  40. def __iter__(self):
  41. for name, options in self.fieldsets:
  42. yield Fieldset(
  43. self.form, name,
  44. readonly_fields=self.readonly_fields,
  45. model_admin=self.model_admin,
  46. **options
  47. )
  48. def _media(self):
  49. media = self.form.media
  50. for fs in self:
  51. media = media + fs.media
  52. return media
  53. media = property(_media)
  54. class Fieldset(object):
  55. def __init__(self, form, name=None, readonly_fields=(), fields=(), classes=(),
  56. description=None, model_admin=None):
  57. self.form = form
  58. self.name, self.fields = name, fields
  59. self.classes = ' '.join(classes)
  60. self.description = description
  61. self.model_admin = model_admin
  62. self.readonly_fields = readonly_fields
  63. def _media(self):
  64. if 'collapse' in self.classes:
  65. extra = '' if settings.DEBUG else '.min'
  66. js = ['vendor/jquery/jquery%s.js' % extra,
  67. 'jquery.init.js',
  68. 'collapse%s.js' % extra]
  69. return forms.Media(js=[static('admin/js/%s' % url) for url in js])
  70. return forms.Media()
  71. media = property(_media)
  72. def __iter__(self):
  73. for field in self.fields:
  74. yield Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin)
  75. class Fieldline(object):
  76. def __init__(self, form, field, readonly_fields=None, model_admin=None):
  77. self.form = form # A django.forms.Form instance
  78. if not hasattr(field, "__iter__") or isinstance(field, six.text_type):
  79. self.fields = [field]
  80. else:
  81. self.fields = field
  82. self.has_visible_field = not all(field in self.form.fields and
  83. self.form.fields[field].widget.is_hidden
  84. for field in self.fields)
  85. self.model_admin = model_admin
  86. if readonly_fields is None:
  87. readonly_fields = ()
  88. self.readonly_fields = readonly_fields
  89. def __iter__(self):
  90. for i, field in enumerate(self.fields):
  91. if field in self.readonly_fields:
  92. yield AdminReadonlyField(self.form, field, is_first=(i == 0),
  93. model_admin=self.model_admin)
  94. else:
  95. yield AdminField(self.form, field, is_first=(i == 0))
  96. def errors(self):
  97. return mark_safe(
  98. '\n'.join(self.form[f].errors.as_ul()
  99. for f in self.fields if f not in self.readonly_fields).strip('\n')
  100. )
  101. class AdminField(object):
  102. def __init__(self, form, field, is_first):
  103. self.field = form[field] # A django.forms.BoundField instance
  104. self.is_first = is_first # Whether this field is first on the line
  105. self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
  106. self.is_readonly = False
  107. def label_tag(self):
  108. classes = []
  109. contents = conditional_escape(force_text(self.field.label))
  110. if self.is_checkbox:
  111. classes.append('vCheckboxLabel')
  112. if self.field.field.required:
  113. classes.append('required')
  114. if not self.is_first:
  115. classes.append('inline')
  116. attrs = {'class': ' '.join(classes)} if classes else {}
  117. # checkboxes should not have a label suffix as the checkbox appears
  118. # to the left of the label.
  119. return self.field.label_tag(contents=mark_safe(contents), attrs=attrs,
  120. label_suffix='' if self.is_checkbox else None)
  121. def errors(self):
  122. return mark_safe(self.field.errors.as_ul())
  123. class AdminReadonlyField(object):
  124. def __init__(self, form, field, is_first, model_admin=None):
  125. # Make self.field look a little bit like a field. This means that
  126. # {{ field.name }} must be a useful class name to identify the field.
  127. # For convenience, store other field-related data here too.
  128. if callable(field):
  129. class_name = field.__name__ if field.__name__ != '<lambda>' else ''
  130. else:
  131. class_name = field
  132. if form._meta.labels and class_name in form._meta.labels:
  133. label = form._meta.labels[class_name]
  134. else:
  135. label = label_for_field(field, form._meta.model, model_admin)
  136. if form._meta.help_texts and class_name in form._meta.help_texts:
  137. help_text = form._meta.help_texts[class_name]
  138. else:
  139. help_text = help_text_for_field(class_name, form._meta.model)
  140. self.field = {
  141. 'name': class_name,
  142. 'label': label,
  143. 'help_text': help_text,
  144. 'field': field,
  145. }
  146. self.form = form
  147. self.model_admin = model_admin
  148. self.is_first = is_first
  149. self.is_checkbox = False
  150. self.is_readonly = True
  151. self.empty_value_display = model_admin.get_empty_value_display()
  152. def label_tag(self):
  153. attrs = {}
  154. if not self.is_first:
  155. attrs["class"] = "inline"
  156. label = self.field['label']
  157. return format_html('<label{}>{}:</label>',
  158. flatatt(attrs),
  159. capfirst(force_text(label)))
  160. def contents(self):
  161. from django.contrib.admin.templatetags.admin_list import _boolean_icon
  162. field, obj, model_admin = self.field['field'], self.form.instance, self.model_admin
  163. try:
  164. f, attr, value = lookup_field(field, obj, model_admin)
  165. except (AttributeError, ValueError, ObjectDoesNotExist):
  166. result_repr = self.empty_value_display
  167. else:
  168. if f is None:
  169. boolean = getattr(attr, "boolean", False)
  170. if boolean:
  171. result_repr = _boolean_icon(value)
  172. else:
  173. if hasattr(value, "__html__"):
  174. result_repr = value
  175. else:
  176. result_repr = smart_text(value)
  177. if getattr(attr, "allow_tags", False):
  178. warnings.warn(
  179. "Deprecated allow_tags attribute used on %s. "
  180. "Use django.utils.safestring.format_html(), "
  181. "format_html_join(), or mark_safe() instead." % attr,
  182. RemovedInDjango20Warning
  183. )
  184. result_repr = mark_safe(value)
  185. else:
  186. result_repr = linebreaksbr(result_repr)
  187. else:
  188. if isinstance(f.remote_field, ManyToManyRel) and value is not None:
  189. result_repr = ", ".join(map(six.text_type, value.all()))
  190. else:
  191. result_repr = display_for_field(value, f, self.empty_value_display)
  192. result_repr = linebreaksbr(result_repr)
  193. return conditional_escape(result_repr)
  194. class InlineAdminFormSet(object):
  195. """
  196. A wrapper around an inline formset for use in the admin system.
  197. """
  198. def __init__(self, inline, formset, fieldsets, prepopulated_fields=None,
  199. readonly_fields=None, model_admin=None):
  200. self.opts = inline
  201. self.formset = formset
  202. self.fieldsets = fieldsets
  203. self.model_admin = model_admin
  204. if readonly_fields is None:
  205. readonly_fields = ()
  206. self.readonly_fields = readonly_fields
  207. if prepopulated_fields is None:
  208. prepopulated_fields = {}
  209. self.prepopulated_fields = prepopulated_fields
  210. def __iter__(self):
  211. for form, original in zip(self.formset.initial_forms, self.formset.get_queryset()):
  212. view_on_site_url = self.opts.get_view_on_site_url(original)
  213. yield InlineAdminForm(self.formset, form, self.fieldsets,
  214. self.prepopulated_fields, original, self.readonly_fields,
  215. model_admin=self.opts, view_on_site_url=view_on_site_url)
  216. for form in self.formset.extra_forms:
  217. yield InlineAdminForm(self.formset, form, self.fieldsets,
  218. self.prepopulated_fields, None, self.readonly_fields,
  219. model_admin=self.opts)
  220. yield InlineAdminForm(self.formset, self.formset.empty_form,
  221. self.fieldsets, self.prepopulated_fields, None,
  222. self.readonly_fields, model_admin=self.opts)
  223. def fields(self):
  224. fk = getattr(self.formset, "fk", None)
  225. for i, field_name in enumerate(flatten_fieldsets(self.fieldsets)):
  226. if fk and fk.name == field_name:
  227. continue
  228. if field_name in self.readonly_fields:
  229. yield {
  230. 'label': label_for_field(field_name, self.opts.model, self.opts),
  231. 'widget': {
  232. 'is_hidden': False
  233. },
  234. 'required': False,
  235. 'help_text': help_text_for_field(field_name, self.opts.model),
  236. }
  237. else:
  238. yield self.formset.form.base_fields[field_name]
  239. def _media(self):
  240. media = self.opts.media + self.formset.media
  241. for fs in self:
  242. media = media + fs.media
  243. return media
  244. media = property(_media)
  245. class InlineAdminForm(AdminForm):
  246. """
  247. A wrapper around an inline form for use in the admin system.
  248. """
  249. def __init__(self, formset, form, fieldsets, prepopulated_fields, original,
  250. readonly_fields=None, model_admin=None, view_on_site_url=None):
  251. self.formset = formset
  252. self.model_admin = model_admin
  253. self.original = original
  254. self.show_url = original and view_on_site_url is not None
  255. self.absolute_url = view_on_site_url
  256. super(InlineAdminForm, self).__init__(form, fieldsets, prepopulated_fields,
  257. readonly_fields, model_admin)
  258. @cached_property
  259. def original_content_type_id(self):
  260. warnings.warn(
  261. 'InlineAdminForm.original_content_type_id is deprecated and will be '
  262. 'removed in Django 1.10. If you were using this attribute to construct '
  263. 'the "view on site" URL, use the `absolute_url` attribute instead.',
  264. RemovedInDjango110Warning, stacklevel=2
  265. )
  266. if self.original is not None:
  267. # Since this module gets imported in the application's root package,
  268. # it cannot import models from other applications at the module level.
  269. from django.contrib.contenttypes.models import ContentType
  270. return ContentType.objects.get_for_model(self.original).pk
  271. raise AttributeError
  272. def __iter__(self):
  273. for name, options in self.fieldsets:
  274. yield InlineFieldset(self.formset, self.form, name,
  275. self.readonly_fields, model_admin=self.model_admin, **options)
  276. def needs_explicit_pk_field(self):
  277. # Auto fields are editable (oddly), so need to check for auto or non-editable pk
  278. if self.form._meta.model._meta.has_auto_field or not self.form._meta.model._meta.pk.editable:
  279. return True
  280. # Also search any parents for an auto field. (The pk info is propagated to child
  281. # models so that does not need to be checked in parents.)
  282. for parent in self.form._meta.model._meta.get_parent_list():
  283. if parent._meta.has_auto_field:
  284. return True
  285. return False
  286. def pk_field(self):
  287. return AdminField(self.form, self.formset._pk_field.name, False)
  288. def fk_field(self):
  289. fk = getattr(self.formset, "fk", None)
  290. if fk:
  291. return AdminField(self.form, fk.name, False)
  292. else:
  293. return ""
  294. def deletion_field(self):
  295. from django.forms.formsets import DELETION_FIELD_NAME
  296. return AdminField(self.form, DELETION_FIELD_NAME, False)
  297. def ordering_field(self):
  298. from django.forms.formsets import ORDERING_FIELD_NAME
  299. return AdminField(self.form, ORDERING_FIELD_NAME, False)
  300. class InlineFieldset(Fieldset):
  301. def __init__(self, formset, *args, **kwargs):
  302. self.formset = formset
  303. super(InlineFieldset, self).__init__(*args, **kwargs)
  304. def __iter__(self):
  305. fk = getattr(self.formset, "fk", None)
  306. for field in self.fields:
  307. if fk and fk.name == field:
  308. continue
  309. yield Fieldline(self.form, field, self.readonly_fields,
  310. model_admin=self.model_admin)
  311. class AdminErrorList(forms.utils.ErrorList):
  312. """
  313. Stores all errors for the form/formsets in an add/change stage view.
  314. """
  315. def __init__(self, form, inline_formsets):
  316. super(AdminErrorList, self).__init__()
  317. if form.is_bound:
  318. self.extend(form.errors.values())
  319. for inline_formset in inline_formsets:
  320. self.extend(inline_formset.non_form_errors())
  321. for errors_in_inline_form in inline_formset.errors:
  322. self.extend(errors_in_inline_form.values())