fields.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. import copy
  2. import datetime
  3. import decimal
  4. import functools
  5. import inspect
  6. import re
  7. import uuid
  8. from collections import OrderedDict
  9. from collections.abc import Mapping
  10. from django.conf import settings
  11. from django.core.exceptions import ObjectDoesNotExist
  12. from django.core.exceptions import ValidationError as DjangoValidationError
  13. from django.core.validators import (
  14. EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
  15. MinValueValidator, ProhibitNullCharactersValidator, RegexValidator,
  16. URLValidator, ip_address_validators
  17. )
  18. from django.forms import FilePathField as DjangoFilePathField
  19. from django.forms import ImageField as DjangoImageField
  20. from django.utils import timezone
  21. from django.utils.dateparse import (
  22. parse_date, parse_datetime, parse_duration, parse_time
  23. )
  24. from django.utils.duration import duration_string
  25. from django.utils.encoding import is_protected_type, smart_str
  26. from django.utils.formats import localize_input, sanitize_separators
  27. from django.utils.ipv6 import clean_ipv6_address
  28. from django.utils.translation import gettext_lazy as _
  29. from pytz.exceptions import InvalidTimeError
  30. from rest_framework import ISO_8601
  31. from rest_framework.exceptions import ErrorDetail, ValidationError
  32. from rest_framework.settings import api_settings
  33. from rest_framework.utils import html, humanize_datetime, json, representation
  34. from rest_framework.utils.formatting import lazy_format
  35. from rest_framework.validators import ProhibitSurrogateCharactersValidator
  36. class empty:
  37. """
  38. This class is used to represent no data being provided for a given input
  39. or output value.
  40. It is required because `None` may be a valid input or output value.
  41. """
  42. pass
  43. class BuiltinSignatureError(Exception):
  44. """
  45. Built-in function signatures are not inspectable. This exception is raised
  46. so the serializer can raise a helpful error message.
  47. """
  48. pass
  49. def is_simple_callable(obj):
  50. """
  51. True if the object is a callable that takes no arguments.
  52. """
  53. if not callable(obj):
  54. return False
  55. # Bail early since we cannot inspect built-in function signatures.
  56. if inspect.isbuiltin(obj):
  57. raise BuiltinSignatureError(
  58. 'Built-in function signatures are not inspectable. '
  59. 'Wrap the function call in a simple, pure Python function.')
  60. if not (inspect.isfunction(obj) or inspect.ismethod(obj) or isinstance(obj, functools.partial)):
  61. return False
  62. sig = inspect.signature(obj)
  63. params = sig.parameters.values()
  64. return all(
  65. param.kind == param.VAR_POSITIONAL or
  66. param.kind == param.VAR_KEYWORD or
  67. param.default != param.empty
  68. for param in params
  69. )
  70. def get_attribute(instance, attrs):
  71. """
  72. Similar to Python's built in `getattr(instance, attr)`,
  73. but takes a list of nested attributes, instead of a single attribute.
  74. Also accepts either attribute lookup on objects or dictionary lookups.
  75. """
  76. for attr in attrs:
  77. try:
  78. if isinstance(instance, Mapping):
  79. instance = instance[attr]
  80. else:
  81. instance = getattr(instance, attr)
  82. except ObjectDoesNotExist:
  83. return None
  84. if is_simple_callable(instance):
  85. try:
  86. instance = instance()
  87. except (AttributeError, KeyError) as exc:
  88. # If we raised an Attribute or KeyError here it'd get treated
  89. # as an omitted field in `Field.get_attribute()`. Instead we
  90. # raise a ValueError to ensure the exception is not masked.
  91. raise ValueError('Exception raised in callable attribute "{}"; original exception was: {}'.format(attr, exc))
  92. return instance
  93. def set_value(dictionary, keys, value):
  94. """
  95. Similar to Python's built in `dictionary[key] = value`,
  96. but takes a list of nested keys instead of a single key.
  97. set_value({'a': 1}, [], {'b': 2}) -> {'a': 1, 'b': 2}
  98. set_value({'a': 1}, ['x'], 2) -> {'a': 1, 'x': 2}
  99. set_value({'a': 1}, ['x', 'y'], 2) -> {'a': 1, 'x': {'y': 2}}
  100. """
  101. if not keys:
  102. dictionary.update(value)
  103. return
  104. for key in keys[:-1]:
  105. if key not in dictionary:
  106. dictionary[key] = {}
  107. dictionary = dictionary[key]
  108. dictionary[keys[-1]] = value
  109. def to_choices_dict(choices):
  110. """
  111. Convert choices into key/value dicts.
  112. to_choices_dict([1]) -> {1: 1}
  113. to_choices_dict([(1, '1st'), (2, '2nd')]) -> {1: '1st', 2: '2nd'}
  114. to_choices_dict([('Group', ((1, '1st'), 2))]) -> {'Group': {1: '1st', 2: '2'}}
  115. """
  116. # Allow single, paired or grouped choices style:
  117. # choices = [1, 2, 3]
  118. # choices = [(1, 'First'), (2, 'Second'), (3, 'Third')]
  119. # choices = [('Category', ((1, 'First'), (2, 'Second'))), (3, 'Third')]
  120. ret = OrderedDict()
  121. for choice in choices:
  122. if not isinstance(choice, (list, tuple)):
  123. # single choice
  124. ret[choice] = choice
  125. else:
  126. key, value = choice
  127. if isinstance(value, (list, tuple)):
  128. # grouped choices (category, sub choices)
  129. ret[key] = to_choices_dict(value)
  130. else:
  131. # paired choice (key, display value)
  132. ret[key] = value
  133. return ret
  134. def flatten_choices_dict(choices):
  135. """
  136. Convert a group choices dict into a flat dict of choices.
  137. flatten_choices_dict({1: '1st', 2: '2nd'}) -> {1: '1st', 2: '2nd'}
  138. flatten_choices_dict({'Group': {1: '1st', 2: '2nd'}}) -> {1: '1st', 2: '2nd'}
  139. """
  140. ret = OrderedDict()
  141. for key, value in choices.items():
  142. if isinstance(value, dict):
  143. # grouped choices (category, sub choices)
  144. for sub_key, sub_value in value.items():
  145. ret[sub_key] = sub_value
  146. else:
  147. # choice (key, display value)
  148. ret[key] = value
  149. return ret
  150. def iter_options(grouped_choices, cutoff=None, cutoff_text=None):
  151. """
  152. Helper function for options and option groups in templates.
  153. """
  154. class StartOptionGroup:
  155. start_option_group = True
  156. end_option_group = False
  157. def __init__(self, label):
  158. self.label = label
  159. class EndOptionGroup:
  160. start_option_group = False
  161. end_option_group = True
  162. class Option:
  163. start_option_group = False
  164. end_option_group = False
  165. def __init__(self, value, display_text, disabled=False):
  166. self.value = value
  167. self.display_text = display_text
  168. self.disabled = disabled
  169. count = 0
  170. for key, value in grouped_choices.items():
  171. if cutoff and count >= cutoff:
  172. break
  173. if isinstance(value, dict):
  174. yield StartOptionGroup(label=key)
  175. for sub_key, sub_value in value.items():
  176. if cutoff and count >= cutoff:
  177. break
  178. yield Option(value=sub_key, display_text=sub_value)
  179. count += 1
  180. yield EndOptionGroup()
  181. else:
  182. yield Option(value=key, display_text=value)
  183. count += 1
  184. if cutoff and count >= cutoff and cutoff_text:
  185. cutoff_text = cutoff_text.format(count=cutoff)
  186. yield Option(value='n/a', display_text=cutoff_text, disabled=True)
  187. def get_error_detail(exc_info):
  188. """
  189. Given a Django ValidationError, return a list of ErrorDetail,
  190. with the `code` populated.
  191. """
  192. code = getattr(exc_info, 'code', None) or 'invalid'
  193. try:
  194. error_dict = exc_info.error_dict
  195. except AttributeError:
  196. return [
  197. ErrorDetail((error.message % error.params) if error.params else error.message,
  198. code=error.code if error.code else code)
  199. for error in exc_info.error_list]
  200. return {
  201. k: [
  202. ErrorDetail((error.message % error.params) if error.params else error.message,
  203. code=error.code if error.code else code)
  204. for error in errors
  205. ] for k, errors in error_dict.items()
  206. }
  207. class CreateOnlyDefault:
  208. """
  209. This class may be used to provide default values that are only used
  210. for create operations, but that do not return any value for update
  211. operations.
  212. """
  213. requires_context = True
  214. def __init__(self, default):
  215. self.default = default
  216. def __call__(self, serializer_field):
  217. is_update = serializer_field.parent.instance is not None
  218. if is_update:
  219. raise SkipField()
  220. if callable(self.default):
  221. if getattr(self.default, 'requires_context', False):
  222. return self.default(serializer_field)
  223. else:
  224. return self.default()
  225. return self.default
  226. def __repr__(self):
  227. return '%s(%s)' % (self.__class__.__name__, repr(self.default))
  228. class CurrentUserDefault:
  229. requires_context = True
  230. def __call__(self, serializer_field):
  231. return serializer_field.context['request'].user
  232. def __repr__(self):
  233. return '%s()' % self.__class__.__name__
  234. class SkipField(Exception):
  235. pass
  236. REGEX_TYPE = type(re.compile(''))
  237. NOT_READ_ONLY_WRITE_ONLY = 'May not set both `read_only` and `write_only`'
  238. NOT_READ_ONLY_REQUIRED = 'May not set both `read_only` and `required`'
  239. NOT_REQUIRED_DEFAULT = 'May not set both `required` and `default`'
  240. USE_READONLYFIELD = 'Field(read_only=True) should be ReadOnlyField'
  241. MISSING_ERROR_MESSAGE = (
  242. 'ValidationError raised by `{class_name}`, but error key `{key}` does '
  243. 'not exist in the `error_messages` dictionary.'
  244. )
  245. class Field:
  246. _creation_counter = 0
  247. default_error_messages = {
  248. 'required': _('This field is required.'),
  249. 'null': _('This field may not be null.')
  250. }
  251. default_validators = []
  252. default_empty_html = empty
  253. initial = None
  254. def __init__(self, *, read_only=False, write_only=False,
  255. required=None, default=empty, initial=empty, source=None,
  256. label=None, help_text=None, style=None,
  257. error_messages=None, validators=None, allow_null=False):
  258. self._creation_counter = Field._creation_counter
  259. Field._creation_counter += 1
  260. # If `required` is unset, then use `True` unless a default is provided.
  261. if required is None:
  262. required = default is empty and not read_only
  263. # Some combinations of keyword arguments do not make sense.
  264. assert not (read_only and write_only), NOT_READ_ONLY_WRITE_ONLY
  265. assert not (read_only and required), NOT_READ_ONLY_REQUIRED
  266. assert not (required and default is not empty), NOT_REQUIRED_DEFAULT
  267. assert not (read_only and self.__class__ == Field), USE_READONLYFIELD
  268. self.read_only = read_only
  269. self.write_only = write_only
  270. self.required = required
  271. self.default = default
  272. self.source = source
  273. self.initial = self.initial if (initial is empty) else initial
  274. self.label = label
  275. self.help_text = help_text
  276. self.style = {} if style is None else style
  277. self.allow_null = allow_null
  278. if self.default_empty_html is not empty:
  279. if default is not empty:
  280. self.default_empty_html = default
  281. if validators is not None:
  282. self.validators = list(validators)
  283. # These are set up by `.bind()` when the field is added to a serializer.
  284. self.field_name = None
  285. self.parent = None
  286. # Collect default error message from self and parent classes
  287. messages = {}
  288. for cls in reversed(self.__class__.__mro__):
  289. messages.update(getattr(cls, 'default_error_messages', {}))
  290. messages.update(error_messages or {})
  291. self.error_messages = messages
  292. def bind(self, field_name, parent):
  293. """
  294. Initializes the field name and parent for the field instance.
  295. Called when a field is added to the parent serializer instance.
  296. """
  297. # In order to enforce a consistent style, we error if a redundant
  298. # 'source' argument has been used. For example:
  299. # my_field = serializer.CharField(source='my_field')
  300. assert self.source != field_name, (
  301. "It is redundant to specify `source='%s'` on field '%s' in "
  302. "serializer '%s', because it is the same as the field name. "
  303. "Remove the `source` keyword argument." %
  304. (field_name, self.__class__.__name__, parent.__class__.__name__)
  305. )
  306. self.field_name = field_name
  307. self.parent = parent
  308. # `self.label` should default to being based on the field name.
  309. if self.label is None:
  310. self.label = field_name.replace('_', ' ').capitalize()
  311. # self.source should default to being the same as the field name.
  312. if self.source is None:
  313. self.source = field_name
  314. # self.source_attrs is a list of attributes that need to be looked up
  315. # when serializing the instance, or populating the validated data.
  316. if self.source == '*':
  317. self.source_attrs = []
  318. else:
  319. self.source_attrs = self.source.split('.')
  320. # .validators is a lazily loaded property, that gets its default
  321. # value from `get_validators`.
  322. @property
  323. def validators(self):
  324. if not hasattr(self, '_validators'):
  325. self._validators = self.get_validators()
  326. return self._validators
  327. @validators.setter
  328. def validators(self, validators):
  329. self._validators = validators
  330. def get_validators(self):
  331. return list(self.default_validators)
  332. def get_initial(self):
  333. """
  334. Return a value to use when the field is being returned as a primitive
  335. value, without any object instance.
  336. """
  337. if callable(self.initial):
  338. return self.initial()
  339. return self.initial
  340. def get_value(self, dictionary):
  341. """
  342. Given the *incoming* primitive data, return the value for this field
  343. that should be validated and transformed to a native value.
  344. """
  345. if html.is_html_input(dictionary):
  346. # HTML forms will represent empty fields as '', and cannot
  347. # represent None or False values directly.
  348. if self.field_name not in dictionary:
  349. if getattr(self.root, 'partial', False):
  350. return empty
  351. return self.default_empty_html
  352. ret = dictionary[self.field_name]
  353. if ret == '' and self.allow_null:
  354. # If the field is blank, and null is a valid value then
  355. # determine if we should use null instead.
  356. return '' if getattr(self, 'allow_blank', False) else None
  357. elif ret == '' and not self.required:
  358. # If the field is blank, and emptiness is valid then
  359. # determine if we should use emptiness instead.
  360. return '' if getattr(self, 'allow_blank', False) else empty
  361. return ret
  362. return dictionary.get(self.field_name, empty)
  363. def get_attribute(self, instance):
  364. """
  365. Given the *outgoing* object instance, return the primitive value
  366. that should be used for this field.
  367. """
  368. try:
  369. return get_attribute(instance, self.source_attrs)
  370. except BuiltinSignatureError as exc:
  371. msg = (
  372. 'Field source for `{serializer}.{field}` maps to a built-in '
  373. 'function type and is invalid. Define a property or method on '
  374. 'the `{instance}` instance that wraps the call to the built-in '
  375. 'function.'.format(
  376. serializer=self.parent.__class__.__name__,
  377. field=self.field_name,
  378. instance=instance.__class__.__name__,
  379. )
  380. )
  381. raise type(exc)(msg)
  382. except (KeyError, AttributeError) as exc:
  383. if self.default is not empty:
  384. return self.get_default()
  385. if self.allow_null:
  386. return None
  387. if not self.required:
  388. raise SkipField()
  389. msg = (
  390. 'Got {exc_type} when attempting to get a value for field '
  391. '`{field}` on serializer `{serializer}`.\nThe serializer '
  392. 'field might be named incorrectly and not match '
  393. 'any attribute or key on the `{instance}` instance.\n'
  394. 'Original exception text was: {exc}.'.format(
  395. exc_type=type(exc).__name__,
  396. field=self.field_name,
  397. serializer=self.parent.__class__.__name__,
  398. instance=instance.__class__.__name__,
  399. exc=exc
  400. )
  401. )
  402. raise type(exc)(msg)
  403. def get_default(self):
  404. """
  405. Return the default value to use when validating data if no input
  406. is provided for this field.
  407. If a default has not been set for this field then this will simply
  408. raise `SkipField`, indicating that no value should be set in the
  409. validated data for this field.
  410. """
  411. if self.default is empty or getattr(self.root, 'partial', False):
  412. # No default, or this is a partial update.
  413. raise SkipField()
  414. if callable(self.default):
  415. if getattr(self.default, 'requires_context', False):
  416. return self.default(self)
  417. else:
  418. return self.default()
  419. return self.default
  420. def validate_empty_values(self, data):
  421. """
  422. Validate empty values, and either:
  423. * Raise `ValidationError`, indicating invalid data.
  424. * Raise `SkipField`, indicating that the field should be ignored.
  425. * Return (True, data), indicating an empty value that should be
  426. returned without any further validation being applied.
  427. * Return (False, data), indicating a non-empty value, that should
  428. have validation applied as normal.
  429. """
  430. if self.read_only:
  431. return (True, self.get_default())
  432. if data is empty:
  433. if getattr(self.root, 'partial', False):
  434. raise SkipField()
  435. if self.required:
  436. self.fail('required')
  437. return (True, self.get_default())
  438. if data is None:
  439. if not self.allow_null:
  440. self.fail('null')
  441. # Nullable `source='*'` fields should not be skipped when its named
  442. # field is given a null value. This is because `source='*'` means
  443. # the field is passed the entire object, which is not null.
  444. elif self.source == '*':
  445. return (False, None)
  446. return (True, None)
  447. return (False, data)
  448. def run_validation(self, data=empty):
  449. """
  450. Validate a simple representation and return the internal value.
  451. The provided data may be `empty` if no representation was included
  452. in the input.
  453. May raise `SkipField` if the field should not be included in the
  454. validated data.
  455. """
  456. (is_empty_value, data) = self.validate_empty_values(data)
  457. if is_empty_value:
  458. return data
  459. value = self.to_internal_value(data)
  460. self.run_validators(value)
  461. return value
  462. def run_validators(self, value):
  463. """
  464. Test the given value against all the validators on the field,
  465. and either raise a `ValidationError` or simply return.
  466. """
  467. errors = []
  468. for validator in self.validators:
  469. try:
  470. if getattr(validator, 'requires_context', False):
  471. validator(value, self)
  472. else:
  473. validator(value)
  474. except ValidationError as exc:
  475. # If the validation error contains a mapping of fields to
  476. # errors then simply raise it immediately rather than
  477. # attempting to accumulate a list of errors.
  478. if isinstance(exc.detail, dict):
  479. raise
  480. errors.extend(exc.detail)
  481. except DjangoValidationError as exc:
  482. errors.extend(get_error_detail(exc))
  483. if errors:
  484. raise ValidationError(errors)
  485. def to_internal_value(self, data):
  486. """
  487. Transform the *incoming* primitive data into a native value.
  488. """
  489. raise NotImplementedError(
  490. '{cls}.to_internal_value() must be implemented for field '
  491. '{field_name}. If you do not need to support write operations '
  492. 'you probably want to subclass `ReadOnlyField` instead.'.format(
  493. cls=self.__class__.__name__,
  494. field_name=self.field_name,
  495. )
  496. )
  497. def to_representation(self, value):
  498. """
  499. Transform the *outgoing* native value into primitive data.
  500. """
  501. raise NotImplementedError(
  502. '{cls}.to_representation() must be implemented for field {field_name}.'.format(
  503. cls=self.__class__.__name__,
  504. field_name=self.field_name,
  505. )
  506. )
  507. def fail(self, key, **kwargs):
  508. """
  509. A helper method that simply raises a validation error.
  510. """
  511. try:
  512. msg = self.error_messages[key]
  513. except KeyError:
  514. class_name = self.__class__.__name__
  515. msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key)
  516. raise AssertionError(msg)
  517. message_string = msg.format(**kwargs)
  518. raise ValidationError(message_string, code=key)
  519. @property
  520. def root(self):
  521. """
  522. Returns the top-level serializer for this field.
  523. """
  524. root = self
  525. while root.parent is not None:
  526. root = root.parent
  527. return root
  528. @property
  529. def context(self):
  530. """
  531. Returns the context as passed to the root serializer on initialization.
  532. """
  533. return getattr(self.root, '_context', {})
  534. def __new__(cls, *args, **kwargs):
  535. """
  536. When a field is instantiated, we store the arguments that were used,
  537. so that we can present a helpful representation of the object.
  538. """
  539. instance = super().__new__(cls)
  540. instance._args = args
  541. instance._kwargs = kwargs
  542. return instance
  543. def __deepcopy__(self, memo):
  544. """
  545. When cloning fields we instantiate using the arguments it was
  546. originally created with, rather than copying the complete state.
  547. """
  548. # Treat regexes and validators as immutable.
  549. # See https://github.com/encode/django-rest-framework/issues/1954
  550. # and https://github.com/encode/django-rest-framework/pull/4489
  551. args = [
  552. copy.deepcopy(item) if not isinstance(item, REGEX_TYPE) else item
  553. for item in self._args
  554. ]
  555. kwargs = {
  556. key: (copy.deepcopy(value, memo) if (key not in ('validators', 'regex')) else value)
  557. for key, value in self._kwargs.items()
  558. }
  559. return self.__class__(*args, **kwargs)
  560. def __repr__(self):
  561. """
  562. Fields are represented using their initial calling arguments.
  563. This allows us to create descriptive representations for serializer
  564. instances that show all the declared fields on the serializer.
  565. """
  566. return representation.field_repr(self)
  567. # Boolean types...
  568. class BooleanField(Field):
  569. default_error_messages = {
  570. 'invalid': _('Must be a valid boolean.')
  571. }
  572. default_empty_html = False
  573. initial = False
  574. TRUE_VALUES = {
  575. 't', 'T',
  576. 'y', 'Y', 'yes', 'Yes', 'YES',
  577. 'true', 'True', 'TRUE',
  578. 'on', 'On', 'ON',
  579. '1', 1,
  580. True
  581. }
  582. FALSE_VALUES = {
  583. 'f', 'F',
  584. 'n', 'N', 'no', 'No', 'NO',
  585. 'false', 'False', 'FALSE',
  586. 'off', 'Off', 'OFF',
  587. '0', 0, 0.0,
  588. False
  589. }
  590. NULL_VALUES = {'null', 'Null', 'NULL', '', None}
  591. def to_internal_value(self, data):
  592. try:
  593. if data in self.TRUE_VALUES:
  594. return True
  595. elif data in self.FALSE_VALUES:
  596. return False
  597. elif data in self.NULL_VALUES and self.allow_null:
  598. return None
  599. except TypeError: # Input is an unhashable type
  600. pass
  601. self.fail('invalid', input=data)
  602. def to_representation(self, value):
  603. if value in self.TRUE_VALUES:
  604. return True
  605. elif value in self.FALSE_VALUES:
  606. return False
  607. if value in self.NULL_VALUES and self.allow_null:
  608. return None
  609. return bool(value)
  610. # String types...
  611. class CharField(Field):
  612. default_error_messages = {
  613. 'invalid': _('Not a valid string.'),
  614. 'blank': _('This field may not be blank.'),
  615. 'max_length': _('Ensure this field has no more than {max_length} characters.'),
  616. 'min_length': _('Ensure this field has at least {min_length} characters.'),
  617. }
  618. initial = ''
  619. def __init__(self, **kwargs):
  620. self.allow_blank = kwargs.pop('allow_blank', False)
  621. self.trim_whitespace = kwargs.pop('trim_whitespace', True)
  622. self.max_length = kwargs.pop('max_length', None)
  623. self.min_length = kwargs.pop('min_length', None)
  624. super().__init__(**kwargs)
  625. if self.max_length is not None:
  626. message = lazy_format(self.error_messages['max_length'], max_length=self.max_length)
  627. self.validators.append(
  628. MaxLengthValidator(self.max_length, message=message))
  629. if self.min_length is not None:
  630. message = lazy_format(self.error_messages['min_length'], min_length=self.min_length)
  631. self.validators.append(
  632. MinLengthValidator(self.min_length, message=message))
  633. self.validators.append(ProhibitNullCharactersValidator())
  634. self.validators.append(ProhibitSurrogateCharactersValidator())
  635. def run_validation(self, data=empty):
  636. # Test for the empty string here so that it does not get validated,
  637. # and so that subclasses do not need to handle it explicitly
  638. # inside the `to_internal_value()` method.
  639. if data == '' or (self.trim_whitespace and str(data).strip() == ''):
  640. if not self.allow_blank:
  641. self.fail('blank')
  642. return ''
  643. return super().run_validation(data)
  644. def to_internal_value(self, data):
  645. # We're lenient with allowing basic numerics to be coerced into strings,
  646. # but other types should fail. Eg. unclear if booleans should represent as `true` or `True`,
  647. # and composites such as lists are likely user error.
  648. if isinstance(data, bool) or not isinstance(data, (str, int, float,)):
  649. self.fail('invalid')
  650. value = str(data)
  651. return value.strip() if self.trim_whitespace else value
  652. def to_representation(self, value):
  653. return str(value)
  654. class EmailField(CharField):
  655. default_error_messages = {
  656. 'invalid': _('Enter a valid email address.')
  657. }
  658. def __init__(self, **kwargs):
  659. super().__init__(**kwargs)
  660. validator = EmailValidator(message=self.error_messages['invalid'])
  661. self.validators.append(validator)
  662. class RegexField(CharField):
  663. default_error_messages = {
  664. 'invalid': _('This value does not match the required pattern.')
  665. }
  666. def __init__(self, regex, **kwargs):
  667. super().__init__(**kwargs)
  668. validator = RegexValidator(regex, message=self.error_messages['invalid'])
  669. self.validators.append(validator)
  670. class SlugField(CharField):
  671. default_error_messages = {
  672. 'invalid': _('Enter a valid "slug" consisting of letters, numbers, underscores or hyphens.'),
  673. 'invalid_unicode': _('Enter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.')
  674. }
  675. def __init__(self, allow_unicode=False, **kwargs):
  676. super().__init__(**kwargs)
  677. self.allow_unicode = allow_unicode
  678. if self.allow_unicode:
  679. validator = RegexValidator(re.compile(r'^[-\w]+\Z', re.UNICODE), message=self.error_messages['invalid_unicode'])
  680. else:
  681. validator = RegexValidator(re.compile(r'^[-a-zA-Z0-9_]+$'), message=self.error_messages['invalid'])
  682. self.validators.append(validator)
  683. class URLField(CharField):
  684. default_error_messages = {
  685. 'invalid': _('Enter a valid URL.')
  686. }
  687. def __init__(self, **kwargs):
  688. super().__init__(**kwargs)
  689. validator = URLValidator(message=self.error_messages['invalid'])
  690. self.validators.append(validator)
  691. class UUIDField(Field):
  692. valid_formats = ('hex_verbose', 'hex', 'int', 'urn')
  693. default_error_messages = {
  694. 'invalid': _('Must be a valid UUID.'),
  695. }
  696. def __init__(self, **kwargs):
  697. self.uuid_format = kwargs.pop('format', 'hex_verbose')
  698. if self.uuid_format not in self.valid_formats:
  699. raise ValueError(
  700. 'Invalid format for uuid representation. '
  701. 'Must be one of "{}"'.format('", "'.join(self.valid_formats))
  702. )
  703. super().__init__(**kwargs)
  704. def to_internal_value(self, data):
  705. if not isinstance(data, uuid.UUID):
  706. try:
  707. if isinstance(data, int):
  708. return uuid.UUID(int=data)
  709. elif isinstance(data, str):
  710. return uuid.UUID(hex=data)
  711. else:
  712. self.fail('invalid', value=data)
  713. except (ValueError):
  714. self.fail('invalid', value=data)
  715. return data
  716. def to_representation(self, value):
  717. if self.uuid_format == 'hex_verbose':
  718. return str(value)
  719. else:
  720. return getattr(value, self.uuid_format)
  721. class IPAddressField(CharField):
  722. """Support both IPAddressField and GenericIPAddressField"""
  723. default_error_messages = {
  724. 'invalid': _('Enter a valid IPv4 or IPv6 address.'),
  725. }
  726. def __init__(self, protocol='both', **kwargs):
  727. self.protocol = protocol.lower()
  728. self.unpack_ipv4 = (self.protocol == 'both')
  729. super().__init__(**kwargs)
  730. validators, error_message = ip_address_validators(protocol, self.unpack_ipv4)
  731. self.validators.extend(validators)
  732. def to_internal_value(self, data):
  733. if not isinstance(data, str):
  734. self.fail('invalid', value=data)
  735. if ':' in data:
  736. try:
  737. if self.protocol in ('both', 'ipv6'):
  738. return clean_ipv6_address(data, self.unpack_ipv4)
  739. except DjangoValidationError:
  740. self.fail('invalid', value=data)
  741. return super().to_internal_value(data)
  742. # Number types...
  743. class IntegerField(Field):
  744. default_error_messages = {
  745. 'invalid': _('A valid integer is required.'),
  746. 'max_value': _('Ensure this value is less than or equal to {max_value}.'),
  747. 'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
  748. 'max_string_length': _('String value too large.')
  749. }
  750. MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
  751. re_decimal = re.compile(r'\.0*\s*$') # allow e.g. '1.0' as an int, but not '1.2'
  752. def __init__(self, **kwargs):
  753. self.max_value = kwargs.pop('max_value', None)
  754. self.min_value = kwargs.pop('min_value', None)
  755. super().__init__(**kwargs)
  756. if self.max_value is not None:
  757. message = lazy_format(self.error_messages['max_value'], max_value=self.max_value)
  758. self.validators.append(
  759. MaxValueValidator(self.max_value, message=message))
  760. if self.min_value is not None:
  761. message = lazy_format(self.error_messages['min_value'], min_value=self.min_value)
  762. self.validators.append(
  763. MinValueValidator(self.min_value, message=message))
  764. def to_internal_value(self, data):
  765. if isinstance(data, str) and len(data) > self.MAX_STRING_LENGTH:
  766. self.fail('max_string_length')
  767. try:
  768. data = int(self.re_decimal.sub('', str(data)))
  769. except (ValueError, TypeError):
  770. self.fail('invalid')
  771. return data
  772. def to_representation(self, value):
  773. return int(value)
  774. class FloatField(Field):
  775. default_error_messages = {
  776. 'invalid': _('A valid number is required.'),
  777. 'max_value': _('Ensure this value is less than or equal to {max_value}.'),
  778. 'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
  779. 'max_string_length': _('String value too large.')
  780. }
  781. MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
  782. def __init__(self, **kwargs):
  783. self.max_value = kwargs.pop('max_value', None)
  784. self.min_value = kwargs.pop('min_value', None)
  785. super().__init__(**kwargs)
  786. if self.max_value is not None:
  787. message = lazy_format(self.error_messages['max_value'], max_value=self.max_value)
  788. self.validators.append(
  789. MaxValueValidator(self.max_value, message=message))
  790. if self.min_value is not None:
  791. message = lazy_format(self.error_messages['min_value'], min_value=self.min_value)
  792. self.validators.append(
  793. MinValueValidator(self.min_value, message=message))
  794. def to_internal_value(self, data):
  795. if isinstance(data, str) and len(data) > self.MAX_STRING_LENGTH:
  796. self.fail('max_string_length')
  797. try:
  798. return float(data)
  799. except (TypeError, ValueError):
  800. self.fail('invalid')
  801. def to_representation(self, value):
  802. return float(value)
  803. class DecimalField(Field):
  804. default_error_messages = {
  805. 'invalid': _('A valid number is required.'),
  806. 'max_value': _('Ensure this value is less than or equal to {max_value}.'),
  807. 'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
  808. 'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'),
  809. 'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'),
  810. 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.'),
  811. 'max_string_length': _('String value too large.')
  812. }
  813. MAX_STRING_LENGTH = 1000 # Guard against malicious string inputs.
  814. def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None,
  815. localize=False, rounding=None, **kwargs):
  816. self.max_digits = max_digits
  817. self.decimal_places = decimal_places
  818. self.localize = localize
  819. if coerce_to_string is not None:
  820. self.coerce_to_string = coerce_to_string
  821. if self.localize:
  822. self.coerce_to_string = True
  823. self.max_value = max_value
  824. self.min_value = min_value
  825. if self.max_digits is not None and self.decimal_places is not None:
  826. self.max_whole_digits = self.max_digits - self.decimal_places
  827. else:
  828. self.max_whole_digits = None
  829. super().__init__(**kwargs)
  830. if self.max_value is not None:
  831. message = lazy_format(self.error_messages['max_value'], max_value=self.max_value)
  832. self.validators.append(
  833. MaxValueValidator(self.max_value, message=message))
  834. if self.min_value is not None:
  835. message = lazy_format(self.error_messages['min_value'], min_value=self.min_value)
  836. self.validators.append(
  837. MinValueValidator(self.min_value, message=message))
  838. if rounding is not None:
  839. valid_roundings = [v for k, v in vars(decimal).items() if k.startswith('ROUND_')]
  840. assert rounding in valid_roundings, (
  841. 'Invalid rounding option %s. Valid values for rounding are: %s' % (rounding, valid_roundings))
  842. self.rounding = rounding
  843. def validate_empty_values(self, data):
  844. if smart_str(data).strip() == '' and self.allow_null:
  845. return (True, None)
  846. return super().validate_empty_values(data)
  847. def to_internal_value(self, data):
  848. """
  849. Validate that the input is a decimal number and return a Decimal
  850. instance.
  851. """
  852. data = smart_str(data).strip()
  853. if self.localize:
  854. data = sanitize_separators(data)
  855. if len(data) > self.MAX_STRING_LENGTH:
  856. self.fail('max_string_length')
  857. try:
  858. value = decimal.Decimal(data)
  859. except decimal.DecimalException:
  860. self.fail('invalid')
  861. if value.is_nan():
  862. self.fail('invalid')
  863. # Check for infinity and negative infinity.
  864. if value in (decimal.Decimal('Inf'), decimal.Decimal('-Inf')):
  865. self.fail('invalid')
  866. return self.quantize(self.validate_precision(value))
  867. def validate_precision(self, value):
  868. """
  869. Ensure that there are no more than max_digits in the number, and no
  870. more than decimal_places digits after the decimal point.
  871. Override this method to disable the precision validation for input
  872. values or to enhance it in any way you need to.
  873. """
  874. sign, digittuple, exponent = value.as_tuple()
  875. if exponent >= 0:
  876. # 1234500.0
  877. total_digits = len(digittuple) + exponent
  878. whole_digits = total_digits
  879. decimal_places = 0
  880. elif len(digittuple) > abs(exponent):
  881. # 123.45
  882. total_digits = len(digittuple)
  883. whole_digits = total_digits - abs(exponent)
  884. decimal_places = abs(exponent)
  885. else:
  886. # 0.001234
  887. total_digits = abs(exponent)
  888. whole_digits = 0
  889. decimal_places = total_digits
  890. if self.max_digits is not None and total_digits > self.max_digits:
  891. self.fail('max_digits', max_digits=self.max_digits)
  892. if self.decimal_places is not None and decimal_places > self.decimal_places:
  893. self.fail('max_decimal_places', max_decimal_places=self.decimal_places)
  894. if self.max_whole_digits is not None and whole_digits > self.max_whole_digits:
  895. self.fail('max_whole_digits', max_whole_digits=self.max_whole_digits)
  896. return value
  897. def to_representation(self, value):
  898. coerce_to_string = getattr(self, 'coerce_to_string', api_settings.COERCE_DECIMAL_TO_STRING)
  899. if value is None:
  900. if coerce_to_string:
  901. return ''
  902. else:
  903. return None
  904. if not isinstance(value, decimal.Decimal):
  905. value = decimal.Decimal(str(value).strip())
  906. quantized = self.quantize(value)
  907. if not coerce_to_string:
  908. return quantized
  909. if self.localize:
  910. return localize_input(quantized)
  911. return '{:f}'.format(quantized)
  912. def quantize(self, value):
  913. """
  914. Quantize the decimal value to the configured precision.
  915. """
  916. if self.decimal_places is None:
  917. return value
  918. context = decimal.getcontext().copy()
  919. if self.max_digits is not None:
  920. context.prec = self.max_digits
  921. return value.quantize(
  922. decimal.Decimal('.1') ** self.decimal_places,
  923. rounding=self.rounding,
  924. context=context
  925. )
  926. # Date & time fields...
  927. class DateTimeField(Field):
  928. default_error_messages = {
  929. 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}.'),
  930. 'date': _('Expected a datetime but got a date.'),
  931. 'make_aware': _('Invalid datetime for the timezone "{timezone}".'),
  932. 'overflow': _('Datetime value out of range.')
  933. }
  934. datetime_parser = datetime.datetime.strptime
  935. def __init__(self, format=empty, input_formats=None, default_timezone=None, **kwargs):
  936. if format is not empty:
  937. self.format = format
  938. if input_formats is not None:
  939. self.input_formats = input_formats
  940. if default_timezone is not None:
  941. self.timezone = default_timezone
  942. super().__init__(**kwargs)
  943. def enforce_timezone(self, value):
  944. """
  945. When `self.default_timezone` is `None`, always return naive datetimes.
  946. When `self.default_timezone` is not `None`, always return aware datetimes.
  947. """
  948. field_timezone = self.timezone if hasattr(self, 'timezone') else self.default_timezone()
  949. if field_timezone is not None:
  950. if timezone.is_aware(value):
  951. try:
  952. return value.astimezone(field_timezone)
  953. except OverflowError:
  954. self.fail('overflow')
  955. try:
  956. return timezone.make_aware(value, field_timezone)
  957. except InvalidTimeError:
  958. self.fail('make_aware', timezone=field_timezone)
  959. elif (field_timezone is None) and timezone.is_aware(value):
  960. return timezone.make_naive(value, datetime.timezone.utc)
  961. return value
  962. def default_timezone(self):
  963. return timezone.get_current_timezone() if settings.USE_TZ else None
  964. def to_internal_value(self, value):
  965. input_formats = getattr(self, 'input_formats', api_settings.DATETIME_INPUT_FORMATS)
  966. if isinstance(value, datetime.date) and not isinstance(value, datetime.datetime):
  967. self.fail('date')
  968. if isinstance(value, datetime.datetime):
  969. return self.enforce_timezone(value)
  970. for input_format in input_formats:
  971. if input_format.lower() == ISO_8601:
  972. try:
  973. parsed = parse_datetime(value)
  974. if parsed is not None:
  975. return self.enforce_timezone(parsed)
  976. except (ValueError, TypeError):
  977. pass
  978. else:
  979. try:
  980. parsed = self.datetime_parser(value, input_format)
  981. return self.enforce_timezone(parsed)
  982. except (ValueError, TypeError):
  983. pass
  984. humanized_format = humanize_datetime.datetime_formats(input_formats)
  985. self.fail('invalid', format=humanized_format)
  986. def to_representation(self, value):
  987. if not value:
  988. return None
  989. output_format = getattr(self, 'format', api_settings.DATETIME_FORMAT)
  990. if output_format is None or isinstance(value, str):
  991. return value
  992. value = self.enforce_timezone(value)
  993. if output_format.lower() == ISO_8601:
  994. value = value.isoformat()
  995. if value.endswith('+00:00'):
  996. value = value[:-6] + 'Z'
  997. return value
  998. return value.strftime(output_format)
  999. class DateField(Field):
  1000. default_error_messages = {
  1001. 'invalid': _('Date has wrong format. Use one of these formats instead: {format}.'),
  1002. 'datetime': _('Expected a date but got a datetime.'),
  1003. }
  1004. datetime_parser = datetime.datetime.strptime
  1005. def __init__(self, format=empty, input_formats=None, **kwargs):
  1006. if format is not empty:
  1007. self.format = format
  1008. if input_formats is not None:
  1009. self.input_formats = input_formats
  1010. super().__init__(**kwargs)
  1011. def to_internal_value(self, value):
  1012. input_formats = getattr(self, 'input_formats', api_settings.DATE_INPUT_FORMATS)
  1013. if isinstance(value, datetime.datetime):
  1014. self.fail('datetime')
  1015. if isinstance(value, datetime.date):
  1016. return value
  1017. for input_format in input_formats:
  1018. if input_format.lower() == ISO_8601:
  1019. try:
  1020. parsed = parse_date(value)
  1021. except (ValueError, TypeError):
  1022. pass
  1023. else:
  1024. if parsed is not None:
  1025. return parsed
  1026. else:
  1027. try:
  1028. parsed = self.datetime_parser(value, input_format)
  1029. except (ValueError, TypeError):
  1030. pass
  1031. else:
  1032. return parsed.date()
  1033. humanized_format = humanize_datetime.date_formats(input_formats)
  1034. self.fail('invalid', format=humanized_format)
  1035. def to_representation(self, value):
  1036. if not value:
  1037. return None
  1038. output_format = getattr(self, 'format', api_settings.DATE_FORMAT)
  1039. if output_format is None or isinstance(value, str):
  1040. return value
  1041. # Applying a `DateField` to a datetime value is almost always
  1042. # not a sensible thing to do, as it means naively dropping
  1043. # any explicit or implicit timezone info.
  1044. assert not isinstance(value, datetime.datetime), (
  1045. 'Expected a `date`, but got a `datetime`. Refusing to coerce, '
  1046. 'as this may mean losing timezone information. Use a custom '
  1047. 'read-only field and deal with timezone issues explicitly.'
  1048. )
  1049. if output_format.lower() == ISO_8601:
  1050. return value.isoformat()
  1051. return value.strftime(output_format)
  1052. class TimeField(Field):
  1053. default_error_messages = {
  1054. 'invalid': _('Time has wrong format. Use one of these formats instead: {format}.'),
  1055. }
  1056. datetime_parser = datetime.datetime.strptime
  1057. def __init__(self, format=empty, input_formats=None, **kwargs):
  1058. if format is not empty:
  1059. self.format = format
  1060. if input_formats is not None:
  1061. self.input_formats = input_formats
  1062. super().__init__(**kwargs)
  1063. def to_internal_value(self, value):
  1064. input_formats = getattr(self, 'input_formats', api_settings.TIME_INPUT_FORMATS)
  1065. if isinstance(value, datetime.time):
  1066. return value
  1067. for input_format in input_formats:
  1068. if input_format.lower() == ISO_8601:
  1069. try:
  1070. parsed = parse_time(value)
  1071. except (ValueError, TypeError):
  1072. pass
  1073. else:
  1074. if parsed is not None:
  1075. return parsed
  1076. else:
  1077. try:
  1078. parsed = self.datetime_parser(value, input_format)
  1079. except (ValueError, TypeError):
  1080. pass
  1081. else:
  1082. return parsed.time()
  1083. humanized_format = humanize_datetime.time_formats(input_formats)
  1084. self.fail('invalid', format=humanized_format)
  1085. def to_representation(self, value):
  1086. if value in (None, ''):
  1087. return None
  1088. output_format = getattr(self, 'format', api_settings.TIME_FORMAT)
  1089. if output_format is None or isinstance(value, str):
  1090. return value
  1091. # Applying a `TimeField` to a datetime value is almost always
  1092. # not a sensible thing to do, as it means naively dropping
  1093. # any explicit or implicit timezone info.
  1094. assert not isinstance(value, datetime.datetime), (
  1095. 'Expected a `time`, but got a `datetime`. Refusing to coerce, '
  1096. 'as this may mean losing timezone information. Use a custom '
  1097. 'read-only field and deal with timezone issues explicitly.'
  1098. )
  1099. if output_format.lower() == ISO_8601:
  1100. return value.isoformat()
  1101. return value.strftime(output_format)
  1102. class DurationField(Field):
  1103. default_error_messages = {
  1104. 'invalid': _('Duration has wrong format. Use one of these formats instead: {format}.'),
  1105. 'max_value': _('Ensure this value is less than or equal to {max_value}.'),
  1106. 'min_value': _('Ensure this value is greater than or equal to {min_value}.'),
  1107. }
  1108. def __init__(self, **kwargs):
  1109. self.max_value = kwargs.pop('max_value', None)
  1110. self.min_value = kwargs.pop('min_value', None)
  1111. super().__init__(**kwargs)
  1112. if self.max_value is not None:
  1113. message = lazy_format(self.error_messages['max_value'], max_value=self.max_value)
  1114. self.validators.append(
  1115. MaxValueValidator(self.max_value, message=message))
  1116. if self.min_value is not None:
  1117. message = lazy_format(self.error_messages['min_value'], min_value=self.min_value)
  1118. self.validators.append(
  1119. MinValueValidator(self.min_value, message=message))
  1120. def to_internal_value(self, value):
  1121. if isinstance(value, datetime.timedelta):
  1122. return value
  1123. parsed = parse_duration(str(value))
  1124. if parsed is not None:
  1125. return parsed
  1126. self.fail('invalid', format='[DD] [HH:[MM:]]ss[.uuuuuu]')
  1127. def to_representation(self, value):
  1128. return duration_string(value)
  1129. # Choice types...
  1130. class ChoiceField(Field):
  1131. default_error_messages = {
  1132. 'invalid_choice': _('"{input}" is not a valid choice.')
  1133. }
  1134. html_cutoff = None
  1135. html_cutoff_text = _('More than {count} items...')
  1136. def __init__(self, choices, **kwargs):
  1137. self.choices = choices
  1138. self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
  1139. self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)
  1140. self.allow_blank = kwargs.pop('allow_blank', False)
  1141. super().__init__(**kwargs)
  1142. def to_internal_value(self, data):
  1143. if data == '' and self.allow_blank:
  1144. return ''
  1145. try:
  1146. return self.choice_strings_to_values[str(data)]
  1147. except KeyError:
  1148. self.fail('invalid_choice', input=data)
  1149. def to_representation(self, value):
  1150. if value in ('', None):
  1151. return value
  1152. return self.choice_strings_to_values.get(str(value), value)
  1153. def iter_options(self):
  1154. """
  1155. Helper method for use with templates rendering select widgets.
  1156. """
  1157. return iter_options(
  1158. self.grouped_choices,
  1159. cutoff=self.html_cutoff,
  1160. cutoff_text=self.html_cutoff_text
  1161. )
  1162. def _get_choices(self):
  1163. return self._choices
  1164. def _set_choices(self, choices):
  1165. self.grouped_choices = to_choices_dict(choices)
  1166. self._choices = flatten_choices_dict(self.grouped_choices)
  1167. # Map the string representation of choices to the underlying value.
  1168. # Allows us to deal with eg. integer choices while supporting either
  1169. # integer or string input, but still get the correct datatype out.
  1170. self.choice_strings_to_values = {
  1171. str(key): key for key in self.choices
  1172. }
  1173. choices = property(_get_choices, _set_choices)
  1174. class MultipleChoiceField(ChoiceField):
  1175. default_error_messages = {
  1176. 'invalid_choice': _('"{input}" is not a valid choice.'),
  1177. 'not_a_list': _('Expected a list of items but got type "{input_type}".'),
  1178. 'empty': _('This selection may not be empty.')
  1179. }
  1180. default_empty_html = []
  1181. def __init__(self, **kwargs):
  1182. self.allow_empty = kwargs.pop('allow_empty', True)
  1183. super().__init__(**kwargs)
  1184. def get_value(self, dictionary):
  1185. if self.field_name not in dictionary:
  1186. if getattr(self.root, 'partial', False):
  1187. return empty
  1188. # We override the default field access in order to support
  1189. # lists in HTML forms.
  1190. if html.is_html_input(dictionary):
  1191. return dictionary.getlist(self.field_name)
  1192. return dictionary.get(self.field_name, empty)
  1193. def to_internal_value(self, data):
  1194. if isinstance(data, str) or not hasattr(data, '__iter__'):
  1195. self.fail('not_a_list', input_type=type(data).__name__)
  1196. if not self.allow_empty and len(data) == 0:
  1197. self.fail('empty')
  1198. return {
  1199. # Arguments for super() are needed because of scoping inside
  1200. # comprehensions.
  1201. super(MultipleChoiceField, self).to_internal_value(item)
  1202. for item in data
  1203. }
  1204. def to_representation(self, value):
  1205. return {
  1206. self.choice_strings_to_values.get(str(item), item) for item in value
  1207. }
  1208. class FilePathField(ChoiceField):
  1209. default_error_messages = {
  1210. 'invalid_choice': _('"{input}" is not a valid path choice.')
  1211. }
  1212. def __init__(self, path, match=None, recursive=False, allow_files=True,
  1213. allow_folders=False, required=None, **kwargs):
  1214. # Defer to Django's FilePathField implementation to get the
  1215. # valid set of choices.
  1216. field = DjangoFilePathField(
  1217. path, match=match, recursive=recursive, allow_files=allow_files,
  1218. allow_folders=allow_folders, required=required
  1219. )
  1220. kwargs['choices'] = field.choices
  1221. super().__init__(**kwargs)
  1222. # File types...
  1223. class FileField(Field):
  1224. default_error_messages = {
  1225. 'required': _('No file was submitted.'),
  1226. 'invalid': _('The submitted data was not a file. Check the encoding type on the form.'),
  1227. 'no_name': _('No filename could be determined.'),
  1228. 'empty': _('The submitted file is empty.'),
  1229. 'max_length': _('Ensure this filename has at most {max_length} characters (it has {length}).'),
  1230. }
  1231. def __init__(self, **kwargs):
  1232. self.max_length = kwargs.pop('max_length', None)
  1233. self.allow_empty_file = kwargs.pop('allow_empty_file', False)
  1234. if 'use_url' in kwargs:
  1235. self.use_url = kwargs.pop('use_url')
  1236. super().__init__(**kwargs)
  1237. def to_internal_value(self, data):
  1238. try:
  1239. # `UploadedFile` objects should have name and size attributes.
  1240. file_name = data.name
  1241. file_size = data.size
  1242. except AttributeError:
  1243. self.fail('invalid')
  1244. if not file_name:
  1245. self.fail('no_name')
  1246. if not self.allow_empty_file and not file_size:
  1247. self.fail('empty')
  1248. if self.max_length and len(file_name) > self.max_length:
  1249. self.fail('max_length', max_length=self.max_length, length=len(file_name))
  1250. return data
  1251. def to_representation(self, value):
  1252. if not value:
  1253. return None
  1254. use_url = getattr(self, 'use_url', api_settings.UPLOADED_FILES_USE_URL)
  1255. if use_url:
  1256. try:
  1257. url = value.url
  1258. except AttributeError:
  1259. return None
  1260. request = self.context.get('request', None)
  1261. if request is not None:
  1262. return request.build_absolute_uri(url)
  1263. return url
  1264. return value.name
  1265. class ImageField(FileField):
  1266. default_error_messages = {
  1267. 'invalid_image': _(
  1268. 'Upload a valid image. The file you uploaded was either not an image or a corrupted image.'
  1269. ),
  1270. }
  1271. def __init__(self, **kwargs):
  1272. self._DjangoImageField = kwargs.pop('_DjangoImageField', DjangoImageField)
  1273. super().__init__(**kwargs)
  1274. def to_internal_value(self, data):
  1275. # Image validation is a bit grungy, so we'll just outright
  1276. # defer to Django's implementation so we don't need to
  1277. # consider it, or treat PIL as a test dependency.
  1278. file_object = super().to_internal_value(data)
  1279. django_field = self._DjangoImageField()
  1280. django_field.error_messages = self.error_messages
  1281. return django_field.clean(file_object)
  1282. # Composite field types...
  1283. class _UnvalidatedField(Field):
  1284. def __init__(self, **kwargs):
  1285. super().__init__(**kwargs)
  1286. self.allow_blank = True
  1287. self.allow_null = True
  1288. def to_internal_value(self, data):
  1289. return data
  1290. def to_representation(self, value):
  1291. return value
  1292. class ListField(Field):
  1293. child = _UnvalidatedField()
  1294. initial = []
  1295. default_error_messages = {
  1296. 'not_a_list': _('Expected a list of items but got type "{input_type}".'),
  1297. 'empty': _('This list may not be empty.'),
  1298. 'min_length': _('Ensure this field has at least {min_length} elements.'),
  1299. 'max_length': _('Ensure this field has no more than {max_length} elements.')
  1300. }
  1301. def __init__(self, **kwargs):
  1302. self.child = kwargs.pop('child', copy.deepcopy(self.child))
  1303. self.allow_empty = kwargs.pop('allow_empty', True)
  1304. self.max_length = kwargs.pop('max_length', None)
  1305. self.min_length = kwargs.pop('min_length', None)
  1306. assert not inspect.isclass(self.child), '`child` has not been instantiated.'
  1307. assert self.child.source is None, (
  1308. "The `source` argument is not meaningful when applied to a `child=` field. "
  1309. "Remove `source=` from the field declaration."
  1310. )
  1311. super().__init__(**kwargs)
  1312. self.child.bind(field_name='', parent=self)
  1313. if self.max_length is not None:
  1314. message = lazy_format(self.error_messages['max_length'], max_length=self.max_length)
  1315. self.validators.append(MaxLengthValidator(self.max_length, message=message))
  1316. if self.min_length is not None:
  1317. message = lazy_format(self.error_messages['min_length'], min_length=self.min_length)
  1318. self.validators.append(MinLengthValidator(self.min_length, message=message))
  1319. def get_value(self, dictionary):
  1320. if self.field_name not in dictionary:
  1321. if getattr(self.root, 'partial', False):
  1322. return empty
  1323. # We override the default field access in order to support
  1324. # lists in HTML forms.
  1325. if html.is_html_input(dictionary):
  1326. val = dictionary.getlist(self.field_name, [])
  1327. if len(val) > 0:
  1328. # Support QueryDict lists in HTML input.
  1329. return val
  1330. return html.parse_html_list(dictionary, prefix=self.field_name, default=empty)
  1331. return dictionary.get(self.field_name, empty)
  1332. def to_internal_value(self, data):
  1333. """
  1334. List of dicts of native values <- List of dicts of primitive datatypes.
  1335. """
  1336. if html.is_html_input(data):
  1337. data = html.parse_html_list(data, default=[])
  1338. if isinstance(data, (str, Mapping)) or not hasattr(data, '__iter__'):
  1339. self.fail('not_a_list', input_type=type(data).__name__)
  1340. if not self.allow_empty and len(data) == 0:
  1341. self.fail('empty')
  1342. return self.run_child_validation(data)
  1343. def to_representation(self, data):
  1344. """
  1345. List of object instances -> List of dicts of primitive datatypes.
  1346. """
  1347. return [self.child.to_representation(item) if item is not None else None for item in data]
  1348. def run_child_validation(self, data):
  1349. result = []
  1350. errors = OrderedDict()
  1351. for idx, item in enumerate(data):
  1352. try:
  1353. result.append(self.child.run_validation(item))
  1354. except ValidationError as e:
  1355. errors[idx] = e.detail
  1356. if not errors:
  1357. return result
  1358. raise ValidationError(errors)
  1359. class DictField(Field):
  1360. child = _UnvalidatedField()
  1361. initial = {}
  1362. default_error_messages = {
  1363. 'not_a_dict': _('Expected a dictionary of items but got type "{input_type}".'),
  1364. 'empty': _('This dictionary may not be empty.'),
  1365. }
  1366. def __init__(self, **kwargs):
  1367. self.child = kwargs.pop('child', copy.deepcopy(self.child))
  1368. self.allow_empty = kwargs.pop('allow_empty', True)
  1369. assert not inspect.isclass(self.child), '`child` has not been instantiated.'
  1370. assert self.child.source is None, (
  1371. "The `source` argument is not meaningful when applied to a `child=` field. "
  1372. "Remove `source=` from the field declaration."
  1373. )
  1374. super().__init__(**kwargs)
  1375. self.child.bind(field_name='', parent=self)
  1376. def get_value(self, dictionary):
  1377. # We override the default field access in order to support
  1378. # dictionaries in HTML forms.
  1379. if html.is_html_input(dictionary):
  1380. return html.parse_html_dict(dictionary, prefix=self.field_name)
  1381. return dictionary.get(self.field_name, empty)
  1382. def to_internal_value(self, data):
  1383. """
  1384. Dicts of native values <- Dicts of primitive datatypes.
  1385. """
  1386. if html.is_html_input(data):
  1387. data = html.parse_html_dict(data)
  1388. if not isinstance(data, dict):
  1389. self.fail('not_a_dict', input_type=type(data).__name__)
  1390. if not self.allow_empty and len(data) == 0:
  1391. self.fail('empty')
  1392. return self.run_child_validation(data)
  1393. def to_representation(self, value):
  1394. return {
  1395. str(key): self.child.to_representation(val) if val is not None else None
  1396. for key, val in value.items()
  1397. }
  1398. def run_child_validation(self, data):
  1399. result = {}
  1400. errors = OrderedDict()
  1401. for key, value in data.items():
  1402. key = str(key)
  1403. try:
  1404. result[key] = self.child.run_validation(value)
  1405. except ValidationError as e:
  1406. errors[key] = e.detail
  1407. if not errors:
  1408. return result
  1409. raise ValidationError(errors)
  1410. class HStoreField(DictField):
  1411. child = CharField(allow_blank=True, allow_null=True)
  1412. def __init__(self, **kwargs):
  1413. super().__init__(**kwargs)
  1414. assert isinstance(self.child, CharField), (
  1415. "The `child` argument must be an instance of `CharField`, "
  1416. "as the hstore extension stores values as strings."
  1417. )
  1418. class JSONField(Field):
  1419. default_error_messages = {
  1420. 'invalid': _('Value must be valid JSON.')
  1421. }
  1422. # Workaround for isinstance calls when importing the field isn't possible
  1423. _is_jsonfield = True
  1424. def __init__(self, **kwargs):
  1425. self.binary = kwargs.pop('binary', False)
  1426. self.encoder = kwargs.pop('encoder', None)
  1427. self.decoder = kwargs.pop('decoder', None)
  1428. super().__init__(**kwargs)
  1429. def get_value(self, dictionary):
  1430. if html.is_html_input(dictionary) and self.field_name in dictionary:
  1431. # When HTML form input is used, mark up the input
  1432. # as being a JSON string, rather than a JSON primitive.
  1433. class JSONString(str):
  1434. def __new__(cls, value):
  1435. ret = str.__new__(cls, value)
  1436. ret.is_json_string = True
  1437. return ret
  1438. return JSONString(dictionary[self.field_name])
  1439. return dictionary.get(self.field_name, empty)
  1440. def to_internal_value(self, data):
  1441. try:
  1442. if self.binary or getattr(data, 'is_json_string', False):
  1443. if isinstance(data, bytes):
  1444. data = data.decode()
  1445. return json.loads(data, cls=self.decoder)
  1446. else:
  1447. json.dumps(data, cls=self.encoder)
  1448. except (TypeError, ValueError):
  1449. self.fail('invalid')
  1450. return data
  1451. def to_representation(self, value):
  1452. if self.binary:
  1453. value = json.dumps(value, cls=self.encoder)
  1454. value = value.encode()
  1455. return value
  1456. # Miscellaneous field types...
  1457. class ReadOnlyField(Field):
  1458. """
  1459. A read-only field that simply returns the field value.
  1460. If the field is a method with no parameters, the method will be called
  1461. and its return value used as the representation.
  1462. For example, the following would call `get_expiry_date()` on the object:
  1463. class ExampleSerializer(Serializer):
  1464. expiry_date = ReadOnlyField(source='get_expiry_date')
  1465. """
  1466. def __init__(self, **kwargs):
  1467. kwargs['read_only'] = True
  1468. super().__init__(**kwargs)
  1469. def to_representation(self, value):
  1470. return value
  1471. class HiddenField(Field):
  1472. """
  1473. A hidden field does not take input from the user, or present any output,
  1474. but it does populate a field in `validated_data`, based on its default
  1475. value. This is particularly useful when we have a `unique_for_date`
  1476. constraint on a pair of fields, as we need some way to include the date in
  1477. the validated data.
  1478. """
  1479. def __init__(self, **kwargs):
  1480. assert 'default' in kwargs, 'default is a required argument.'
  1481. kwargs['write_only'] = True
  1482. super().__init__(**kwargs)
  1483. def get_value(self, dictionary):
  1484. # We always use the default value for `HiddenField`.
  1485. # User input is never provided or accepted.
  1486. return empty
  1487. def to_internal_value(self, data):
  1488. return data
  1489. class SerializerMethodField(Field):
  1490. """
  1491. A read-only field that get its representation from calling a method on the
  1492. parent serializer class. The method called will be of the form
  1493. "get_{field_name}", and should take a single argument, which is the
  1494. object being serialized.
  1495. For example:
  1496. class ExampleSerializer(self):
  1497. extra_info = SerializerMethodField()
  1498. def get_extra_info(self, obj):
  1499. return ... # Calculate some data to return.
  1500. """
  1501. def __init__(self, method_name=None, **kwargs):
  1502. self.method_name = method_name
  1503. kwargs['source'] = '*'
  1504. kwargs['read_only'] = True
  1505. super().__init__(**kwargs)
  1506. def bind(self, field_name, parent):
  1507. # The method name defaults to `get_{field_name}`.
  1508. if self.method_name is None:
  1509. self.method_name = 'get_{field_name}'.format(field_name=field_name)
  1510. super().bind(field_name, parent)
  1511. def to_representation(self, value):
  1512. method = getattr(self.parent, self.method_name)
  1513. return method(value)
  1514. class ModelField(Field):
  1515. """
  1516. A generic field that can be used against an arbitrary model field.
  1517. This is used by `ModelSerializer` when dealing with custom model fields,
  1518. that do not have a serializer field to be mapped to.
  1519. """
  1520. default_error_messages = {
  1521. 'max_length': _('Ensure this field has no more than {max_length} characters.'),
  1522. }
  1523. def __init__(self, model_field, **kwargs):
  1524. self.model_field = model_field
  1525. # The `max_length` option is supported by Django's base `Field` class,
  1526. # so we'd better support it here.
  1527. self.max_length = kwargs.pop('max_length', None)
  1528. super().__init__(**kwargs)
  1529. if self.max_length is not None:
  1530. message = lazy_format(self.error_messages['max_length'], max_length=self.max_length)
  1531. self.validators.append(
  1532. MaxLengthValidator(self.max_length, message=message))
  1533. def to_internal_value(self, data):
  1534. rel = self.model_field.remote_field
  1535. if rel is not None:
  1536. return rel.model._meta.get_field(rel.field_name).to_python(data)
  1537. return self.model_field.to_python(data)
  1538. def get_attribute(self, obj):
  1539. # We pass the object instance onto `to_representation`,
  1540. # not just the field attribute.
  1541. return obj
  1542. def to_representation(self, obj):
  1543. value = self.model_field.value_from_object(obj)
  1544. if is_protected_type(value):
  1545. return value
  1546. return self.model_field.value_to_string(obj)