Skip to content

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

UN-3375 [FIX] Support multiple nullable FK paths in OrganizationFilterBackend (#1889)

ali2c8b89925c80
diff --git a/backend/notification_v2/views.py b/backend/notification_v2/views.py
index 53025bae7..1410256ab 100644
--- a/backend/notification_v2/views.py
+++ b/backend/notification_v2/views.py
@@ -13,6 +13,10 @@ from .serializers import NotificationSerializer
class NotificationViewSet(viewsets.ModelViewSet):
serializer_class = NotificationSerializer
+ org_filter_paths = [
+ "pipeline__workflow__organization",
+ "api__workflow__organization",
+ ]
def get_queryset(self):
queryset = Notification.objects.all()
diff --git a/backend/utils/filters/organization_filter.py b/backend/utils/filters/organization_filter.py
index ebe274e25..a0eba72da 100644
--- a/backend/utils/filters/organization_filter.py
+++ b/backend/utils/filters/organization_filter.py
@@ -1,5 +1,6 @@
import logging
+from django.db.models import Q
from rest_framework.filters import BaseFilterBackend
from utils.models.org_path_discovery import get_org_path
@@ -7,6 +8,11 @@ from utils.user_context import UserContext
logger = logging.getLogger(__name__)
+# ViewSet attributes read by OrganizationFilterBackend.
+# Use these constants when setting attributes on ViewSets to avoid typos.
+SKIP_ORG_FILTER = "skip_org_filter"
+ORG_FILTER_PATHS = "org_filter_paths"
+
class OrganizationFilterBackend(BaseFilterBackend):
"""Global filter backend that enforces organization scoping.
@@ -24,10 +30,20 @@ class OrganizationFilterBackend(BaseFilterBackend):
rather than replacing it.
Opt-out: set skip_org_filter = True on the viewset.
+
+ For models with multiple nullable FK paths to Organization, set
+ org_filter_paths on the viewset to use OR filtering across all
+ paths (skips BFS):
+
+ class NotificationViewSet(viewsets.ModelViewSet):
+ org_filter_paths = [
+ "pipeline__workflow__organization",
+ "api__workflow__organization",
+ ]
"""
def filter_queryset(self, request, queryset, view):
- if getattr(view, "skip_org_filter", False):
+ if getattr(view, SKIP_ORG_FILTER, False):
return queryset
model = queryset.model
@@ -43,6 +59,15 @@ class OrganizationFilterBackend(BaseFilterBackend):
)
return queryset.none()
+ # Use explicit paths if set on the viewset (for models with
+ # multiple nullable FK paths to Organization).
+ explicit_paths = getattr(view, ORG_FILTER_PATHS, None)
+ if explicit_paths:
+ q = Q()
+ for path in explicit_paths:
+ q |= Q(**{path: org})
+ return queryset.filter(q)
+
path = get_org_path(model)
if path:
return queryset.filter(**{path: org})