django-bulk-hooks 0.1.164__tar.gz → 0.1.166__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.164 → django_bulk_hooks-0.1.166}/PKG-INFO +1 -1
- django_bulk_hooks-0.1.166/django_bulk_hooks/engine.py +73 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/manager.py +6 -1
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/queryset.py +19 -1
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/pyproject.toml +1 -1
- django_bulk_hooks-0.1.164/django_bulk_hooks/engine.py +0 -43
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/LICENSE +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/README.md +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/__init__.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/conditions.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/constants.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/context.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/decorators.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/enums.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/handler.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/models.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/priority.py +0 -0
- {django_bulk_hooks-0.1.164 → django_bulk_hooks-0.1.166}/django_bulk_hooks/registry.py +0 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from django.core.exceptions import ValidationError
|
|
4
|
+
|
|
5
|
+
from django_bulk_hooks.registry import get_hooks
|
|
6
|
+
|
|
7
|
+
logger = logging.getLogger(__name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def run(model_cls, event, new_records, old_records=None, ctx=None):
|
|
11
|
+
"""
|
|
12
|
+
Run hooks for a given model, event, and records.
|
|
13
|
+
"""
|
|
14
|
+
print(f"DEBUG: engine.run called for {model_cls} with event {event}")
|
|
15
|
+
print(f"DEBUG: Number of new_records: {len(new_records) if new_records else 0}")
|
|
16
|
+
print(f"DEBUG: Number of old_records: {len(old_records) if old_records else 0}")
|
|
17
|
+
|
|
18
|
+
if not new_records:
|
|
19
|
+
print(f"DEBUG: No new_records, skipping hooks")
|
|
20
|
+
return
|
|
21
|
+
|
|
22
|
+
# Get hooks for this model and event
|
|
23
|
+
hooks = get_hooks(model_cls, event)
|
|
24
|
+
print(f"DEBUG: Found {len(hooks)} hooks for {model_cls}.{event}")
|
|
25
|
+
|
|
26
|
+
if not hooks:
|
|
27
|
+
print(f"DEBUG: No hooks found for {model_cls}.{event}")
|
|
28
|
+
return
|
|
29
|
+
|
|
30
|
+
# For BEFORE_* events, run model.clean() first for validation
|
|
31
|
+
if event.startswith("before_"):
|
|
32
|
+
for instance in new_records:
|
|
33
|
+
try:
|
|
34
|
+
instance.clean()
|
|
35
|
+
except ValidationError as e:
|
|
36
|
+
logger.error("Validation failed for %s: %s", instance, e)
|
|
37
|
+
raise
|
|
38
|
+
|
|
39
|
+
# Process hooks
|
|
40
|
+
for handler_cls, method_name, condition, priority in hooks:
|
|
41
|
+
print(f"DEBUG: Processing hook: {handler_cls.__name__}.{method_name}")
|
|
42
|
+
handler_instance = handler_cls()
|
|
43
|
+
func = getattr(handler_instance, method_name)
|
|
44
|
+
|
|
45
|
+
to_process_new = []
|
|
46
|
+
to_process_old = []
|
|
47
|
+
|
|
48
|
+
for new, original in zip(
|
|
49
|
+
new_records,
|
|
50
|
+
old_records or [None] * len(new_records),
|
|
51
|
+
strict=True,
|
|
52
|
+
):
|
|
53
|
+
print(f"DEBUG: Checking condition for {handler_cls.__name__}.{method_name}")
|
|
54
|
+
print(f"DEBUG: - new: {type(new)}")
|
|
55
|
+
print(f"DEBUG: - original: {type(original)}")
|
|
56
|
+
print(f"DEBUG: - condition: {condition}")
|
|
57
|
+
if not condition or condition.check(new, original):
|
|
58
|
+
print(f"DEBUG: - condition passed, adding to process list")
|
|
59
|
+
to_process_new.append(new)
|
|
60
|
+
to_process_old.append(original)
|
|
61
|
+
else:
|
|
62
|
+
print(f"DEBUG: - condition failed, skipping")
|
|
63
|
+
|
|
64
|
+
if to_process_new:
|
|
65
|
+
print(f"DEBUG: Executing hook {handler_cls.__name__}.{method_name} with {len(to_process_new)} records")
|
|
66
|
+
try:
|
|
67
|
+
func(new_records=to_process_new, old_records=to_process_old if any(to_process_old) else None)
|
|
68
|
+
print(f"DEBUG: Hook {handler_cls.__name__}.{method_name} executed successfully")
|
|
69
|
+
except Exception as e:
|
|
70
|
+
print(f"DEBUG: Hook {handler_cls.__name__}.{method_name} failed with error: {e}")
|
|
71
|
+
raise
|
|
72
|
+
else:
|
|
73
|
+
print(f"DEBUG: Skipping hook {handler_cls.__name__}.{method_name} - no records to process")
|
|
@@ -5,7 +5,10 @@ from django_bulk_hooks.queryset import HookQuerySet
|
|
|
5
5
|
|
|
6
6
|
class BulkHookManager(models.Manager):
|
|
7
7
|
def get_queryset(self):
|
|
8
|
-
|
|
8
|
+
qs = HookQuerySet(self.model, using=self._db)
|
|
9
|
+
print(f"DEBUG: BulkHookManager.get_queryset() called for {self.model}")
|
|
10
|
+
print(f"DEBUG: Returning QuerySet type: {type(qs)}")
|
|
11
|
+
return qs
|
|
9
12
|
|
|
10
13
|
def bulk_update(
|
|
11
14
|
self, objs, fields, bypass_hooks=False, bypass_validation=False, **kwargs
|
|
@@ -20,6 +23,8 @@ class BulkHookManager(models.Manager):
|
|
|
20
23
|
print(f"DEBUG: bulk_update method signature: {inspect.signature(method)}")
|
|
21
24
|
print(f"DEBUG: Calling with args: objs={type(objs)}, fields={fields}, bypass_hooks={bypass_hooks}, bypass_validation={bypass_validation}, kwargs={kwargs}")
|
|
22
25
|
print(f"DEBUG: QuerySet class: {type(qs)}")
|
|
26
|
+
print(f"DEBUG: Manager type: {type(self)}")
|
|
27
|
+
print(f"DEBUG: Model: {self.model}")
|
|
23
28
|
|
|
24
29
|
# Check if this is our HookQuerySet or a different QuerySet
|
|
25
30
|
if hasattr(qs, 'bulk_update') and 'bypass_hooks' in inspect.signature(qs.bulk_update).parameters:
|
|
@@ -113,9 +113,15 @@ class HookQuerySet(models.QuerySet):
|
|
|
113
113
|
# Fire hooks before DB ops
|
|
114
114
|
if not bypass_hooks:
|
|
115
115
|
print(f"DEBUG: Firing BEFORE_CREATE hooks for {model_cls}")
|
|
116
|
+
print(f"DEBUG: Number of objects: {len(objs)}")
|
|
117
|
+
print(f"DEBUG: Object types: {[type(obj) for obj in objs]}")
|
|
118
|
+
print(f"DEBUG: QuerySet type: {type(self)}")
|
|
119
|
+
print(f"DEBUG: Is this HookQuerySet? {isinstance(self, HookQuerySet)}")
|
|
116
120
|
ctx = HookContext(model_cls)
|
|
117
121
|
if not bypass_validation:
|
|
122
|
+
print(f"DEBUG: Running VALIDATE_CREATE hooks")
|
|
118
123
|
engine.run(model_cls, VALIDATE_CREATE, objs, ctx=ctx)
|
|
124
|
+
print(f"DEBUG: Running BEFORE_CREATE hooks")
|
|
119
125
|
engine.run(model_cls, BEFORE_CREATE, objs, ctx=ctx)
|
|
120
126
|
else:
|
|
121
127
|
print(f"DEBUG: Skipping hooks due to bypass_hooks=True for {model_cls}")
|
|
@@ -156,6 +162,9 @@ class HookQuerySet(models.QuerySet):
|
|
|
156
162
|
# Fire AFTER_CREATE hooks
|
|
157
163
|
if not bypass_hooks:
|
|
158
164
|
print(f"DEBUG: Firing AFTER_CREATE hooks for {model_cls}")
|
|
165
|
+
print(f"DEBUG: Number of objects: {len(objs)}")
|
|
166
|
+
print(f"DEBUG: QuerySet type: {type(self)}")
|
|
167
|
+
print(f"DEBUG: Is this HookQuerySet? {isinstance(self, HookQuerySet)}")
|
|
159
168
|
engine.run(model_cls, AFTER_CREATE, objs, ctx=ctx)
|
|
160
169
|
else:
|
|
161
170
|
print(f"DEBUG: Skipping AFTER_CREATE hooks due to bypass_hooks=True for {model_cls}")
|
|
@@ -180,6 +189,8 @@ class HookQuerySet(models.QuerySet):
|
|
|
180
189
|
model_cls = self.model
|
|
181
190
|
print(f"DEBUG: Model class: {model_cls}")
|
|
182
191
|
print(f"DEBUG: bypass_hooks value: {bypass_hooks}")
|
|
192
|
+
print(f"DEBUG: QuerySet type: {type(self)}")
|
|
193
|
+
print(f"DEBUG: Is this HookQuerySet? {isinstance(self, HookQuerySet)}")
|
|
183
194
|
|
|
184
195
|
if not objs:
|
|
185
196
|
return []
|
|
@@ -204,7 +215,13 @@ class HookQuerySet(models.QuerySet):
|
|
|
204
215
|
engine.run(model_cls, VALIDATE_UPDATE, objs, originals, ctx=ctx)
|
|
205
216
|
|
|
206
217
|
# Then run business logic hooks
|
|
207
|
-
|
|
218
|
+
if not bypass_hooks:
|
|
219
|
+
print(f"DEBUG: Firing BEFORE_UPDATE hooks for {model_cls}")
|
|
220
|
+
print(f"DEBUG: Number of objects: {len(objs)}")
|
|
221
|
+
print(f"DEBUG: Object types: {[type(obj) for obj in objs]}")
|
|
222
|
+
engine.run(model_cls, BEFORE_UPDATE, objs, originals, ctx=ctx)
|
|
223
|
+
else:
|
|
224
|
+
print(f"DEBUG: Skipping hooks due to bypass_hooks=True for {model_cls}")
|
|
208
225
|
|
|
209
226
|
# Automatically detect fields that were modified during BEFORE_UPDATE hooks
|
|
210
227
|
modified_fields = self._detect_modified_fields(objs, originals)
|
|
@@ -224,6 +241,7 @@ class HookQuerySet(models.QuerySet):
|
|
|
224
241
|
|
|
225
242
|
if not bypass_hooks:
|
|
226
243
|
print(f"DEBUG: Firing AFTER_UPDATE hooks for {model_cls}")
|
|
244
|
+
print(f"DEBUG: Number of objects: {len(objs)}")
|
|
227
245
|
engine.run(model_cls, AFTER_UPDATE, objs, originals, ctx=ctx)
|
|
228
246
|
else:
|
|
229
247
|
print(f"DEBUG: Skipping AFTER_UPDATE hooks due to bypass_hooks=True for {model_cls}")
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "django-bulk-hooks"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.166"
|
|
4
4
|
description = "Hook-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,43 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
|
|
3
|
-
from django.core.exceptions import ValidationError
|
|
4
|
-
|
|
5
|
-
from django_bulk_hooks.registry import get_hooks
|
|
6
|
-
|
|
7
|
-
logger = logging.getLogger(__name__)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def run(model_cls, event, new_instances, original_instances=None, ctx=None):
|
|
11
|
-
hooks = get_hooks(model_cls, event)
|
|
12
|
-
|
|
13
|
-
if not hooks:
|
|
14
|
-
return
|
|
15
|
-
|
|
16
|
-
# For BEFORE_* events, run model.clean() first for validation
|
|
17
|
-
if event.startswith("before_"):
|
|
18
|
-
for instance in new_instances:
|
|
19
|
-
try:
|
|
20
|
-
instance.clean()
|
|
21
|
-
except ValidationError as e:
|
|
22
|
-
logger.error("Validation failed for %s: %s", instance, e)
|
|
23
|
-
raise
|
|
24
|
-
|
|
25
|
-
for handler_cls, method_name, condition, priority in hooks:
|
|
26
|
-
handler_instance = handler_cls()
|
|
27
|
-
func = getattr(handler_instance, method_name)
|
|
28
|
-
|
|
29
|
-
to_process_new = []
|
|
30
|
-
to_process_old = []
|
|
31
|
-
|
|
32
|
-
for new, original in zip(
|
|
33
|
-
new_instances,
|
|
34
|
-
original_instances or [None] * len(new_instances),
|
|
35
|
-
strict=True,
|
|
36
|
-
):
|
|
37
|
-
if not condition or condition.check(new, original):
|
|
38
|
-
to_process_new.append(new)
|
|
39
|
-
to_process_old.append(original)
|
|
40
|
-
|
|
41
|
-
if to_process_new:
|
|
42
|
-
# Call the function with keyword arguments
|
|
43
|
-
func(new_records=to_process_new, old_records=to_process_old if any(to_process_old) else None)
|
|
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
|