fields.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. """
  2. Field classes.
  3. """
  4. import copy
  5. import datetime
  6. import json
  7. import math
  8. import operator
  9. import os
  10. import re
  11. import uuid
  12. from decimal import Decimal, DecimalException
  13. from io import BytesIO
  14. from urllib.parse import urlsplit, urlunsplit
  15. from django.core import validators
  16. from django.core.exceptions import ValidationError
  17. from django.forms.boundfield import BoundField
  18. from django.forms.utils import from_current_timezone, to_current_timezone
  19. from django.forms.widgets import (
  20. FILE_INPUT_CONTRADICTION,
  21. CheckboxInput,
  22. ClearableFileInput,
  23. DateInput,
  24. DateTimeInput,
  25. EmailInput,
  26. FileInput,
  27. HiddenInput,
  28. MultipleHiddenInput,
  29. NullBooleanSelect,
  30. NumberInput,
  31. Select,
  32. SelectMultiple,
  33. SplitDateTimeWidget,
  34. SplitHiddenDateTimeWidget,
  35. Textarea,
  36. TextInput,
  37. TimeInput,
  38. URLInput,
  39. )
  40. from django.utils import formats
  41. from django.utils.dateparse import parse_datetime, parse_duration
  42. from django.utils.duration import duration_string
  43. from django.utils.ipv6 import MAX_IPV6_ADDRESS_LENGTH, clean_ipv6_address
  44. from django.utils.regex_helper import _lazy_re_compile
  45. from django.utils.translation import gettext_lazy as _
  46. from django.utils.translation import ngettext_lazy
  47. __all__ = (
  48. "Field",
  49. "CharField",
  50. "IntegerField",
  51. "DateField",
  52. "TimeField",
  53. "DateTimeField",
  54. "DurationField",
  55. "RegexField",
  56. "EmailField",
  57. "FileField",
  58. "ImageField",
  59. "URLField",
  60. "BooleanField",
  61. "NullBooleanField",
  62. "ChoiceField",
  63. "MultipleChoiceField",
  64. "ComboField",
  65. "MultiValueField",
  66. "FloatField",
  67. "DecimalField",
  68. "SplitDateTimeField",
  69. "GenericIPAddressField",
  70. "FilePathField",
  71. "JSONField",
  72. "SlugField",
  73. "TypedChoiceField",
  74. "TypedMultipleChoiceField",
  75. "UUIDField",
  76. )
  77. class Field:
  78. widget = TextInput # Default widget to use when rendering this type of Field.
  79. hidden_widget = (
  80. HiddenInput # Default widget to use when rendering this as "hidden".
  81. )
  82. default_validators = [] # Default set of validators
  83. # Add an 'invalid' entry to default_error_message if you want a specific
  84. # field error message not raised by the field validators.
  85. default_error_messages = {
  86. "required": _("This field is required."),
  87. }
  88. empty_values = list(validators.EMPTY_VALUES)
  89. def __init__(
  90. self,
  91. *,
  92. required=True,
  93. widget=None,
  94. label=None,
  95. initial=None,
  96. help_text="",
  97. error_messages=None,
  98. show_hidden_initial=False,
  99. validators=(),
  100. localize=False,
  101. disabled=False,
  102. label_suffix=None,
  103. ):
  104. # required -- Boolean that specifies whether the field is required.
  105. # True by default.
  106. # widget -- A Widget class, or instance of a Widget class, that should
  107. # be used for this Field when displaying it. Each Field has a
  108. # default Widget that it'll use if you don't specify this. In
  109. # most cases, the default widget is TextInput.
  110. # label -- A verbose name for this field, for use in displaying this
  111. # field in a form. By default, Django will use a "pretty"
  112. # version of the form field name, if the Field is part of a
  113. # Form.
  114. # initial -- A value to use in this Field's initial display. This value
  115. # is *not* used as a fallback if data isn't given.
  116. # help_text -- An optional string to use as "help text" for this Field.
  117. # error_messages -- An optional dictionary to override the default
  118. # messages that the field will raise.
  119. # show_hidden_initial -- Boolean that specifies if it is needed to render a
  120. # hidden widget with initial value after widget.
  121. # validators -- List of additional validators to use
  122. # localize -- Boolean that specifies if the field should be localized.
  123. # disabled -- Boolean that specifies whether the field is disabled, that
  124. # is its widget is shown in the form but not editable.
  125. # label_suffix -- Suffix to be added to the label. Overrides
  126. # form's label_suffix.
  127. self.required, self.label, self.initial = required, label, initial
  128. self.show_hidden_initial = show_hidden_initial
  129. self.help_text = help_text
  130. self.disabled = disabled
  131. self.label_suffix = label_suffix
  132. widget = widget or self.widget
  133. if isinstance(widget, type):
  134. widget = widget()
  135. else:
  136. widget = copy.deepcopy(widget)
  137. # Trigger the localization machinery if needed.
  138. self.localize = localize
  139. if self.localize:
  140. widget.is_localized = True
  141. # Let the widget know whether it should display as required.
  142. widget.is_required = self.required
  143. # Hook into self.widget_attrs() for any Field-specific HTML attributes.
  144. extra_attrs = self.widget_attrs(widget)
  145. if extra_attrs:
  146. widget.attrs.update(extra_attrs)
  147. self.widget = widget
  148. messages = {}
  149. for c in reversed(self.__class__.__mro__):
  150. messages.update(getattr(c, "default_error_messages", {}))
  151. messages.update(error_messages or {})
  152. self.error_messages = messages
  153. self.validators = [*self.default_validators, *validators]
  154. super().__init__()
  155. def prepare_value(self, value):
  156. return value
  157. def to_python(self, value):
  158. return value
  159. def validate(self, value):
  160. if value in self.empty_values and self.required:
  161. raise ValidationError(self.error_messages["required"], code="required")
  162. def run_validators(self, value):
  163. if value in self.empty_values:
  164. return
  165. errors = []
  166. for v in self.validators:
  167. try:
  168. v(value)
  169. except ValidationError as e:
  170. if hasattr(e, "code") and e.code in self.error_messages:
  171. e.message = self.error_messages[e.code]
  172. errors.extend(e.error_list)
  173. if errors:
  174. raise ValidationError(errors)
  175. def clean(self, value):
  176. """
  177. Validate the given value and return its "cleaned" value as an
  178. appropriate Python object. Raise ValidationError for any errors.
  179. """
  180. value = self.to_python(value)
  181. self.validate(value)
  182. self.run_validators(value)
  183. return value
  184. def bound_data(self, data, initial):
  185. """
  186. Return the value that should be shown for this field on render of a
  187. bound form, given the submitted POST data for the field and the initial
  188. data, if any.
  189. For most fields, this will simply be data; FileFields need to handle it
  190. a bit differently.
  191. """
  192. if self.disabled:
  193. return initial
  194. return data
  195. def widget_attrs(self, widget):
  196. """
  197. Given a Widget instance (*not* a Widget class), return a dictionary of
  198. any HTML attributes that should be added to the Widget, based on this
  199. Field.
  200. """
  201. return {}
  202. def has_changed(self, initial, data):
  203. """Return True if data differs from initial."""
  204. # Always return False if the field is disabled since self.bound_data
  205. # always uses the initial value in this case.
  206. if self.disabled:
  207. return False
  208. try:
  209. data = self.to_python(data)
  210. if hasattr(self, "_coerce"):
  211. return self._coerce(data) != self._coerce(initial)
  212. except ValidationError:
  213. return True
  214. # For purposes of seeing whether something has changed, None is
  215. # the same as an empty string, if the data or initial value we get
  216. # is None, replace it with ''.
  217. initial_value = initial if initial is not None else ""
  218. data_value = data if data is not None else ""
  219. return initial_value != data_value
  220. def get_bound_field(self, form, field_name):
  221. """
  222. Return a BoundField instance that will be used when accessing the form
  223. field in a template.
  224. """
  225. return BoundField(form, self, field_name)
  226. def __deepcopy__(self, memo):
  227. result = copy.copy(self)
  228. memo[id(self)] = result
  229. result.widget = copy.deepcopy(self.widget, memo)
  230. result.error_messages = self.error_messages.copy()
  231. result.validators = self.validators[:]
  232. return result
  233. class CharField(Field):
  234. def __init__(
  235. self, *, max_length=None, min_length=None, strip=True, empty_value="", **kwargs
  236. ):
  237. self.max_length = max_length
  238. self.min_length = min_length
  239. self.strip = strip
  240. self.empty_value = empty_value
  241. super().__init__(**kwargs)
  242. if min_length is not None:
  243. self.validators.append(validators.MinLengthValidator(int(min_length)))
  244. if max_length is not None:
  245. self.validators.append(validators.MaxLengthValidator(int(max_length)))
  246. self.validators.append(validators.ProhibitNullCharactersValidator())
  247. def to_python(self, value):
  248. """Return a string."""
  249. if value not in self.empty_values:
  250. value = str(value)
  251. if self.strip:
  252. value = value.strip()
  253. if value in self.empty_values:
  254. return self.empty_value
  255. return value
  256. def widget_attrs(self, widget):
  257. attrs = super().widget_attrs(widget)
  258. if self.max_length is not None and not widget.is_hidden:
  259. # The HTML attribute is maxlength, not max_length.
  260. attrs["maxlength"] = str(self.max_length)
  261. if self.min_length is not None and not widget.is_hidden:
  262. # The HTML attribute is minlength, not min_length.
  263. attrs["minlength"] = str(self.min_length)
  264. return attrs
  265. class IntegerField(Field):
  266. widget = NumberInput
  267. default_error_messages = {
  268. "invalid": _("Enter a whole number."),
  269. }
  270. re_decimal = _lazy_re_compile(r"\.0*\s*$")
  271. def __init__(self, *, max_value=None, min_value=None, step_size=None, **kwargs):
  272. self.max_value, self.min_value, self.step_size = max_value, min_value, step_size
  273. if kwargs.get("localize") and self.widget == NumberInput:
  274. # Localized number input is not well supported on most browsers
  275. kwargs.setdefault("widget", super().widget)
  276. super().__init__(**kwargs)
  277. if max_value is not None:
  278. self.validators.append(validators.MaxValueValidator(max_value))
  279. if min_value is not None:
  280. self.validators.append(validators.MinValueValidator(min_value))
  281. if step_size is not None:
  282. self.validators.append(validators.StepValueValidator(step_size))
  283. def to_python(self, value):
  284. """
  285. Validate that int() can be called on the input. Return the result
  286. of int() or None for empty values.
  287. """
  288. value = super().to_python(value)
  289. if value in self.empty_values:
  290. return None
  291. if self.localize:
  292. value = formats.sanitize_separators(value)
  293. # Strip trailing decimal and zeros.
  294. try:
  295. value = int(self.re_decimal.sub("", str(value)))
  296. except (ValueError, TypeError):
  297. raise ValidationError(self.error_messages["invalid"], code="invalid")
  298. return value
  299. def widget_attrs(self, widget):
  300. attrs = super().widget_attrs(widget)
  301. if isinstance(widget, NumberInput):
  302. if self.min_value is not None:
  303. attrs["min"] = self.min_value
  304. if self.max_value is not None:
  305. attrs["max"] = self.max_value
  306. if self.step_size is not None:
  307. attrs["step"] = self.step_size
  308. return attrs
  309. class FloatField(IntegerField):
  310. default_error_messages = {
  311. "invalid": _("Enter a number."),
  312. }
  313. def to_python(self, value):
  314. """
  315. Validate that float() can be called on the input. Return the result
  316. of float() or None for empty values.
  317. """
  318. value = super(IntegerField, self).to_python(value)
  319. if value in self.empty_values:
  320. return None
  321. if self.localize:
  322. value = formats.sanitize_separators(value)
  323. try:
  324. value = float(value)
  325. except (ValueError, TypeError):
  326. raise ValidationError(self.error_messages["invalid"], code="invalid")
  327. return value
  328. def validate(self, value):
  329. super().validate(value)
  330. if value in self.empty_values:
  331. return
  332. if not math.isfinite(value):
  333. raise ValidationError(self.error_messages["invalid"], code="invalid")
  334. def widget_attrs(self, widget):
  335. attrs = super().widget_attrs(widget)
  336. if isinstance(widget, NumberInput) and "step" not in widget.attrs:
  337. if self.step_size is not None:
  338. step = str(self.step_size)
  339. else:
  340. step = "any"
  341. attrs.setdefault("step", step)
  342. return attrs
  343. class DecimalField(IntegerField):
  344. default_error_messages = {
  345. "invalid": _("Enter a number."),
  346. }
  347. def __init__(
  348. self,
  349. *,
  350. max_value=None,
  351. min_value=None,
  352. max_digits=None,
  353. decimal_places=None,
  354. **kwargs,
  355. ):
  356. self.max_digits, self.decimal_places = max_digits, decimal_places
  357. super().__init__(max_value=max_value, min_value=min_value, **kwargs)
  358. self.validators.append(validators.DecimalValidator(max_digits, decimal_places))
  359. def to_python(self, value):
  360. """
  361. Validate that the input is a decimal number. Return a Decimal
  362. instance or None for empty values. Ensure that there are no more
  363. than max_digits in the number and no more than decimal_places digits
  364. after the decimal point.
  365. """
  366. if value in self.empty_values:
  367. return None
  368. if self.localize:
  369. value = formats.sanitize_separators(value)
  370. try:
  371. value = Decimal(str(value))
  372. except DecimalException:
  373. raise ValidationError(self.error_messages["invalid"], code="invalid")
  374. return value
  375. def validate(self, value):
  376. super().validate(value)
  377. if value in self.empty_values:
  378. return
  379. if not value.is_finite():
  380. raise ValidationError(
  381. self.error_messages["invalid"],
  382. code="invalid",
  383. params={"value": value},
  384. )
  385. def widget_attrs(self, widget):
  386. attrs = super().widget_attrs(widget)
  387. if isinstance(widget, NumberInput) and "step" not in widget.attrs:
  388. if self.decimal_places is not None:
  389. # Use exponential notation for small values since they might
  390. # be parsed as 0 otherwise. ref #20765
  391. step = str(Decimal(1).scaleb(-self.decimal_places)).lower()
  392. else:
  393. step = "any"
  394. attrs.setdefault("step", step)
  395. return attrs
  396. class BaseTemporalField(Field):
  397. def __init__(self, *, input_formats=None, **kwargs):
  398. super().__init__(**kwargs)
  399. if input_formats is not None:
  400. self.input_formats = input_formats
  401. def to_python(self, value):
  402. value = value.strip()
  403. # Try to strptime against each input format.
  404. for format in self.input_formats:
  405. try:
  406. return self.strptime(value, format)
  407. except (ValueError, TypeError):
  408. continue
  409. raise ValidationError(self.error_messages["invalid"], code="invalid")
  410. def strptime(self, value, format):
  411. raise NotImplementedError("Subclasses must define this method.")
  412. class DateField(BaseTemporalField):
  413. widget = DateInput
  414. input_formats = formats.get_format_lazy("DATE_INPUT_FORMATS")
  415. default_error_messages = {
  416. "invalid": _("Enter a valid date."),
  417. }
  418. def to_python(self, value):
  419. """
  420. Validate that the input can be converted to a date. Return a Python
  421. datetime.date object.
  422. """
  423. if value in self.empty_values:
  424. return None
  425. if isinstance(value, datetime.datetime):
  426. return value.date()
  427. if isinstance(value, datetime.date):
  428. return value
  429. return super().to_python(value)
  430. def strptime(self, value, format):
  431. return datetime.datetime.strptime(value, format).date()
  432. class TimeField(BaseTemporalField):
  433. widget = TimeInput
  434. input_formats = formats.get_format_lazy("TIME_INPUT_FORMATS")
  435. default_error_messages = {"invalid": _("Enter a valid time.")}
  436. def to_python(self, value):
  437. """
  438. Validate that the input can be converted to a time. Return a Python
  439. datetime.time object.
  440. """
  441. if value in self.empty_values:
  442. return None
  443. if isinstance(value, datetime.time):
  444. return value
  445. return super().to_python(value)
  446. def strptime(self, value, format):
  447. return datetime.datetime.strptime(value, format).time()
  448. class DateTimeFormatsIterator:
  449. def __iter__(self):
  450. yield from formats.get_format("DATETIME_INPUT_FORMATS")
  451. yield from formats.get_format("DATE_INPUT_FORMATS")
  452. class DateTimeField(BaseTemporalField):
  453. widget = DateTimeInput
  454. input_formats = DateTimeFormatsIterator()
  455. default_error_messages = {
  456. "invalid": _("Enter a valid date/time."),
  457. }
  458. def prepare_value(self, value):
  459. if isinstance(value, datetime.datetime):
  460. value = to_current_timezone(value)
  461. return value
  462. def to_python(self, value):
  463. """
  464. Validate that the input can be converted to a datetime. Return a
  465. Python datetime.datetime object.
  466. """
  467. if value in self.empty_values:
  468. return None
  469. if isinstance(value, datetime.datetime):
  470. return from_current_timezone(value)
  471. if isinstance(value, datetime.date):
  472. result = datetime.datetime(value.year, value.month, value.day)
  473. return from_current_timezone(result)
  474. try:
  475. result = parse_datetime(value.strip())
  476. except ValueError:
  477. raise ValidationError(self.error_messages["invalid"], code="invalid")
  478. if not result:
  479. result = super().to_python(value)
  480. return from_current_timezone(result)
  481. def strptime(self, value, format):
  482. return datetime.datetime.strptime(value, format)
  483. class DurationField(Field):
  484. default_error_messages = {
  485. "invalid": _("Enter a valid duration."),
  486. "overflow": _("The number of days must be between {min_days} and {max_days}."),
  487. }
  488. def prepare_value(self, value):
  489. if isinstance(value, datetime.timedelta):
  490. return duration_string(value)
  491. return value
  492. def to_python(self, value):
  493. if value in self.empty_values:
  494. return None
  495. if isinstance(value, datetime.timedelta):
  496. return value
  497. try:
  498. value = parse_duration(str(value))
  499. except OverflowError:
  500. raise ValidationError(
  501. self.error_messages["overflow"].format(
  502. min_days=datetime.timedelta.min.days,
  503. max_days=datetime.timedelta.max.days,
  504. ),
  505. code="overflow",
  506. )
  507. if value is None:
  508. raise ValidationError(self.error_messages["invalid"], code="invalid")
  509. return value
  510. class RegexField(CharField):
  511. def __init__(self, regex, **kwargs):
  512. """
  513. regex can be either a string or a compiled regular expression object.
  514. """
  515. kwargs.setdefault("strip", False)
  516. super().__init__(**kwargs)
  517. self._set_regex(regex)
  518. def _get_regex(self):
  519. return self._regex
  520. def _set_regex(self, regex):
  521. if isinstance(regex, str):
  522. regex = re.compile(regex)
  523. self._regex = regex
  524. if (
  525. hasattr(self, "_regex_validator")
  526. and self._regex_validator in self.validators
  527. ):
  528. self.validators.remove(self._regex_validator)
  529. self._regex_validator = validators.RegexValidator(regex=regex)
  530. self.validators.append(self._regex_validator)
  531. regex = property(_get_regex, _set_regex)
  532. class EmailField(CharField):
  533. widget = EmailInput
  534. default_validators = [validators.validate_email]
  535. def __init__(self, **kwargs):
  536. # The default maximum length of an email is 320 characters per RFC 3696
  537. # section 3.
  538. kwargs.setdefault("max_length", 320)
  539. super().__init__(strip=True, **kwargs)
  540. class FileField(Field):
  541. widget = ClearableFileInput
  542. default_error_messages = {
  543. "invalid": _("No file was submitted. Check the encoding type on the form."),
  544. "missing": _("No file was submitted."),
  545. "empty": _("The submitted file is empty."),
  546. "max_length": ngettext_lazy(
  547. "Ensure this filename has at most %(max)d character (it has %(length)d).",
  548. "Ensure this filename has at most %(max)d characters (it has %(length)d).",
  549. "max",
  550. ),
  551. "contradiction": _(
  552. "Please either submit a file or check the clear checkbox, not both."
  553. ),
  554. }
  555. def __init__(self, *, max_length=None, allow_empty_file=False, **kwargs):
  556. self.max_length = max_length
  557. self.allow_empty_file = allow_empty_file
  558. super().__init__(**kwargs)
  559. def to_python(self, data):
  560. if data in self.empty_values:
  561. return None
  562. # UploadedFile objects should have name and size attributes.
  563. try:
  564. file_name = data.name
  565. file_size = data.size
  566. except AttributeError:
  567. raise ValidationError(self.error_messages["invalid"], code="invalid")
  568. if self.max_length is not None and len(file_name) > self.max_length:
  569. params = {"max": self.max_length, "length": len(file_name)}
  570. raise ValidationError(
  571. self.error_messages["max_length"], code="max_length", params=params
  572. )
  573. if not file_name:
  574. raise ValidationError(self.error_messages["invalid"], code="invalid")
  575. if not self.allow_empty_file and not file_size:
  576. raise ValidationError(self.error_messages["empty"], code="empty")
  577. return data
  578. def clean(self, data, initial=None):
  579. # If the widget got contradictory inputs, we raise a validation error
  580. if data is FILE_INPUT_CONTRADICTION:
  581. raise ValidationError(
  582. self.error_messages["contradiction"], code="contradiction"
  583. )
  584. # False means the field value should be cleared; further validation is
  585. # not needed.
  586. if data is False:
  587. if not self.required:
  588. return False
  589. # If the field is required, clearing is not possible (the widget
  590. # shouldn't return False data in that case anyway). False is not
  591. # in self.empty_value; if a False value makes it this far
  592. # it should be validated from here on out as None (so it will be
  593. # caught by the required check).
  594. data = None
  595. if not data and initial:
  596. return initial
  597. return super().clean(data)
  598. def bound_data(self, _, initial):
  599. return initial
  600. def has_changed(self, initial, data):
  601. return not self.disabled and data is not None
  602. class ImageField(FileField):
  603. default_validators = [validators.validate_image_file_extension]
  604. default_error_messages = {
  605. "invalid_image": _(
  606. "Upload a valid image. The file you uploaded was either not an "
  607. "image or a corrupted image."
  608. ),
  609. }
  610. def to_python(self, data):
  611. """
  612. Check that the file-upload field data contains a valid image (GIF, JPG,
  613. PNG, etc. -- whatever Pillow supports).
  614. """
  615. f = super().to_python(data)
  616. if f is None:
  617. return None
  618. from PIL import Image
  619. # We need to get a file object for Pillow. We might have a path or we might
  620. # have to read the data into memory.
  621. if hasattr(data, "temporary_file_path"):
  622. file = data.temporary_file_path()
  623. else:
  624. if hasattr(data, "read"):
  625. file = BytesIO(data.read())
  626. else:
  627. file = BytesIO(data["content"])
  628. try:
  629. # load() could spot a truncated JPEG, but it loads the entire
  630. # image in memory, which is a DoS vector. See #3848 and #18520.
  631. image = Image.open(file)
  632. # verify() must be called immediately after the constructor.
  633. image.verify()
  634. # Annotating so subclasses can reuse it for their own validation
  635. f.image = image
  636. # Pillow doesn't detect the MIME type of all formats. In those
  637. # cases, content_type will be None.
  638. f.content_type = Image.MIME.get(image.format)
  639. except Exception as exc:
  640. # Pillow doesn't recognize it as an image.
  641. raise ValidationError(
  642. self.error_messages["invalid_image"],
  643. code="invalid_image",
  644. ) from exc
  645. if hasattr(f, "seek") and callable(f.seek):
  646. f.seek(0)
  647. return f
  648. def widget_attrs(self, widget):
  649. attrs = super().widget_attrs(widget)
  650. if isinstance(widget, FileInput) and "accept" not in widget.attrs:
  651. attrs.setdefault("accept", "image/*")
  652. return attrs
  653. class URLField(CharField):
  654. widget = URLInput
  655. default_error_messages = {
  656. "invalid": _("Enter a valid URL."),
  657. }
  658. default_validators = [validators.URLValidator()]
  659. def __init__(self, **kwargs):
  660. super().__init__(strip=True, **kwargs)
  661. def to_python(self, value):
  662. def split_url(url):
  663. """
  664. Return a list of url parts via urlparse.urlsplit(), or raise
  665. ValidationError for some malformed URLs.
  666. """
  667. try:
  668. return list(urlsplit(url))
  669. except ValueError:
  670. # urlparse.urlsplit can raise a ValueError with some
  671. # misformatted URLs.
  672. raise ValidationError(self.error_messages["invalid"], code="invalid")
  673. value = super().to_python(value)
  674. if value:
  675. url_fields = split_url(value)
  676. if not url_fields[0]:
  677. # If no URL scheme given, assume http://
  678. url_fields[0] = "http"
  679. if not url_fields[1]:
  680. # Assume that if no domain is provided, that the path segment
  681. # contains the domain.
  682. url_fields[1] = url_fields[2]
  683. url_fields[2] = ""
  684. # Rebuild the url_fields list, since the domain segment may now
  685. # contain the path too.
  686. url_fields = split_url(urlunsplit(url_fields))
  687. value = urlunsplit(url_fields)
  688. return value
  689. class BooleanField(Field):
  690. widget = CheckboxInput
  691. def to_python(self, value):
  692. """Return a Python boolean object."""
  693. # Explicitly check for the string 'False', which is what a hidden field
  694. # will submit for False. Also check for '0', since this is what
  695. # RadioSelect will provide. Because bool("True") == bool('1') == True,
  696. # we don't need to handle that explicitly.
  697. if isinstance(value, str) and value.lower() in ("false", "0"):
  698. value = False
  699. else:
  700. value = bool(value)
  701. return super().to_python(value)
  702. def validate(self, value):
  703. if not value and self.required:
  704. raise ValidationError(self.error_messages["required"], code="required")
  705. def has_changed(self, initial, data):
  706. if self.disabled:
  707. return False
  708. # Sometimes data or initial may be a string equivalent of a boolean
  709. # so we should run it through to_python first to get a boolean value
  710. return self.to_python(initial) != self.to_python(data)
  711. class NullBooleanField(BooleanField):
  712. """
  713. A field whose valid values are None, True, and False. Clean invalid values
  714. to None.
  715. """
  716. widget = NullBooleanSelect
  717. def to_python(self, value):
  718. """
  719. Explicitly check for the string 'True' and 'False', which is what a
  720. hidden field will submit for True and False, for 'true' and 'false',
  721. which are likely to be returned by JavaScript serializations of forms,
  722. and for '1' and '0', which is what a RadioField will submit. Unlike
  723. the Booleanfield, this field must check for True because it doesn't
  724. use the bool() function.
  725. """
  726. if value in (True, "True", "true", "1"):
  727. return True
  728. elif value in (False, "False", "false", "0"):
  729. return False
  730. else:
  731. return None
  732. def validate(self, value):
  733. pass
  734. class CallableChoiceIterator:
  735. def __init__(self, choices_func):
  736. self.choices_func = choices_func
  737. def __iter__(self):
  738. yield from self.choices_func()
  739. class ChoiceField(Field):
  740. widget = Select
  741. default_error_messages = {
  742. "invalid_choice": _(
  743. "Select a valid choice. %(value)s is not one of the available choices."
  744. ),
  745. }
  746. def __init__(self, *, choices=(), **kwargs):
  747. super().__init__(**kwargs)
  748. self.choices = choices
  749. def __deepcopy__(self, memo):
  750. result = super().__deepcopy__(memo)
  751. result._choices = copy.deepcopy(self._choices, memo)
  752. return result
  753. def _get_choices(self):
  754. return self._choices
  755. def _set_choices(self, value):
  756. # Setting choices also sets the choices on the widget.
  757. # choices can be any iterable, but we call list() on it because
  758. # it will be consumed more than once.
  759. if callable(value):
  760. value = CallableChoiceIterator(value)
  761. else:
  762. value = list(value)
  763. self._choices = self.widget.choices = value
  764. choices = property(_get_choices, _set_choices)
  765. def to_python(self, value):
  766. """Return a string."""
  767. if value in self.empty_values:
  768. return ""
  769. return str(value)
  770. def validate(self, value):
  771. """Validate that the input is in self.choices."""
  772. super().validate(value)
  773. if value and not self.valid_value(value):
  774. raise ValidationError(
  775. self.error_messages["invalid_choice"],
  776. code="invalid_choice",
  777. params={"value": value},
  778. )
  779. def valid_value(self, value):
  780. """Check to see if the provided value is a valid choice."""
  781. text_value = str(value)
  782. for k, v in self.choices:
  783. if isinstance(v, (list, tuple)):
  784. # This is an optgroup, so look inside the group for options
  785. for k2, v2 in v:
  786. if value == k2 or text_value == str(k2):
  787. return True
  788. else:
  789. if value == k or text_value == str(k):
  790. return True
  791. return False
  792. class TypedChoiceField(ChoiceField):
  793. def __init__(self, *, coerce=lambda val: val, empty_value="", **kwargs):
  794. self.coerce = coerce
  795. self.empty_value = empty_value
  796. super().__init__(**kwargs)
  797. def _coerce(self, value):
  798. """
  799. Validate that the value can be coerced to the right type (if not empty).
  800. """
  801. if value == self.empty_value or value in self.empty_values:
  802. return self.empty_value
  803. try:
  804. value = self.coerce(value)
  805. except (ValueError, TypeError, ValidationError):
  806. raise ValidationError(
  807. self.error_messages["invalid_choice"],
  808. code="invalid_choice",
  809. params={"value": value},
  810. )
  811. return value
  812. def clean(self, value):
  813. value = super().clean(value)
  814. return self._coerce(value)
  815. class MultipleChoiceField(ChoiceField):
  816. hidden_widget = MultipleHiddenInput
  817. widget = SelectMultiple
  818. default_error_messages = {
  819. "invalid_choice": _(
  820. "Select a valid choice. %(value)s is not one of the available choices."
  821. ),
  822. "invalid_list": _("Enter a list of values."),
  823. }
  824. def to_python(self, value):
  825. if not value:
  826. return []
  827. elif not isinstance(value, (list, tuple)):
  828. raise ValidationError(
  829. self.error_messages["invalid_list"], code="invalid_list"
  830. )
  831. return [str(val) for val in value]
  832. def validate(self, value):
  833. """Validate that the input is a list or tuple."""
  834. if self.required and not value:
  835. raise ValidationError(self.error_messages["required"], code="required")
  836. # Validate that each value in the value list is in self.choices.
  837. for val in value:
  838. if not self.valid_value(val):
  839. raise ValidationError(
  840. self.error_messages["invalid_choice"],
  841. code="invalid_choice",
  842. params={"value": val},
  843. )
  844. def has_changed(self, initial, data):
  845. if self.disabled:
  846. return False
  847. if initial is None:
  848. initial = []
  849. if data is None:
  850. data = []
  851. if len(initial) != len(data):
  852. return True
  853. initial_set = {str(value) for value in initial}
  854. data_set = {str(value) for value in data}
  855. return data_set != initial_set
  856. class TypedMultipleChoiceField(MultipleChoiceField):
  857. def __init__(self, *, coerce=lambda val: val, **kwargs):
  858. self.coerce = coerce
  859. self.empty_value = kwargs.pop("empty_value", [])
  860. super().__init__(**kwargs)
  861. def _coerce(self, value):
  862. """
  863. Validate that the values are in self.choices and can be coerced to the
  864. right type.
  865. """
  866. if value == self.empty_value or value in self.empty_values:
  867. return self.empty_value
  868. new_value = []
  869. for choice in value:
  870. try:
  871. new_value.append(self.coerce(choice))
  872. except (ValueError, TypeError, ValidationError):
  873. raise ValidationError(
  874. self.error_messages["invalid_choice"],
  875. code="invalid_choice",
  876. params={"value": choice},
  877. )
  878. return new_value
  879. def clean(self, value):
  880. value = super().clean(value)
  881. return self._coerce(value)
  882. def validate(self, value):
  883. if value != self.empty_value:
  884. super().validate(value)
  885. elif self.required:
  886. raise ValidationError(self.error_messages["required"], code="required")
  887. class ComboField(Field):
  888. """
  889. A Field whose clean() method calls multiple Field clean() methods.
  890. """
  891. def __init__(self, fields, **kwargs):
  892. super().__init__(**kwargs)
  893. # Set 'required' to False on the individual fields, because the
  894. # required validation will be handled by ComboField, not by those
  895. # individual fields.
  896. for f in fields:
  897. f.required = False
  898. self.fields = fields
  899. def clean(self, value):
  900. """
  901. Validate the given value against all of self.fields, which is a
  902. list of Field instances.
  903. """
  904. super().clean(value)
  905. for field in self.fields:
  906. value = field.clean(value)
  907. return value
  908. class MultiValueField(Field):
  909. """
  910. Aggregate the logic of multiple Fields.
  911. Its clean() method takes a "decompressed" list of values, which are then
  912. cleaned into a single value according to self.fields. Each value in
  913. this list is cleaned by the corresponding field -- the first value is
  914. cleaned by the first field, the second value is cleaned by the second
  915. field, etc. Once all fields are cleaned, the list of clean values is
  916. "compressed" into a single value.
  917. Subclasses should not have to implement clean(). Instead, they must
  918. implement compress(), which takes a list of valid values and returns a
  919. "compressed" version of those values -- a single value.
  920. You'll probably want to use this with MultiWidget.
  921. """
  922. default_error_messages = {
  923. "invalid": _("Enter a list of values."),
  924. "incomplete": _("Enter a complete value."),
  925. }
  926. def __init__(self, fields, *, require_all_fields=True, **kwargs):
  927. self.require_all_fields = require_all_fields
  928. super().__init__(**kwargs)
  929. for f in fields:
  930. f.error_messages.setdefault("incomplete", self.error_messages["incomplete"])
  931. if self.disabled:
  932. f.disabled = True
  933. if self.require_all_fields:
  934. # Set 'required' to False on the individual fields, because the
  935. # required validation will be handled by MultiValueField, not
  936. # by those individual fields.
  937. f.required = False
  938. self.fields = fields
  939. def __deepcopy__(self, memo):
  940. result = super().__deepcopy__(memo)
  941. result.fields = tuple(x.__deepcopy__(memo) for x in self.fields)
  942. return result
  943. def validate(self, value):
  944. pass
  945. def clean(self, value):
  946. """
  947. Validate every value in the given list. A value is validated against
  948. the corresponding Field in self.fields.
  949. For example, if this MultiValueField was instantiated with
  950. fields=(DateField(), TimeField()), clean() would call
  951. DateField.clean(value[0]) and TimeField.clean(value[1]).
  952. """
  953. clean_data = []
  954. errors = []
  955. if self.disabled and not isinstance(value, list):
  956. value = self.widget.decompress(value)
  957. if not value or isinstance(value, (list, tuple)):
  958. if not value or not [v for v in value if v not in self.empty_values]:
  959. if self.required:
  960. raise ValidationError(
  961. self.error_messages["required"], code="required"
  962. )
  963. else:
  964. return self.compress([])
  965. else:
  966. raise ValidationError(self.error_messages["invalid"], code="invalid")
  967. for i, field in enumerate(self.fields):
  968. try:
  969. field_value = value[i]
  970. except IndexError:
  971. field_value = None
  972. if field_value in self.empty_values:
  973. if self.require_all_fields:
  974. # Raise a 'required' error if the MultiValueField is
  975. # required and any field is empty.
  976. if self.required:
  977. raise ValidationError(
  978. self.error_messages["required"], code="required"
  979. )
  980. elif field.required:
  981. # Otherwise, add an 'incomplete' error to the list of
  982. # collected errors and skip field cleaning, if a required
  983. # field is empty.
  984. if field.error_messages["incomplete"] not in errors:
  985. errors.append(field.error_messages["incomplete"])
  986. continue
  987. try:
  988. clean_data.append(field.clean(field_value))
  989. except ValidationError as e:
  990. # Collect all validation errors in a single list, which we'll
  991. # raise at the end of clean(), rather than raising a single
  992. # exception for the first error we encounter. Skip duplicates.
  993. errors.extend(m for m in e.error_list if m not in errors)
  994. if errors:
  995. raise ValidationError(errors)
  996. out = self.compress(clean_data)
  997. self.validate(out)
  998. self.run_validators(out)
  999. return out
  1000. def compress(self, data_list):
  1001. """
  1002. Return a single value for the given list of values. The values can be
  1003. assumed to be valid.
  1004. For example, if this MultiValueField was instantiated with
  1005. fields=(DateField(), TimeField()), this might return a datetime
  1006. object created by combining the date and time in data_list.
  1007. """
  1008. raise NotImplementedError("Subclasses must implement this method.")
  1009. def has_changed(self, initial, data):
  1010. if self.disabled:
  1011. return False
  1012. if initial is None:
  1013. initial = ["" for x in range(0, len(data))]
  1014. else:
  1015. if not isinstance(initial, list):
  1016. initial = self.widget.decompress(initial)
  1017. for field, initial, data in zip(self.fields, initial, data):
  1018. try:
  1019. initial = field.to_python(initial)
  1020. except ValidationError:
  1021. return True
  1022. if field.has_changed(initial, data):
  1023. return True
  1024. return False
  1025. class FilePathField(ChoiceField):
  1026. def __init__(
  1027. self,
  1028. path,
  1029. *,
  1030. match=None,
  1031. recursive=False,
  1032. allow_files=True,
  1033. allow_folders=False,
  1034. **kwargs,
  1035. ):
  1036. self.path, self.match, self.recursive = path, match, recursive
  1037. self.allow_files, self.allow_folders = allow_files, allow_folders
  1038. super().__init__(choices=(), **kwargs)
  1039. if self.required:
  1040. self.choices = []
  1041. else:
  1042. self.choices = [("", "---------")]
  1043. if self.match is not None:
  1044. self.match_re = re.compile(self.match)
  1045. if recursive:
  1046. for root, dirs, files in sorted(os.walk(self.path)):
  1047. if self.allow_files:
  1048. for f in sorted(files):
  1049. if self.match is None or self.match_re.search(f):
  1050. f = os.path.join(root, f)
  1051. self.choices.append((f, f.replace(path, "", 1)))
  1052. if self.allow_folders:
  1053. for f in sorted(dirs):
  1054. if f == "__pycache__":
  1055. continue
  1056. if self.match is None or self.match_re.search(f):
  1057. f = os.path.join(root, f)
  1058. self.choices.append((f, f.replace(path, "", 1)))
  1059. else:
  1060. choices = []
  1061. with os.scandir(self.path) as entries:
  1062. for f in entries:
  1063. if f.name == "__pycache__":
  1064. continue
  1065. if (
  1066. (self.allow_files and f.is_file())
  1067. or (self.allow_folders and f.is_dir())
  1068. ) and (self.match is None or self.match_re.search(f.name)):
  1069. choices.append((f.path, f.name))
  1070. choices.sort(key=operator.itemgetter(1))
  1071. self.choices.extend(choices)
  1072. self.widget.choices = self.choices
  1073. class SplitDateTimeField(MultiValueField):
  1074. widget = SplitDateTimeWidget
  1075. hidden_widget = SplitHiddenDateTimeWidget
  1076. default_error_messages = {
  1077. "invalid_date": _("Enter a valid date."),
  1078. "invalid_time": _("Enter a valid time."),
  1079. }
  1080. def __init__(self, *, input_date_formats=None, input_time_formats=None, **kwargs):
  1081. errors = self.default_error_messages.copy()
  1082. if "error_messages" in kwargs:
  1083. errors.update(kwargs["error_messages"])
  1084. localize = kwargs.get("localize", False)
  1085. fields = (
  1086. DateField(
  1087. input_formats=input_date_formats,
  1088. error_messages={"invalid": errors["invalid_date"]},
  1089. localize=localize,
  1090. ),
  1091. TimeField(
  1092. input_formats=input_time_formats,
  1093. error_messages={"invalid": errors["invalid_time"]},
  1094. localize=localize,
  1095. ),
  1096. )
  1097. super().__init__(fields, **kwargs)
  1098. def compress(self, data_list):
  1099. if data_list:
  1100. # Raise a validation error if time or date is empty
  1101. # (possible if SplitDateTimeField has required=False).
  1102. if data_list[0] in self.empty_values:
  1103. raise ValidationError(
  1104. self.error_messages["invalid_date"], code="invalid_date"
  1105. )
  1106. if data_list[1] in self.empty_values:
  1107. raise ValidationError(
  1108. self.error_messages["invalid_time"], code="invalid_time"
  1109. )
  1110. result = datetime.datetime.combine(*data_list)
  1111. return from_current_timezone(result)
  1112. return None
  1113. class GenericIPAddressField(CharField):
  1114. def __init__(self, *, protocol="both", unpack_ipv4=False, **kwargs):
  1115. self.unpack_ipv4 = unpack_ipv4
  1116. self.default_validators = validators.ip_address_validators(
  1117. protocol, unpack_ipv4
  1118. )[0]
  1119. kwargs.setdefault("max_length", MAX_IPV6_ADDRESS_LENGTH)
  1120. super().__init__(**kwargs)
  1121. def to_python(self, value):
  1122. if value in self.empty_values:
  1123. return ""
  1124. value = value.strip()
  1125. if value and ":" in value:
  1126. return clean_ipv6_address(
  1127. value, self.unpack_ipv4, max_length=self.max_length
  1128. )
  1129. return value
  1130. class SlugField(CharField):
  1131. default_validators = [validators.validate_slug]
  1132. def __init__(self, *, allow_unicode=False, **kwargs):
  1133. self.allow_unicode = allow_unicode
  1134. if self.allow_unicode:
  1135. self.default_validators = [validators.validate_unicode_slug]
  1136. super().__init__(**kwargs)
  1137. class UUIDField(CharField):
  1138. default_error_messages = {
  1139. "invalid": _("Enter a valid UUID."),
  1140. }
  1141. def prepare_value(self, value):
  1142. if isinstance(value, uuid.UUID):
  1143. return str(value)
  1144. return value
  1145. def to_python(self, value):
  1146. value = super().to_python(value)
  1147. if value in self.empty_values:
  1148. return None
  1149. if not isinstance(value, uuid.UUID):
  1150. try:
  1151. value = uuid.UUID(value)
  1152. except ValueError:
  1153. raise ValidationError(self.error_messages["invalid"], code="invalid")
  1154. return value
  1155. class InvalidJSONInput(str):
  1156. pass
  1157. class JSONString(str):
  1158. pass
  1159. class JSONField(CharField):
  1160. default_error_messages = {
  1161. "invalid": _("Enter a valid JSON."),
  1162. }
  1163. widget = Textarea
  1164. def __init__(self, encoder=None, decoder=None, **kwargs):
  1165. self.encoder = encoder
  1166. self.decoder = decoder
  1167. super().__init__(**kwargs)
  1168. def to_python(self, value):
  1169. if self.disabled:
  1170. return value
  1171. if value in self.empty_values:
  1172. return None
  1173. elif isinstance(value, (list, dict, int, float, JSONString)):
  1174. return value
  1175. try:
  1176. converted = json.loads(value, cls=self.decoder)
  1177. except json.JSONDecodeError:
  1178. raise ValidationError(
  1179. self.error_messages["invalid"],
  1180. code="invalid",
  1181. params={"value": value},
  1182. )
  1183. if isinstance(converted, str):
  1184. return JSONString(converted)
  1185. else:
  1186. return converted
  1187. def bound_data(self, data, initial):
  1188. if self.disabled:
  1189. return initial
  1190. if data is None:
  1191. return None
  1192. try:
  1193. return json.loads(data, cls=self.decoder)
  1194. except json.JSONDecodeError:
  1195. return InvalidJSONInput(data)
  1196. def prepare_value(self, value):
  1197. if isinstance(value, InvalidJSONInput):
  1198. return value
  1199. return json.dumps(value, ensure_ascii=False, cls=self.encoder)
  1200. def has_changed(self, initial, data):
  1201. if super().has_changed(initial, data):
  1202. return True
  1203. # For purposes of seeing whether something has changed, True isn't the
  1204. # same as 1 and the order of keys doesn't matter.
  1205. return json.dumps(initial, sort_keys=True, cls=self.encoder) != json.dumps(
  1206. self.to_python(data), sort_keys=True, cls=self.encoder
  1207. )