cardo-python-utils 0.5.dev49__py3-none-any.whl → 0.5.dev51__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.
- {cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/METADATA +1 -1
- {cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/RECORD +7 -7
- python_utils/django/README.md +5 -2
- python_utils/django/celery/tenant_aware_task.py +90 -6
- {cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/WHEEL +0 -0
- {cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/licenses/LICENSE +0 -0
- {cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/top_level.txt +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
cardo_python_utils-0.5.
|
|
1
|
+
cardo_python_utils-0.5.dev51.dist-info/licenses/LICENSE,sha256=N-YtxDy8n5A1Mo7JKKItNIlboiK_pMOZ48ojx76jo3g,1046
|
|
2
2
|
python_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
python_utils/choices.py,sha256=_sLNkSnQqhg55gGKNRsOQCJ75W6gnz8J8Q00528MEYk,2548
|
|
4
4
|
python_utils/data_structures.py,sha256=ZqkZYPy20zyGYOVhwb9qst4vF_P7X2A9z5E36rMUC6I,16820
|
|
@@ -11,7 +11,7 @@ python_utils/math.py,sha256=p_v8a9nVSe9426nR8H_SM8hOQrkzESVpCnn3gntw7TA,5603
|
|
|
11
11
|
python_utils/text.py,sha256=pw9CZeM_Lcw-6k4GyR-4D1Wix8A7F_V1u1IIZTIazW4,3792
|
|
12
12
|
python_utils/time.py,sha256=7Wei3uJ02Bk-BFRf-e1axoG418XQOhrXPvTwNZgTdnw,9614
|
|
13
13
|
python_utils/types_hinting.py,sha256=QVWzmXRgNxhvln14tEX_FbQYryuVYhjWJ0dVOnlF6G4,120
|
|
14
|
-
python_utils/django/README.md,sha256=
|
|
14
|
+
python_utils/django/README.md,sha256=ujInAhwiT87P6P0mr3TtMT_cO9-xCFihIjOOhzGi1gw,6909
|
|
15
15
|
python_utils/django/__init__.py,sha256=uXyqF-_5gZAlSIKoQkUTedAeBjnUHqh6lR6SzA1DEOM,64
|
|
16
16
|
python_utils/django/apps.py,sha256=vH2Ov8XgavTGKFLSjbH1kvuG7RWQCjeJepw6BSp2o3E,126
|
|
17
17
|
python_utils/django/oidc_settings.py,sha256=JqF-qOfW23JhmmVciN1B7ZV-KI7qrdn5VigTE7E2k_0,4367
|
|
@@ -30,7 +30,7 @@ python_utils/django/api/utils.py,sha256=ycpSnTtGcfdGP1_Hk0P2c8ZNId70xOYtjx1m0nAU
|
|
|
30
30
|
python_utils/django/auth/service.py,sha256=9gMURteyVnfjpZLyrvjAvgglfi2NPwrdia2LDVPfxBs,7475
|
|
31
31
|
python_utils/django/celery/__init__.py,sha256=eqKpBqhClH-7oK-kD1SUEpzt4Gqu7VWLWmhFUktee0A,79
|
|
32
32
|
python_utils/django/celery/tenant_aware_database_scheduler.py,sha256=Qcz8mFhfFcX8Opzb75qvuF9jxwuY8Nsn0gxIBKFaU8w,8087
|
|
33
|
-
python_utils/django/celery/tenant_aware_task.py,sha256=
|
|
33
|
+
python_utils/django/celery/tenant_aware_task.py,sha256=kjcAfAAOUAveOKaoOBe-yJLdhXIT7CMgNKOPOFKjEbQ,4065
|
|
34
34
|
python_utils/django/db/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
35
35
|
python_utils/django/db/alias.py,sha256=sybxhZsShJdnz13hqEyYLxuaRert9zpRPPi-bAayyM4,574
|
|
36
36
|
python_utils/django/db/routers.py,sha256=Q4YAvr37wgEgL04miPKSREhzFB5LDZrXulqStbqBZeA,252
|
|
@@ -63,7 +63,7 @@ python_utils/django/storage/__init__.py,sha256=mNn2YmD7pkXhBLHMM1444BLsCMq78YdYx
|
|
|
63
63
|
python_utils/django/storage/tenant_aware_storage.py,sha256=5dDes6xLv7_R8hIBbFIzRvPL7HL9K_RM-G6LI8qUSxM,2550
|
|
64
64
|
python_utils/django/tests/__init__.py,sha256=Nkt0a7LEHyjLvuEBZ7113VjjAWJlyZlMy-H-JZ5tNcs,252
|
|
65
65
|
python_utils/django/tests/conftest.py,sha256=KozXmXUWVcDLbkVAb7Aq4sDydGLh2YZkbRa4tkA8Z6U,3167
|
|
66
|
-
cardo_python_utils-0.5.
|
|
67
|
-
cardo_python_utils-0.5.
|
|
68
|
-
cardo_python_utils-0.5.
|
|
69
|
-
cardo_python_utils-0.5.
|
|
66
|
+
cardo_python_utils-0.5.dev51.dist-info/METADATA,sha256=lEGjnoXdpwdhOZu4C70RNapEfkoTknOUCc3DaE46AKQ,3007
|
|
67
|
+
cardo_python_utils-0.5.dev51.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
|
|
68
|
+
cardo_python_utils-0.5.dev51.dist-info/top_level.txt,sha256=zAx6OfEsjJs8BEW3okSiG_j9gpkI69xWShzum6oBgKI,13
|
|
69
|
+
cardo_python_utils-0.5.dev51.dist-info/RECORD,,
|
python_utils/django/README.md
CHANGED
|
@@ -157,14 +157,17 @@ If using `django-ninja`, apart from the settings configured above, auth utils ar
|
|
|
157
157
|
|
|
158
158
|
## Atomic Transactions
|
|
159
159
|
|
|
160
|
-
Django's `transaction.atomic` uses the default database. To make it tenant-aware, use `tenant_atomic
|
|
160
|
+
Django's `transaction.atomic` uses the default database. To make it tenant-aware, use `tenant_atomic`.
|
|
161
|
+
If `transaction.on_commit` is used, make sure to pass the tenant as DB alias as well:
|
|
161
162
|
|
|
162
163
|
```python3
|
|
163
164
|
from python_utils.django.db.transaction import tenant_atomic
|
|
164
165
|
|
|
165
166
|
@tenant_atomic
|
|
166
167
|
def my_function():
|
|
167
|
-
|
|
168
|
+
# Some logic
|
|
169
|
+
|
|
170
|
+
transaction.on_commit(do_smth, using=TenantContext.get())
|
|
168
171
|
```
|
|
169
172
|
|
|
170
173
|
## Django Shell
|
|
@@ -1,10 +1,22 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
|
|
1
3
|
from celery import Task
|
|
2
4
|
|
|
3
5
|
from ..settings import TENANT_KEY
|
|
4
6
|
from ..tenant_context import TenantContext
|
|
5
7
|
|
|
8
|
+
try:
|
|
9
|
+
# If celery_once is installed, use QueueOnce as the base class for TenantAwareTask.
|
|
10
|
+
from celery_once.tasks import QueueOnce
|
|
11
|
+
|
|
12
|
+
TaskClass = QueueOnce
|
|
13
|
+
USE_QUEUE_ONCE = True
|
|
14
|
+
except ImportError:
|
|
15
|
+
TaskClass = Task
|
|
16
|
+
USE_QUEUE_ONCE = False
|
|
17
|
+
|
|
6
18
|
|
|
7
|
-
class TenantAwareTask(
|
|
19
|
+
class TenantAwareTask(TaskClass):
|
|
8
20
|
#: Enable argument checking.
|
|
9
21
|
#: You can set this to false if you don't want the signature to be
|
|
10
22
|
#: checked when calling the task.
|
|
@@ -13,12 +25,58 @@ class TenantAwareTask(Task):
|
|
|
13
25
|
#: Defaults to :attr:`app.strict_typing <@Celery.strict_typing>`.
|
|
14
26
|
typing = False
|
|
15
27
|
|
|
28
|
+
once = {"graceful": True, "unlock_before_run": False}
|
|
29
|
+
|
|
30
|
+
def apply(
|
|
31
|
+
self,
|
|
32
|
+
args=None,
|
|
33
|
+
kwargs=None,
|
|
34
|
+
link=None,
|
|
35
|
+
link_error=None,
|
|
36
|
+
task_id=None,
|
|
37
|
+
retries=None,
|
|
38
|
+
throw=None,
|
|
39
|
+
logfile=None,
|
|
40
|
+
loglevel=None,
|
|
41
|
+
headers=None,
|
|
42
|
+
**options,
|
|
43
|
+
):
|
|
44
|
+
"""Pass the tenant name from the context in the kwargs."""
|
|
45
|
+
|
|
46
|
+
if kwargs is None:
|
|
47
|
+
kwargs = {}
|
|
48
|
+
|
|
49
|
+
if TENANT_KEY not in kwargs:
|
|
50
|
+
tenant = TenantContext.get()
|
|
51
|
+
kwargs[TENANT_KEY] = tenant
|
|
52
|
+
|
|
53
|
+
return super().apply(
|
|
54
|
+
args, kwargs, link, link_error, task_id, retries, throw, logfile, loglevel, headers, **options
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def apply_async(
|
|
58
|
+
self, args=None, kwargs=None, task_id=None, producer=None, link=None, link_error=None, shadow=None, **options
|
|
59
|
+
):
|
|
60
|
+
"""Pass the tenant name from the context in the kwargs."""
|
|
61
|
+
|
|
62
|
+
if kwargs is None:
|
|
63
|
+
kwargs = {}
|
|
64
|
+
|
|
65
|
+
if TENANT_KEY not in kwargs:
|
|
66
|
+
tenant = TenantContext.get()
|
|
67
|
+
kwargs[TENANT_KEY] = tenant
|
|
68
|
+
|
|
69
|
+
return super().apply_async(args, kwargs, task_id, producer, link, link_error, shadow, **options)
|
|
70
|
+
|
|
16
71
|
def __call__(self, *args, **kwargs):
|
|
17
|
-
"""
|
|
72
|
+
"""Use the tenant name from the kwargs to update the context."""
|
|
73
|
+
|
|
74
|
+
# Only clear the lock before the task's execution if the
|
|
75
|
+
# "unlock_before_run" option is True
|
|
76
|
+
if USE_QUEUE_ONCE and self.unlock_before_run():
|
|
77
|
+
key = self.get_key(args, kwargs)
|
|
78
|
+
self.once_backend.clear_lock(key)
|
|
18
79
|
|
|
19
|
-
# Celery backend_cleanup doesn't need a tenant and cannot be configured to pass the tenant kwarg
|
|
20
|
-
# because it is dynamically generated at @connect_on_app_finalize by celery itself
|
|
21
|
-
# ref: celery/app/builtins.py def add_backend_cleanup_task
|
|
22
80
|
if self.name != "celery.backend_cleanup":
|
|
23
81
|
tenant = kwargs.pop(TENANT_KEY)
|
|
24
82
|
TenantContext.set(tenant)
|
|
@@ -26,6 +84,32 @@ class TenantAwareTask(Task):
|
|
|
26
84
|
return self.run(*args, **kwargs)
|
|
27
85
|
|
|
28
86
|
def after_return(self, status, retval, task_id, args, kwargs, einfo):
|
|
29
|
-
"""Clear the tenant from the
|
|
87
|
+
"""Clear the tenant from the context after the task has returned."""
|
|
88
|
+
|
|
30
89
|
TenantContext.clear()
|
|
31
90
|
super().after_return(status, retval, task_id, args, kwargs, einfo)
|
|
91
|
+
|
|
92
|
+
def _get_call_args(self, args, kwargs):
|
|
93
|
+
"""This method is used by QueueOnce, to create the key for the lock."""
|
|
94
|
+
|
|
95
|
+
# Celery backend_cleanup doesn't need a tenant and cannot be configured to pass the tenant kwarg
|
|
96
|
+
# because it is dynamically generated at @connect_on_app_finalize by celery itself
|
|
97
|
+
# ref: celery/app/builtins.py def add_backend_cleanup_task
|
|
98
|
+
if self.name == "celery.backend_cleanup":
|
|
99
|
+
return super()._get_call_args(args, kwargs)
|
|
100
|
+
|
|
101
|
+
# QueueOnce._get_call_args validates the args and kwargs
|
|
102
|
+
# by binding them to the task signature.
|
|
103
|
+
# Since the TENANT_KEY kwarg is not part of the signature,
|
|
104
|
+
# we need to remove it from the kwargs before passing them
|
|
105
|
+
# to QueueOnce._get_call_args.
|
|
106
|
+
# Copy the kwargs so that we don't modify the original kwargs.
|
|
107
|
+
_kwargs = deepcopy(kwargs)
|
|
108
|
+
tenant = _kwargs.pop(TENANT_KEY)
|
|
109
|
+
tenant_kwarg = {TENANT_KEY: tenant}
|
|
110
|
+
task_call_args = super()._get_call_args(args, _kwargs)
|
|
111
|
+
|
|
112
|
+
# Add the tenant kwarg back to the return value
|
|
113
|
+
# since this value is being used to create the key for the lock.
|
|
114
|
+
# We want to lock the task for the tenant that is running it.
|
|
115
|
+
return task_call_args | tenant_kwarg
|
|
File without changes
|
{cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{cardo_python_utils-0.5.dev49.dist-info → cardo_python_utils-0.5.dev51.dist-info}/top_level.txt
RENAMED
|
File without changes
|