forked from django-json-api/django-rest-framework-json-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
262 lines (208 loc) · 8.8 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
"""
Utils.
"""
import copy
from collections import OrderedDict
import inflection
from django.conf import settings
from django.utils import six
from django.utils.module_loading import import_string as import_class_from_dotted_path
from django.utils.translation import ugettext_lazy as _
from rest_framework.exceptions import APIException
try:
from rest_framework.serializers import ManyRelatedField
except ImportError:
ManyRelatedField = type(None)
try:
from rest_framework_nested.relations import HyperlinkedRouterField
except ImportError:
HyperlinkedRouterField = type(None)
def get_resource_name(context):
"""
Return the name of a resource.
"""
view = context.get('view')
# Sanity check to make sure we have a view.
if not view:
raise APIException(_('Could not find view.'))
# Check to see if there is a status code and return early
# with the resource_name value of `errors`.
try:
code = str(view.response.status_code)
except (AttributeError, ValueError):
pass
else:
if code.startswith('4') or code.startswith('5'):
return 'errors'
try:
resource_name = getattr(view, 'resource_name')
except AttributeError:
try:
serializer = view.get_serializer_class()
return get_resource_type_from_serializer(serializer)
except AttributeError:
try:
resource_name = get_resource_type_from_model(view.model)
except AttributeError:
resource_name = view.__class__.__name__
if not isinstance(resource_name, six.string_types):
# The resource name is not a string - return as is
return resource_name
# the name was calculated automatically from the view > pluralize and format
resource_name = format_relation_name(resource_name)
return resource_name
def get_serializer_fields(serializer):
fields = None
if hasattr(serializer, 'child'):
fields = getattr(serializer.child, 'fields')
meta = getattr(serializer.child, 'Meta', None)
if hasattr(serializer, 'fields'):
fields = getattr(serializer, 'fields')
meta = getattr(serializer, 'Meta', None)
if fields:
meta_fields = getattr(meta, 'meta_fields', {})
for field in meta_fields:
try:
fields.pop(field)
except KeyError:
pass
return fields
def format_keys(obj, format_type=None):
"""
Takes either a dict or list and returns it with camelized keys only if
JSON_API_FORMAT_KEYS is set.
:format_type: Either 'dasherize', 'camelize' or 'underscore'
"""
if format_type is None:
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
if format_type in ('dasherize', 'camelize', 'underscore', 'capitalize'):
if isinstance(obj, dict):
formatted = OrderedDict()
for key, value in obj.items():
if format_type == 'dasherize':
# inflection can't dasherize camelCase
key = inflection.underscore(key)
formatted[inflection.dasherize(key)] \
= format_keys(value, format_type)
elif format_type == 'camelize':
formatted[inflection.camelize(key, False)] \
= format_keys(value, format_type)
elif format_type == 'capitalize':
formatted[inflection.camelize(key)] \
= format_keys(value, format_type)
elif format_type == 'underscore':
formatted[inflection.underscore(key)] \
= format_keys(value, format_type)
return formatted
if isinstance(obj, list):
return [format_keys(item, format_type) for item in obj]
else:
return obj
else:
return obj
def format_value(value, format_type=None):
if format_type is None:
format_type = getattr(settings, 'JSON_API_FORMAT_KEYS', False)
if format_type == 'dasherize':
# inflection can't dasherize camelCase
value = inflection.underscore(value)
value = inflection.dasherize(value)
elif format_type == 'camelize':
value = inflection.camelize(value, False)
elif format_type == 'capitalize':
value = inflection.camelize(value)
elif format_type == 'underscore':
value = inflection.underscore(value)
return value
def format_relation_name(value, format_type=None):
if format_type is None:
format_type = getattr(settings, 'JSON_API_FORMAT_RELATION_KEYS', False)
pluralize = getattr(settings, 'JSON_API_PLURALIZE_RELATION_TYPE', False)
if format_type:
# format_type will never be None here so we can use format_value
value = format_value(value, format_type)
return inflection.pluralize(value) if pluralize else value
def parse_relation_name(value, format_type=None):
if format_type is None:
format_type = getattr(settings, 'JSON_API_PARSE_INCLUDE_KEYS', False)
singularize = getattr(settings, 'JSON_API_SINGULARIZE_INCLUDE_TYPE', False)
if format_type:
value = format_value(value, format_type)
return inflection.singularize(value) if singularize else value
def get_related_resource_type(relation):
if hasattr(relation, '_meta'):
relation_model = relation._meta.model
elif hasattr(relation, 'model'):
# the model type was explicitly passed as a kwarg to ResourceRelatedField
relation_model = relation.model
elif hasattr(relation, 'get_queryset') and relation.get_queryset() is not None:
relation_model = relation.get_queryset().model
else:
parent_serializer = relation.parent
if hasattr(parent_serializer, 'Meta'):
parent_model = parent_serializer.Meta.model
else:
parent_model = parent_serializer.parent.Meta.model
if relation.source:
if relation.source != '*':
parent_model_relation = getattr(parent_model, relation.source)
else:
parent_model_relation = getattr(parent_model, relation.field_name)
else:
parent_model_relation = getattr(parent_model, parent_serializer.field_name)
if hasattr(parent_model_relation, 'related'):
try:
relation_model = parent_model_relation.related.related_model
except AttributeError:
# Django 1.7
relation_model = parent_model_relation.related.model
elif hasattr(parent_model_relation, 'field'):
relation_model = parent_model_relation.field.related.model
else:
return get_related_resource_type(parent_model_relation)
return get_resource_type_from_model(relation_model)
def get_instance_or_manager_resource_type(resource_instance_or_manager):
if hasattr(resource_instance_or_manager, 'model'):
return get_resource_type_from_manager(resource_instance_or_manager)
if hasattr(resource_instance_or_manager, '_meta'):
return get_resource_type_from_instance(resource_instance_or_manager)
pass
def get_resource_type_from_model(model):
json_api_meta = getattr(model, 'JSONAPIMeta', None)
return getattr(
json_api_meta,
'resource_name',
format_relation_name(model.__name__))
def get_resource_type_from_queryset(qs):
return get_resource_type_from_model(qs.model)
def get_resource_type_from_instance(instance):
return get_resource_type_from_model(instance._meta.model)
def get_resource_type_from_manager(manager):
return get_resource_type_from_model(manager.model)
def get_resource_type_from_serializer(serializer):
return getattr(
serializer.Meta,
'resource_name',
get_resource_type_from_model(serializer.Meta.model))
def get_included_serializers(serializer):
included_serializers = copy.copy(getattr(serializer, 'included_serializers', dict()))
for name, value in six.iteritems(included_serializers):
if not isinstance(value, type):
if value == 'self':
included_serializers[name] = serializer if isinstance(serializer, type) else serializer.__class__
else:
included_serializers[name] = import_class_from_dotted_path(value)
return included_serializers
class Hyperlink(six.text_type):
"""
A string like object that additionally has an associated name.
We use this for hyperlinked URLs that may render as a named link
in some contexts, or render as a plain URL in others.
Comes from Django REST framework 3.2
https://github.com/tomchristie/django-rest-framework
"""
def __new__(self, url, name):
ret = six.text_type.__new__(self, url)
ret.name = name
return ret
is_hyperlink = True