Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit f2cb7e9

Browse files
committed
make incomming data available to update them in before_post and before_patch and also for relationship resource manager
1 parent 8389361 commit f2cb7e9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

flask_rest_jsonapi/resource.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ def get(self, *args, **kwargs):
137137
def post(self, *args, **kwargs):
138138
"""Create an object
139139
"""
140-
self.before_post(args, kwargs)
141-
142140
json_data = request.get_json()
143141

144142
qs = QSManager(request.args, self.schema)
@@ -169,6 +167,8 @@ def post(self, *args, **kwargs):
169167
error['title'] = "Validation error"
170168
return errors, 422
171169

170+
self.before_post(args, kwargs, data=data)
171+
172172
obj = self._data_layer.create_object(data, kwargs)
173173

174174
result = schema.dump(obj).data
@@ -181,7 +181,7 @@ def before_get(self, args, kwargs):
181181
def after_get(self, result):
182182
pass
183183

184-
def before_post(self, args, kwargs):
184+
def before_post(self, args, kwargs, data=None):
185185
pass
186186

187187
def after_post(self, result):
@@ -214,8 +214,6 @@ def get(self, *args, **kwargs):
214214
def patch(self, *args, **kwargs):
215215
"""Update an object
216216
"""
217-
self.before_patch(args, kwargs)
218-
219217
json_data = request.get_json()
220218

221219
qs = QSManager(request.args, self.schema)
@@ -253,6 +251,8 @@ def patch(self, *args, **kwargs):
253251
if json_data['data']['id'] != str(kwargs[self.data_layer.get('url_field', 'id')]):
254252
raise BadRequest('/data/id', 'Value of id does not match the resource identifier in url')
255253

254+
self.before_patch(args, kwargs, data=data)
255+
256256
obj = self._data_layer.get_object(kwargs)
257257
self._data_layer.update_object(obj, data, kwargs)
258258

@@ -280,7 +280,7 @@ def before_get(self, args, kwargs):
280280
def after_get(self, result):
281281
pass
282282

283-
def before_patch(self, args, kwargs):
283+
def before_patch(self, args, kwargs, data=None):
284284
pass
285285

286286
def after_patch(self, result):
@@ -335,8 +335,6 @@ def get(self, *args, **kwargs):
335335
def post(self, *args, **kwargs):
336336
"""Add / create relationship(s)
337337
"""
338-
self.before_post(args, kwargs)
339-
340338
json_data = request.get_json()
341339

342340
relationship_field, model_relationship_field, related_type_, related_id_field = self._get_relationship_data()
@@ -359,6 +357,8 @@ def post(self, *args, **kwargs):
359357
if obj['type'] != related_type_:
360358
raise InvalidType('/data/type', 'The type provided does not match the resource type')
361359

360+
self.before_post(args, kwargs, json_data=json_data)
361+
362362
obj_, updated = self._data_layer.create_relationship(json_data,
363363
model_relationship_field,
364364
related_id_field,
@@ -383,8 +383,6 @@ def post(self, *args, **kwargs):
383383
def patch(self, *args, **kwargs):
384384
"""Update a relationship
385385
"""
386-
self.before_patch(args, kwargs)
387-
388386
json_data = request.get_json()
389387

390388
relationship_field, model_relationship_field, related_type_, related_id_field = self._get_relationship_data()
@@ -407,6 +405,8 @@ def patch(self, *args, **kwargs):
407405
if obj['type'] != related_type_:
408406
raise InvalidType('/data/type', 'The type provided does not match the resource type')
409407

408+
self.before_patch(args, kwargs, json_data=json_data)
409+
410410
obj_, updated = self._data_layer.update_relationship(json_data,
411411
model_relationship_field,
412412
related_id_field,
@@ -431,8 +431,6 @@ def patch(self, *args, **kwargs):
431431
def delete(self, *args, **kwargs):
432432
"""Delete relationship(s)
433433
"""
434-
self.before_delete(args, kwargs)
435-
436434
json_data = request.get_json()
437435

438436
relationship_field, model_relationship_field, related_type_, related_id_field = self._get_relationship_data()
@@ -455,6 +453,8 @@ def delete(self, *args, **kwargs):
455453
if obj['type'] != related_type_:
456454
raise InvalidType('/data/type', 'The type provided does not match the resource type')
457455

456+
self.before_delete(args, kwargs, json_data=json_data)
457+
458458
obj_, updated = self._data_layer.delete_relationship(json_data,
459459
model_relationship_field,
460460
related_id_field,
@@ -493,19 +493,19 @@ def before_get(self, args, kwargs):
493493
def after_get(self, result):
494494
pass
495495

496-
def before_post(self, args, kwargs):
496+
def before_post(self, args, kwargs, json_data=None):
497497
pass
498498

499499
def after_post(self, result):
500500
pass
501501

502-
def before_patch(self, args, kwargs):
502+
def before_patch(self, args, kwargs, json_data=None):
503503
pass
504504

505505
def after_patch(self, result):
506506
pass
507507

508-
def before_delete(self, args, kwargs):
508+
def before_delete(self, args, kwargs, json_data=None):
509509
pass
510510

511511
def after_delete(self, result):

0 commit comments

Comments
 (0)