Debian Codegraph @

tastypie/serializers.py

raw symbols SCIP
1 import datetimescip-python python python-stdlib 3.11 datetime/__init__:
2 import jsonscip-python python python-stdlib 3.11 json/__init__:
3 import rescip-python python python-stdlib 3.11 re/__init__:
4 import ioscip-python python python-stdlib 3.11 io/__init__:
5
6 from django.confscip-python python django-tastypie 0.15.1 `django.conf`/__init__: import settings
7 from django.core.exceptionsscip-python python django-tastypie 0.15.1 `django.core.exceptions`/__init__: import ImproperlyConfigured
8 from django.utils.encodingscip-python python django-tastypie 0.15.1 `django.utils.encoding`/__init__: import smart_bytes
9 from django.core.serializersscip-python python django-tastypie 0.15.1 `django.core.serializers`/__init__: import json as djangojson
10
11 from tastypie.bundlescip-python python django-tastypie 0.15.1 `tastypie.bundle`/__init__: import Bundlescip-python python django-tastypie 0.15.1 `tastypie.bundle`/Bundle#
12 from tastypie.compatscip-python python django-tastypie 0.15.1 `tastypie.compat`/__init__: import force_str
13 from tastypie.exceptionsscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/__init__: import BadRequestscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/BadRequest#, UnsupportedSerializationFormatscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/UnsupportedSerializationFormat#, \
14 UnsupportedDeserializationFormatscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/UnsupportedDeserializationFormat#
15 from tastypie.utilsscip-python python django-tastypie 0.15.1 `tastypie.utils`/__init__: import format_datetimescip-python python django-tastypie 0.15.1 `tastypie.utils.formatting`/format_datetime()., format_datescip-python python django-tastypie 0.15.1 `tastypie.utils.formatting`/format_date()., format_timescip-python python django-tastypie 0.15.1 `tastypie.utils.formatting`/format_time()., \
16 make_naivescip-python python django-tastypie 0.15.1 `tastypie.utils.timezone`/make_naive().
17
18
19 import warningsscip-python python python-stdlib 3.11 warnings/__init__:
20 try:
21 with warningsscip-python python python-stdlib 3.11 warnings/__init__:.catch_warningsscip-python python python-stdlib 3.11 warnings/catch_warnings#():
22 warningsscip-python python python-stdlib 3.11 warnings/__init__:.simplefilterscip-python python python-stdlib 3.11 warnings/simplefilter().("ignore", DeprecationWarningscip-python python python-stdlib 3.11 builtins/DeprecationWarning#)
23 import defusedxml.lxml as lxml
24 from defusedxml.commonscip-python python django-tastypie 0.15.1 `defusedxml.common`/__init__: import DefusedXmlException
25 from defusedxml.lxmlscip-python python django-tastypie 0.15.1 `defusedxml.lxml`/__init__: import parse as parse_xml
26 from lxml.etreescip-python python django-tastypie 0.15.1 `lxml.etree`/__init__: import Element, tostring, LxmlError
27 except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#:
28 lxml = None
29
30 try:
31 import yaml
32 except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#:
33 yaml = None
34
35 try:
36 import biplist
37 except ImportErrorscip-python python python-stdlib 3.11 builtins/ImportError#:
38 biplist = None
39
40
41 XML_ENCODINGscip-python python django-tastypie 0.15.1 `tastypie.serializers`/XML_ENCODING. = rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().(r'<\?xml.*?\?>', rescip-python python python-stdlib 3.11 re/__init__:.IGNORECASEscip-python python python-stdlib 3.11 re/IGNORECASE.)
42
43
44 # Ugh & blah.
45 # So doing a regular dump is generally fine, since Tastypie doesn't usually
46 # serialize advanced types. *HOWEVER*, it will dump out Python Unicode strings
47 # as a custom YAML tag, which of course ``yaml.safe_load`` can't handle.
48 if yaml is not None:
49 from yaml.constructorscip-python python django-tastypie 0.15.1 `yaml.constructor`/__init__: import SafeConstructor
50 from yaml.loaderscip-python python django-tastypie 0.15.1 `yaml.loader`/__init__: import Reader, Scanner, Parser, Composer, Resolver
51
52 class TastypieConstructorscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#(SafeConstructor):
53 def construct_yaml_unicode_dammitscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#construct_yaml_unicode_dammit().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#construct_yaml_unicode_dammit().(self), nodescip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#construct_yaml_unicode_dammit().(node)):
54 value = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#construct_yaml_unicode_dammit().(self).construct_scalar(nodescip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#construct_yaml_unicode_dammit().(node))
55 try:
56 return value.encodescip-python python python-stdlib 3.11 builtins/str#encode().('ascii')
57 except UnicodeEncodeErrorscip-python python python-stdlib 3.11 builtins/UnicodeEncodeError#:
58 return value
59
60 TastypieConstructorscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#.add_constructor(
61 u'tag:yaml.org,2002:python/unicode',
62 TastypieConstructorscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#.construct_yaml_unicode_dammitscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#construct_yaml_unicode_dammit().
63 )
64
65 class TastypieLoaderscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#(Reader, Scanner, Parser, Composer,
66 TastypieConstructorscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#, Resolver):
67 def __init__scip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self), streamscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(stream)):
68 Reader.__init__(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self), streamscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(stream))
69 Scanner.__init__(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self))
70 Parser.__init__(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self))
71 Composer.__init__(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self))
72 TastypieConstructorscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieConstructor#.__init__(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self))
73 Resolver.__init__(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#__init__().(self))
74
75
76 def _get_default_formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_get_default_formats().():
77 formats = ['json']
78 if lxml:
79 formats.appendscip-python python python-stdlib 3.11 builtins/list#append().('xml')
80 if yaml:
81 formats.appendscip-python python python-stdlib 3.11 builtins/list#append().('yaml')
82 if biplist:
83 formats.appendscip-python python python-stdlib 3.11 builtins/list#append().('plist')
84 return formats
85
86
87 _NUMscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_NUM. = 0
88 _DICTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DICT. = 1
89 _LISTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_LIST. = 2
90 _STRscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_STR. = 3
91 _BUNDLEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_BUNDLE. = 4
92 _DATETIMEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DATETIME. = 5
93 _DATEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DATE. = 6
94 _TIMEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_TIME. = 7
95
96 _SIMPLETYPESscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_SIMPLETYPES. = {
97 floatscip-python python python-stdlib 3.11 builtins/float#: _NUMscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_NUM.,
98 intscip-python python python-stdlib 3.11 builtins/int#: _NUMscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_NUM.,
99 strscip-python python python-stdlib 3.11 builtins/str#: _STRscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_STR.,
100 boolscip-python python python-stdlib 3.11 builtins/bool#: _NUMscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_NUM.,
101 dictscip-python python python-stdlib 3.11 builtins/dict#: _DICTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DICT.,
102 listscip-python python python-stdlib 3.11 builtins/list#: _LISTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_LIST.,
103 tuplescip-python python python-stdlib 3.11 builtins/tuple#: _LISTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_LIST.,
104 Bundlescip-python python django-tastypie 0.15.1 `tastypie.bundle`/Bundle#: _BUNDLEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_BUNDLE.,
105 datetimescip-python python python-stdlib 3.11 datetime/__init__:.datetimescip-python python python-stdlib 3.11 datetime/datetime#: _DATETIMEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DATETIME.,
106 datetimescip-python python python-stdlib 3.11 datetime/__init__:.datescip-python python python-stdlib 3.11 datetime/date#: _DATEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DATE.,
107 datetimescip-python python python-stdlib 3.11 datetime/__init__:.timescip-python python python-stdlib 3.11 datetime/time#: _TIMEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_TIME.,
108 }
109
110
111 class Serializerscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#(objectscip-python python python-stdlib 3.11 builtins/object#):
112 """
113 A swappable class for serialization.
114
115 This handles most types of data as well as the following output formats::
116
117 * json
118 * jsonp (Disabled by default)
119 * xml
120 * yaml
121 * plist (see https://bitbucket.org/wooster/biplist)
122
123 It was designed to make changing behavior easy, either by overridding the
124 various format methods (i.e. ``to_json``), by changing the
125 ``formats/content_types`` options or by altering the other hook methods.
126 """
127
128 formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats. = _get_default_formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_get_default_formats().()
129
130 content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#content_types. = {
131 'json': 'application/json',
132 'jsonp': 'text/javascript',
133 'xml': 'application/xml',
134 'yaml': 'text/yaml',
135 'plist': 'application/x-plist'
136 }
137
138 def __init__scip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self), formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(formats)=None, content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(content_types)=None, datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(datetime_formatting)=None):
139 if datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(datetime_formatting) is not None:
140 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. = datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(datetime_formatting)
141 else:
142 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. = getattrscip-python python python-stdlib 3.11 builtins/getattr().(settings,
143 'TASTYPIE_DATETIME_FORMATTING', 'iso-8601')
144
145 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).supported_formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#supported_formats. = []
146
147 if content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(content_types) is not None:
148 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#content_types. = content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(content_types)
149
150 if formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(formats) is not None:
151 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats. = formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(formats)
152
153 if selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats. is Serializerscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#.formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats. and hasattr(settings, 'TASTYPIE_DEFAULT_FORMATS'):
154 # We want TASTYPIE_DEFAULT_FORMATS to override unmodified defaults
155 # but not intentational changes on Serializer subclasses:
156 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats. = settings.TASTYPIE_DEFAULT_FORMATS
157
158 if not isinstance(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats., (listscip-python python python-stdlib 3.11 builtins/list#, tuplescip-python python python-stdlib 3.11 builtins/tuple#)):
159 raise ImproperlyConfigured(
160 'Formats should be a list or tuple, not %r' % selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats.)
161
162 for format in selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#formats.:
163 try:
164 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).supported_formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#supported_formats..appendscip-python python python-stdlib 3.11 builtins/list#append().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#content_types.[format])
165 except KeyErrorscip-python python python-stdlib 3.11 builtins/KeyError#:
166 raise ImproperlyConfigured("Content type for specified type '%s' not found. Please provide it at either the class level or via the arguments." % format)
167
168 # Reverse the list, because mimeparse is weird like that. See also
169 # https://github.com/django-tastypie/django-tastypie/issues#issue/12 for
170 # more information.
171 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).supported_formats_reversedscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#supported_formats_reversed. = listscip-python python python-stdlib 3.11 builtins/list#(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).supported_formatsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#supported_formats.)
172 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).supported_formats_reversedscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#supported_formats_reversed..reversescip-python python python-stdlib 3.11 typing/MutableSequence#reverse().()
173
174 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self)._from_methodsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#_from_methods. = {}
175 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self)._to_methodsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#_to_methods. = {}
176
177 for short_format, long_format in selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self).content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#content_types..itemsscip-python python python-stdlib 3.11 builtins/dict#items().():
178 method = getattrscip-python python python-stdlib 3.11 builtins/getattr().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self), "from_%s" % short_format, None)
179
180 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self)._from_methodsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#_from_methods.[long_format] = method
181
182 method = getattrscip-python python python-stdlib 3.11 builtins/getattr().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self), "to_%s" % short_format, None)
183
184 selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#__init__().(self)._to_methodsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#_to_methods.[long_format] = method
185
186 def get_mime_for_formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#get_mime_for_format().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#get_mime_for_format().(self), formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#get_mime_for_format().(format)):
187 """
188 Given a format, attempts to determine the correct MIME type.
189
190 If not available on the current ``Serializer``, returns
191 ``application/json`` by default.
192 """
193 try:
194 return selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#get_mime_for_format().(self).content_typesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#content_types.[formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#get_mime_for_format().(format)]
195 except KeyErrorscip-python python python-stdlib 3.11 builtins/KeyError#:
196 return 'application/json'
197
198 def format_datetimescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data)):
199 """
200 A hook to control how datetimes are formatted.
201
202 Can be overridden at the ``Serializer`` level (``datetime_formatting``)
203 or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).
204
205 Default is ``iso-8601``, which looks like "2010-12-16T03:02:14".
206 """
207 datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data) = make_naivescip-python python django-tastypie 0.15.1 `tastypie.utils.timezone`/make_naive().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data))
208 if selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. == 'rfc-2822':
209 return format_datetimescip-python python django-tastypie 0.15.1 `tastypie.utils.formatting`/format_datetime().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data))
210 if selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. == 'iso-8601-strict':
211 # Remove microseconds to strictly adhere to iso-8601
212 datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data) = datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data) - datetimescip-python python python-stdlib 3.11 datetime/__init__:.timedeltascip-python python python-stdlib 3.11 datetime/timedelta#(microseconds=datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data).microsecond)
213
214 return datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(data).isoformat()
215
216 def format_datescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(data)):
217 """
218 A hook to control how dates are formatted.
219
220 Can be overridden at the ``Serializer`` level (``datetime_formatting``)
221 or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).
222
223 Default is ``iso-8601``, which looks like "2010-12-16".
224 """
225 if selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. == 'rfc-2822':
226 return format_datescip-python python django-tastypie 0.15.1 `tastypie.utils.formatting`/format_date().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(data))
227
228 return datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(data).isoformat()
229
230 def format_timescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(data)):
231 """
232 A hook to control how times are formatted.
233
234 Can be overridden at the ``Serializer`` level (``datetime_formatting``)
235 or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).
236
237 Default is ``iso-8601``, which looks like "03:02:14".
238 """
239 if selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. == 'rfc-2822':
240 return format_timescip-python python django-tastypie 0.15.1 `tastypie.utils.formatting`/format_time().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(data))
241 if selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(self).datetime_formattingscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#datetime_formatting. == 'iso-8601-strict':
242 # Remove microseconds to strictly adhere to iso-8601
243 datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(data) = (
244 datetimescip-python python python-stdlib 3.11 datetime/__init__:.datetimescip-python python python-stdlib 3.11 datetime/datetime#.combinescip-python python python-stdlib 3.11 datetime/datetime#combine().(datetimescip-python python python-stdlib 3.11 datetime/__init__:.datescip-python python python-stdlib 3.11 datetime/date#(1, 1, 1), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(data))
245 - datetimescip-python python python-stdlib 3.11 datetime/__init__:.timedeltascip-python python python-stdlib 3.11 datetime/timedelta#(microseconds=datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(data).microsecond)
246 ).timescip-python python python-stdlib 3.11 datetime/datetime#time().()
247
248 return datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(data).isoformatscip-python python python-stdlib 3.11 datetime/time#isoformat().()
249
250 def serializescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(self), bundlescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(bundle), formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(format)='application/json', optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(options)=None):
251 """
252 Given some data and a format, calls the correct method to serialize
253 the data and returns the result.
254 """
255 method = None
256 if optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(options) is None:
257 optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(options) = {}
258
259 method = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(self)._to_methodsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#_to_methods..getscip-python python python-stdlib 3.11 builtins/dict#get().(formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(format))
260
261 if method is None:
262 raise UnsupportedSerializationFormatscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/UnsupportedSerializationFormat#(formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(format))
263
264 return method(bundlescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(bundle), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#serialize().(options))
265
266 def deserializescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(self), contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(content), formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(format)='application/json'):
267 """
268 Given some data and a format, calls the correct method to deserialize
269 the data and returns the result.
270 """
271 method = None
272
273 formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(format) = formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(format).splitscip-python python python-stdlib 3.11 builtins/str#split().(';')[0]
274
275 method = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(self)._from_methodsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#_from_methods..getscip-python python python-stdlib 3.11 builtins/dict#get().(formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(format))
276
277 if method is None:
278 raise UnsupportedDeserializationFormatscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/UnsupportedDeserializationFormat#(formatscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(format))
279
280 if isinstance(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(content), bytesscip-python python python-stdlib 3.11 builtins/bytes#):
281 contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(content) = force_str(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(content))
282
283 return method(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#deserialize().(content))
284
285 def to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(options)):
286 """
287 For a piece of data, attempts to recognize it and provide a simplified
288 form of something complex.
289
290 This brings complex Python data structures down to native types of the
291 serialization format(s).
292 """
293 if datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data) is None:
294 return None
295
296 data_type = typescip-python python python-stdlib 3.11 builtins/type#(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data))
297
298 stype = _STRscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_STR.
299
300 for dt in data_type.__mro__:
301 try:
302 stype = _SIMPLETYPESscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_SIMPLETYPES.[dt]
303 break
304 except KeyErrorscip-python python python-stdlib 3.11 builtins/KeyError#:
305 pass
306
307 if stype == _NUMscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_NUM.:
308 return datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data)
309 if stype == _DICTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DICT.:
310 to_simple = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().
311 return {key: to_simple(val, optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(options)) for key, val in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data).items()}
312 if stype == _STRscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_STR.:
313 return force_str(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data))
314 if stype == _LISTscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_LIST.:
315 to_simple = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().
316 return [to_simple(item, optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(options)) for item in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data)]
317 if stype == _BUNDLEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_BUNDLE.:
318 to_simple = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().
319 return {key: to_simple(val, optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(options)) for key, val in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data).data.items()}
320 if stype == _DATETIMEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DATETIME.:
321 return selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self).format_datetimescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_datetime().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data))
322 if stype == _DATEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_DATE.:
323 return selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self).format_datescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_date().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data))
324 if stype == _TIMEscip-python python django-tastypie 0.15.1 `tastypie.serializers`/_TIME.:
325 return selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(self).format_timescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#format_time().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(data))
326
327 def to_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(options)=None, namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name)=None, depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth)=0):
328 """
329 Given some data, converts that data to an ``etree.Element`` suitable
330 for use in the XML output.
331 """
332 if isinstance(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data), (listscip-python python python-stdlib 3.11 builtins/list#, tuplescip-python python python-stdlib 3.11 builtins/tuple#)):
333 element = Element(namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name) or 'objects')
334 if namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name):
335 element = Element(namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name))
336 element.set('type', 'list')
337 else:
338 element = Element('objects')
339 for item in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data):
340 element.append(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(self).to_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(item, optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(options), depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth)=depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth) + 1))
341 element[:] = sortedscip-python python python-stdlib 3.11 builtins/sorted().(element, keyscip-python python python-stdlib 3.11 builtins/sorted().(key)=lambda x: x.tag)
342 elif isinstance(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data), dictscip-python python python-stdlib 3.11 builtins/dict#):
343 if depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth) == 0:
344 element = Element(namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name) or 'response')
345 else:
346 element = Element(namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name) or 'object')
347 element.set('type', 'hash')
348 for (key, value) in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data).itemsscip-python python python-stdlib 3.11 builtins/dict#items().():
349 element.append(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(self).to_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(
350 value, optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(options), namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name)=key, depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth)=depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth) + 1))
351 element[:] = sortedscip-python python python-stdlib 3.11 builtins/sorted().(element, keyscip-python python python-stdlib 3.11 builtins/sorted().(key)=lambda x: x.tag)
352 elif isinstance(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data), Bundlescip-python python django-tastypie 0.15.1 `tastypie.bundle`/Bundle#):
353 element = Element(namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name) or 'object')
354 for field_name, field_object in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data).datascip-python python django-tastypie 0.15.1 `tastypie.bundle`/Bundle#data..itemsscip-python python python-stdlib 3.11 builtins/dict#items().():
355 element.append(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(self).to_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(
356 field_object, optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(options), namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name)=field_name, depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth)=depthscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(depth) + 1))
357 element[:] = sortedscip-python python python-stdlib 3.11 builtins/sorted().(element, keyscip-python python python-stdlib 3.11 builtins/sorted().(key)=lambda x: x.tag)
358 else:
359 element = Element(namescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(name) or 'value')
360 simple_data = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(options))
361 data_type = get_type_stringscip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(simple_data)
362
363 if data_type != 'string':
364 element.set('type', get_type_stringscip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(simple_data))
365
366 if data_type != 'null':
367 if isinstance(simple_data, strscip-python python python-stdlib 3.11 builtins/str#):
368 element.text = simple_data
369 else:
370 element.text = force_str(simple_data)
371
372 return element
373
374 def from_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data)):
375 """
376 Not the smartest deserializer on the planet. At the request level,
377 it first tries to output the deserialized subelement called "object"
378 or "objects" and falls back to deserializing based on hinted types in
379 the XML element attribute "type".
380 """
381 if datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).tag == 'request':
382 # if "object" or "objects" exists, return deserialized forms.
383 elements = datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).getchildren()
384 for element in elements:
385 if element.tag in ('object', 'objects'):
386 return selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(self).from_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(element)
387 return {
388 element.tag: selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(self).from_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(element)
389 for element in elements
390 }
391 elif datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).tag == 'object' or datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).get('type') == 'hash':
392 return {
393 element.tag: selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(self).from_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(element)
394 for element in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).getchildren()
395 }
396 elif datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).tag == 'objects' or datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).get('type') == 'list':
397 return [selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(self).from_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(element) for element in datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).getchildren()]
398 else:
399 type_string = datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).get('type')
400 if type_string in ('string', None):
401 return datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).text
402 elif type_string == 'integer':
403 return intscip-python python python-stdlib 3.11 builtins/int#(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).text)
404 elif type_string == 'float':
405 return floatscip-python python python-stdlib 3.11 builtins/float#(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).text)
406 elif type_string == 'boolean':
407 if datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(data).text == 'True':
408 return True
409 else:
410 return False
411 else:
412 return None
413
414 def to_jsonscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(options)=None):
415 """
416 Given some Python data, produces JSON output.
417 """
418 optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(options) = optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(options) or {}
419 datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(data) = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(options))
420
421 return djangojson.json.dumps(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(data), cls=djangojson.DjangoJSONEncoder,
422 sort_keys=True, ensure_ascii=False)
423
424 def from_jsonscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_json().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_json().(self), contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_json().(content)):
425 """
426 Given some JSON data, returns a Python dictionary of the decoded data.
427 """
428 try:
429 return jsonscip-python python python-stdlib 3.11 json/__init__:.loadsscip-python python python-stdlib 3.11 json/loads().(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_json().(content))
430 except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#:
431 raise BadRequestscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/BadRequest#('Request is not valid JSON.')
432
433 def to_jsonpscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(options)=None):
434 """
435 Given some Python data, produces JSON output wrapped in the provided
436 callback.
437
438 Due to a difference between JSON and Javascript, two
439 newline characters, \u2028 and \u2029, need to be escaped.
440 See http://timelessrepo.com/json-isnt-a-javascript-subset for
441 details.
442 """
443 optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(options) = optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(options) or {}
444 jsonstr = selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(self).to_jsonscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_json().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(options)).replace(
445 u'\u2028', u'\\u2028').replace(u'\u2029', u'\\u2029')
446 return u'%s(%s)' % (optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_jsonp().(options)['callback'], jsonstr)
447
448 def to_xmlscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(options)=None):
449 """
450 Given some Python data, produces XML output.
451 """
452 optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(options) = optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(options) or {}
453
454 if lxml is None:
455 raise ImproperlyConfigured(
456 "Usage of the XML aspects requires lxml and defusedxml.")
457
458 return tostring(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(self).to_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_etree().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_xml().(options)), xml_declaration=True,
459 encoding='utf-8')
460
461 def from_xmlscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(self), contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(content), forbid_dtdscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(forbid_dtd)=True, forbid_entitiesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(forbid_entities)=True):
462 """
463 Given some XML data, returns a Python dictionary of the decoded data.
464
465 By default XML entity declarations and DTDs will raise a BadRequest
466 exception content but subclasses may choose to override this if
467 necessary.
468 """
469 if lxml is None:
470 raise ImproperlyConfigured(
471 "Usage of the XML aspects requires lxml and defusedxml.")
472
473 try:
474 # Stripping the encoding declaration. Because lxml.
475 # See http://lxml.de/parsing.html, "Python unicode strings".
476 contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(content) = XML_ENCODINGscip-python python django-tastypie 0.15.1 `tastypie.serializers`/XML_ENCODING..subscip-python python python-stdlib 3.11 re/Pattern#sub().('', contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(content))
477 parsed = parse_xml(
478 ioscip-python python python-stdlib 3.11 io/__init__:.StringIOscip-python python python-stdlib 3.11 io/StringIO#(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(content)),
479 forbid_dtd=forbid_dtdscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(forbid_dtd),
480 forbid_entities=forbid_entitiesscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(forbid_entities)
481 )
482 except (LxmlError, DefusedXmlException):
483 raise BadRequestscip-python python django-tastypie 0.15.1 `tastypie.exceptions`/BadRequest#()
484
485 return selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_xml().(self).from_etreescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_etree().(parsed.getroot())
486
487 def to_yamlscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(options)=None):
488 """
489 Given some Python data, produces YAML output.
490 """
491 optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(options) = optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(options) or {}
492
493 if yaml is None:
494 raise ImproperlyConfigured(
495 "Usage of the YAML aspects requires yaml.")
496
497 return yaml.dump(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_yaml().(options)))
498
499 def from_yamlscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_yaml().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_yaml().(self), contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_yaml().(content)):
500 """
501 Given some YAML data, returns a Python dictionary of the decoded data.
502 """
503 if yaml is None:
504 raise ImproperlyConfigured(
505 "Usage of the YAML aspects requires yaml.")
506
507 return yaml.load(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_yaml().(content), Loader=TastypieLoaderscip-python python django-tastypie 0.15.1 `tastypie.serializers`/TastypieLoader#)
508
509 def to_plistscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(self), datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(options)=None):
510 """
511 Given some Python data, produces binary plist output.
512 """
513 optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(options) = optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(options) or {}
514
515 if biplist is None:
516 raise ImproperlyConfigured(
517 "Usage of the plist aspects requires biplist.")
518
519 return biplist.writePlistToString(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(self).to_simplescip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_simple().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(data), optionsscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#to_plist().(options)))
520
521 def from_plistscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(selfscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(self), contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(content)):
522 """
523 Given some binary plist data, returns a Python dictionary of the
524 decoded data.
525 """
526 if biplist is None:
527 raise ImproperlyConfigured(
528 "Usage of the plist aspects requires biplist.")
529
530 if isinstance(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(content), strscip-python python python-stdlib 3.11 builtins/str#):
531 contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(content) = smart_bytes(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(content))
532
533 return biplist.readPlistFromString(contentscip-python python django-tastypie 0.15.1 `tastypie.serializers`/Serializer#from_plist().(content))
534
535
536 def get_type_stringscip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(data)):
537 """
538 Translates a Python data type into a string format.
539 """
540 data_type = typescip-python python python-stdlib 3.11 builtins/type#(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(data))
541
542 if data_type == intscip-python python python-stdlib 3.11 builtins/int#:
543 return 'integer'
544 elif data_type == floatscip-python python python-stdlib 3.11 builtins/float#:
545 return 'float'
546 elif data_type == boolscip-python python python-stdlib 3.11 builtins/bool#:
547 return 'boolean'
548 elif data_type in (listscip-python python python-stdlib 3.11 builtins/list#, tuplescip-python python python-stdlib 3.11 builtins/tuple#):
549 return 'list'
550 elif data_type == dictscip-python python python-stdlib 3.11 builtins/dict#:
551 return 'hash'
552 elif datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(data) is None:
553 return 'null'
554 elif isinstance(datascip-python python django-tastypie 0.15.1 `tastypie.serializers`/get_type_string().(data), strscip-python python python-stdlib 3.11 builtins/str#):
555 return 'string'