Skip to content

Instantly share code, notes, and snippets.

@mazlum
Last active December 26, 2017 09:02
Show Gist options
  • Select an option

  • Save mazlum/413d61a982e2ffef4b644c1d1424c19f to your computer and use it in GitHub Desktop.

Select an option

Save mazlum/413d61a982e2ffef4b644c1d1424c19f to your computer and use it in GitHub Desktop.
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