Skip to content

Commit 9aa2564

Browse files
committed
update after_xxxx hook to be able to create a custom response
1 parent b1b98e8 commit 9aa2564

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

flask_rest_jsonapi/resource.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ def get(self, *args, **kwargs):
128128

129129
result.update({'meta': {'count': objects_count}})
130130

131-
self.after_get(result)
131+
final_result = self.after_get(result)
132132

133-
return result
133+
return final_result
134134

135135
@check_method_requirements
136136
def post(self, *args, **kwargs):
@@ -171,30 +171,30 @@ def post(self, *args, **kwargs):
171171

172172
result = schema.dump(obj).data
173173

174-
self.after_post(result)
175-
176174
if result['data'].get('links', {}).get('self'):
177175
final_result = (result, 201, {'Location': result['data']['links']['self']})
178176
else:
179177
final_result = (result, 201)
180178

181-
return final_result
179+
result = self.after_post(final_result)
180+
181+
return result
182182

183183
def before_get(self, args, kwargs):
184184
"""Hook to make custom work before get method"""
185185
pass
186186

187187
def after_get(self, result):
188188
"""Hook to make custom work after get method"""
189-
pass
189+
return result
190190

191191
def before_post(self, args, kwargs, data=None):
192192
"""Hook to make custom work before post method"""
193193
pass
194194

195195
def after_post(self, result):
196196
"""Hook to make custom work after post method"""
197-
pass
197+
return result
198198

199199
def before_marshmallow(self, args, kwargs):
200200
pass
@@ -227,9 +227,9 @@ def get(self, *args, **kwargs):
227227

228228
result = schema.dump(obj).data
229229

230-
self.after_get(result)
230+
final_result = self.after_get(result)
231231

232-
return result
232+
return final_result
233233

234234
@check_method_requirements
235235
def patch(self, *args, **kwargs):
@@ -281,9 +281,9 @@ def patch(self, *args, **kwargs):
281281

282282
result = schema.dump(obj).data
283283

284-
self.after_patch(result)
284+
final_result = self.after_patch(result)
285285

286-
return result
286+
return final_result
287287

288288
@check_method_requirements
289289
def delete(self, *args, **kwargs):
@@ -294,33 +294,33 @@ def delete(self, *args, **kwargs):
294294

295295
result = {'meta': {'message': 'Object successfully deleted'}}
296296

297-
self.after_delete(result)
297+
final_result = self.after_delete(result)
298298

299-
return result
299+
return final_result
300300

301301
def before_get(self, args, kwargs):
302302
"""Hook to make custom work before get method"""
303303
pass
304304

305305
def after_get(self, result):
306306
"""Hook to make custom work after get method"""
307-
pass
307+
return result
308308

309309
def before_patch(self, args, kwargs, data=None):
310310
"""Hook to make custom work before patch method"""
311311
pass
312312

313313
def after_patch(self, result):
314314
"""Hook to make custom work after patch method"""
315-
pass
315+
return result
316316

317317
def before_delete(self, args, kwargs):
318318
"""Hook to make custom work before delete method"""
319319
pass
320320

321321
def after_delete(self, result):
322322
"""Hook to make custom work after delete method"""
323-
pass
323+
return result
324324

325325
def before_marshmallow(self, args, kwargs):
326326
pass
@@ -365,9 +365,9 @@ def get(self, *args, **kwargs):
365365
serialized_obj = schema.dump(obj)
366366
result['included'] = serialized_obj.data.get('included', dict())
367367

368-
self.after_get(result)
368+
final_result = self.after_get(result)
369369

370-
return result
370+
return final_result
371371

372372
@check_method_requirements
373373
def post(self, *args, **kwargs):
@@ -409,9 +409,9 @@ def post(self, *args, **kwargs):
409409
result = ''
410410
status_code = 204
411411

412-
self.after_post(result)
412+
final_result = self.after_post(result, status_code)
413413

414-
return result, status_code
414+
return final_result
415415

416416
@check_method_requirements
417417
def patch(self, *args, **kwargs):
@@ -453,9 +453,9 @@ def patch(self, *args, **kwargs):
453453
result = ''
454454
status_code = 204
455455

456-
self.after_patch(result)
456+
final_result = self.after_patch(result, status_code)
457457

458-
return result, status_code
458+
return final_result
459459

460460
@check_method_requirements
461461
def delete(self, *args, **kwargs):
@@ -497,9 +497,9 @@ def delete(self, *args, **kwargs):
497497
result = ''
498498
status_code = 204
499499

500-
self.after_delete(result)
500+
final_result = self.after_delete(result, status_code)
501501

502-
return result, status_code
502+
return final_result
503503

504504
def _get_relationship_data(self):
505505
"""Get useful data for relationship management"""
@@ -520,28 +520,28 @@ def before_get(self, args, kwargs):
520520

521521
def after_get(self, result):
522522
"""Hook to make custom work after get method"""
523-
pass
523+
return result
524524

525525
def before_post(self, args, kwargs, json_data=None):
526526
"""Hook to make custom work before post method"""
527527
pass
528528

529-
def after_post(self, result):
529+
def after_post(self, result, status_code):
530530
"""Hook to make custom work after post method"""
531-
pass
531+
return result, status_code
532532

533533
def before_patch(self, args, kwargs, json_data=None):
534534
"""Hook to make custom work before patch method"""
535535
pass
536536

537-
def after_patch(self, result):
537+
def after_patch(self, result, status_code):
538538
"""Hook to make custom work after patch method"""
539-
pass
539+
return result, status_code
540540

541541
def before_delete(self, args, kwargs, json_data=None):
542542
"""Hook to make custom work before delete method"""
543543
pass
544544

545-
def after_delete(self, result):
545+
def after_delete(self, result, status_code):
546546
"""Hook to make custom work after delete method"""
547-
pass
547+
return result, status_code

0 commit comments

Comments
 (0)