Skip to content

Commit 29045fc

Browse files
authored
Docstring (#88) Thanks https://codeium.com for the great pycharm plugin :)
1 parent 11f98df commit 29045fc

File tree

8 files changed

+451
-101
lines changed

8 files changed

+451
-101
lines changed

Diff for: easy/admin/decorators.py

+77-30
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
from collections.abc import Iterable
1+
from __future__ import annotations
2+
3+
from dataclasses import asdict
24
from functools import wraps
5+
from typing import Optional, Callable, Union, List
6+
37
from django import utils as django_utils
48
from django.core.cache import cache as django_cache
59
from django.utils.safestring import mark_safe
610

711
from easy import helper
812

13+
Model: "django.db.models.Model"
914

1015
def smart(**kwargs):
1116
"""
1217
Simple decorator to get custom fields on admin class, using this you will use less line codes
1318
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)
3131
"""
3232

3333
def decorator(func):
@@ -46,8 +46,15 @@ def decorator(func):
4646
}
4747

4848

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) :
5158
for key, value in kwargs.items():
5259
if key in FUNCTION_MAP:
5360
setattr(func, FUNCTION_MAP[key], value)
@@ -64,8 +71,16 @@ def wrapper(*args, **kwargs):
6471
return decorator
6572

6673

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:
6984
func.short_description = short_description
7085
if permission:
7186
if isinstance(permission, str):
@@ -77,8 +92,15 @@ def decorator(func):
7792
return decorator
7893

7994

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):
82104
util_function = helper.deep_getattribute(django_utils, django_utils_function)
83105
if isinstance(util_function, helper.Nothing):
84106
raise Exception('Function {} not exist on django.utils module.'.format(django_utils_function))
@@ -92,9 +114,17 @@ def wrapper(*args, **kwargs):
92114
return decorator
93115

94116

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.
96120
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:
98128
filter_method = helper.get_django_filter(django_builtin_filter, load)
99129

100130
@wraps(func)
@@ -107,6 +137,10 @@ def wrapper(*args, **kwargs):
107137

108138

109139
def with_tags():
140+
"""
141+
Decorator to mark result of method as safe and allow tags.
142+
"""
143+
110144
def decorator(func):
111145
@wraps(func)
112146
def wrapper(*args, **kwargs):
@@ -115,8 +149,15 @@ def wrapper(*args, **kwargs):
115149
return decorator
116150

117151

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:
120161
@wraps(func)
121162
def wrapper(admin, model):
122163
cache_method_key = helper.cache_method_key(model, func.__name__)
@@ -135,8 +176,14 @@ def wrapper(admin, model):
135176
return decorator
136177

137178

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+
"""
139186
cache_object_key = helper.cache_object_key(model)
140187
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 []
142189
django_cache.delete_many(methods_key)

0 commit comments

Comments
 (0)