File tree Expand file tree Collapse file tree 3 files changed +57
-1
lines changed
Expand file tree Collapse file tree 3 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44
55[project ]
66name = " cardo-python-utils"
7- version = " 0.5.dev48 "
7+ version = " 0.5.dev49 "
88description = " Python library enhanced with a wide range of functions for different scenarios."
99readme = " README.rst"
1010requires-python = " >=3.8"
Original file line number Diff line number Diff line change @@ -155,6 +155,30 @@ admin.site.has_permission = has_admin_site_permission
155155
156156If using ` django-ninja ` , apart from the settings configured above, auth utils are provided in the django/api/ninja.py module.
157157
158+ ## Atomic Transactions
159+
160+ Django's ` transaction.atomic ` uses the default database. To make it tenant-aware, use ` tenant_atomic `
161+
162+ ``` python3
163+ from python_utils.django.db.transaction import tenant_atomic
164+
165+ @tenant_atomic
166+ def my_function ():
167+ ...
168+ ```
169+
170+ ## Django Shell
171+
172+ This library overrides the shell command of Django, so that it requires the ` tenant ` arg.
173+ This way, the shell is automatically initialized with the context set to the tenant.
174+
175+ ``` bash
176+ ./manage.py shell --tenant=tenant1
177+
178+ Starting shell for tenant: -tenant=tenant
179+ >>>
180+ ```
181+
158182## Testing
159183
160184In order for tests to work, create the following autouse fixtures:
Original file line number Diff line number Diff line change 1+ from django .core .management .commands .shell import Command as ShellCommand
2+
3+ from ...settings import TENANT_DATABASES
4+ from ...tenant_context import TenantContext
5+
6+
7+ class Command (ShellCommand ):
8+ help = "Runs a Python interactive interpreter for a specific tenant."
9+
10+ def add_arguments (self , parser ):
11+ super ().add_arguments (parser )
12+
13+ parser .add_argument (
14+ "--tenant" ,
15+ action = "store" ,
16+ dest = "tenant" ,
17+ help = "Specify the tenant to run the shell for." ,
18+ required = True ,
19+ type = str ,
20+ )
21+
22+ def handle (self , ** options ):
23+ tenant = options ["tenant" ]
24+
25+ if tenant not in TENANT_DATABASES :
26+ self .stdout .write (self .style .ERROR (f"Tenant '{ tenant } ' not found in DATABASES settings." ))
27+ return
28+
29+ self .stdout .write (self .style .SUCCESS (f"Starting shell for tenant: { tenant } " ))
30+
31+ with TenantContext (tenant ):
32+ super ().handle (** options )
You can’t perform that action at this time.
0 commit comments