django-bulk-hooks 0.1.102__py3-none-any.whl → 0.1.103__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.

@@ -0,0 +1,217 @@
1
+ Metadata-Version: 2.1
2
+ Name: django-bulk-hooks
3
+ Version: 0.1.103
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
6
+ License: MIT
7
+ Keywords: django,bulk,hooks
8
+ Author: Konrad Beck
9
+ Author-email: konrad.beck@merchantcapital.co.za
10
+ Requires-Python: >=3.11,<4.0
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Requires-Dist: Django (>=4.0)
17
+ Project-URL: Repository, https://github.com/AugendLimited/django-bulk-hooks
18
+ Description-Content-Type: text/markdown
19
+
20
+
21
+ # django-bulk-hooks
22
+
23
+ ⚡ Bulk hooks for Django bulk operations and individual model lifecycle events.
24
+
25
+ `django-bulk-hooks` brings a declarative, hook-like experience to Django's `bulk_create`, `bulk_update`, and `bulk_delete` — including support for `BEFORE_` and `AFTER_` hooks, conditions, batching, and transactional safety. It also provides comprehensive lifecycle hooks for individual model operations.
26
+
27
+ ## ✨ Features
28
+
29
+ - Declarative hook system: `@hook(AFTER_UPDATE, condition=...)`
30
+ - BEFORE/AFTER hooks for create, update, delete
31
+ - Hook-aware manager that wraps Django's `bulk_` operations
32
+ - **NEW**: `HookModelMixin` for individual model lifecycle events
33
+ - Hook chaining, hook deduplication, and atomicity
34
+ - Class-based hook handlers with DI support
35
+ - Support for both bulk and individual model operations
36
+
37
+ ## 🚀 Quickstart
38
+
39
+ ```bash
40
+ pip install django-bulk-hooks
41
+ ```
42
+
43
+ ### Define Your Model
44
+
45
+ ```python
46
+ from django.db import models
47
+ from django_bulk_hooks.models import HookModelMixin
48
+
49
+ class Account(HookModelMixin):
50
+ balance = models.DecimalField(max_digits=10, decimal_places=2)
51
+ # The HookModelMixin automatically provides BulkHookManager
52
+ ```
53
+
54
+ ### Create a Hook Handler
55
+
56
+ ```python
57
+ from django_bulk_hooks import hook, AFTER_UPDATE, Hook
58
+ from django_bulk_hooks.conditions import WhenFieldHasChanged
59
+ from .models import Account
60
+
61
+ class AccountHooks(Hook):
62
+ @hook(AFTER_UPDATE, model=Account, condition=WhenFieldHasChanged("balance"))
63
+ def log_balance_change(self, new_records, old_records):
64
+ print("Accounts updated:", [a.pk for a in new_records])
65
+
66
+ @hook(BEFORE_CREATE, model=Account)
67
+ def before_create(self, new_records, old_records):
68
+ for account in new_records:
69
+ if account.balance < 0:
70
+ raise ValueError("Account cannot have negative balance")
71
+
72
+ @hook(AFTER_DELETE, model=Account)
73
+ def after_delete(self, new_records, old_records):
74
+ print("Accounts deleted:", [a.pk for a in old_records])
75
+ ```
76
+
77
+ ## 🛠 Supported Hook Events
78
+
79
+ - `BEFORE_CREATE`, `AFTER_CREATE`
80
+ - `BEFORE_UPDATE`, `AFTER_UPDATE`
81
+ - `BEFORE_DELETE`, `AFTER_DELETE`
82
+
83
+ ## 🔄 Lifecycle Events
84
+
85
+ ### Individual Model Operations
86
+
87
+ The `HookModelMixin` automatically triggers hooks for individual model operations:
88
+
89
+ ```python
90
+ # These will trigger BEFORE_CREATE and AFTER_CREATE hooks
91
+ account = Account.objects.create(balance=100.00)
92
+ account.save() # for new instances
93
+
94
+ # These will trigger BEFORE_UPDATE and AFTER_UPDATE hooks
95
+ account.balance = 200.00
96
+ account.save() # for existing instances
97
+
98
+ # This will trigger BEFORE_DELETE and AFTER_DELETE hooks
99
+ account.delete()
100
+ ```
101
+
102
+ ### Bulk Operations
103
+
104
+ Bulk operations also trigger the same hooks:
105
+
106
+ ```python
107
+ # Bulk create - triggers BEFORE_CREATE and AFTER_CREATE hooks
108
+ accounts = [
109
+ Account(balance=100.00),
110
+ Account(balance=200.00),
111
+ ]
112
+ Account.objects.bulk_create(accounts)
113
+
114
+ # Bulk update - triggers BEFORE_UPDATE and AFTER_UPDATE hooks
115
+ for account in accounts:
116
+ account.balance *= 1.1
117
+ Account.objects.bulk_update(accounts, ['balance'])
118
+
119
+ # Bulk delete - triggers BEFORE_DELETE and AFTER_DELETE hooks
120
+ Account.objects.bulk_delete(accounts)
121
+ ```
122
+
123
+ ### Queryset Operations
124
+
125
+ Queryset operations are also supported:
126
+
127
+ ```python
128
+ # Queryset update - triggers BEFORE_UPDATE and AFTER_UPDATE hooks
129
+ Account.objects.update(balance=0.00)
130
+
131
+ # Queryset delete - triggers BEFORE_DELETE and AFTER_DELETE hooks
132
+ Account.objects.delete()
133
+ ```
134
+
135
+ ## 🧠 Why?
136
+
137
+ Django's `bulk_` methods bypass signals and `save()`. This package fills that gap with:
138
+
139
+ - Hooks that behave consistently across creates/updates/deletes
140
+ - **NEW**: Individual model lifecycle hooks that work with `save()` and `delete()`
141
+ - Scalable performance via chunking (default 200)
142
+ - Support for `@hook` decorators and centralized hook classes
143
+ - **NEW**: Automatic hook triggering for admin operations and other Django features
144
+ - **NEW**: Proper ordering guarantees for old/new record pairing in hooks (Salesforce-like behavior)
145
+
146
+ ## 📦 Usage Examples
147
+
148
+ ### Individual Model Operations
149
+
150
+ ```python
151
+ # These automatically trigger hooks
152
+ account = Account.objects.create(balance=100.00)
153
+ account.balance = 200.00
154
+ account.save()
155
+ account.delete()
156
+ ```
157
+
158
+ ### Bulk Operations
159
+
160
+ ```python
161
+ # These also trigger hooks
162
+ Account.objects.bulk_create(accounts)
163
+ Account.objects.bulk_update(accounts, ['balance'])
164
+ Account.objects.bulk_delete(accounts)
165
+ ```
166
+
167
+ ### Advanced Hook Usage
168
+
169
+ ```python
170
+ class AdvancedAccountHooks(Hook):
171
+ @hook(BEFORE_UPDATE, model=Account, condition=WhenFieldHasChanged("balance"))
172
+ def validate_balance_change(self, new_records, old_records):
173
+ for new_account, old_account in zip(new_records, old_records):
174
+ if new_account.balance < 0 and old_account.balance >= 0:
175
+ raise ValueError("Cannot set negative balance")
176
+
177
+ @hook(AFTER_CREATE, model=Account)
178
+ def send_welcome_email(self, new_records, old_records):
179
+ for account in new_records:
180
+ # Send welcome email logic here
181
+ pass
182
+ ```
183
+
184
+ ### Salesforce-like Ordering Guarantees
185
+
186
+ The system ensures that `old_records` and `new_records` are always properly paired, regardless of the order in which you pass objects to bulk operations:
187
+
188
+ ```python
189
+ class LoanAccountHooks(Hook):
190
+ @hook(BEFORE_UPDATE, model=LoanAccount)
191
+ def validate_account_number(self, new_records, old_records):
192
+ # old_records[i] always corresponds to new_records[i]
193
+ for new_account, old_account in zip(new_records, old_records):
194
+ if old_account.account_number != new_account.account_number:
195
+ raise ValidationError("Account number cannot be changed")
196
+
197
+ # This works correctly even with reordered objects:
198
+ accounts = [account1, account2, account3] # IDs: 1, 2, 3
199
+ reordered = [account3, account1, account2] # IDs: 3, 1, 2
200
+
201
+ # The hook will still receive properly paired old/new records
202
+ LoanAccount.objects.bulk_update(reordered, ['balance'])
203
+ ```
204
+
205
+ ## 🧩 Integration with Queryable Properties
206
+
207
+ You can extend from `BulkHookManager` to support formula fields or property querying.
208
+
209
+ ```python
210
+ class MyManager(BulkHookManager, QueryablePropertiesManager):
211
+ pass
212
+ ```
213
+
214
+ ## 📝 License
215
+
216
+ MIT © 2024 Augend / Konrad Beck
217
+
@@ -0,0 +1,17 @@
1
+ django_bulk_hooks/__init__.py,sha256=uUgpnb9AWjIAcWNpCMqBcOewSnpJjJYH6cjPbQkzoNU,140
2
+ django_bulk_hooks/conditions.py,sha256=mTvlLcttixbXRkTSNZU5VewkPUavbXRuD2BkJbVWMkw,6041
3
+ django_bulk_hooks/constants.py,sha256=3x1H1fSUUNo0DZONN7GUVDuySZctTR-jtByBHmAIX5w,303
4
+ django_bulk_hooks/context.py,sha256=HVDT73uSzvgrOR6mdXTvsBm3hLOgBU8ant_mB7VlFuM,380
5
+ django_bulk_hooks/decorators.py,sha256=tckDcxtOzKCbgvS9QydgeIAWTFDEl-ch3_Q--ruEGdQ,4831
6
+ django_bulk_hooks/engine.py,sha256=3HbgV12JRYIy9IlygHPxZiHnFXj7EwzLyTuJNQeVIoI,1402
7
+ django_bulk_hooks/enums.py,sha256=Zo8_tJzuzZ2IKfVc7gZ-0tWPT8q1QhqZbAyoh9ZVJbs,381
8
+ django_bulk_hooks/handler.py,sha256=xZt8iNdYF-ACz-MnKMY0co6scWINU5V5wC1lyDn844k,4854
9
+ django_bulk_hooks/manager.py,sha256=JkhOw9XbEUt8VpunhUSP0z-NlazIFUWmAesj7H4ge8w,7148
10
+ django_bulk_hooks/models.py,sha256=7RG7GrOdHXFjGVPV4FPRZVNMIHHW-hMCi6hn9LH_hVI,3331
11
+ django_bulk_hooks/priority.py,sha256=HG_2D35nga68lBCZmSXTcplXrjFoRgZFRDOy4ROKonY,376
12
+ django_bulk_hooks/queryset.py,sha256=iet4z-9SKhnresA4FBQbxx9rdYnoaOWbw9LUlGftlP0,1466
13
+ django_bulk_hooks/registry.py,sha256=-mQBizJ06nz_tajZBinViKx_uP2Tbc1tIpTEMv7lwKA,705
14
+ django_bulk_hooks-0.1.103.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
15
+ django_bulk_hooks-0.1.103.dist-info/METADATA,sha256=Y_okaTk73sz_FCHWBOczBT8tzRz-wQJmNzmofLWy3MI,6939
16
+ django_bulk_hooks-0.1.103.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
17
+ django_bulk_hooks-0.1.103.dist-info/RECORD,,
@@ -1,228 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: django-bulk-hooks
3
- Version: 0.1.102
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
6
- License: MIT
7
- Keywords: django,bulk,hooks
8
- Author: Konrad Beck
9
- Author-email: konrad.beck@merchantcapital.co.za
10
- Requires-Python: >=3.11,<4.0
11
- Classifier: License :: OSI Approved :: MIT License
12
- Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.11
14
- Classifier: Programming Language :: Python :: 3.12
15
- Classifier: Programming Language :: Python :: 3.13
16
- Requires-Dist: Django (>=4.0)
17
- Project-URL: Repository, https://github.com/AugendLimited/django-bulk-hooks
18
- Description-Content-Type: text/markdown
19
-
20
-
21
- # django-bulk-hooks
22
-
23
- ⚡ Bulk hooks for Django bulk operations and individual model lifecycle events.
24
-
25
- `django-bulk-hooks` brings a declarative, hook-like experience to Django's `bulk_create`, `bulk_update`, and `bulk_delete` — including support for `BEFORE_` and `AFTER_` hooks, conditions, batching, and transactional safety. It also provides comprehensive lifecycle hooks for individual model operations.
26
-
27
- ## ✨ Features
28
-
29
- - Declarative hook system: `@hook(AFTER_UPDATE, condition=...)`
30
- - BEFORE/AFTER hooks for create, update, delete
31
- - Hook-aware manager that wraps Django's `bulk_` operations
32
- - **NEW**: `HookModelMixin` for individual model lifecycle events
33
- - Hook chaining, hook deduplication, and atomicity
34
- - Class-based hook handlers with DI support
35
- - Support for both bulk and individual model operations
36
- - **NEW**: Safe handling of related objects to prevent `RelatedObjectDoesNotExist` errors
37
-
38
- ## 🚀 Quickstart
39
-
40
- ```bash
41
- pip install django-bulk-hooks
42
- ```
43
-
44
- ### Define Your Model
45
-
46
- ```python
47
- from django.db import models
48
- from django_bulk_hooks.models import HookModelMixin
49
-
50
- class Account(HookModelMixin):
51
- balance = models.DecimalField(max_digits=10, decimal_places=2)
52
- # The HookModelMixin automatically provides BulkHookManager
53
- ```
54
-
55
- ### Create a Hook Handler
56
-
57
- ```python
58
- from django_bulk_hooks import hook, AFTER_UPDATE, Hook
59
- from django_bulk_hooks.conditions import WhenFieldHasChanged
60
- from .models import Account
61
-
62
- class AccountHooks(HookHandler):
63
- @hook(AFTER_UPDATE, model=Account, condition=WhenFieldHasChanged("balance"))
64
- def log_balance_change(self, new_records, old_records):
65
- print("Accounts updated:", [a.pk for a in new_records])
66
-
67
- @hook(BEFORE_CREATE, model=Account)
68
- def before_create(self, new_records, old_records):
69
- for account in new_records:
70
- if account.balance < 0:
71
- raise ValueError("Account cannot have negative balance")
72
-
73
- @hook(AFTER_DELETE, model=Account)
74
- def after_delete(self, new_records, old_records):
75
- print("Accounts deleted:", [a.pk for a in old_records])
76
- ```
77
-
78
- ### Advanced Hook Usage
79
-
80
- ```python
81
- class AdvancedAccountHooks(HookHandler):
82
- @hook(BEFORE_UPDATE, model=Account, condition=WhenFieldHasChanged("balance"))
83
- def validate_balance_change(self, new_records, old_records):
84
- for new_account, old_account in zip(new_records, old_records):
85
- if new_account.balance < 0 and old_account.balance >= 0:
86
- raise ValueError("Cannot set negative balance")
87
-
88
- @hook(AFTER_CREATE, model=Account)
89
- def send_welcome_email(self, new_records, old_records):
90
- for account in new_records:
91
- # Send welcome email logic here
92
- pass
93
- ```
94
-
95
- ## 🔒 Safely Handling Related Objects
96
-
97
- One of the most common issues when working with hooks is the `RelatedObjectDoesNotExist` exception. This occurs when you try to access a related object that doesn't exist or hasn't been saved yet.
98
-
99
- ### The Problem
100
-
101
- ```python
102
- # ❌ DANGEROUS: This can raise RelatedObjectDoesNotExist
103
- @hook(AFTER_CREATE, model=Transaction)
104
- def process_transaction(self, new_records, old_records):
105
- for transaction in new_records:
106
- # This will fail if transaction.status is None or doesn't exist
107
- if transaction.status.name == "COMPLETE":
108
- # Process the transaction
109
- pass
110
- ```
111
-
112
- ### The Solution
113
-
114
- Use the `safe_get_related_attr` utility function to safely access related object attributes:
115
-
116
- ```python
117
- from django_bulk_hooks.conditions import safe_get_related_attr
118
-
119
- # ✅ SAFE: Use safe_get_related_attr to handle None values
120
- @hook(AFTER_CREATE, model=Transaction)
121
- def process_transaction(self, new_records, old_records):
122
- for transaction in new_records:
123
- # Safely get the status name, returns None if status doesn't exist
124
- status_name = safe_get_related_attr(transaction, 'status', 'name')
125
-
126
- if status_name == "COMPLETE":
127
- # Process the transaction
128
- pass
129
- elif status_name is None:
130
- # Handle case where status is not set
131
- print(f"Transaction {transaction.id} has no status")
132
- ```
133
-
134
- ### Complete Example
135
-
136
- ```python
137
- from django.db import models
138
- from django_bulk_hooks import hook
139
- from django_bulk_hooks.conditions import safe_get_related_attr
140
-
141
- class Status(models.Model):
142
- name = models.CharField(max_length=50)
143
-
144
- class Transaction(HookModelMixin, models.Model):
145
- amount = models.DecimalField(max_digits=10, decimal_places=2)
146
- status = models.ForeignKey(Status, on_delete=models.CASCADE, null=True, blank=True)
147
- category = models.ForeignKey('Category', on_delete=models.CASCADE, null=True, blank=True)
148
-
149
- class TransactionHandler:
150
- @hook(Transaction, "before_create")
151
- def set_default_status(self, new_records, old_records=None):
152
- """Set default status for new transactions."""
153
- default_status = Status.objects.filter(name="PENDING").first()
154
- for transaction in new_records:
155
- if transaction.status is None:
156
- transaction.status = default_status
157
-
158
- @hook(Transaction, "after_create")
159
- def process_transactions(self, new_records, old_records=None):
160
- """Process transactions based on their status."""
161
- for transaction in new_records:
162
- # ✅ SAFE: Get status name safely
163
- status_name = safe_get_related_attr(transaction, 'status', 'name')
164
-
165
- if status_name == "COMPLETE":
166
- self._process_complete_transaction(transaction)
167
- elif status_name == "FAILED":
168
- self._process_failed_transaction(transaction)
169
- elif status_name is None:
170
- print(f"Transaction {transaction.id} has no status")
171
-
172
- # ✅ SAFE: Check for related object existence
173
- category = safe_get_related_attr(transaction, 'category')
174
- if category:
175
- print(f"Transaction {transaction.id} belongs to category: {category.name}")
176
-
177
- def _process_complete_transaction(self, transaction):
178
- # Process complete transaction logic
179
- pass
180
-
181
- def _process_failed_transaction(self, transaction):
182
- # Process failed transaction logic
183
- pass
184
- ```
185
-
186
- ### Best Practices for Related Objects
187
-
188
- 1. **Always use `safe_get_related_attr`** when accessing related object attributes in hooks
189
- 2. **Set default values in `BEFORE_CREATE` hooks** to ensure related objects exist
190
- 3. **Handle None cases explicitly** to avoid unexpected behavior
191
- 4. **Use bulk operations efficiently** by fetching related objects once and reusing them
192
-
193
- ```python
194
- class EfficientTransactionHandler:
195
- @hook(Transaction, "before_create")
196
- def prepare_transactions(self, new_records, old_records=None):
197
- """Efficiently prepare transactions for bulk creation."""
198
- # Get default objects once to avoid multiple queries
199
- default_status = Status.objects.filter(name="PENDING").first()
200
- default_category = Category.objects.filter(name="GENERAL").first()
201
-
202
- for transaction in new_records:
203
- if transaction.status is None:
204
- transaction.status = default_status
205
- if transaction.category is None:
206
- transaction.category = default_category
207
-
208
- @hook(Transaction, "after_create")
209
- def post_creation_processing(self, new_records, old_records=None):
210
- """Process transactions after creation."""
211
- # Group by status for efficient processing
212
- transactions_by_status = {}
213
-
214
- for transaction in new_records:
215
- status_name = safe_get_related_attr(transaction, 'status', 'name')
216
- if status_name not in transactions_by_status:
217
- transactions_by_status[status_name] = []
218
- transactions_by_status[status_name].append(transaction)
219
-
220
- # Process each group
221
- for status_name, transactions in transactions_by_status.items():
222
- if status_name == "COMPLETE":
223
- self._batch_process_complete(transactions)
224
- elif status_name == "FAILED":
225
- self._batch_process_failed(transactions)
226
- ```
227
-
228
- This approach ensures your hooks are robust and won't fail due to missing related objects, while also being efficient with database queries.
@@ -1,16 +0,0 @@
1
- django_bulk_hooks/__init__.py,sha256=b5LIO5oWX9ZVITZddma_E_Hosx8Zy9B3_v3z8HmSykg,1132
2
- django_bulk_hooks/conditions.py,sha256=iCdIrpVciGsmyKgIEjcC0nl_2-mAxay4Tss-ZaenSuY,13735
3
- django_bulk_hooks/constants.py,sha256=3x1H1fSUUNo0DZONN7GUVDuySZctTR-jtByBHmAIX5w,303
4
- django_bulk_hooks/context.py,sha256=HVDT73uSzvgrOR6mdXTvsBm3hLOgBU8ant_mB7VlFuM,380
5
- django_bulk_hooks/decorators.py,sha256=zstmb27dKcOHu3Atg7cauewCTzPvUmq03mzVKJRi56o,7230
6
- django_bulk_hooks/engine.py,sha256=kWxAggInO8GhmQfSzJrGATAPgnuG7580llpO9NoxHA8,2897
7
- django_bulk_hooks/enums.py,sha256=Zo8_tJzuzZ2IKfVc7gZ-0tWPT8q1QhqZbAyoh9ZVJbs,381
8
- django_bulk_hooks/handler.py,sha256=Qpg_zT6SsQiTlhduvzXxPdG6uynjyR2fBjj-R6HZiXI,4861
9
- django_bulk_hooks/manager.py,sha256=vyIc7ktNbjXCJrqP7SO7lsamBYFrZSCIQBmXSnpK874,12481
10
- django_bulk_hooks/models.py,sha256=9KvWkmrR0wbTHN6r7-FrSSO9ViS83NvG7iXLBw_iDZs,4793
11
- django_bulk_hooks/queryset.py,sha256=7lLqhZ-XOYsZ1I3Loxi4Nhz79M8HlTYE413AW8nyeDI,1330
12
- django_bulk_hooks/registry.py,sha256=Vh78exKYcdZhM27120kQm-iXGOjd_kf9ZUYBZ8eQ2V0,683
13
- django_bulk_hooks-0.1.102.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
14
- django_bulk_hooks-0.1.102.dist-info/METADATA,sha256=xpdCGrDTHoLswDQAXL67JvvvUkyEyTkyQfdbRGDiuz8,9040
15
- django_bulk_hooks-0.1.102.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
16
- django_bulk_hooks-0.1.102.dist-info/RECORD,,