fields.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. from calendar import timegm
  2. from decimal import Decimal as MyDecimal, ROUND_HALF_EVEN
  3. from email.utils import formatdate
  4. import six
  5. try:
  6. from urlparse import urlparse, urlunparse
  7. except ImportError:
  8. # python3
  9. from urllib.parse import urlparse, urlunparse
  10. from flask_restful import marshal
  11. from flask import url_for, request
  12. __all__ = ["String", "FormattedString", "Url", "DateTime", "Float",
  13. "Integer", "Arbitrary", "Nested", "List", "Raw", "Boolean",
  14. "Fixed", "Price"]
  15. class MarshallingException(Exception):
  16. """
  17. This is an encapsulating Exception in case of marshalling error.
  18. """
  19. def __init__(self, underlying_exception):
  20. # just put the contextual representation of the error to hint on what
  21. # went wrong without exposing internals
  22. super(MarshallingException, self).__init__(six.text_type(underlying_exception))
  23. def is_indexable_but_not_string(obj):
  24. return not hasattr(obj, "strip") and hasattr(obj, "__iter__")
  25. def get_value(key, obj, default=None):
  26. """Helper for pulling a keyed value off various types of objects"""
  27. if isinstance(key, int):
  28. return _get_value_for_key(key, obj, default)
  29. elif callable(key):
  30. return key(obj)
  31. else:
  32. return _get_value_for_keys(key.split('.'), obj, default)
  33. def _get_value_for_keys(keys, obj, default):
  34. if len(keys) == 1:
  35. return _get_value_for_key(keys[0], obj, default)
  36. else:
  37. return _get_value_for_keys(
  38. keys[1:], _get_value_for_key(keys[0], obj, default), default)
  39. def _get_value_for_key(key, obj, default):
  40. if is_indexable_but_not_string(obj):
  41. try:
  42. return obj[key]
  43. except (IndexError, TypeError, KeyError):
  44. pass
  45. return getattr(obj, key, default)
  46. def to_marshallable_type(obj):
  47. """Helper for converting an object to a dictionary only if it is not
  48. dictionary already or an indexable object nor a simple type"""
  49. if obj is None:
  50. return None # make it idempotent for None
  51. if hasattr(obj, '__marshallable__'):
  52. return obj.__marshallable__()
  53. if hasattr(obj, '__getitem__'):
  54. return obj # it is indexable it is ok
  55. return dict(obj.__dict__)
  56. class Raw(object):
  57. """Raw provides a base field class from which others should extend. It
  58. applies no formatting by default, and should only be used in cases where
  59. data does not need to be formatted before being serialized. Fields should
  60. throw a :class:`MarshallingException` in case of parsing problem.
  61. :param default: The default value for the field, if no value is
  62. specified.
  63. :param attribute: If the public facing value differs from the internal
  64. value, use this to retrieve a different attribute from the response
  65. than the publicly named value.
  66. """
  67. def __init__(self, default=None, attribute=None):
  68. self.attribute = attribute
  69. self.default = default
  70. def format(self, value):
  71. """Formats a field's value. No-op by default - field classes that
  72. modify how the value of existing object keys should be presented should
  73. override this and apply the appropriate formatting.
  74. :param value: The value to format
  75. :exception MarshallingException: In case of formatting problem
  76. Ex::
  77. class TitleCase(Raw):
  78. def format(self, value):
  79. return unicode(value).title()
  80. """
  81. return value
  82. def output(self, key, obj):
  83. """Pulls the value for the given key from the object, applies the
  84. field's formatting and returns the result. If the key is not found
  85. in the object, returns the default value. Field classes that create
  86. values which do not require the existence of the key in the object
  87. should override this and return the desired value.
  88. :exception MarshallingException: In case of formatting problem
  89. """
  90. value = get_value(key if self.attribute is None else self.attribute, obj)
  91. if value is None:
  92. return self.default
  93. return self.format(value)
  94. class Nested(Raw):
  95. """Allows you to nest one set of fields inside another.
  96. See :ref:`nested-field` for more information
  97. :param dict nested: The dictionary to nest
  98. :param bool allow_null: Whether to return None instead of a dictionary
  99. with null keys, if a nested dictionary has all-null keys
  100. :param kwargs: If ``default`` keyword argument is present, a nested
  101. dictionary will be marshaled as its value if nested dictionary is
  102. all-null keys (e.g. lets you return an empty JSON object instead of
  103. null)
  104. """
  105. def __init__(self, nested, allow_null=False, **kwargs):
  106. self.nested = nested
  107. self.allow_null = allow_null
  108. super(Nested, self).__init__(**kwargs)
  109. def output(self, key, obj):
  110. value = get_value(key if self.attribute is None else self.attribute, obj)
  111. if value is None:
  112. if self.allow_null:
  113. return None
  114. elif self.default is not None:
  115. return self.default
  116. return marshal(value, self.nested)
  117. class List(Raw):
  118. """
  119. Field for marshalling lists of other fields.
  120. See :ref:`list-field` for more information.
  121. :param cls_or_instance: The field type the list will contain.
  122. """
  123. def __init__(self, cls_or_instance, **kwargs):
  124. super(List, self).__init__(**kwargs)
  125. error_msg = ("The type of the list elements must be a subclass of "
  126. "flask_restful.fields.Raw")
  127. if isinstance(cls_or_instance, type):
  128. if not issubclass(cls_or_instance, Raw):
  129. raise MarshallingException(error_msg)
  130. self.container = cls_or_instance()
  131. else:
  132. if not isinstance(cls_or_instance, Raw):
  133. raise MarshallingException(error_msg)
  134. self.container = cls_or_instance
  135. def format(self, value):
  136. # Convert all instances in typed list to container type
  137. if isinstance(value, set):
  138. value = list(value)
  139. return [
  140. self.container.output(idx,
  141. val if (isinstance(val, dict)
  142. or (self.container.attribute
  143. and hasattr(val, self.container.attribute)))
  144. and not isinstance(self.container, Nested)
  145. and not type(self.container) is Raw
  146. else value)
  147. for idx, val in enumerate(value)
  148. ]
  149. def output(self, key, data):
  150. value = get_value(key if self.attribute is None else self.attribute, data)
  151. # we cannot really test for external dict behavior
  152. if is_indexable_but_not_string(value) and not isinstance(value, dict):
  153. return self.format(value)
  154. if value is None:
  155. return self.default
  156. return [marshal(value, self.container.nested)]
  157. class String(Raw):
  158. """
  159. Marshal a value as a string. Uses ``six.text_type`` so values will
  160. be converted to :class:`unicode` in python2 and :class:`str` in
  161. python3.
  162. """
  163. def format(self, value):
  164. try:
  165. return six.text_type(value)
  166. except ValueError as ve:
  167. raise MarshallingException(ve)
  168. class Integer(Raw):
  169. """ Field for outputting an integer value.
  170. :param int default: The default value for the field, if no value is
  171. specified.
  172. """
  173. def __init__(self, default=0, **kwargs):
  174. super(Integer, self).__init__(default=default, **kwargs)
  175. def format(self, value):
  176. try:
  177. if value is None:
  178. return self.default
  179. return int(value)
  180. except ValueError as ve:
  181. raise MarshallingException(ve)
  182. class Boolean(Raw):
  183. """
  184. Field for outputting a boolean value.
  185. Empty collections such as ``""``, ``{}``, ``[]``, etc. will be converted to
  186. ``False``.
  187. """
  188. def format(self, value):
  189. return bool(value)
  190. class FormattedString(Raw):
  191. """
  192. FormattedString is used to interpolate other values from
  193. the response into this field. The syntax for the source string is
  194. the same as the string :meth:`~str.format` method from the python
  195. stdlib.
  196. Ex::
  197. fields = {
  198. 'name': fields.String,
  199. 'greeting': fields.FormattedString("Hello {name}")
  200. }
  201. data = {
  202. 'name': 'Doug',
  203. }
  204. marshal(data, fields)
  205. """
  206. def __init__(self, src_str):
  207. """
  208. :param string src_str: the string to format with the other
  209. values from the response.
  210. """
  211. super(FormattedString, self).__init__()
  212. self.src_str = six.text_type(src_str)
  213. def output(self, key, obj):
  214. try:
  215. data = to_marshallable_type(obj)
  216. return self.src_str.format(**data)
  217. except (TypeError, IndexError) as error:
  218. raise MarshallingException(error)
  219. class Url(Raw):
  220. """
  221. A string representation of a Url
  222. :param endpoint: Endpoint name. If endpoint is ``None``,
  223. ``request.endpoint`` is used instead
  224. :type endpoint: str
  225. :param absolute: If ``True``, ensures that the generated urls will have the
  226. hostname included
  227. :type absolute: bool
  228. :param scheme: URL scheme specifier (e.g. ``http``, ``https``)
  229. :type scheme: str
  230. """
  231. def __init__(self, endpoint=None, absolute=False, scheme=None, **kwargs):
  232. super(Url, self).__init__(**kwargs)
  233. self.endpoint = endpoint
  234. self.absolute = absolute
  235. self.scheme = scheme
  236. def output(self, key, obj):
  237. try:
  238. data = to_marshallable_type(obj)
  239. endpoint = self.endpoint if self.endpoint is not None else request.endpoint
  240. o = urlparse(url_for(endpoint, _external=self.absolute, **data))
  241. if self.absolute:
  242. scheme = self.scheme if self.scheme is not None else o.scheme
  243. return urlunparse((scheme, o.netloc, o.path, "", "", ""))
  244. return urlunparse(("", "", o.path, "", "", ""))
  245. except TypeError as te:
  246. raise MarshallingException(te)
  247. class Float(Raw):
  248. """
  249. A double as IEEE-754 double precision.
  250. ex : 3.141592653589793 3.1415926535897933e-06 3.141592653589793e+24 nan inf
  251. -inf
  252. """
  253. def format(self, value):
  254. try:
  255. return float(value)
  256. except ValueError as ve:
  257. raise MarshallingException(ve)
  258. class Arbitrary(Raw):
  259. """
  260. A floating point number with an arbitrary precision
  261. ex: 634271127864378216478362784632784678324.23432
  262. """
  263. def format(self, value):
  264. return six.text_type(MyDecimal(value))
  265. class DateTime(Raw):
  266. """
  267. Return a formatted datetime string in UTC. Supported formats are RFC 822
  268. and ISO 8601.
  269. See :func:`email.utils.formatdate` for more info on the RFC 822 format.
  270. See :meth:`datetime.datetime.isoformat` for more info on the ISO 8601
  271. format.
  272. :param dt_format: ``'rfc822'`` or ``'iso8601'``
  273. :type dt_format: str
  274. """
  275. def __init__(self, dt_format='rfc822', **kwargs):
  276. super(DateTime, self).__init__(**kwargs)
  277. self.dt_format = dt_format
  278. def format(self, value):
  279. try:
  280. if self.dt_format == 'rfc822':
  281. return _rfc822(value)
  282. elif self.dt_format == 'iso8601':
  283. return _iso8601(value)
  284. else:
  285. raise MarshallingException(
  286. 'Unsupported date format %s' % self.dt_format
  287. )
  288. except AttributeError as ae:
  289. raise MarshallingException(ae)
  290. ZERO = MyDecimal()
  291. class Fixed(Raw):
  292. """
  293. A decimal number with a fixed precision.
  294. """
  295. def __init__(self, decimals=5, **kwargs):
  296. super(Fixed, self).__init__(**kwargs)
  297. self.precision = MyDecimal('0.' + '0' * (decimals - 1) + '1')
  298. def format(self, value):
  299. dvalue = MyDecimal(value)
  300. if not dvalue.is_normal() and dvalue != ZERO:
  301. raise MarshallingException('Invalid Fixed precision number.')
  302. return six.text_type(dvalue.quantize(self.precision, rounding=ROUND_HALF_EVEN))
  303. """Alias for :class:`~fields.Fixed`"""
  304. Price = Fixed
  305. def _rfc822(dt):
  306. """Turn a datetime object into a formatted date.
  307. Example::
  308. fields._rfc822(datetime(2011, 1, 1)) => "Sat, 01 Jan 2011 00:00:00 -0000"
  309. :param dt: The datetime to transform
  310. :type dt: datetime
  311. :return: A RFC 822 formatted date string
  312. """
  313. return formatdate(timegm(dt.utctimetuple()))
  314. def _iso8601(dt):
  315. """Turn a datetime object into an ISO8601 formatted date.
  316. Example::
  317. fields._iso8601(datetime(2012, 1, 1, 0, 0)) => "2012-01-01T00:00:00"
  318. :param dt: The datetime to transform
  319. :type dt: datetime
  320. :return: A ISO 8601 formatted date string
  321. """
  322. return dt.isoformat()