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.
 
 
 
 

454 lines
18 KiB

  1. from __future__ import unicode_literals
  2. from django.core.exceptions import ValidationError
  3. from django.forms import Form
  4. from django.forms.fields import BooleanField, IntegerField
  5. from django.forms.utils import ErrorList
  6. from django.forms.widgets import HiddenInput
  7. from django.utils import six
  8. from django.utils.encoding import python_2_unicode_compatible
  9. from django.utils.functional import cached_property
  10. from django.utils.html import html_safe
  11. from django.utils.safestring import mark_safe
  12. from django.utils.six.moves import range
  13. from django.utils.translation import ugettext as _, ungettext
  14. __all__ = ('BaseFormSet', 'formset_factory', 'all_valid')
  15. # special field names
  16. TOTAL_FORM_COUNT = 'TOTAL_FORMS'
  17. INITIAL_FORM_COUNT = 'INITIAL_FORMS'
  18. MIN_NUM_FORM_COUNT = 'MIN_NUM_FORMS'
  19. MAX_NUM_FORM_COUNT = 'MAX_NUM_FORMS'
  20. ORDERING_FIELD_NAME = 'ORDER'
  21. DELETION_FIELD_NAME = 'DELETE'
  22. # default minimum number of forms in a formset
  23. DEFAULT_MIN_NUM = 0
  24. # default maximum number of forms in a formset, to prevent memory exhaustion
  25. DEFAULT_MAX_NUM = 1000
  26. class ManagementForm(Form):
  27. """
  28. ``ManagementForm`` is used to keep track of how many form instances
  29. are displayed on the page. If adding new forms via javascript, you should
  30. increment the count field of this form as well.
  31. """
  32. def __init__(self, *args, **kwargs):
  33. self.base_fields[TOTAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
  34. self.base_fields[INITIAL_FORM_COUNT] = IntegerField(widget=HiddenInput)
  35. # MIN_NUM_FORM_COUNT and MAX_NUM_FORM_COUNT are output with the rest of
  36. # the management form, but only for the convenience of client-side
  37. # code. The POST value of them returned from the client is not checked.
  38. self.base_fields[MIN_NUM_FORM_COUNT] = IntegerField(required=False, widget=HiddenInput)
  39. self.base_fields[MAX_NUM_FORM_COUNT] = IntegerField(required=False, widget=HiddenInput)
  40. super(ManagementForm, self).__init__(*args, **kwargs)
  41. @html_safe
  42. @python_2_unicode_compatible
  43. class BaseFormSet(object):
  44. """
  45. A collection of instances of the same Form class.
  46. """
  47. def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
  48. initial=None, error_class=ErrorList, form_kwargs=None):
  49. self.is_bound = data is not None or files is not None
  50. self.prefix = prefix or self.get_default_prefix()
  51. self.auto_id = auto_id
  52. self.data = data or {}
  53. self.files = files or {}
  54. self.initial = initial
  55. self.form_kwargs = form_kwargs or {}
  56. self.error_class = error_class
  57. self._errors = None
  58. self._non_form_errors = None
  59. def __str__(self):
  60. return self.as_table()
  61. def __iter__(self):
  62. """Yields the forms in the order they should be rendered"""
  63. return iter(self.forms)
  64. def __getitem__(self, index):
  65. """Returns the form at the given index, based on the rendering order"""
  66. return self.forms[index]
  67. def __len__(self):
  68. return len(self.forms)
  69. def __bool__(self):
  70. """All formsets have a management form which is not included in the length"""
  71. return True
  72. def __nonzero__(self): # Python 2 compatibility
  73. return type(self).__bool__(self)
  74. @property
  75. def management_form(self):
  76. """Returns the ManagementForm instance for this FormSet."""
  77. if self.is_bound:
  78. form = ManagementForm(self.data, auto_id=self.auto_id, prefix=self.prefix)
  79. if not form.is_valid():
  80. raise ValidationError(
  81. _('ManagementForm data is missing or has been tampered with'),
  82. code='missing_management_form',
  83. )
  84. else:
  85. form = ManagementForm(auto_id=self.auto_id, prefix=self.prefix, initial={
  86. TOTAL_FORM_COUNT: self.total_form_count(),
  87. INITIAL_FORM_COUNT: self.initial_form_count(),
  88. MIN_NUM_FORM_COUNT: self.min_num,
  89. MAX_NUM_FORM_COUNT: self.max_num
  90. })
  91. return form
  92. def total_form_count(self):
  93. """Returns the total number of forms in this FormSet."""
  94. if self.is_bound:
  95. # return absolute_max if it is lower than the actual total form
  96. # count in the data; this is DoS protection to prevent clients
  97. # from forcing the server to instantiate arbitrary numbers of
  98. # forms
  99. return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
  100. else:
  101. initial_forms = self.initial_form_count()
  102. total_forms = max(initial_forms, self.min_num) + self.extra
  103. # Allow all existing related objects/inlines to be displayed,
  104. # but don't allow extra beyond max_num.
  105. if initial_forms > self.max_num >= 0:
  106. total_forms = initial_forms
  107. elif total_forms > self.max_num >= 0:
  108. total_forms = self.max_num
  109. return total_forms
  110. def initial_form_count(self):
  111. """Returns the number of forms that are required in this FormSet."""
  112. if self.is_bound:
  113. return self.management_form.cleaned_data[INITIAL_FORM_COUNT]
  114. else:
  115. # Use the length of the initial data if it's there, 0 otherwise.
  116. initial_forms = len(self.initial) if self.initial else 0
  117. return initial_forms
  118. @cached_property
  119. def forms(self):
  120. """
  121. Instantiate forms at first property access.
  122. """
  123. # DoS protection is included in total_form_count()
  124. forms = [self._construct_form(i, **self.get_form_kwargs(i))
  125. for i in range(self.total_form_count())]
  126. return forms
  127. def get_form_kwargs(self, index):
  128. """
  129. Return additional keyword arguments for each individual formset form.
  130. index will be None if the form being constructed is a new empty
  131. form.
  132. """
  133. return self.form_kwargs.copy()
  134. def _construct_form(self, i, **kwargs):
  135. """
  136. Instantiates and returns the i-th form instance in a formset.
  137. """
  138. defaults = {
  139. 'auto_id': self.auto_id,
  140. 'prefix': self.add_prefix(i),
  141. 'error_class': self.error_class,
  142. }
  143. if self.is_bound:
  144. defaults['data'] = self.data
  145. defaults['files'] = self.files
  146. if self.initial and 'initial' not in kwargs:
  147. try:
  148. defaults['initial'] = self.initial[i]
  149. except IndexError:
  150. pass
  151. # Allow extra forms to be empty, unless they're part of
  152. # the minimum forms.
  153. if i >= self.initial_form_count() and i >= self.min_num:
  154. defaults['empty_permitted'] = True
  155. defaults.update(kwargs)
  156. form = self.form(**defaults)
  157. self.add_fields(form, i)
  158. return form
  159. @property
  160. def initial_forms(self):
  161. """Return a list of all the initial forms in this formset."""
  162. return self.forms[:self.initial_form_count()]
  163. @property
  164. def extra_forms(self):
  165. """Return a list of all the extra forms in this formset."""
  166. return self.forms[self.initial_form_count():]
  167. @property
  168. def empty_form(self):
  169. form = self.form(
  170. auto_id=self.auto_id,
  171. prefix=self.add_prefix('__prefix__'),
  172. empty_permitted=True,
  173. **self.get_form_kwargs(None)
  174. )
  175. self.add_fields(form, None)
  176. return form
  177. @property
  178. def cleaned_data(self):
  179. """
  180. Returns a list of form.cleaned_data dicts for every form in self.forms.
  181. """
  182. if not self.is_valid():
  183. raise AttributeError("'%s' object has no attribute 'cleaned_data'" % self.__class__.__name__)
  184. return [form.cleaned_data for form in self.forms]
  185. @property
  186. def deleted_forms(self):
  187. """
  188. Returns a list of forms that have been marked for deletion.
  189. """
  190. if not self.is_valid() or not self.can_delete:
  191. return []
  192. # construct _deleted_form_indexes which is just a list of form indexes
  193. # that have had their deletion widget set to True
  194. if not hasattr(self, '_deleted_form_indexes'):
  195. self._deleted_form_indexes = []
  196. for i in range(0, self.total_form_count()):
  197. form = self.forms[i]
  198. # if this is an extra form and hasn't changed, don't consider it
  199. if i >= self.initial_form_count() and not form.has_changed():
  200. continue
  201. if self._should_delete_form(form):
  202. self._deleted_form_indexes.append(i)
  203. return [self.forms[i] for i in self._deleted_form_indexes]
  204. @property
  205. def ordered_forms(self):
  206. """
  207. Returns a list of form in the order specified by the incoming data.
  208. Raises an AttributeError if ordering is not allowed.
  209. """
  210. if not self.is_valid() or not self.can_order:
  211. raise AttributeError("'%s' object has no attribute 'ordered_forms'" % self.__class__.__name__)
  212. # Construct _ordering, which is a list of (form_index, order_field_value)
  213. # tuples. After constructing this list, we'll sort it by order_field_value
  214. # so we have a way to get to the form indexes in the order specified
  215. # by the form data.
  216. if not hasattr(self, '_ordering'):
  217. self._ordering = []
  218. for i in range(0, self.total_form_count()):
  219. form = self.forms[i]
  220. # if this is an extra form and hasn't changed, don't consider it
  221. if i >= self.initial_form_count() and not form.has_changed():
  222. continue
  223. # don't add data marked for deletion to self.ordered_data
  224. if self.can_delete and self._should_delete_form(form):
  225. continue
  226. self._ordering.append((i, form.cleaned_data[ORDERING_FIELD_NAME]))
  227. # After we're done populating self._ordering, sort it.
  228. # A sort function to order things numerically ascending, but
  229. # None should be sorted below anything else. Allowing None as
  230. # a comparison value makes it so we can leave ordering fields
  231. # blank.
  232. def compare_ordering_key(k):
  233. if k[1] is None:
  234. return (1, 0) # +infinity, larger than any number
  235. return (0, k[1])
  236. self._ordering.sort(key=compare_ordering_key)
  237. # Return a list of form.cleaned_data dicts in the order specified by
  238. # the form data.
  239. return [self.forms[i[0]] for i in self._ordering]
  240. @classmethod
  241. def get_default_prefix(cls):
  242. return 'form'
  243. def non_form_errors(self):
  244. """
  245. Returns an ErrorList of errors that aren't associated with a particular
  246. form -- i.e., from formset.clean(). Returns an empty ErrorList if there
  247. are none.
  248. """
  249. if self._non_form_errors is None:
  250. self.full_clean()
  251. return self._non_form_errors
  252. @property
  253. def errors(self):
  254. """
  255. Returns a list of form.errors for every form in self.forms.
  256. """
  257. if self._errors is None:
  258. self.full_clean()
  259. return self._errors
  260. def total_error_count(self):
  261. """
  262. Returns the number of errors across all forms in the formset.
  263. """
  264. return len(self.non_form_errors()) +\
  265. sum(len(form_errors) for form_errors in self.errors)
  266. def _should_delete_form(self, form):
  267. """
  268. Returns whether or not the form was marked for deletion.
  269. """
  270. return form.cleaned_data.get(DELETION_FIELD_NAME, False)
  271. def is_valid(self):
  272. """
  273. Returns True if every form in self.forms is valid.
  274. """
  275. if not self.is_bound:
  276. return False
  277. # We loop over every form.errors here rather than short circuiting on the
  278. # first failure to make sure validation gets triggered for every form.
  279. forms_valid = True
  280. # This triggers a full clean.
  281. self.errors
  282. for i in range(0, self.total_form_count()):
  283. form = self.forms[i]
  284. if self.can_delete:
  285. if self._should_delete_form(form):
  286. # This form is going to be deleted so any of its errors
  287. # should not cause the entire formset to be invalid.
  288. continue
  289. forms_valid &= form.is_valid()
  290. return forms_valid and not self.non_form_errors()
  291. def full_clean(self):
  292. """
  293. Cleans all of self.data and populates self._errors and
  294. self._non_form_errors.
  295. """
  296. self._errors = []
  297. self._non_form_errors = self.error_class()
  298. if not self.is_bound: # Stop further processing.
  299. return
  300. for i in range(0, self.total_form_count()):
  301. form = self.forms[i]
  302. self._errors.append(form.errors)
  303. try:
  304. if (self.validate_max and
  305. self.total_form_count() - len(self.deleted_forms) > self.max_num) or \
  306. self.management_form.cleaned_data[TOTAL_FORM_COUNT] > self.absolute_max:
  307. raise ValidationError(ungettext(
  308. "Please submit %d or fewer forms.",
  309. "Please submit %d or fewer forms.", self.max_num) % self.max_num,
  310. code='too_many_forms',
  311. )
  312. if (self.validate_min and
  313. self.total_form_count() - len(self.deleted_forms) < self.min_num):
  314. raise ValidationError(ungettext(
  315. "Please submit %d or more forms.",
  316. "Please submit %d or more forms.", self.min_num) % self.min_num,
  317. code='too_few_forms')
  318. # Give self.clean() a chance to do cross-form validation.
  319. self.clean()
  320. except ValidationError as e:
  321. self._non_form_errors = self.error_class(e.error_list)
  322. def clean(self):
  323. """
  324. Hook for doing any extra formset-wide cleaning after Form.clean() has
  325. been called on every form. Any ValidationError raised by this method
  326. will not be associated with a particular form; it will be accessible
  327. via formset.non_form_errors()
  328. """
  329. pass
  330. def has_changed(self):
  331. """
  332. Returns true if data in any form differs from initial.
  333. """
  334. return any(form.has_changed() for form in self)
  335. def add_fields(self, form, index):
  336. """A hook for adding extra fields on to each form instance."""
  337. if self.can_order:
  338. # Only pre-fill the ordering field for initial forms.
  339. if index is not None and index < self.initial_form_count():
  340. form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_('Order'), initial=index + 1, required=False)
  341. else:
  342. form.fields[ORDERING_FIELD_NAME] = IntegerField(label=_('Order'), required=False)
  343. if self.can_delete:
  344. form.fields[DELETION_FIELD_NAME] = BooleanField(label=_('Delete'), required=False)
  345. def add_prefix(self, index):
  346. return '%s-%s' % (self.prefix, index)
  347. def is_multipart(self):
  348. """
  349. Returns True if the formset needs to be multipart, i.e. it
  350. has FileInput. Otherwise, False.
  351. """
  352. if self.forms:
  353. return self.forms[0].is_multipart()
  354. else:
  355. return self.empty_form.is_multipart()
  356. @property
  357. def media(self):
  358. # All the forms on a FormSet are the same, so you only need to
  359. # interrogate the first form for media.
  360. if self.forms:
  361. return self.forms[0].media
  362. else:
  363. return self.empty_form.media
  364. def as_table(self):
  365. "Returns this formset rendered as HTML <tr>s -- excluding the <table></table>."
  366. # XXX: there is no semantic division between forms here, there
  367. # probably should be. It might make sense to render each form as a
  368. # table row with each field as a td.
  369. forms = ' '.join(form.as_table() for form in self)
  370. return mark_safe('\n'.join([six.text_type(self.management_form), forms]))
  371. def as_p(self):
  372. "Returns this formset rendered as HTML <p>s."
  373. forms = ' '.join(form.as_p() for form in self)
  374. return mark_safe('\n'.join([six.text_type(self.management_form), forms]))
  375. def as_ul(self):
  376. "Returns this formset rendered as HTML <li>s."
  377. forms = ' '.join(form.as_ul() for form in self)
  378. return mark_safe('\n'.join([six.text_type(self.management_form), forms]))
  379. def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
  380. can_delete=False, max_num=None, validate_max=False,
  381. min_num=None, validate_min=False):
  382. """Return a FormSet for the given form class."""
  383. if min_num is None:
  384. min_num = DEFAULT_MIN_NUM
  385. if max_num is None:
  386. max_num = DEFAULT_MAX_NUM
  387. # hard limit on forms instantiated, to prevent memory-exhaustion attacks
  388. # limit is simply max_num + DEFAULT_MAX_NUM (which is 2*DEFAULT_MAX_NUM
  389. # if max_num is None in the first place)
  390. absolute_max = max_num + DEFAULT_MAX_NUM
  391. attrs = {'form': form, 'extra': extra,
  392. 'can_order': can_order, 'can_delete': can_delete,
  393. 'min_num': min_num, 'max_num': max_num,
  394. 'absolute_max': absolute_max, 'validate_min': validate_min,
  395. 'validate_max': validate_max}
  396. return type(form.__name__ + str('FormSet'), (formset,), attrs)
  397. def all_valid(formsets):
  398. """Returns true if every formset in formsets is valid."""
  399. valid = True
  400. for formset in formsets:
  401. if not formset.is_valid():
  402. valid = False
  403. return valid