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

@@ -14,6 +14,35 @@ from django_bulk_hooks.constants import (
14
14
  from django_bulk_hooks.context import HookContext
15
15
  from django_bulk_hooks.engine import run
16
16
  from django_bulk_hooks.manager import BulkHookManager
17
+ from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor
18
+ from functools import wraps
19
+ import contextlib
20
+
21
+
22
+ @contextlib.contextmanager
23
+ def patch_foreign_key_behavior():
24
+ """
25
+ Temporarily patches Django's foreign key descriptor to return None instead of raising
26
+ RelatedObjectDoesNotExist when accessing an unset foreign key field.
27
+ """
28
+ original_get = ForwardManyToOneDescriptor.__get__
29
+
30
+ @wraps(original_get)
31
+ def safe_get(self, instance, cls=None):
32
+ if instance is None:
33
+ return self
34
+ try:
35
+ return original_get(self, instance, cls)
36
+ except self.RelatedObjectDoesNotExist:
37
+ return None
38
+
39
+ # Patch the descriptor
40
+ ForwardManyToOneDescriptor.__get__ = safe_get
41
+ try:
42
+ yield
43
+ finally:
44
+ # Restore original behavior
45
+ ForwardManyToOneDescriptor.__get__ = original_get
17
46
 
18
47
 
19
48
  class HookModelMixin(models.Model):
@@ -43,17 +72,20 @@ class HookModelMixin(models.Model):
43
72
  if is_create:
44
73
  # For create operations, run VALIDATE_CREATE hooks for validation
45
74
  ctx = HookContext(self.__class__)
46
- run(self.__class__, VALIDATE_CREATE, [self], ctx=ctx)
75
+ with patch_foreign_key_behavior():
76
+ run(self.__class__, VALIDATE_CREATE, [self], ctx=ctx)
47
77
  else:
48
78
  # For update operations, run VALIDATE_UPDATE hooks for validation
49
79
  try:
50
80
  old_instance = self.__class__.objects.get(pk=self.pk)
51
81
  ctx = HookContext(self.__class__)
52
- run(self.__class__, VALIDATE_UPDATE, [self], [old_instance], ctx=ctx)
82
+ with patch_foreign_key_behavior():
83
+ run(self.__class__, VALIDATE_UPDATE, [self], [old_instance], ctx=ctx)
53
84
  except self.__class__.DoesNotExist:
54
85
  # If the old instance doesn't exist, treat as create
55
86
  ctx = HookContext(self.__class__)
56
- run(self.__class__, VALIDATE_CREATE, [self], ctx=ctx)
87
+ with patch_foreign_key_behavior():
88
+ run(self.__class__, VALIDATE_CREATE, [self], ctx=ctx)
57
89
 
58
90
  def save(self, *args, **kwargs):
59
91
  is_create = self.pk is None
@@ -61,33 +93,39 @@ class HookModelMixin(models.Model):
61
93
 
62
94
  if is_create:
63
95
  # For create operations, run BEFORE hooks first
64
- run(self.__class__, BEFORE_CREATE, [self], ctx=ctx)
96
+ with patch_foreign_key_behavior():
97
+ run(self.__class__, BEFORE_CREATE, [self], ctx=ctx)
65
98
 
66
99
  # Then let Django save
67
100
  super().save(*args, **kwargs)
68
101
 
69
102
  # Then run AFTER hooks
70
- run(self.__class__, AFTER_CREATE, [self], ctx=ctx)
103
+ with patch_foreign_key_behavior():
104
+ run(self.__class__, AFTER_CREATE, [self], ctx=ctx)
71
105
  else:
72
106
  # For update operations, we need to get the old record
73
107
  try:
74
108
  old_instance = self.__class__.objects.get(pk=self.pk)
75
109
 
76
110
  # Run BEFORE hooks first
77
- run(self.__class__, BEFORE_UPDATE, [self], [old_instance], ctx=ctx)
111
+ with patch_foreign_key_behavior():
112
+ run(self.__class__, BEFORE_UPDATE, [self], [old_instance], ctx=ctx)
78
113
 
79
114
  # Then let Django save
80
115
  super().save(*args, **kwargs)
81
116
 
82
117
  # Then run AFTER hooks
83
- run(self.__class__, AFTER_UPDATE, [self], [old_instance], ctx=ctx)
118
+ with patch_foreign_key_behavior():
119
+ run(self.__class__, AFTER_UPDATE, [self], [old_instance], ctx=ctx)
84
120
  except self.__class__.DoesNotExist:
85
121
  # If the old instance doesn't exist, treat as create
86
- run(self.__class__, BEFORE_CREATE, [self], ctx=ctx)
122
+ with patch_foreign_key_behavior():
123
+ run(self.__class__, BEFORE_CREATE, [self], ctx=ctx)
87
124
 
88
125
  super().save(*args, **kwargs)
89
126
 
90
- run(self.__class__, AFTER_CREATE, [self], ctx=ctx)
127
+ with patch_foreign_key_behavior():
128
+ run(self.__class__, AFTER_CREATE, [self], ctx=ctx)
91
129
 
92
130
  return self
93
131
 
@@ -95,12 +133,15 @@ class HookModelMixin(models.Model):
95
133
  ctx = HookContext(self.__class__)
96
134
 
97
135
  # Run validation hooks first
98
- run(self.__class__, VALIDATE_DELETE, [self], ctx=ctx)
136
+ with patch_foreign_key_behavior():
137
+ run(self.__class__, VALIDATE_DELETE, [self], ctx=ctx)
99
138
 
100
139
  # Then run business logic hooks
101
- run(self.__class__, BEFORE_DELETE, [self], ctx=ctx)
140
+ with patch_foreign_key_behavior():
141
+ run(self.__class__, BEFORE_DELETE, [self], ctx=ctx)
102
142
 
103
143
  result = super().delete(*args, **kwargs)
104
144
 
105
- run(self.__class__, AFTER_DELETE, [self], ctx=ctx)
145
+ with patch_foreign_key_behavior():
146
+ run(self.__class__, AFTER_DELETE, [self], ctx=ctx)
106
147
  return result
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: django-bulk-hooks
3
- Version: 0.1.84
3
+ Version: 0.1.85
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
@@ -7,10 +7,10 @@ django_bulk_hooks/engine.py,sha256=b1AO8qyl2VMt1CHyefn1Gt1_ARaV80yBZ3mGXsJ9_eA,2
7
7
  django_bulk_hooks/enums.py,sha256=Zo8_tJzuzZ2IKfVc7gZ-0tWPT8q1QhqZbAyoh9ZVJbs,381
8
8
  django_bulk_hooks/handler.py,sha256=Qpg_zT6SsQiTlhduvzXxPdG6uynjyR2fBjj-R6HZiXI,4861
9
9
  django_bulk_hooks/manager.py,sha256=-V128ACxPAz82ua4jQRFUkjAKtKW4MN5ppz0bHcv5s4,7138
10
- django_bulk_hooks/models.py,sha256=R-_OLnDUYF9gE985UQp4xZnP71on940-jvPILML2Pj8,3857
10
+ django_bulk_hooks/models.py,sha256=CD-v8JAZCOnGAHsAxOrlJz5HiOOXNVhGi7p8n3m4oLk,5423
11
11
  django_bulk_hooks/queryset.py,sha256=7lLqhZ-XOYsZ1I3Loxi4Nhz79M8HlTYE413AW8nyeDI,1330
12
12
  django_bulk_hooks/registry.py,sha256=Vh78exKYcdZhM27120kQm-iXGOjd_kf9ZUYBZ8eQ2V0,683
13
- django_bulk_hooks-0.1.84.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
14
- django_bulk_hooks-0.1.84.dist-info/METADATA,sha256=rNlGfhaSmiA--K8GQ2tMEQ29wlFz1IAGJzeEDIfNh7E,9051
15
- django_bulk_hooks-0.1.84.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
16
- django_bulk_hooks-0.1.84.dist-info/RECORD,,
13
+ django_bulk_hooks-0.1.85.dist-info/LICENSE,sha256=dguKIcbDGeZD-vXWdLyErPUALYOvtX_fO4Zjhq481uk,1088
14
+ django_bulk_hooks-0.1.85.dist-info/METADATA,sha256=JJ0uZaiHpf5An0kqNQySULj4hSSpn0JHEppz-xUwnMg,9051
15
+ django_bulk_hooks-0.1.85.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
16
+ django_bulk_hooks-0.1.85.dist-info/RECORD,,