epok-toolkit 1.6.0__py3-none-any.whl → 1.7.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of epok-toolkit might be problematic. Click here for more details.
- epok_toolkit/cache/__init__.py +1 -0
- epok_toolkit/cache/cacher.py +130 -0
- {epok_toolkit-1.6.0.dist-info → epok_toolkit-1.7.1.dist-info}/METADATA +3 -2
- {epok_toolkit-1.6.0.dist-info → epok_toolkit-1.7.1.dist-info}/RECORD +7 -5
- {epok_toolkit-1.6.0.dist-info → epok_toolkit-1.7.1.dist-info}/WHEEL +0 -0
- {epok_toolkit-1.6.0.dist-info → epok_toolkit-1.7.1.dist-info}/licenses/LICENSE +0 -0
- {epok_toolkit-1.6.0.dist-info → epok_toolkit-1.7.1.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .cacher import cache_full, cache_get, CachedViewSet, TimeToLive
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# utils/cacher.py
|
|
2
|
+
from rest_framework import viewsets
|
|
3
|
+
from rest_framework_extensions.cache.mixins import CacheResponseMixin
|
|
4
|
+
from rest_framework_extensions.cache.decorators import cache_response
|
|
5
|
+
from django.views.decorators.cache import cache_page
|
|
6
|
+
from colorstreak import log
|
|
7
|
+
from functools import wraps
|
|
8
|
+
from django.utils.decorators import method_decorator
|
|
9
|
+
# --- clave por usuario + params + página ----------------------
|
|
10
|
+
from rest_framework_extensions.key_constructor.constructors import DefaultKeyConstructor
|
|
11
|
+
from rest_framework_extensions.key_constructor.bits import UserKeyBit, QueryParamsKeyBit, PaginationKeyBit
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
cacher.py — utilidades de caché centralizadas
|
|
16
|
+
============================================
|
|
17
|
+
|
|
18
|
+
Este módulo agrupa, en un solo lugar, las dos capas de caché que aplicamos en
|
|
19
|
+
nuestra API DRF:
|
|
20
|
+
|
|
21
|
+
📦 cache_get → capa de *serialización* (drf extensions @cache_response)
|
|
22
|
+
🚀 cache_full → capa de *vista completa* (Django @cache_page)
|
|
23
|
+
|
|
24
|
+
Para no repetir la misma lógica en cada vista, exportamos:
|
|
25
|
+
|
|
26
|
+
• `CachedViewSet` -> incluye CacheResponseMixin listo para usar.
|
|
27
|
+
• Decoradores -> `cache_get`, `cache_full`.
|
|
28
|
+
• `TimeToLive` -> constantes de segundos ligadas a colores/emoji.
|
|
29
|
+
|
|
30
|
+
Leyenda TTL (la misma del CSV)
|
|
31
|
+
------------------------------
|
|
32
|
+
🔴 1 min (60 s) | cambios frecuentes
|
|
33
|
+
🟡 2 --> 5 min (~300 s) | lecturas comunes
|
|
34
|
+
🟢 10 --> 30 min (~1800 s) | datos casi estáticos
|
|
35
|
+
|
|
36
|
+
Emojis ↔ capas
|
|
37
|
+
--------------
|
|
38
|
+
🗄️ consultas ORM (cacheops / get_or_set)
|
|
39
|
+
📦 serialización (cache_response)
|
|
40
|
+
🚀 vista completa (cache_page)
|
|
41
|
+
🌐 CDN / navegador (Cache‑Control)
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TimeToLive:
|
|
48
|
+
"""
|
|
49
|
+
Constantes de TTL ligadas a la paleta del CSV:
|
|
50
|
+
|
|
51
|
+
🔴 RED | 1 min (60 s)
|
|
52
|
+
🟡 YELLOW | 5 min (300 s)
|
|
53
|
+
🟢 GREEN | 30 min (1800 s)
|
|
54
|
+
"""
|
|
55
|
+
RED = 60 # 1 minuto
|
|
56
|
+
YELLOW = 60 * 5 # 5 minutos
|
|
57
|
+
GREEN = 60 * 30 # 30 minutos
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class UserQueryKey(DefaultKeyConstructor):
|
|
61
|
+
user = UserKeyBit()
|
|
62
|
+
query_params = QueryParamsKeyBit()
|
|
63
|
+
pagination = PaginationKeyBit()
|
|
64
|
+
|
|
65
|
+
_DEFAULT_KEY = UserQueryKey().get_key
|
|
66
|
+
_DEFAULT_TTL = TimeToLive.RED
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class CachedViewSet(CacheResponseMixin, viewsets.ModelViewSet):
|
|
71
|
+
"""
|
|
72
|
+
Herédame si vas a usar cache_get o cache_full.
|
|
73
|
+
Nada más que eso; no impone TTL.
|
|
74
|
+
"""
|
|
75
|
+
pass
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def cache_get(ttl=_DEFAULT_TTL, key_func=_DEFAULT_KEY):
|
|
81
|
+
"""
|
|
82
|
+
Capa: Serialización 📦
|
|
83
|
+
Devuelve un decorador que cachea la serialización DRF *una sola vez*,
|
|
84
|
+
evitando envolver la vista nuevamente en cada petición.
|
|
85
|
+
|
|
86
|
+
Ejemplo:
|
|
87
|
+
@cache_get(ttl=TimeToLive.RED)
|
|
88
|
+
def view(...): ...
|
|
89
|
+
"""
|
|
90
|
+
def decorator(view_fn):
|
|
91
|
+
# Pre‑construimos la función cacheada UNA vez
|
|
92
|
+
cached_fn = cache_response(ttl, key_func=key_func)(view_fn)
|
|
93
|
+
|
|
94
|
+
@wraps(view_fn)
|
|
95
|
+
def wrapped(*args, **kwargs):
|
|
96
|
+
log.debug(
|
|
97
|
+
f"[📦 cache_get] {view_fn.__qualname__} | ttl={ttl}s"
|
|
98
|
+
)
|
|
99
|
+
# Llamamos directamente a la versión ya decorada,
|
|
100
|
+
# para no crear cadenas de closures ni excepciones duplicadas.
|
|
101
|
+
return cached_fn(*args, **kwargs)
|
|
102
|
+
|
|
103
|
+
return wrapped
|
|
104
|
+
|
|
105
|
+
return decorator
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def cache_full(ttl=_DEFAULT_TTL, key_prefix=""):
|
|
109
|
+
"""
|
|
110
|
+
Capa: Respuesta HTTP 🚀
|
|
111
|
+
Devuelve un decorador que cachea la vista completa vía cache_page
|
|
112
|
+
*al momento de ejecutar la vista*.
|
|
113
|
+
|
|
114
|
+
Ejemplo:
|
|
115
|
+
@cache_full(ttl=TimeToLive.GREEN, key_prefix="ticket_pdf")
|
|
116
|
+
def view(...): ...
|
|
117
|
+
"""
|
|
118
|
+
def decorator(view_fn):
|
|
119
|
+
# Adapt the function-level cache_page decorator to a bound‑method
|
|
120
|
+
page_deco = cache_page(ttl, key_prefix=key_prefix)
|
|
121
|
+
decorated_fn = method_decorator(page_deco)(view_fn)
|
|
122
|
+
|
|
123
|
+
@wraps(view_fn)
|
|
124
|
+
def wrapped(self, request, *args, **kwargs):
|
|
125
|
+
log.debug(f"[🚀 cache_full] {view_fn.__qualname__} | ttl={ttl}s | prefix={key_prefix}")
|
|
126
|
+
return decorated_fn(self, request, *args, **kwargs)
|
|
127
|
+
|
|
128
|
+
return wrapped
|
|
129
|
+
|
|
130
|
+
return decorator
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: epok-toolkit
|
|
3
|
-
Version: 1.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 1.7.1
|
|
4
|
+
Summary: Una herramienta para la gestión de tareas y procesos en Django con Celery.
|
|
5
5
|
Author-email: Fernando Leon Franco <fernanlee2131@gmail.com>
|
|
6
6
|
License: MIT
|
|
7
7
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -14,6 +14,7 @@ License-File: LICENSE
|
|
|
14
14
|
Requires-Dist: Django>=5.0.2
|
|
15
15
|
Requires-Dist: celery[redis]>=5.5.3
|
|
16
16
|
Requires-Dist: django-celery-beat>=2.6.0
|
|
17
|
+
Requires-Dist: colorstreak>=0.1.0
|
|
17
18
|
Dynamic: license-file
|
|
18
19
|
|
|
19
20
|
# EPOK Toolkit
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
epok_toolkit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
epok_toolkit/apps.py,sha256=O3q3CcucJOHjlYIS0VgbKsbtim2hpng_FxpKEG_MlWs,486
|
|
3
3
|
epok_toolkit/default_settings.py,sha256=hxsRmsAcjk4ar-pRz_X6AI17mJ3192Ljbx-xBuP5rdY,452
|
|
4
|
+
epok_toolkit/cache/__init__.py,sha256=q1-pMbXk-r80a3woyq01E84Tt6kB-UH5u06XoQROaM0,68
|
|
5
|
+
epok_toolkit/cache/cacher.py,sha256=R179_Hrlw1k0tsry9EN0zGl4-Of02L-VzE-xqFuXjaU,4107
|
|
4
6
|
epok_toolkit/email/__init__.py,sha256=pyJwysyVoq6DuYAG72fulsKFoOuAfjw3aBH7FhmYGHc,35
|
|
5
7
|
epok_toolkit/email/email_async.py,sha256=oC0WowWNUpTpXdxo6hJag5gWUaStxI6VBEArEKQxXko,1521
|
|
6
8
|
epok_toolkit/email/engine.py,sha256=IIifqRI9z76pHdrO5oSSZ25aP5txOTAgrj1JuVVPlMY,2387
|
|
@@ -16,8 +18,8 @@ epok_toolkit/pdf/fuentes/Kollektif-Italic.ttf,sha256=1CXPyw43il9u0tQ_7aRzsEaVtg3
|
|
|
16
18
|
epok_toolkit/pdf/fuentes/Kollektif.ttf,sha256=7wTLkVVNUm1giLjIZcWRUH5r2r3o0GjdKic4V1A-pNQ,51128
|
|
17
19
|
epok_toolkit/pdf/plantillas/Ticket_congrats.png,sha256=OSQhVR0j_nLHE6kSJ33BTR-77HM1fNAfJBe2EuX6wVk,157141
|
|
18
20
|
epok_toolkit/pdf/plantillas/Ticket_congrats2.png,sha256=1RBogBdo-8WSMpD3H73HoLgJtr5EC5oVKfOCIWOxPSo,373605
|
|
19
|
-
epok_toolkit-1.
|
|
20
|
-
epok_toolkit-1.
|
|
21
|
-
epok_toolkit-1.
|
|
22
|
-
epok_toolkit-1.
|
|
23
|
-
epok_toolkit-1.
|
|
21
|
+
epok_toolkit-1.7.1.dist-info/licenses/LICENSE,sha256=iLDbGXdLSIOT5OsxzHCvtmxHtonE21GiFlS3LNkug4A,128
|
|
22
|
+
epok_toolkit-1.7.1.dist-info/METADATA,sha256=ty3DKZsCJ_239FAUKBJAIK46nbU9ZPZhfP5MRUIbFnM,786
|
|
23
|
+
epok_toolkit-1.7.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
24
|
+
epok_toolkit-1.7.1.dist-info/top_level.txt,sha256=Wo72AqIFcfWwBGM5F5iGFw9PrO3WBnTSprFZIJk_pNg,13
|
|
25
|
+
epok_toolkit-1.7.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|