django-bulk-hooks 0.1.110__py3-none-any.whl → 0.1.112__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 django-bulk-hooks might be problematic. Click here for more details.

@@ -1,4 +1,4 @@
1
1
  from django_bulk_hooks.handler import Hook
2
- from django_bulk_hooks.manager import BulkHookManager
2
+ from django_bulk_hooks.manager import BulkManager
3
3
 
4
- __all__ = ["BulkHookManager", "Hook"]
4
+ __all__ = ["BulkManager", "Hook"]
@@ -1,4 +1,5 @@
1
1
  from django.db import models, transaction
2
+ from django.db.models import AutoField
2
3
 
3
4
  from django_bulk_hooks import engine
4
5
  from django_bulk_hooks.constants import (
@@ -22,185 +23,6 @@ class BulkHookManager(models.Manager):
22
23
  def get_queryset(self):
23
24
  return HookQuerySet(self.model, using=self._db)
24
25
 
25
- def _has_multi_table_inheritance(self, model_cls):
26
- """
27
- Check if this model uses multi-table inheritance.
28
- """
29
- if not model_cls._meta.parents:
30
- return False
31
-
32
- # Check if any parent is not abstract
33
- for parent_model in model_cls._meta.parents.keys():
34
- if not parent_model._meta.abstract:
35
- return True
36
-
37
- return False
38
-
39
- def _get_base_model(self, model_cls):
40
- """
41
- Get the base model (first non-abstract parent or self).
42
- """
43
- base_model = model_cls
44
- while base_model._meta.parents:
45
- # Get the first non-abstract parent model
46
- for parent_model in base_model._meta.parents.keys():
47
- if not parent_model._meta.abstract:
48
- base_model = parent_model
49
- break
50
- else:
51
- # No non-abstract parents found, break the loop
52
- break
53
- return base_model
54
-
55
- def _extract_base_objects(self, objs, model_cls):
56
- """
57
- Extract base model objects from inherited objects.
58
- """
59
- base_model = self._get_base_model(model_cls)
60
- base_objects = []
61
-
62
- for obj in objs:
63
- base_obj = base_model()
64
- for field in base_model._meta.fields:
65
- # Skip ID field
66
- if field.name == 'id':
67
- continue
68
-
69
- # Safely copy field values
70
- try:
71
- if hasattr(obj, field.name):
72
- setattr(base_obj, field.name, getattr(obj, field.name))
73
- except (AttributeError, ValueError):
74
- # Skip fields that can't be copied
75
- continue
76
-
77
- base_objects.append(base_obj)
78
-
79
- return base_objects
80
-
81
- def _extract_child_objects(self, objs, model_cls):
82
- """
83
- Extract child model objects from inherited objects.
84
- """
85
- child_objects = []
86
-
87
- for obj in objs:
88
- child_obj = model_cls()
89
- child_obj.pk = obj.pk # Set the same PK as base
90
-
91
- # Copy only fields specific to this model
92
- for field in model_cls._meta.fields:
93
- # Skip ID field and fields that don't belong to this model
94
- if field.name == 'id':
95
- continue
96
-
97
- # Check if this field belongs to the current model
98
- # Use a safer way to check field ownership
99
- try:
100
- if hasattr(field, 'model') and field.model == model_cls:
101
- # This field belongs to the current model
102
- if hasattr(obj, field.name):
103
- setattr(child_obj, field.name, getattr(obj, field.name))
104
- except AttributeError:
105
- # Skip fields that don't have proper model reference
106
- continue
107
-
108
- child_objects.append(child_obj)
109
-
110
- return child_objects
111
-
112
- def _bulk_create_inherited(self, objs, **kwargs):
113
- """
114
- Handle bulk create for inherited models by handling each table separately.
115
- """
116
- if not objs:
117
- return []
118
-
119
- model_cls = self.model
120
- result = []
121
-
122
- # Group objects by their actual class
123
- objects_by_class = {}
124
- for obj in objs:
125
- obj_class = obj.__class__
126
- if obj_class not in objects_by_class:
127
- objects_by_class[obj_class] = []
128
- objects_by_class[obj_class].append(obj)
129
-
130
- for obj_class, class_objects in objects_by_class.items():
131
- try:
132
- # Check if this class has multi-table inheritance
133
- parent_models = [p for p in obj_class._meta.get_parent_list()
134
- if not p._meta.abstract]
135
-
136
- if not parent_models:
137
- # No inheritance, use standard bulk_create
138
- chunk_result = super(models.Manager, self).bulk_create(class_objects, **kwargs)
139
- result.extend(chunk_result)
140
- continue
141
-
142
- # Handle multi-table inheritance
143
- # Step 1: Bulk create base objects with hooks
144
- base_objects = self._extract_base_objects(class_objects, obj_class)
145
-
146
- # Use the model's manager with hooks
147
- base_model = self._get_base_model(obj_class)
148
-
149
- # Try to avoid recursion by using raw SQL or _base_manager
150
- try:
151
- if hasattr(base_model.objects, 'bulk_create'):
152
- # Use the base model's manager with hooks
153
- created_base = base_model.objects.bulk_create(base_objects, **kwargs)
154
- else:
155
- # Fallback to _base_manager
156
- created_base = base_model._base_manager.bulk_create(base_objects, **kwargs)
157
- except RecursionError:
158
- # If recursion error, use _base_manager directly
159
- created_base = base_model._base_manager.bulk_create(base_objects, **kwargs)
160
-
161
- # Step 2: Update original objects with base IDs
162
- for obj, base_obj in zip(class_objects, created_base):
163
- obj.pk = base_obj.pk
164
- obj._state.adding = False
165
-
166
- # Step 3: Bulk create child objects with hooks
167
- child_objects = self._extract_child_objects(class_objects, obj_class)
168
- if child_objects:
169
- # Use _base_manager to avoid recursion with custom managers
170
- try:
171
- obj_class._base_manager.bulk_create(child_objects, **kwargs)
172
- except RecursionError:
173
- # If recursion error, use individual saves
174
- for obj in child_objects:
175
- obj.save()
176
-
177
- result.extend(class_objects)
178
-
179
- except Exception as e:
180
- # Add debugging information
181
- import logging
182
- logger = logging.getLogger(__name__)
183
- logger.error(f"Error in _bulk_create_inherited for {obj_class}: {e}")
184
- logger.error(f"Model fields: {[f.name for f in obj_class._meta.fields]}")
185
- logger.error(f"Base model: {self._get_base_model(obj_class)}")
186
- logger.error(f"Base model manager: {self._get_base_model(obj_class).objects}")
187
-
188
- # If it's a recursion error, try a simpler approach
189
- if isinstance(e, RecursionError):
190
- logger.error("Recursion error detected, trying fallback approach")
191
- try:
192
- # Fallback: use individual saves
193
- for obj in class_objects:
194
- obj.save()
195
- result.extend(class_objects)
196
- continue
197
- except Exception as fallback_error:
198
- logger.error(f"Fallback approach also failed: {fallback_error}")
199
-
200
- raise
201
-
202
- return result
203
-
204
26
  @transaction.atomic
205
27
  def bulk_update(
206
28
  self, objs, fields, bypass_hooks=False, bypass_validation=False, **kwargs
@@ -218,7 +40,8 @@ class BulkHookManager(models.Manager):
218
40
  if not bypass_hooks:
219
41
  # Load originals for hook comparison and ensure they match the order of new instances
220
42
  original_map = {
221
- obj.pk: obj for obj in model_cls.objects.filter(pk__in=[obj.pk for obj in objs])
43
+ obj.pk: obj
44
+ for obj in model_cls.objects.filter(pk__in=[obj.pk for obj in objs])
222
45
  }
223
46
  originals = [original_map.get(obj.pk) for obj in objs]
224
47
 
@@ -288,46 +111,150 @@ class BulkHookManager(models.Manager):
288
111
 
289
112
  @transaction.atomic
290
113
  def bulk_create(self, objs, bypass_hooks=False, bypass_validation=False, **kwargs):
114
+ """
115
+ Enhanced bulk_create that handles multi-table inheritance (MTI) and single-table models.
116
+ Falls back to Django's standard bulk_create for single-table models.
117
+ Fires hooks as usual.
118
+ """
291
119
  model_cls = self.model
292
120
 
121
+ if not objs:
122
+ return []
123
+
293
124
  if any(not isinstance(obj, model_cls) for obj in objs):
294
125
  raise TypeError(
295
126
  f"bulk_create expected instances of {model_cls.__name__}, but got {set(type(obj).__name__ for obj in objs)}"
296
127
  )
297
128
 
298
- # Check if this model uses multi-table inheritance
299
- has_multi_table_inheritance = self._has_multi_table_inheritance(model_cls)
300
-
301
- result = []
302
-
129
+ # Fire hooks before DB ops
303
130
  if not bypass_hooks:
304
131
  ctx = HookContext(model_cls)
305
-
306
- # Run validation hooks first
307
132
  if not bypass_validation:
308
133
  engine.run(model_cls, VALIDATE_CREATE, objs, ctx=ctx)
309
-
310
- # Then run business logic hooks
311
134
  engine.run(model_cls, BEFORE_CREATE, objs, ctx=ctx)
312
135
 
313
- # Perform bulk create in chunks
314
- for i in range(0, len(objs), self.CHUNK_SIZE):
315
- chunk = objs[i : i + self.CHUNK_SIZE]
316
-
317
- if has_multi_table_inheritance:
318
- # Use our multi-table bulk create
319
- created_chunk = self._bulk_create_inherited(chunk, **kwargs)
320
- else:
321
- # Use Django's standard bulk create
322
- created_chunk = super(models.Manager, self).bulk_create(chunk, **kwargs)
323
-
324
- result.extend(created_chunk)
136
+ # MTI detection: if inheritance chain > 1, use MTI logic
137
+ inheritance_chain = self._get_inheritance_chain()
138
+ if len(inheritance_chain) <= 1:
139
+ # Single-table: use Django's standard bulk_create
140
+ result = []
141
+ for i in range(0, len(objs), self.CHUNK_SIZE):
142
+ chunk = objs[i : i + self.CHUNK_SIZE]
143
+ result.extend(super(models.Manager, self).bulk_create(chunk, **kwargs))
144
+ else:
145
+ # Multi-table: use workaround (parent saves, child bulk)
146
+ result = self._mti_bulk_create(objs, inheritance_chain, **kwargs)
325
147
 
326
148
  if not bypass_hooks:
327
149
  engine.run(model_cls, AFTER_CREATE, result, ctx=ctx)
328
150
 
329
151
  return result
330
152
 
153
+ def _get_inheritance_chain(self):
154
+ """
155
+ Get the complete inheritance chain from root parent to current model.
156
+ Returns list of model classes in order: [RootParent, Parent, Child]
157
+ """
158
+ chain = []
159
+ current_model = self.model
160
+ while current_model:
161
+ if not current_model._meta.proxy:
162
+ chain.append(current_model)
163
+ parents = [
164
+ parent
165
+ for parent in current_model._meta.parents.keys()
166
+ if not parent._meta.proxy
167
+ ]
168
+ current_model = parents[0] if parents else None
169
+ chain.reverse()
170
+ return chain
171
+
172
+ def _mti_bulk_create(self, objs, inheritance_chain, **kwargs):
173
+ """
174
+ Implements workaround: individual saves for parents, bulk create for child.
175
+ """
176
+ batch_size = kwargs.get("batch_size") or len(objs)
177
+ created_objects = []
178
+ with transaction.atomic(using=self.db, savepoint=False):
179
+ for i in range(0, len(objs), batch_size):
180
+ batch = objs[i : i + batch_size]
181
+ batch_result = self._process_mti_batch(
182
+ batch, inheritance_chain, **kwargs
183
+ )
184
+ created_objects.extend(batch_result)
185
+ return created_objects
186
+
187
+ def _process_mti_batch(self, batch, inheritance_chain, **kwargs):
188
+ """
189
+ Process a single batch of objects through the inheritance chain.
190
+ """
191
+ # Step 1: Handle parent tables with individual saves (needed for PKs)
192
+ parent_objects_map = {}
193
+ for obj in batch:
194
+ parent_instances = {}
195
+ current_parent = None
196
+ for model_class in inheritance_chain[:-1]:
197
+ parent_obj = self._create_parent_instance(
198
+ obj, model_class, current_parent
199
+ )
200
+ parent_obj.save()
201
+ parent_instances[model_class] = parent_obj
202
+ current_parent = parent_obj
203
+ parent_objects_map[id(obj)] = parent_instances
204
+ # Step 2: Bulk insert for child objects
205
+ child_model = inheritance_chain[-1]
206
+ child_objects = []
207
+ for obj in batch:
208
+ child_obj = self._create_child_instance(
209
+ obj, child_model, parent_objects_map.get(id(obj), {})
210
+ )
211
+ child_objects.append(child_obj)
212
+ # Use Django's _base_manager for child table to avoid recursion
213
+ child_manager = child_model._base_manager
214
+ child_manager._for_write = True
215
+ created = child_manager.bulk_create(child_objects, **kwargs)
216
+ # Step 3: Update original objects with generated PKs and state
217
+ pk_field_name = child_model._meta.pk.name
218
+ for orig_obj, child_obj in zip(batch, created):
219
+ setattr(orig_obj, pk_field_name, getattr(child_obj, pk_field_name))
220
+ orig_obj._state.adding = False
221
+ orig_obj._state.db = self.db
222
+ return batch
223
+
224
+ def _create_parent_instance(self, source_obj, parent_model, current_parent):
225
+ parent_obj = parent_model()
226
+ for field in parent_model._meta.local_fields:
227
+ # Only copy if the field exists on the source and is not None
228
+ if hasattr(source_obj, field.name):
229
+ value = getattr(source_obj, field.name, None)
230
+ if value is not None:
231
+ setattr(parent_obj, field.name, value)
232
+ if current_parent is not None:
233
+ for field in parent_model._meta.local_fields:
234
+ if (
235
+ hasattr(field, "remote_field")
236
+ and field.remote_field
237
+ and field.remote_field.model == current_parent.__class__
238
+ ):
239
+ setattr(parent_obj, field.name, current_parent)
240
+ break
241
+ return parent_obj
242
+
243
+ def _create_child_instance(self, source_obj, child_model, parent_instances):
244
+ child_obj = child_model()
245
+ for field in child_model._meta.local_fields:
246
+ if isinstance(field, AutoField):
247
+ continue
248
+ if hasattr(source_obj, field.name):
249
+ value = getattr(source_obj, field.name, None)
250
+ if value is not None:
251
+ setattr(child_obj, field.name, value)
252
+ for parent_model, parent_instance in parent_instances.items():
253
+ parent_link = child_model._meta.get_ancestor_link(parent_model)
254
+ if parent_link:
255
+ setattr(child_obj, parent_link.name, parent_instance)
256
+ return child_obj
257
+
331
258
  @transaction.atomic
332
259
  def bulk_delete(
333
260
  self, objs, batch_size=None, bypass_hooks=False, bypass_validation=False
@@ -353,7 +280,7 @@ class BulkHookManager(models.Manager):
353
280
  engine.run(model_cls, BEFORE_DELETE, objs, ctx=ctx)
354
281
 
355
282
  pks = [obj.pk for obj in objs if obj.pk is not None]
356
-
283
+
357
284
  # Use base manager for the actual deletion to prevent recursion
358
285
  # The hooks have already been fired above, so we don't need them again
359
286
  model_cls._base_manager.filter(pk__in=pks).delete()
@@ -13,11 +13,11 @@ from django_bulk_hooks.constants import (
13
13
  )
14
14
  from django_bulk_hooks.context import HookContext
15
15
  from django_bulk_hooks.engine import run
16
- from django_bulk_hooks.manager import BulkHookManager
16
+ from django_bulk_hooks.manager import BulkManager
17
17
 
18
18
 
19
19
  class HookModelMixin(models.Model):
20
- objects = BulkHookManager()
20
+ objects = BulkManager()
21
21
 
22
22
  class Meta:
23
23
  abstract = True
@@ -1,7 +1,8 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.1
2
2
  Name: django-bulk-hooks
3
- Version: 0.1.110
3
+ Version: 0.1.112
4
4
  Summary: Hook-style hooks for Django bulk operations like bulk_create and bulk_update.
5
+ Home-page: https://github.com/AugendLimited/django-bulk-hooks
5
6
  License: MIT
6
7
  Keywords: django,bulk,hooks
7
8
  Author: Konrad Beck
@@ -13,7 +14,6 @@ Classifier: Programming Language :: Python :: 3.11
13
14
  Classifier: Programming Language :: Python :: 3.12
14
15
  Classifier: Programming Language :: Python :: 3.13
15
16
  Requires-Dist: Django (>=4.0)
16
- Project-URL: Homepage, https://github.com/AugendLimited/django-bulk-hooks
17
17
  Project-URL: Repository, https://github.com/AugendLimited/django-bulk-hooks
18
18
  Description-Content-Type: text/markdown
19
19
 
@@ -48,7 +48,7 @@ from django_bulk_hooks.models import HookModelMixin
48
48
 
49
49
  class Account(HookModelMixin):
50
50
  balance = models.DecimalField(max_digits=10, decimal_places=2)
51
- # The HookModelMixin automatically provides BulkHookManager
51
+ # The HookModelMixin automatically provides BulkManager
52
52
  ```
53
53
 
54
54
  ### Create a Hook Handler
@@ -204,10 +204,10 @@ LoanAccount.objects.bulk_update(reordered, ['balance'])
204
204
 
205
205
  ## 🧩 Integration with Queryable Properties
206
206
 
207
- You can extend from `BulkHookManager` to support formula fields or property querying.
207
+ You can extend from `BulkManager` to support formula fields or property querying.
208
208
 
209
209
  ```python
210
- class MyManager(BulkHookManager, QueryablePropertiesManager):
210
+ class MyManager(BulkManager, QueryablePropertiesManager):
211
211
  pass
212
212
  ```
213
213
 
@@ -1,4 +1,4 @@
1
- django_bulk_hooks/__init__.py,sha256=uUgpnb9AWjIAcWNpCMqBcOewSnpJjJYH6cjPbQkzoNU,140
1
+ django_bulk_hooks/__init__.py,sha256=F3VzeJFp9XaZBBuAzLM5xq_5JmAUAb3UJZ-3vTEyU8I,132
2
2
  django_bulk_hooks/conditions.py,sha256=mTvlLcttixbXRkTSNZU5VewkPUavbXRuD2BkJbVWMkw,6041
3
3
  django_bulk_hooks/constants.py,sha256=3x1H1fSUUNo0DZONN7GUVDuySZctTR-jtByBHmAIX5w,303
4
4
  django_bulk_hooks/context.py,sha256=HVDT73uSzvgrOR6mdXTvsBm3hLOgBU8ant_mB7VlFuM,380
@@ -6,12 +6,12 @@ django_bulk_hooks/decorators.py,sha256=tckDcxtOzKCbgvS9QydgeIAWTFDEl-ch3_Q--ruEG
6
6
  django_bulk_hooks/engine.py,sha256=3HbgV12JRYIy9IlygHPxZiHnFXj7EwzLyTuJNQeVIoI,1402
7
7
  django_bulk_hooks/enums.py,sha256=Zo8_tJzuzZ2IKfVc7gZ-0tWPT8q1QhqZbAyoh9ZVJbs,381
8
8
  django_bulk_hooks/handler.py,sha256=xZt8iNdYF-ACz-MnKMY0co6scWINU5V5wC1lyDn844k,4854
9
- django_bulk_hooks/manager.py,sha256=MzX9mMLbxAKMqYiKwFkieYn9EtxpLOUfA-S57co2q8I,15295
10
- django_bulk_hooks/models.py,sha256=7RG7GrOdHXFjGVPV4FPRZVNMIHHW-hMCi6hn9LH_hVI,3331
9
+ django_bulk_hooks/manager.py,sha256=XJzWQmkJB-gkAn3YGDnOdkNG-cZlsw8FdNljlH4LMYo,12510
10
+ django_bulk_hooks/models.py,sha256=zb5DGqVezzQe3wCHdtwmwcqjRE8RBZ9Vy8nU5e6zrTw,3323
11
11
  django_bulk_hooks/priority.py,sha256=HG_2D35nga68lBCZmSXTcplXrjFoRgZFRDOy4ROKonY,376
12
12
  django_bulk_hooks/queryset.py,sha256=iet4z-9SKhnresA4FBQbxx9rdYnoaOWbw9LUlGftlP0,1466
13
13
  django_bulk_hooks/registry.py,sha256=-mQBizJ06nz_tajZBinViKx_uP2Tbc1tIpTEMv7lwKA,705
14
- django_bulk_hooks-0.1.110.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
15
- django_bulk_hooks-0.1.110.dist-info/METADATA,sha256=P2a5pByPA96kdKHrURz7Rp1AGUHfV_c0JdbVo7Ost90,6951
16
- django_bulk_hooks-0.1.110.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
17
- django_bulk_hooks-0.1.110.dist-info/RECORD,,
14
+ django_bulk_hooks-0.1.112.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
15
+ django_bulk_hooks-0.1.112.dist-info/METADATA,sha256=Mu-uSiR2qwQ1UKV7-qrkUNXDBdVItA0KB8iiSf74tUQ,6927
16
+ django_bulk_hooks-0.1.112.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
17
+ django_bulk_hooks-0.1.112.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.3
2
+ Generator: poetry-core 1.9.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any