decorators.py 620 B

12345678910111213141516
  1. from .models import APIKey
  2. from rest_framework.decorators import api_view, throttle_classes
  3. from django.shortcuts import render, HttpResponse
  4. def api_permission_required(model, request_method):
  5. def decorator(view_func):
  6. def wrap(request, *args, **kwargs):
  7. header_key = request.META["HTTP_X_API_KEY"]
  8. db_key = APIKey.objects.get_from_key(header_key)
  9. if db_key.has_permission_method(model, request_method):
  10. return view_func(request, *args, **kwargs)
  11. else:
  12. return HttpResponse("NO ACCES")
  13. return wrap
  14. return decorator