django-bulk-hooks 0.1.163__tar.gz → 0.1.165__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.

Files changed (18) hide show
  1. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/PKG-INFO +1 -1
  2. django_bulk_hooks-0.1.165/django_bulk_hooks/engine.py +66 -0
  3. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/manager.py +6 -0
  4. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/queryset.py +19 -1
  5. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/pyproject.toml +1 -1
  6. django_bulk_hooks-0.1.163/django_bulk_hooks/engine.py +0 -43
  7. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/LICENSE +0 -0
  8. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/README.md +0 -0
  9. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/__init__.py +0 -0
  10. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/conditions.py +0 -0
  11. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/constants.py +0 -0
  12. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/context.py +0 -0
  13. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/decorators.py +0 -0
  14. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/enums.py +0 -0
  15. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/handler.py +0 -0
  16. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/models.py +0 -0
  17. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/priority.py +0 -0
  18. {django_bulk_hooks-0.1.163 → django_bulk_hooks-0.1.165}/django_bulk_hooks/registry.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: django-bulk-hooks
3
- Version: 0.1.163
3
+ Version: 0.1.165
4
4
  Summary: Hook-style hooks for Django bulk operations like bulk_create and bulk_update.
5
5
  License: MIT
6
6
  Keywords: django,bulk,hooks
@@ -0,0 +1,66 @@
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
+ if not condition or condition.check(new, original):
54
+ to_process_new.append(new)
55
+ to_process_old.append(original)
56
+
57
+ if to_process_new:
58
+ print(f"DEBUG: Executing hook {handler_cls.__name__}.{method_name} with {len(to_process_new)} records")
59
+ try:
60
+ func(new_records=to_process_new, old_records=to_process_old if any(to_process_old) else None)
61
+ print(f"DEBUG: Hook {handler_cls.__name__}.{method_name} executed successfully")
62
+ except Exception as e:
63
+ print(f"DEBUG: Hook {handler_cls.__name__}.{method_name} failed with error: {e}")
64
+ raise
65
+ else:
66
+ print(f"DEBUG: Skipping hook {handler_cls.__name__}.{method_name} - no records to process")
@@ -24,9 +24,11 @@ class BulkHookManager(models.Manager):
24
24
  # Check if this is our HookQuerySet or a different QuerySet
25
25
  if hasattr(qs, 'bulk_update') and 'bypass_hooks' in inspect.signature(qs.bulk_update).parameters:
26
26
  # Our HookQuerySet - pass all parameters
27
+ print(f"DEBUG: Using our HookQuerySet for {self.model}")
27
28
  return qs.bulk_update(objs, fields, bypass_hooks=bypass_hooks, bypass_validation=bypass_validation, **kwargs)
28
29
  else:
29
30
  # Different QuerySet (like queryable_properties) - only pass standard parameters
31
+ print(f"DEBUG: Using different QuerySet ({type(qs)}) for {self.model}, bypassing hooks")
30
32
  django_kwargs = {k: v for k, v in kwargs.items() if k not in ['bypass_hooks', 'bypass_validation']}
31
33
  return qs.bulk_update(objs, fields, **django_kwargs)
32
34
 
@@ -52,6 +54,7 @@ class BulkHookManager(models.Manager):
52
54
  # Check if this is our HookQuerySet or a different QuerySet
53
55
  if hasattr(qs, 'bulk_create') and 'bypass_hooks' in inspect.signature(qs.bulk_create).parameters:
54
56
  # Our HookQuerySet - pass all parameters
57
+ print(f"DEBUG: Using our HookQuerySet for {self.model}")
55
58
  kwargs = {
56
59
  'batch_size': batch_size,
57
60
  'ignore_conflicts': ignore_conflicts,
@@ -62,6 +65,7 @@ class BulkHookManager(models.Manager):
62
65
  return qs.bulk_create(objs, bypass_hooks=bypass_hooks, bypass_validation=bypass_validation, **kwargs)
63
66
  else:
64
67
  # Different QuerySet - only pass standard parameters
68
+ print(f"DEBUG: Using different QuerySet ({type(qs)}) for {self.model}, bypassing hooks")
65
69
  kwargs = {
66
70
  'batch_size': batch_size,
67
71
  'ignore_conflicts': ignore_conflicts,
@@ -85,12 +89,14 @@ class BulkHookManager(models.Manager):
85
89
  # Check if this is our HookQuerySet or a different QuerySet
86
90
  if hasattr(qs, 'bulk_delete') and 'bypass_hooks' in inspect.signature(qs.bulk_delete).parameters:
87
91
  # Our HookQuerySet - pass all parameters
92
+ print(f"DEBUG: Using our HookQuerySet for {self.model}")
88
93
  kwargs = {
89
94
  'batch_size': batch_size,
90
95
  }
91
96
  return qs.bulk_delete(objs, bypass_hooks=bypass_hooks, bypass_validation=bypass_validation, **kwargs)
92
97
  else:
93
98
  # Different QuerySet - only pass standard parameters
99
+ print(f"DEBUG: Using different QuerySet ({type(qs)}) for {self.model}, bypassing hooks")
94
100
  kwargs = {
95
101
  'batch_size': batch_size,
96
102
  }
@@ -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
- engine.run(model_cls, BEFORE_UPDATE, objs, originals, ctx=ctx)
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.163"
3
+ version = "0.1.165"
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)