Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

refactor(approvals): serve the approval handling mixin from presentation (#125442)

Andersdd9bf6f3cdca
diff --git a/ee/clickhouse/views/experiment_holdouts.py b/ee/clickhouse/views/experiment_holdouts.py
index 08db18901fd5..5192eb680eef 100644
--- a/ee/clickhouse/views/experiment_holdouts.py
+++ b/ee/clickhouse/views/experiment_holdouts.py
@@ -14,7 +14,7 @@ from posthog.api.shared import UserBasicSerializer
from products.access_control.backend.presentation.access_control import UserAccessControlSerializerMixin
from products.approvals.backend.decorators import approval_gate
-from products.approvals.backend.mixins import ApprovalHandlingMixin
+from products.approvals.backend.presentation.mixins import ApprovalHandlingMixin
from products.experiments.backend.models.experiment import ExperimentHoldout
from products.feature_flags.backend.facade.api import update_flag
from products.feature_flags.backend.facade.filters import set_holdout
diff --git a/products/approvals/backend/presentation/__init__.py b/products/approvals/backend/presentation/__init__.py
new file mode 100644
index 000000000000..e69de29bb2d1
diff --git a/products/approvals/backend/mixins.py b/products/approvals/backend/presentation/mixins.py
similarity index 91%
rename from products/approvals/backend/mixins.py
rename to products/approvals/backend/presentation/mixins.py
index 99d8af706366..2c85c47c5954 100644
--- a/products/approvals/backend/mixins.py
+++ b/products/approvals/backend/presentation/mixins.py
@@ -1,3 +1,9 @@
+"""Approval-gate response handling that other products' viewsets mix in.
+
+It lives in presentation, not in a facade, because a facade must not import DRF. Another
+product's presentation may import this module, but not approvals internals.
+"""
+
from rest_framework import status
from rest_framework.response import Response
diff --git a/products/experiments/backend/presentation/views.py b/products/experiments/backend/presentation/views.py
index 3b67670d1d3d..7314ca7c1534 100644
--- a/products/experiments/backend/presentation/views.py
+++ b/products/experiments/backend/presentation/views.py
@@ -78,7 +78,7 @@ from products.access_control.backend.facade.user_access_control import (
access_level_satisfied_for_resource,
)
from products.access_control.backend.presentation.access_control import AccessControlViewSetMixin
-from products.approvals.backend.mixins import ApprovalHandlingMixin
+from products.approvals.backend.presentation.mixins import ApprovalHandlingMixin
from products.experiments.backend.experiment_service import ExperimentService, ExperimentVersionConflict
from products.experiments.backend.facade.api import (
get_experiment_health,
diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.py
index a7faa7b53104..f8e01c84b4b3 100644
--- a/products/feature_flags/backend/api/feature_flag.py
+++ b/products/feature_flags/backend/api/feature_flag.py
@@ -95,10 +95,10 @@ from products.access_control.backend.presentation.access_control import (
UserAccessControlSerializerMixin,
)
from products.approvals.backend.decorators import approval_gate
-from products.approvals.backend.mixins import ApprovalHandlingMixin
from products.approvals.backend.models import ApprovalPolicy
from products.approvals.backend.ownership import scope_by_owner_enabled
from products.approvals.backend.policies import lock_approval_policies
+from products.approvals.backend.presentation.mixins import ApprovalHandlingMixin
from products.cohorts.backend.models.cohort import Cohort, CohortType
from products.cohorts.backend.models.util import get_all_cohort_dependencies
from products.dashboards.backend.api.dashboard import Dashboard
diff --git a/products/feature_flags/backend/api/scheduled_change.py b/products/feature_flags/backend/api/scheduled_change.py
index bcc6f232c0bc..fd003030c286 100644
--- a/products/feature_flags/backend/api/scheduled_change.py
+++ b/products/feature_flags/backend/api/scheduled_change.py
@@ -11,8 +11,8 @@ from rest_framework.exceptions import PermissionDenied
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.shared import UserBasicSerializer
-from products.approvals.backend.mixins import ApprovalHandlingMixin
from products.approvals.backend.models import ChangeRequest, ChangeRequestState
+from products.approvals.backend.presentation.mixins import ApprovalHandlingMixin
from products.approvals.backend.scheduled_changes import gate_scheduled_change
from products.approvals.backend.serializers import ChangeRequestSummarySerializer
from products.feature_flags.backend.api.feature_flag import CanEditFeatureFlag
diff --git a/products/product_tours/backend/api/product_tour.py b/products/product_tours/backend/api/product_tour.py
index fe404a2183a6..69824bcd4745 100644
--- a/products/product_tours/backend/api/product_tour.py
+++ b/products/product_tours/backend/api/product_tour.py
@@ -40,7 +40,7 @@ from posthog.models.user import User
from posthog.utils_cors import cors_response
from products.access_control.backend.presentation.access_control import AccessControlViewSetMixin
-from products.approvals.backend.mixins import ApprovalHandlingMixin
+from products.approvals.backend.presentation.mixins import ApprovalHandlingMixin
from products.feature_flags.backend.api.feature_flag import MinimalFeatureFlagSerializer
from products.feature_flags.backend.facade.api import create_flag, set_flag_active, update_flag
from products.feature_flags.backend.facade.config import ConfigFormatError
diff --git a/products/surveys/backend/api/survey.py b/products/surveys/backend/api/survey.py
index 6bdea9e2dcc2..e7053ddd0683 100644
--- a/products/surveys/backend/api/survey.py
+++ b/products/surveys/backend/api/survey.py
@@ -72,7 +72,7 @@ from products.access_control.backend.presentation.access_control import (
)
from products.actions.backend.api.action import ActionSerializer, ActionStepJSONSerializer
from products.actions.backend.models.action import Action
-from products.approvals.backend.mixins import ApprovalHandlingMixin
+from products.approvals.backend.presentation.mixins import ApprovalHandlingMixin
from products.approvals.backend.transactions import gated_atomic
from products.feature_flags.backend.api.feature_flag import (
BEHAVIOURAL_COHORT_FOUND_ERROR_CODE,
diff --git a/pyproject.toml b/pyproject.toml
index deed5e7844c7..548e7125c7e6 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -618,6 +618,12 @@ ignore_imports = [
"products.cdp.backend.presentation.serializers -> products.cdp.backend.validation",
"products.cdp.backend.presentation.hog_functions -> products.cdp.backend.flag_gated_templates",
"products.cdp.backend.presentation.icons -> products.cdp.backend.services.icons",
+ # TODO: approvals presentation wave. The approval handling mixin that other products' viewsets
+ # use still reads the gate exceptions and the change request serializer from approvals
+ # internals. Delete both entries once the facade exposes the exceptions and a change request
+ # contract for presentation to serialize, because ApprovalRequired carries the ChangeRequest model.
+ "products.approvals.backend.presentation.mixins -> products.approvals.backend.exceptions",
+ "products.approvals.backend.presentation.mixins -> products.approvals.backend.serializers",
# TODO: existing violations — fix by routing through facade
"products.alerts.backend.presentation.views.alert -> products.alerts.backend.evaluation.contract",
"products.alerts.backend.presentation.views.alert -> products.alerts.backend.evaluation.detector",
@@ -722,7 +728,6 @@ ignore_imports = [
"products.experiments.backend.presentation.serializers -> products.experiments.backend.setup_context",
"products.experiments.backend.presentation.serializers -> products.feature_flags.backend.api.feature_flag",
"products.experiments.backend.presentation.serializers -> products.feature_flags.backend.models.feature_flag",
- "products.experiments.backend.presentation.views -> products.approvals.backend.mixins",
"products.experiments.backend.presentation.views -> products.experiments.backend.experiment_service",
"products.experiments.backend.presentation.views -> products.experiments.backend.llm_metric_templates",
"products.experiments.backend.presentation.views -> products.experiments.backend.models.experiment",