Last active
December 26, 2017 09:02
-
-
Save mazlum/413d61a982e2ffef4b644c1d1424c19f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import wraps | |
| import json | |
| from django.http.response import JsonResponse | |
| from django.utils.decorators import available_attrs | |
| def custom_decorator(view_func): | |
| def wrapped_view(request, *args, **kwargs): | |
| if request.method == "POST": | |
| try: | |
| data = json.loads(request.body.decode('utf-8')) | |
| except ValueError: | |
| return JsonResponse({'message': 'Invalid JSON Parameters'}) | |
| token = request_data.get('token', None) | |
| if not token: | |
| return JsonResponse({'message': 'Invalid token'}) | |
| kwargs['data'] = data | |
| return view_func(request, *args, **kwargs) | |
| return JsonResponse({'message': 'Invalid request type'}) | |
| return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment