Skip to content

Commit 9e50c1c

Browse files
TayHobbskhornberg
authored andcommitted
Feature: Allow bulk creation of objects
1 parent 63d0122 commit 9e50c1c

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

rest_framework_json_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22

33
__title__ = 'djangorestframework-jsonapi'
4-
__version__ = '2.1.0'
4+
__version__ = '2.2.0'
55
__author__ = ''
66
__license__ = 'MIT'
77
__copyright__ = ''

rest_framework_json_api/parsers.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,41 +62,48 @@ def parse(self, stream, media_type=None, parser_context=None):
6262
data = result.get('data')
6363

6464
if data:
65-
from rest_framework_json_api.views import RelationshipView
66-
if isinstance(parser_context['view'], RelationshipView):
67-
# We skip parsing the object as JSONAPI Resource Identifier Object and not a regular Resource Object
68-
if isinstance(data, list):
69-
for resource_identifier_object in data:
70-
if not (resource_identifier_object.get('id') and resource_identifier_object.get('type')):
71-
raise ParseError(
72-
'Received data contains one or more malformed JSONAPI Resource Identifier Object(s)'
73-
)
74-
elif not (data.get('id') and data.get('type')):
75-
raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object')
76-
77-
return data
78-
79-
request = parser_context.get('request')
80-
81-
# Check for inconsistencies
82-
resource_name = utils.get_resource_name(parser_context)
83-
if data.get('type') != resource_name and request.method in ('PUT', 'POST', 'PATCH'):
84-
raise exceptions.Conflict(
85-
"The resource object's type ({data_type}) is not the type "
86-
"that constitute the collection represented by the endpoint ({resource_type}).".format(
87-
data_type=data.get('type'),
88-
resource_type=resource_name
89-
)
90-
)
91-
if not data.get('id') and request.method in ('PATCH', 'PUT'):
92-
raise ParseError("The resource identifier object must contain an 'id' member")
93-
94-
# Construct the return data
95-
parsed_data = {'id': data.get('id')}
96-
parsed_data.update(self.parse_attributes(data))
97-
parsed_data.update(self.parse_relationships(data))
98-
parsed_data.update(self.parse_metadata(result))
99-
return parsed_data
100-
65+
if isinstance(data, list):
66+
parsed_items = []
67+
for d in data:
68+
parsed_items.append(self.parse_data(d, stream, media_type, parser_context, result))
69+
return parsed_items
70+
return self.parse_data(data, stream, media_type, parser_context, result)
10171
else:
10272
raise ParseError('Received document does not contain primary data')
73+
74+
def parse_data(self, data, stream, media_type, parser_context, result):
75+
from rest_framework_json_api.views import RelationshipView
76+
if isinstance(parser_context['view'], RelationshipView):
77+
# We skip parsing the object as JSONAPI Resource Identifier Object and not a regular Resource Object
78+
if isinstance(data, list):
79+
for resource_identifier_object in data:
80+
if not (resource_identifier_object.get('id') and resource_identifier_object.get('type')):
81+
raise ParseError(
82+
'Received data contains one or more malformed JSONAPI Resource Identifier Object(s)'
83+
)
84+
elif not (data.get('id') and data.get('type')):
85+
raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object')
86+
87+
return data
88+
89+
request = parser_context.get('request')
90+
91+
# Check for inconsistencies
92+
resource_name = utils.get_resource_name(parser_context)
93+
if data.get('type') != resource_name and request.method in ('PUT', 'POST', 'PATCH'):
94+
raise exceptions.Conflict(
95+
"The resource object's type ({data_type}) is not the type "
96+
"that constitute the collection represented by the endpoint ({resource_type}).".format(
97+
data_type=data.get('type'),
98+
resource_type=resource_name
99+
)
100+
)
101+
if not data.get('id') and request.method in ('PATCH', 'PUT'):
102+
raise ParseError("The resource identifier object must contain an 'id' member")
103+
104+
# Construct the return data
105+
parsed_data = {'id': data.get('id')}
106+
parsed_data.update(self.parse_attributes(data))
107+
parsed_data.update(self.parse_relationships(data))
108+
parsed_data.update(self.parse_metadata(result))
109+
return parsed_data

0 commit comments

Comments
 (0)