Skip to content

Commit a416516

Browse files
TayHobbskhornberg
authored andcommitted
Feature: Allow bulk creation of objects
1 parent 71637c0 commit a416516

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
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: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,38 +54,45 @@ def parse(self, stream, media_type=None, parser_context=None):
5454
data = result.get('data')
5555

5656
if data:
57-
from rest_framework_json_api.views import RelationshipView
58-
if isinstance(parser_context['view'], RelationshipView):
59-
# We skip parsing the object as JSONAPI Resource Identifier Object and not a regular Resource Object
60-
if isinstance(data, list):
61-
for resource_identifier_object in data:
62-
if not (resource_identifier_object.get('id') and resource_identifier_object.get('type')):
63-
raise ParseError(
64-
'Received data contains one or more malformed JSONAPI Resource Identifier Object(s)'
65-
)
66-
elif not (data.get('id') and data.get('type')):
67-
raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object')
57+
if isinstance(data, list):
58+
parsed_items = []
59+
for d in data:
60+
parsed_items.append(self.parse_data(d, stream, media_type, parser_context))
61+
return parsed_items
62+
return self.parse_data(data, stream, media_type, parser_context)
63+
else:
64+
raise ParseError('Received document does not contain primary data')
6865

69-
return data
66+
def parse_data(self, data, stream, media_type, parser_context):
67+
from rest_framework_json_api.views import RelationshipView
68+
if isinstance(parser_context['view'], RelationshipView):
69+
# We skip parsing the object as JSONAPI Resource Identifier Object and not a regular Resource Object
70+
if isinstance(data, list):
71+
for resource_identifier_object in data:
72+
if not (resource_identifier_object.get('id') and resource_identifier_object.get('type')):
73+
raise ParseError(
74+
'Received data contains one or more malformed JSONAPI Resource Identifier Object(s)'
75+
)
76+
elif not (data.get('id') and data.get('type')):
77+
raise ParseError('Received data is not a valid JSONAPI Resource Identifier Object')
7078

71-
request = parser_context.get('request')
79+
return data
7280

73-
# Check for inconsistencies
74-
resource_name = utils.get_resource_name(parser_context)
75-
if data.get('type') != resource_name and request.method in ('PUT', 'POST', 'PATCH'):
76-
raise exceptions.Conflict(
77-
"The resource object's type ({data_type}) is not the type "
78-
"that constitute the collection represented by the endpoint ({resource_type}).".format(
79-
data_type=data.get('type'),
80-
resource_type=resource_name
81-
)
82-
)
81+
request = parser_context.get('request')
8382

84-
# Construct the return data
85-
parsed_data = {'id': data.get('id')}
86-
parsed_data.update(self.parse_attributes(data))
87-
parsed_data.update(self.parse_relationships(data))
88-
return parsed_data
83+
# Check for inconsistencies
84+
resource_name = utils.get_resource_name(parser_context)
85+
if data.get('type') != resource_name and request.method in ('PUT', 'POST', 'PATCH'):
86+
raise exceptions.Conflict(
87+
"The resource object's type ({data_type}) is not the type "
88+
"that constitute the collection represented by the endpoint ({resource_type}).".format(
89+
data_type=data.get('type'),
90+
resource_type=resource_name
91+
)
92+
)
8993

90-
else:
91-
raise ParseError('Received document does not contain primary data')
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+
return parsed_data

0 commit comments

Comments
 (0)