1
- from collections .abc import Iterable
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import asdict
2
4
from functools import wraps
5
+ from typing import Optional , Callable , Union , List
6
+
3
7
from django import utils as django_utils
4
8
from django .core .cache import cache as django_cache
5
9
from django .utils .safestring import mark_safe
6
10
7
11
from easy import helper
8
12
13
+ Model : "django.db.models.Model"
9
14
10
15
def smart (** kwargs ):
11
16
"""
12
17
Simple decorator to get custom fields on admin class, using this you will use less line codes
13
18
14
- :param short_description: description of custom field
15
- :type str:
16
-
17
- :param admin_order_field: field to order on click
18
- :type str:
19
-
20
- :param allow_tags: allow html tags
21
- :type bool:
22
-
23
- :param boolean: if field is True, False or None
24
- :type bool:
25
-
26
- :param empty_value_display: Default value when field is null
27
- :type str:
28
-
29
- :return: method decorated
30
- :rtype: method
19
+ :param short_description: description of custom field (Optional[str])
20
+ :type short_description: str
21
+ :param admin_order_field: field to order on click (Optional[str])
22
+ :type admin_order_field: str
23
+ :param allow_tags: allow html tags (Optional[bool])
24
+ :type allow_tags: bool
25
+ :param boolean: if field boolean (Optional[bool])
26
+ :type boolean: bool
27
+ :param empty_value_display: Default value when field is null (Optional[str])
28
+ :type empty_value_display: str
29
+
30
+ :return: method decorated (Callable)
31
31
"""
32
32
33
33
def decorator (func ):
@@ -46,8 +46,15 @@ def decorator(func):
46
46
}
47
47
48
48
49
- def short (** kwargs ):
50
- def decorator (func ):
49
+ def short (** kwargs : Union [str , bool ]) -> Callable :
50
+ """
51
+ Short decorator to set some attrs on admin method.
52
+
53
+ :param kwargs: key-value pairs to set on method.
54
+ :return: method decorated (Callable)
55
+ """
56
+
57
+ def decorator (func : Callable ) :
51
58
for key , value in kwargs .items ():
52
59
if key in FUNCTION_MAP :
53
60
setattr (func , FUNCTION_MAP [key ], value )
@@ -64,8 +71,16 @@ def wrapper(*args, **kwargs):
64
71
return decorator
65
72
66
73
67
- def action (short_description , permission = None ):
68
- def decorator (func ):
74
+ def action (short_description : str , permission : Optional [Union [str , List [str ]]] = None ) -> Callable :
75
+ """
76
+ Action decorator to set some attrs on admin method.
77
+
78
+ :param short_description: description of custom field (str)
79
+ :param permission: permission to use. (Optional[Union[str, List[str]]])
80
+ :return: method decorated (Callable)
81
+ """
82
+
83
+ def decorator (func : Callable ) -> Callable :
69
84
func .short_description = short_description
70
85
if permission :
71
86
if isinstance (permission , str ):
@@ -77,8 +92,15 @@ def decorator(func):
77
92
return decorator
78
93
79
94
80
- def utils (django_utils_function ):
81
- def decorator (func ):
95
+ def utils (django_utils_function : str ) -> Callable [[Callable ], Callable ]:
96
+ """
97
+ Util decorator to apply a django.utils function on the method result.
98
+
99
+ :param django_utils_function: name of the function to apply (str)
100
+ :return: function decorated (Callable[[Callable], Callable])
101
+ """
102
+
103
+ def decorator (func : Callable ):
82
104
util_function = helper .deep_getattribute (django_utils , django_utils_function )
83
105
if isinstance (util_function , helper .Nothing ):
84
106
raise Exception ('Function {} not exist on django.utils module.' .format (django_utils_function ))
@@ -92,9 +114,17 @@ def wrapper(*args, **kwargs):
92
114
return decorator
93
115
94
116
95
- def filter (django_builtin_filter , load = None , * extra ): # noqa
117
+ def filter (django_builtin_filter : str , load : Optional [str ] = None , * extra : Union [str , List [str ]]) -> Callable [[Callable ], Callable ]:
118
+ """
119
+ Filter decorator to apply a django builtin filter on the method result.
96
120
97
- def decorator (func ):
121
+ :param django_builtin_filter: name of the filter to apply (str)
122
+ :param load: library to be loaded like load in templatetag. (Optional[str])
123
+ :param extra: extra arguments to pass to the filter. (Union[str, List[str]])
124
+ :return: method decorated (Callable[[Callable], Callable])
125
+ """
126
+
127
+ def decorator (func : Callable ) -> Callable :
98
128
filter_method = helper .get_django_filter (django_builtin_filter , load )
99
129
100
130
@wraps (func )
@@ -107,6 +137,10 @@ def wrapper(*args, **kwargs):
107
137
108
138
109
139
def with_tags ():
140
+ """
141
+ Decorator to mark result of method as safe and allow tags.
142
+ """
143
+
110
144
def decorator (func ):
111
145
@wraps (func )
112
146
def wrapper (* args , ** kwargs ):
@@ -115,8 +149,15 @@ def wrapper(*args, **kwargs):
115
149
return decorator
116
150
117
151
118
- def cache (seconds = 60 ):
119
- def decorator (func ):
152
+ def cache (seconds : int = 60 ):
153
+ """
154
+ Cache decorator to cache the result of a method.
155
+
156
+ :param seconds: The cache time in seconds. (int)
157
+ :return: The cached method
158
+ """
159
+
160
+ def decorator (func : Callable ) -> Callable :
120
161
@wraps (func )
121
162
def wrapper (admin , model ):
122
163
cache_method_key = helper .cache_method_key (model , func .__name__ )
@@ -135,8 +176,14 @@ def wrapper(admin, model):
135
176
return decorator
136
177
137
178
138
- def clear_cache (model ):
179
+ def clear_cache (model : Model ) -> None :
180
+ """
181
+ Clear cache for specific model.
182
+
183
+ :param model: The model to clear cache for.
184
+ :type model: django.db.models.Model
185
+ """
139
186
cache_object_key = helper .cache_object_key (model )
140
187
obj_methods_caches = django_cache .get (cache_object_key )
141
- methods_key = obj_methods_caches .split ('|' )
188
+ methods_key = obj_methods_caches .split ('|' ) if obj_methods_caches else []
142
189
django_cache .delete_many (methods_key )
0 commit comments