Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

Feat/multi tenancy v2 context and organization (#430)

alic3f2d9442411
diff --git a/backend/utils/constants.py b/backend/utils/constants.py
index 0ca37d3c4..d431dbbb1 100644
--- a/backend/utils/constants.py
+++ b/backend/utils/constants.py
@@ -12,6 +12,13 @@ os.environ.setdefault(
class Account:
CREATED_BY = "created_by"
MODIFIED_BY = "modified_by"
+ ORGANIZATION_ID = "organization_id"
+
+
+class FeatureFlag:
+ """Temporary feature flags."""
+
+ MULTI_TENANCY_V2 = "multi_tenancy_v2"
class Common:
@@ -57,4 +64,6 @@ class ExecutionLogConstants:
LOG_QUEUE_NAME: str = "log_history_queue"
CELERY_QUEUE_NAME = "celery_periodic_logs"
PERIODIC_TASK_NAME = "workflow_log_history"
+ PERIODIC_TASK_NAME_V2 = "workflow_log_history_v2"
TASK = "workflow_manager.workflow.execution_log_utils.consume_log_history"
+ TASK_V2 = "consume_log_history"
diff --git a/backend/utils/models/organization_mixin.py b/backend/utils/models/organization_mixin.py
new file mode 100644
index 000000000..18d496c9d
--- /dev/null
+++ b/backend/utils/models/organization_mixin.py
@@ -0,0 +1,34 @@
+# TODO:V2 class
+from account_v2.models import Organization
+from django.db import models
+from utils.constants import FeatureFlag
+from utils.user_context import UserContext
+
+from unstract.flags.feature_flag import check_feature_flag_status
+
+
+class DefaultOrganizationMixin(models.Model):
+ organization = models.ForeignKey(
+ Organization,
+ on_delete=models.CASCADE,
+ db_comment="Foreign key reference to the Organization model.",
+ null=True,
+ blank=True,
+ default=None,
+ )
+
+ class Meta:
+ abstract = True
+
+ def save(self, *args, **kwargs):
+ if self.organization is None:
+ self.organization = UserContext.get_organization()
+ super().save(*args, **kwargs)
+
+
+class DefaultOrganizationManagerMixin(models.Manager):
+ def get_queryset(self):
+ if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ organization = UserContext.get_organization()
+ return super().get_queryset().filter(organization=organization)
+ return super().get_queryset()
diff --git a/backend/utils/user_context.py b/backend/utils/user_context.py
new file mode 100644
index 000000000..87addb52f
--- /dev/null
+++ b/backend/utils/user_context.py
@@ -0,0 +1,42 @@
+from typing import Optional
+
+from django.db import connection
+from utils.constants import Account, FeatureFlag
+from utils.local_context import StateStore
+
+from unstract.flags.feature_flag import check_feature_flag_status
+
+if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ # TODO: uncomment Once v2 implemented
+ # from account_v2.models import Organization
+ pass
+else:
+ from account.models import Organization
+
+
+class UserContext:
+ @staticmethod
+ def get_organization_identifier() -> str:
+ if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ organization_id = StateStore.get(Account.ORGANIZATION_ID)
+ else:
+ organization_id = connection.tenant.schema_name
+ return organization_id
+
+ @staticmethod
+ def set_organization_identifier(organization_identifier: str) -> None:
+ StateStore.set(Account.ORGANIZATION_ID, organization_identifier)
+
+ @staticmethod
+ def get_organization() -> Optional[Organization]:
+ if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ organization_id = StateStore.get(Account.ORGANIZATION_ID)
+ try:
+ organization: Organization = Organization.objects.get(
+ organization_id=organization_id
+ )
+ except Organization.DoesNotExist:
+ return None
+ else:
+ organization: Organization = connection.tenant
+ return organization
diff --git a/backend/utils/user_session.py b/backend/utils/user_session.py
index 7df8c2995..d8758b25e 100644
--- a/backend/utils/user_session.py
+++ b/backend/utils/user_session.py
@@ -1,12 +1,20 @@
from typing import Optional
from django.http import HttpRequest
+from utils.constants import FeatureFlag
+
+from unstract.flags.feature_flag import check_feature_flag_status
class UserSessionUtils:
@staticmethod
def get_organization_id(request: HttpRequest) -> Optional[str]:
- return request.session.get("organization")
+ session_org_id = request.session.get("organization")
+ if check_feature_flag_status(FeatureFlag.MULTI_TENANCY_V2):
+ requested_org_id = request.organization_id
+ if requested_org_id and (session_org_id != requested_org_id):
+ return None
+ return session_org_id
@staticmethod
def set_organization_id(request: HttpRequest, organization_id: str) -> None: