django-bulk-hooks 0.1.53__tar.gz → 0.1.54__tar.gz
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 django-bulk-hooks might be problematic. Click here for more details.
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/PKG-INFO +1 -1
- django_bulk_hooks-0.1.54/django_bulk_hooks/context.py +16 -0
- django_bulk_hooks-0.1.54/django_bulk_hooks/handler.py +102 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/queryset.py +0 -1
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/pyproject.toml +1 -1
- django_bulk_hooks-0.1.53/django_bulk_hooks/context.py +0 -4
- django_bulk_hooks-0.1.53/django_bulk_hooks/handler.py +0 -208
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/LICENSE +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/README.md +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/__init__.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/conditions.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/constants.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/decorators.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/engine.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/enums.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/manager.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/models.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/priority.py +0 -0
- {django_bulk_hooks-0.1.53 → django_bulk_hooks-0.1.54}/django_bulk_hooks/registry.py +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import threading
|
|
2
|
+
from collections import deque
|
|
3
|
+
|
|
4
|
+
_hook_context = threading.local()
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def get_hook_queue():
|
|
8
|
+
if not hasattr(_hook_context, "queue"):
|
|
9
|
+
_hook_context.queue = deque()
|
|
10
|
+
return _hook_context.queue
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TriggerContext:
|
|
14
|
+
def __init__(self, model_cls, metadata=None):
|
|
15
|
+
self.model_cls = model_cls
|
|
16
|
+
self.metadata = metadata or {}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import threading
|
|
3
|
+
from collections import deque
|
|
4
|
+
|
|
5
|
+
from django.db import transaction
|
|
6
|
+
from django_bulk_hooks.registry import get_hooks, register_hook
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
# Thread-local hook queue context
|
|
11
|
+
_hook_context = threading.local()
|
|
12
|
+
|
|
13
|
+
def get_hook_queue():
|
|
14
|
+
if not hasattr(_hook_context, "queue"):
|
|
15
|
+
_hook_context.queue = deque()
|
|
16
|
+
return _hook_context.queue
|
|
17
|
+
|
|
18
|
+
class TriggerHandlerMeta(type):
|
|
19
|
+
_registered = set()
|
|
20
|
+
|
|
21
|
+
def __new__(mcs, name, bases, namespace):
|
|
22
|
+
cls = super().__new__(mcs, name, bases, namespace)
|
|
23
|
+
for method_name, method in namespace.items():
|
|
24
|
+
if hasattr(method, "hooks_hooks"):
|
|
25
|
+
for model_cls, event, condition, priority in method.hooks_hooks:
|
|
26
|
+
key = (model_cls, event, cls, method_name)
|
|
27
|
+
if key not in TriggerHandlerMeta._registered:
|
|
28
|
+
register_hook(
|
|
29
|
+
model=model_cls,
|
|
30
|
+
event=event,
|
|
31
|
+
handler_cls=cls,
|
|
32
|
+
method_name=method_name,
|
|
33
|
+
condition=condition,
|
|
34
|
+
priority=priority,
|
|
35
|
+
)
|
|
36
|
+
TriggerHandlerMeta._registered.add(key)
|
|
37
|
+
return cls
|
|
38
|
+
|
|
39
|
+
class TriggerHandler(metaclass=TriggerHandlerMeta):
|
|
40
|
+
@classmethod
|
|
41
|
+
def handle(
|
|
42
|
+
cls,
|
|
43
|
+
event: str,
|
|
44
|
+
model: type,
|
|
45
|
+
*,
|
|
46
|
+
new_records: list = None,
|
|
47
|
+
old_records: list = None,
|
|
48
|
+
**kwargs,
|
|
49
|
+
) -> None:
|
|
50
|
+
queue = get_hook_queue()
|
|
51
|
+
queue.append((cls, event, model, new_records, old_records, kwargs))
|
|
52
|
+
|
|
53
|
+
if len(queue) > 1:
|
|
54
|
+
return # nested call, will be processed by outermost
|
|
55
|
+
|
|
56
|
+
# only outermost handle will process the queue
|
|
57
|
+
while queue:
|
|
58
|
+
cls_, event_, model_, new_, old_, kw_ = queue.popleft()
|
|
59
|
+
cls_._process(event_, model_, new_, old_, **kw_)
|
|
60
|
+
|
|
61
|
+
@classmethod
|
|
62
|
+
def _process(
|
|
63
|
+
cls,
|
|
64
|
+
event,
|
|
65
|
+
model,
|
|
66
|
+
new_records,
|
|
67
|
+
old_records,
|
|
68
|
+
**kwargs,
|
|
69
|
+
):
|
|
70
|
+
hooks = sorted(get_hooks(model, event), key=lambda x: x[3])
|
|
71
|
+
logger.debug("Processing %d hooks for %s.%s", len(hooks), model.__name__, event)
|
|
72
|
+
|
|
73
|
+
def _execute():
|
|
74
|
+
new_local = new_records or []
|
|
75
|
+
old_local = old_records or []
|
|
76
|
+
if len(old_local) < len(new_local):
|
|
77
|
+
old_local += [None] * (len(new_local) - len(old_local))
|
|
78
|
+
|
|
79
|
+
for handler_cls, method_name, condition, priority in hooks:
|
|
80
|
+
if condition is not None:
|
|
81
|
+
checks = [condition.check(n, o) for n, o in zip(new_local, old_local)]
|
|
82
|
+
if not any(checks):
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
handler = handler_cls()
|
|
86
|
+
method = getattr(handler, method_name)
|
|
87
|
+
|
|
88
|
+
logger.info("Running hook %s.%s on %d items", handler_cls.__name__, method_name, len(new_local))
|
|
89
|
+
try:
|
|
90
|
+
method(
|
|
91
|
+
new_records=new_local,
|
|
92
|
+
old_records=old_local,
|
|
93
|
+
**kwargs,
|
|
94
|
+
)
|
|
95
|
+
except Exception:
|
|
96
|
+
logger.exception("Error in hook %s.%s", handler_cls.__name__, method_name)
|
|
97
|
+
|
|
98
|
+
conn = transaction.get_connection()
|
|
99
|
+
if conn.in_atomic_block and event.startswith("after_"):
|
|
100
|
+
transaction.on_commit(_execute)
|
|
101
|
+
else:
|
|
102
|
+
_execute()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "django-bulk-hooks"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.54"
|
|
4
4
|
description = "Lifecycle-style hooks for Django bulk operations like bulk_create and bulk_update."
|
|
5
5
|
authors = ["Konrad Beck <konrad.beck@merchantcapital.co.za>"]
|
|
6
6
|
readme = "README.md"
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
|
|
3
|
-
from django.db import transaction
|
|
4
|
-
from django_bulk_hooks.conditions import HookCondition
|
|
5
|
-
from django_bulk_hooks.registry import get_hooks, register_hook
|
|
6
|
-
|
|
7
|
-
logger = logging.getLogger(__name__)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class TriggerHandlerMeta(type):
|
|
11
|
-
_registered = set()
|
|
12
|
-
|
|
13
|
-
def __new__(mcs, name, bases, namespace):
|
|
14
|
-
cls = super().__new__(mcs, name, bases, namespace)
|
|
15
|
-
for method_name, method in namespace.items():
|
|
16
|
-
if hasattr(method, "hooks_hooks"):
|
|
17
|
-
for model_cls, event, condition, priority in method.hooks_hooks:
|
|
18
|
-
key = (model_cls, event, cls, method_name)
|
|
19
|
-
if key in TriggerHandlerMeta._registered:
|
|
20
|
-
logger.debug(
|
|
21
|
-
"Skipping duplicate registration for %s.%s on %s.%s",
|
|
22
|
-
cls.__name__,
|
|
23
|
-
method_name,
|
|
24
|
-
model_cls.__name__,
|
|
25
|
-
event,
|
|
26
|
-
)
|
|
27
|
-
else:
|
|
28
|
-
register_hook(
|
|
29
|
-
model=model_cls,
|
|
30
|
-
event=event,
|
|
31
|
-
handler_cls=cls,
|
|
32
|
-
method_name=method_name,
|
|
33
|
-
condition=condition,
|
|
34
|
-
priority=priority,
|
|
35
|
-
)
|
|
36
|
-
TriggerHandlerMeta._registered.add(key)
|
|
37
|
-
logger.debug(
|
|
38
|
-
"Registered hook %s.%s → %s.%s (cond=%r, prio=%s)",
|
|
39
|
-
model_cls.__name__,
|
|
40
|
-
event,
|
|
41
|
-
cls.__name__,
|
|
42
|
-
method_name,
|
|
43
|
-
condition,
|
|
44
|
-
priority,
|
|
45
|
-
)
|
|
46
|
-
return cls
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
class TriggerHandler(metaclass=TriggerHandlerMeta):
|
|
50
|
-
@classmethod
|
|
51
|
-
def handle(
|
|
52
|
-
cls,
|
|
53
|
-
event: str,
|
|
54
|
-
model: type,
|
|
55
|
-
*,
|
|
56
|
-
new_records: list = None,
|
|
57
|
-
old_records: list = None,
|
|
58
|
-
**kwargs,
|
|
59
|
-
) -> None:
|
|
60
|
-
# Prepare hook list and log names
|
|
61
|
-
hooks = get_hooks(model, event)
|
|
62
|
-
|
|
63
|
-
# Sort hooks by priority (ascending: lower number = higher priority)
|
|
64
|
-
hooks = sorted(hooks, key=lambda x: x[3])
|
|
65
|
-
|
|
66
|
-
hook_names = [f"{h.__name__}.{m}" for h, m, _, _ in hooks]
|
|
67
|
-
logger.debug(
|
|
68
|
-
"Found %d hooks for %s.%s: %s",
|
|
69
|
-
len(hooks),
|
|
70
|
-
model.__name__,
|
|
71
|
-
event,
|
|
72
|
-
hook_names,
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
def _process():
|
|
76
|
-
# Ensure new_records is a list
|
|
77
|
-
new_records_local = new_records or []
|
|
78
|
-
|
|
79
|
-
# Normalize old_records: ensure list and pad with None
|
|
80
|
-
old_records_local = list(old_records) if old_records else []
|
|
81
|
-
if len(old_records_local) < len(new_records_local):
|
|
82
|
-
old_records_local += [None] * (
|
|
83
|
-
len(new_records_local) - len(old_records_local)
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
logger.debug(
|
|
87
|
-
"ℹ️ bulk_hooks.handle() start: model=%s event=%s new_count=%d old_count=%d",
|
|
88
|
-
model.__name__,
|
|
89
|
-
event,
|
|
90
|
-
len(new_records_local),
|
|
91
|
-
len(old_records_local),
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
for handler_cls, method_name, condition, priority in hooks:
|
|
95
|
-
logger.debug(
|
|
96
|
-
"→ evaluating hook %s.%s (cond=%r, prio=%s)",
|
|
97
|
-
handler_cls.__name__,
|
|
98
|
-
method_name,
|
|
99
|
-
condition,
|
|
100
|
-
priority,
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
# Evaluate condition
|
|
104
|
-
passed = True
|
|
105
|
-
if condition is not None:
|
|
106
|
-
if isinstance(condition, HookCondition):
|
|
107
|
-
cond_info = getattr(condition, "__dict__", str(condition))
|
|
108
|
-
logger.debug(
|
|
109
|
-
" [cond-info] %s.%s → %r",
|
|
110
|
-
handler_cls.__name__,
|
|
111
|
-
method_name,
|
|
112
|
-
cond_info,
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
checks = []
|
|
116
|
-
for new, old in zip(new_records_local, old_records_local):
|
|
117
|
-
field_name = getattr(condition, "field", None) or getattr(
|
|
118
|
-
condition, "field_name", None
|
|
119
|
-
)
|
|
120
|
-
if field_name:
|
|
121
|
-
actual_val = getattr(new, field_name, None)
|
|
122
|
-
expected = getattr(condition, "value", None) or getattr(
|
|
123
|
-
condition, "value", None
|
|
124
|
-
)
|
|
125
|
-
logger.debug(
|
|
126
|
-
" [field-lookup] %s.%s → field=%r actual=%r expected=%r",
|
|
127
|
-
handler_cls.__name__,
|
|
128
|
-
method_name,
|
|
129
|
-
field_name,
|
|
130
|
-
actual_val,
|
|
131
|
-
expected,
|
|
132
|
-
)
|
|
133
|
-
result = condition.check(new, old)
|
|
134
|
-
checks.append(result)
|
|
135
|
-
logger.debug(
|
|
136
|
-
" [cond-check] %s.%s → new=%r old=%r => %s",
|
|
137
|
-
handler_cls.__name__,
|
|
138
|
-
method_name,
|
|
139
|
-
new,
|
|
140
|
-
old,
|
|
141
|
-
result,
|
|
142
|
-
)
|
|
143
|
-
passed = any(checks)
|
|
144
|
-
logger.debug(
|
|
145
|
-
" [cond-summary] %s.%s any-passed=%s",
|
|
146
|
-
handler_cls.__name__,
|
|
147
|
-
method_name,
|
|
148
|
-
passed,
|
|
149
|
-
)
|
|
150
|
-
else:
|
|
151
|
-
# Legacy callable conditions
|
|
152
|
-
passed = condition(
|
|
153
|
-
new_records=new_records_local,
|
|
154
|
-
old_records=old_records_local,
|
|
155
|
-
)
|
|
156
|
-
logger.debug(
|
|
157
|
-
" [legacy-cond] %s.%s → full-list => %s",
|
|
158
|
-
handler_cls.__name__,
|
|
159
|
-
method_name,
|
|
160
|
-
passed,
|
|
161
|
-
)
|
|
162
|
-
|
|
163
|
-
if not passed:
|
|
164
|
-
logger.debug(
|
|
165
|
-
"↳ skipping %s.%s (condition not met)",
|
|
166
|
-
handler_cls.__name__,
|
|
167
|
-
method_name,
|
|
168
|
-
)
|
|
169
|
-
continue
|
|
170
|
-
|
|
171
|
-
# Instantiate & invoke handler method
|
|
172
|
-
handler = handler_cls()
|
|
173
|
-
method = getattr(handler, method_name)
|
|
174
|
-
logger.info(
|
|
175
|
-
"✨ invoking %s.%s on %d record(s)",
|
|
176
|
-
handler_cls.__name__,
|
|
177
|
-
method_name,
|
|
178
|
-
len(new_records_local),
|
|
179
|
-
)
|
|
180
|
-
try:
|
|
181
|
-
method(
|
|
182
|
-
new_records=new_records_local,
|
|
183
|
-
old_records=old_records_local,
|
|
184
|
-
**kwargs,
|
|
185
|
-
)
|
|
186
|
-
except Exception:
|
|
187
|
-
logger.exception(
|
|
188
|
-
"❌ exception in %s.%s",
|
|
189
|
-
handler_cls.__name__,
|
|
190
|
-
method_name,
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
logger.debug(
|
|
194
|
-
"✔️ bulk_hooks.handle() complete for %s.%s",
|
|
195
|
-
model.__name__,
|
|
196
|
-
event,
|
|
197
|
-
)
|
|
198
|
-
|
|
199
|
-
# Defer if in atomic block and event is after_*
|
|
200
|
-
conn = transaction.get_connection()
|
|
201
|
-
if conn.in_atomic_block and event.startswith("after_"):
|
|
202
|
-
logger.debug(
|
|
203
|
-
"Deferring hook execution until after transaction commit for event '%s'",
|
|
204
|
-
event,
|
|
205
|
-
)
|
|
206
|
-
transaction.on_commit(_process)
|
|
207
|
-
else:
|
|
208
|
-
_process()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|