cardo-python-utils 0.5.dev49__tar.gz → 0.5.dev50__tar.gz

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.
Files changed (76) hide show
  1. {cardo_python_utils-0.5.dev49/cardo_python_utils.egg-info → cardo_python_utils-0.5.dev50}/PKG-INFO +1 -1
  2. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50/cardo_python_utils.egg-info}/PKG-INFO +1 -1
  3. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/pyproject.toml +1 -1
  4. cardo_python_utils-0.5.dev50/python_utils/django/celery/tenant_aware_task.py +73 -0
  5. cardo_python_utils-0.5.dev49/python_utils/django/celery/tenant_aware_task.py +0 -31
  6. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/LICENSE +0 -0
  7. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/MANIFEST.in +0 -0
  8. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/README.rst +0 -0
  9. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/cardo_python_utils.egg-info/SOURCES.txt +0 -0
  10. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/cardo_python_utils.egg-info/dependency_links.txt +0 -0
  11. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/cardo_python_utils.egg-info/requires.txt +0 -0
  12. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/cardo_python_utils.egg-info/top_level.txt +0 -0
  13. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/__init__.py +0 -0
  14. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/choices.py +0 -0
  15. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/data_structures.py +0 -0
  16. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/db.py +0 -0
  17. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/README.md +0 -0
  18. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/__init__.py +0 -0
  19. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/admin/__init__.py +0 -0
  20. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/admin/auth.py +0 -0
  21. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/admin/templates/__init__.py +0 -0
  22. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/admin/templates/user_groups_changelist.html +0 -0
  23. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/admin/user_group.py +0 -0
  24. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/admin/views.py +0 -0
  25. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/api/__init__.py +0 -0
  26. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/api/drf.py +0 -0
  27. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/api/ninja.py +0 -0
  28. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/api/utils.py +0 -0
  29. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/apps.py +0 -0
  30. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/auth/service.py +0 -0
  31. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/celery/__init__.py +0 -0
  32. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/celery/tenant_aware_database_scheduler.py +0 -0
  33. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/db/__init__.py +0 -0
  34. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/db/alias.py +0 -0
  35. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/db/routers.py +0 -0
  36. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/db/transaction.py +0 -0
  37. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/db/utils.py +0 -0
  38. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/management/__init__.py +0 -0
  39. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/management/commands/__init__.py +0 -0
  40. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/management/commands/migrateall.py +0 -0
  41. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/management/commands/shell.py +0 -0
  42. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/management/commands/tenant_aware_command.py +0 -0
  43. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/middleware/__init__.py +0 -0
  44. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/middleware/tenant_aware_http_middleware.py +0 -0
  45. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/middleware/tenant_aware_websocket_middleware.py +0 -0
  46. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0001_initial.py +0 -0
  47. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0001_initial_squashed_0005_alter_userrole_id.py +0 -0
  48. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0002_auto_20220120_1617.py +0 -0
  49. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0003_auto_20220513_1025.py +0 -0
  50. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0004_auto_20220817_1526.py +0 -0
  51. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0005_alter_userrole_id.py +0 -0
  52. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0006_userrole_organization_and_more.py +0 -0
  53. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0007_user_demo.py +0 -0
  54. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/0008_delete_userrole.py +0 -0
  55. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/migrations/__init__.py +0 -0
  56. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/models/__init__.py +0 -0
  57. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/models/user.py +0 -0
  58. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/models/user_group.py +0 -0
  59. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/oidc_settings.py +0 -0
  60. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/redis/__init__.py +0 -0
  61. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/redis/key_function.py +0 -0
  62. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/settings.py +0 -0
  63. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/storage/__init__.py +0 -0
  64. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/storage/tenant_aware_storage.py +0 -0
  65. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/tenant_context.py +0 -0
  66. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/tests/__init__.py +0 -0
  67. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django/tests/conftest.py +0 -0
  68. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/django_utils.py +0 -0
  69. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/esma_choices.py +0 -0
  70. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/exceptions.py +0 -0
  71. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/imports.py +0 -0
  72. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/math.py +0 -0
  73. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/text.py +0 -0
  74. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/time.py +0 -0
  75. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/python_utils/types_hinting.py +0 -0
  76. {cardo_python_utils-0.5.dev49 → cardo_python_utils-0.5.dev50}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cardo-python-utils
3
- Version: 0.5.dev49
3
+ Version: 0.5.dev50
4
4
  Summary: Python library enhanced with a wide range of functions for different scenarios.
5
5
  Author-email: CardoAI <hello@cardoai.com>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cardo-python-utils
3
- Version: 0.5.dev49
3
+ Version: 0.5.dev50
4
4
  Summary: Python library enhanced with a wide range of functions for different scenarios.
5
5
  Author-email: CardoAI <hello@cardoai.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cardo-python-utils"
7
- version = "0.5.dev49"
7
+ version = "0.5.dev50"
8
8
  description = "Python library enhanced with a wide range of functions for different scenarios."
9
9
  readme = "README.rst"
10
10
  requires-python = ">=3.8"
@@ -0,0 +1,73 @@
1
+ from copy import deepcopy
2
+
3
+ from celery import Task
4
+
5
+ from ..settings import TENANT_KEY
6
+ from ..tenant_context import TenantContext
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
+
18
+
19
+ class TenantAwareTask(TaskClass):
20
+ #: Enable argument checking.
21
+ #: You can set this to false if you don't want the signature to be
22
+ #: checked when calling the task.
23
+ #: Set to false because we are attaching a tenant to the task
24
+ #: and we don't want to check the signature.
25
+ #: Defaults to :attr:`app.strict_typing <@Celery.strict_typing>`.
26
+ typing = False
27
+
28
+ once = {"graceful": True, "unlock_before_run": False}
29
+
30
+ def __call__(self, *args, **kwargs):
31
+ """Override the __call__ method to set the tenant name in the thread namespace."""
32
+
33
+ # Only clear the lock before the task's execution if the
34
+ # "unlock_before_run" option is True
35
+ if USE_QUEUE_ONCE and self.unlock_before_run():
36
+ key = self.get_key(args, kwargs)
37
+ self.once_backend.clear_lock(key)
38
+
39
+ if self.name != "celery.backend_cleanup":
40
+ tenant = kwargs.pop(TENANT_KEY)
41
+ TenantContext.set(tenant)
42
+
43
+ return self.run(*args, **kwargs)
44
+
45
+ def after_return(self, status, retval, task_id, args, kwargs, einfo):
46
+ """Clear the tenant from the thread namespace after the task has returned."""
47
+ TenantContext.clear()
48
+ super().after_return(status, retval, task_id, args, kwargs, einfo)
49
+
50
+ def _get_call_args(self, args, kwargs):
51
+ """This method is used by QueueOnce, to create the key for the lock."""
52
+
53
+ # Celery backend_cleanup doesn't need a tenant and cannot be configured to pass the tenant kwarg
54
+ # because it is dynamically generated at @connect_on_app_finalize by celery itself
55
+ # ref: celery/app/builtins.py def add_backend_cleanup_task
56
+ if self.name == "celery.backend_cleanup":
57
+ return super()._get_call_args(args, kwargs)
58
+
59
+ # QueueOnce._get_call_args validates the args and kwargs
60
+ # by binding them to the task signature.
61
+ # Since the TENANT_KEY kwarg is not part of the signature,
62
+ # we need to remove it from the kwargs before passing them
63
+ # to QueueOnce._get_call_args.
64
+ # Copy the kwargs so that we don't modify the original kwargs.
65
+ _kwargs = deepcopy(kwargs)
66
+ tenant = _kwargs.pop(TENANT_KEY)
67
+ tenant_kwarg = {TENANT_KEY: tenant}
68
+ task_call_args = super()._get_call_args(args, _kwargs)
69
+
70
+ # Add the tenant kwarg back to the return value
71
+ # since this value is being used to create the key for the lock.
72
+ # We want to lock the task for the tenant that is running it.
73
+ return task_call_args | tenant_kwarg
@@ -1,31 +0,0 @@
1
- from celery import Task
2
-
3
- from ..settings import TENANT_KEY
4
- from ..tenant_context import TenantContext
5
-
6
-
7
- class TenantAwareTask(Task):
8
- #: Enable argument checking.
9
- #: You can set this to false if you don't want the signature to be
10
- #: checked when calling the task.
11
- #: Set to false because we are attaching a tenant to the task
12
- #: and we don't want to check the signature.
13
- #: Defaults to :attr:`app.strict_typing <@Celery.strict_typing>`.
14
- typing = False
15
-
16
- def __call__(self, *args, **kwargs):
17
- """Override the __call__ method to set the tenant name in the thread namespace."""
18
-
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
- if self.name != "celery.backend_cleanup":
23
- tenant = kwargs.pop(TENANT_KEY)
24
- TenantContext.set(tenant)
25
-
26
- return self.run(*args, **kwargs)
27
-
28
- def after_return(self, status, retval, task_id, args, kwargs, einfo):
29
- """Clear the tenant from the thread namespace after the task has returned."""
30
- TenantContext.clear()
31
- super().after_return(status, retval, task_id, args, kwargs, einfo)