refactor: Implement require_api_key with functools.wraps
The @decorator mechanism does not seem to work with @method_decorator in Django 4.0, have not tracked down why.
This commit is contained in:
parent
223c679942
commit
8cf609bfa9
|
@ -5,6 +5,7 @@
|
|||
import datetime
|
||||
|
||||
from decorator import decorator, decorate
|
||||
from functools import wraps
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import login
|
||||
|
@ -39,9 +40,10 @@ def person_required(f, request, *args, **kwargs):
|
|||
return render(request, 'registration/missing_person.html')
|
||||
return f(request, *args, **kwargs)
|
||||
|
||||
@decorator
|
||||
def require_api_key(f, request, *args, **kwargs):
|
||||
|
||||
def require_api_key(f):
|
||||
@wraps(f)
|
||||
def _wrapper(request, *args, **kwargs):
|
||||
def err(code, text):
|
||||
return HttpResponse(text, status=code, content_type='text/plain')
|
||||
# Check method and get hash
|
||||
|
@ -85,6 +87,7 @@ def require_api_key(f, request, *args, **kwargs):
|
|||
log.log("Bad API call: args: %s, kwargs: %s, exception: %s" % (args, kwargs, e))
|
||||
return err(400, "Bad or missing parameters")
|
||||
return ret
|
||||
return _wrapper
|
||||
|
||||
|
||||
def _memoize(func, self, *args, **kwargs):
|
||||
|
|
|
@ -11,7 +11,9 @@ from django.utils.encoding import force_str
|
|||
from django.views.generic import View
|
||||
|
||||
def url(regex, view, kwargs=None, name=None):
|
||||
if callable(view) and hasattr(view, '__name__'):
|
||||
if hasattr(view, "view_class"):
|
||||
view_name = "%s.%s" % (view.__module__, view.view_class.__name__)
|
||||
elif callable(view) and hasattr(view, '__name__'):
|
||||
view_name = "%s.%s" % (view.__module__, view.__name__)
|
||||
else:
|
||||
view_name = regex
|
||||
|
|
Loading…
Reference in a new issue