|
4 | 4 |
|
5 | 5 | import datetime |
6 | 6 |
|
7 | | -from decorator import decorator, decorate |
8 | 7 | from functools import wraps |
9 | 8 |
|
10 | 9 | from django.conf import settings |
|
20 | 19 | from ietf.person.models import Person, PersonalApiKey, PersonApiKeyEvent |
21 | 20 | from ietf.utils import log |
22 | 21 |
|
23 | | -@decorator |
24 | | -def skip_coverage(f, *args, **kwargs): |
25 | | - if settings.TEST_CODE_COVERAGE_CHECKER: |
26 | | - set_coverage_checking(False) |
27 | | - result = f(*args, **kwargs) |
28 | | - set_coverage_checking(True) |
29 | | - return result |
30 | | - else: |
31 | | - return f(*args, **kwargs) |
32 | | - |
33 | | -@decorator |
34 | | -def person_required(f, request, *args, **kwargs): |
35 | | - if not request.user.is_authenticated: |
36 | | - raise ValueError("The @person_required decorator should be called after @login_required.") |
37 | | - try: |
38 | | - request.user.person |
39 | | - except Person.DoesNotExist: |
40 | | - return render(request, 'registration/missing_person.html') |
41 | | - return f(request, *args, **kwargs) |
| 22 | +def skip_coverage(f): |
| 23 | + @wraps(f) |
| 24 | + def _wrapper(*args, **kwargs): |
| 25 | + if settings.TEST_CODE_COVERAGE_CHECKER: |
| 26 | + set_coverage_checking(False) |
| 27 | + result = f(*args, **kwargs) |
| 28 | + set_coverage_checking(True) |
| 29 | + return result |
| 30 | + else: |
| 31 | + return f(*args, **kwargs) |
| 32 | + return _wrapper |
| 33 | + |
| 34 | +def person_required(f): |
| 35 | + @wraps(f) |
| 36 | + def _wrapper(request, *args, **kwargs): |
| 37 | + if not request.user.is_authenticated: |
| 38 | + raise ValueError("The @person_required decorator should be called after @login_required.") |
| 39 | + try: |
| 40 | + request.user.person |
| 41 | + except Person.DoesNotExist: |
| 42 | + return render(request, 'registration/missing_person.html') |
| 43 | + return f(request, *args, **kwargs) |
| 44 | + return _wrapper |
42 | 45 |
|
43 | 46 |
|
44 | 47 | def require_api_key(f): |
@@ -90,29 +93,31 @@ def err(code, text): |
90 | 93 | return _wrapper |
91 | 94 |
|
92 | 95 |
|
93 | | -def _memoize(func, self, *args, **kwargs): |
94 | | - '''Memoize wrapper for instance methods. Use @lru_cache for functions.''' |
95 | | - if kwargs: # frozenset is used to ensure hashability |
96 | | - key = args, frozenset(list(kwargs.items())) |
97 | | - else: |
98 | | - key = args |
99 | | - # instance method, set up cache if needed |
100 | | - if not hasattr(self, '_cache'): |
101 | | - self._cache = {} |
102 | | - if not func in self._cache: |
103 | | - self._cache[func] = {} |
104 | | - # |
105 | | - cache = self._cache[func] |
106 | | - if key not in cache: |
107 | | - cache[key] = func(self, *args, **kwargs) |
108 | | - return cache[key] |
109 | 96 | def memoize(func): |
| 97 | + @wraps(func) |
| 98 | + def _memoize(self, *args, **kwargs): |
| 99 | + '''Memoize wrapper for instance methods. Use @lru_cache for functions.''' |
| 100 | + if kwargs: # frozenset is used to ensure hashability |
| 101 | + key = args, frozenset(list(kwargs.items())) |
| 102 | + else: |
| 103 | + key = args |
| 104 | + # instance method, set up cache if needed |
| 105 | + if not hasattr(self, '_cache'): |
| 106 | + self._cache = {} |
| 107 | + if not func in self._cache: |
| 108 | + self._cache[func] = {} |
| 109 | + # |
| 110 | + cache = self._cache[func] |
| 111 | + if key not in cache: |
| 112 | + cache[key] = func(self, *args, **kwargs) |
| 113 | + return cache[key] |
| 114 | + |
110 | 115 | if not hasattr(func, '__class__'): |
111 | 116 | raise NotImplementedError("Use @lru_cache instead of memoize() for functions.") |
112 | 117 | # For methods, we want the cache on the object, not on the class, in order |
113 | 118 | # to not having to think about cache bloat and content becoming stale, so |
114 | 119 | # we cannot set up the cache here. |
115 | | - return decorate(func, _memoize) |
| 120 | + return _memoize |
116 | 121 |
|
117 | 122 |
|
118 | 123 | def ignore_view_kwargs(*args): |
|
0 commit comments