refactor: Drop dependency on decorator package (#7199)
This commit is contained in:
parent
21f467f062
commit
1a9a11176f
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from decorator import decorator, decorate
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -20,25 +19,29 @@ from ietf.utils.test_runner import set_coverage_checking
|
||||||
from ietf.person.models import Person, PersonalApiKey, PersonApiKeyEvent
|
from ietf.person.models import Person, PersonalApiKey, PersonApiKeyEvent
|
||||||
from ietf.utils import log
|
from ietf.utils import log
|
||||||
|
|
||||||
@decorator
|
def skip_coverage(f):
|
||||||
def skip_coverage(f, *args, **kwargs):
|
@wraps(f)
|
||||||
if settings.TEST_CODE_COVERAGE_CHECKER:
|
def _wrapper(*args, **kwargs):
|
||||||
set_coverage_checking(False)
|
if settings.TEST_CODE_COVERAGE_CHECKER:
|
||||||
result = f(*args, **kwargs)
|
set_coverage_checking(False)
|
||||||
set_coverage_checking(True)
|
result = f(*args, **kwargs)
|
||||||
return result
|
set_coverage_checking(True)
|
||||||
else:
|
return result
|
||||||
return f(*args, **kwargs)
|
else:
|
||||||
|
return f(*args, **kwargs)
|
||||||
|
return _wrapper
|
||||||
|
|
||||||
@decorator
|
def person_required(f):
|
||||||
def person_required(f, request, *args, **kwargs):
|
@wraps(f)
|
||||||
if not request.user.is_authenticated:
|
def _wrapper(request, *args, **kwargs):
|
||||||
raise ValueError("The @person_required decorator should be called after @login_required.")
|
if not request.user.is_authenticated:
|
||||||
try:
|
raise ValueError("The @person_required decorator should be called after @login_required.")
|
||||||
request.user.person
|
try:
|
||||||
except Person.DoesNotExist:
|
request.user.person
|
||||||
return render(request, 'registration/missing_person.html')
|
except Person.DoesNotExist:
|
||||||
return f(request, *args, **kwargs)
|
return render(request, 'registration/missing_person.html')
|
||||||
|
return f(request, *args, **kwargs)
|
||||||
|
return _wrapper
|
||||||
|
|
||||||
|
|
||||||
def require_api_key(f):
|
def require_api_key(f):
|
||||||
|
@ -90,29 +93,31 @@ def require_api_key(f):
|
||||||
return _wrapper
|
return _wrapper
|
||||||
|
|
||||||
|
|
||||||
def _memoize(func, self, *args, **kwargs):
|
|
||||||
'''Memoize wrapper for instance methods. Use @lru_cache for functions.'''
|
|
||||||
if kwargs: # frozenset is used to ensure hashability
|
|
||||||
key = args, frozenset(list(kwargs.items()))
|
|
||||||
else:
|
|
||||||
key = args
|
|
||||||
# instance method, set up cache if needed
|
|
||||||
if not hasattr(self, '_cache'):
|
|
||||||
self._cache = {}
|
|
||||||
if not func in self._cache:
|
|
||||||
self._cache[func] = {}
|
|
||||||
#
|
|
||||||
cache = self._cache[func]
|
|
||||||
if key not in cache:
|
|
||||||
cache[key] = func(self, *args, **kwargs)
|
|
||||||
return cache[key]
|
|
||||||
def memoize(func):
|
def memoize(func):
|
||||||
|
@wraps(func)
|
||||||
|
def _memoize(self, *args, **kwargs):
|
||||||
|
'''Memoize wrapper for instance methods. Use @lru_cache for functions.'''
|
||||||
|
if kwargs: # frozenset is used to ensure hashability
|
||||||
|
key = args, frozenset(list(kwargs.items()))
|
||||||
|
else:
|
||||||
|
key = args
|
||||||
|
# instance method, set up cache if needed
|
||||||
|
if not hasattr(self, '_cache'):
|
||||||
|
self._cache = {}
|
||||||
|
if not func in self._cache:
|
||||||
|
self._cache[func] = {}
|
||||||
|
#
|
||||||
|
cache = self._cache[func]
|
||||||
|
if key not in cache:
|
||||||
|
cache[key] = func(self, *args, **kwargs)
|
||||||
|
return cache[key]
|
||||||
|
|
||||||
if not hasattr(func, '__class__'):
|
if not hasattr(func, '__class__'):
|
||||||
raise NotImplementedError("Use @lru_cache instead of memoize() for functions.")
|
raise NotImplementedError("Use @lru_cache instead of memoize() for functions.")
|
||||||
# For methods, we want the cache on the object, not on the class, in order
|
# For methods, we want the cache on the object, not on the class, in order
|
||||||
# to not having to think about cache bloat and content becoming stale, so
|
# to not having to think about cache bloat and content becoming stale, so
|
||||||
# we cannot set up the cache here.
|
# we cannot set up the cache here.
|
||||||
return decorate(func, _memoize)
|
return _memoize
|
||||||
|
|
||||||
|
|
||||||
def ignore_view_kwargs(*args):
|
def ignore_view_kwargs(*args):
|
||||||
|
|
Loading…
Reference in a new issue