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.

12.0 KB
Raw Blame
"""DRF views for warehouse_suggestions."""

from collections.abc import Callable
from functools import cached_property
from typing import Any, Literal, cast
from uuid import UUID

from drf_spectacular.utils import OpenApiResponse, extend_schema
from rest_framework import viewsets
from rest_framework.exceptions import APIException, NotFound, PermissionDenied, ValidationError
from rest_framework.permissions import BasePermission, IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from posthog.api.mixins import TypedRequest, ValidatedRequest, validated_request
from posthog.api.pagination import FacadeLimitOffsetPagination, facade_page_response
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import action
from posthog.exceptions import Conflict
from posthog.helpers.impersonation import is_impersonated
from posthog.models import User
from posthog.permissions import APIScopePermission, TeamMemberAccessPermission, get_authenticator_scopes
from posthog.scopes import scopes_not_covered
from posthog.utils import UUID_REGEX

from ..facade import api
from ..facade.api import is_warehouse_suggestions_enabled
from ..facade.contracts import (
    SUBJECT_SCOPE_OBJECTS,
    WAREHOUSE_SCOPE_OBJECT,
    AcceptFailedError,
    AcceptOptionRefusedError,
    AcceptOptions,
    AssigneeCannotActError,
    CatalogEditAccessRequiredError,
    SubjectAlreadyCertifiedError,
    SubjectEditAccessRequiredError,
    SubjectKinds,
    Suggestion,
    SuggestionAlreadyDecidedError,
    SuggestionNotFoundError,
    SuggestionNotOpenError,
    SuggestionOutdatedError,
    SuggestionSubjectGoneError,
)
from ..facade.enums import (
    WarehouseSuggestionKind,
    WarehouseSuggestionStatus,
    WarehouseSuggestionSubjectKind,
    WarehouseSuggestionSurface,
)
from .serializers import (
    AcceptWarehouseSuggestionSerializer,
    AssignWarehouseSuggestionSerializer,
    DismissWarehouseSuggestionSerializer,
    WarehouseSuggestionDecisionQuerySerializer,
    WarehouseSuggestionListQuerySerializer,
    WarehouseSuggestionSerializer,
    WarehouseSuggestionStatusSerializer,
)

DEFAULT_PAGE_SIZE = 100
EDIT_ACCESS_REQUIRED = {
    WarehouseSuggestionSubjectKind.SAVED_QUERY: "You need edit access to this view to change its suggestion.",
    WarehouseSuggestionSubjectKind.TABLE: "You need edit access to this table to change its suggestion.",
}


SUBJECT_GONE = {
    WarehouseSuggestionSubjectKind.SAVED_QUERY: "This view no longer exists.",
    WarehouseSuggestionSubjectKind.TABLE: "This table no longer exists.",
}
ALREADY_CERTIFIED = {
    WarehouseSuggestionSubjectKind.SAVED_QUERY: "This view already has a certification.",
    WarehouseSuggestionSubjectKind.TABLE: "This table already has a certification.",
}
CATALOG_EDIT_ACCESS_REQUIRED = "You need edit access to the data catalog to accept this suggestion."
ASSIGNEE_CANNOT_ACT = (
    "This person can't accept this suggestion. Pick someone with edit access to the view or table, "
    "and to the data catalog for certify and deprecate suggestions."
)
ONLY_OPEN_ASSIGNABLE = "Only open suggestions can be assigned."
ACCEPT_SCOPES = ["data_catalog_approval:write", "warehouse_view:write"]
ScopeLevel = Literal["read", "write"]


def _surface(request: ValidatedRequest) -> WarehouseSuggestionSurface | None:
    surface = request.validated_query_data.get("surface")
    return WarehouseSuggestionSurface(surface) if surface else None


class SuggestionPagination(FacadeLimitOffsetPagination):
    default_limit = DEFAULT_PAGE_SIZE
    max_limit = DEFAULT_PAGE_SIZE


class ProjectAccessPermission(BasePermission):
    def has_permission(self, request: Request, view: APIView) -> bool:
        viewset = cast("WarehouseSuggestionViewSet", view)
        return viewset.user_access_control.check_access_level_for_object(viewset.team, required_level="member")


class WarehouseSuggestionViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
    scope_object = WAREHOUSE_SCOPE_OBJECT
    scope_object_read_actions = ["list", "retrieve", "status"]
    scope_object_write_actions = ["accept", "assign", "dismiss", "resume"]
    pagination_class = SuggestionPagination
    lookup_value_regex = UUID_REGEX

    def dangerously_get_permissions(self) -> list[BasePermission]:
        return [IsAuthenticated(), APIScopePermission(), TeamMemberAccessPermission(), ProjectAccessPermission()]

    def initial(self, request: Request, *args: Any, **kwargs: Any) -> None:
        super().initial(request, *args, **kwargs)
        if not is_warehouse_suggestions_enabled(self.team):
            raise PermissionDenied("Warehouse suggestions are not enabled for this project.")

    def dangerously_get_required_scopes(self, request: Request, view: APIView) -> list[str] | None:
        level = self._scope_level
        fallback = f"{SUBJECT_SCOPE_OBJECTS[WarehouseSuggestionSubjectKind.SAVED_QUERY]}:{level}"
        subject_scope = next(iter(self._token_scopes_by_kind(level).values()), fallback)
        return [subject_scope, *ACCEPT_SCOPES] if self.action == "accept" else [subject_scope]

    @property
    def _scope_level(self) -> ScopeLevel:
        return "write" if self.action in self.scope_object_write_actions else "read"

    def _token_scopes_by_kind(self, level: ScopeLevel) -> dict[WarehouseSuggestionSubjectKind, str]:
        held = get_authenticator_scopes(self.request.successful_authenticator) or ()
        reaching: dict[WarehouseSuggestionSubjectKind, str] = {}
        for subject_kind, scope_object in SUBJECT_SCOPE_OBJECTS.items():
            candidates = (f"{self.scope_object}:{level}", f"{scope_object}:{level}")
            held_scope = next((scope for scope in candidates if not scopes_not_covered(held, [scope])), None)
            if held_scope is not None:
                reaching[subject_kind] = held_scope
        return reaching

    def _kinds_reached(self, level: ScopeLevel) -> frozenset[WarehouseSuggestionSubjectKind]:
        held = get_authenticator_scopes(self.request.successful_authenticator)
        if held is None or "*" in held:
            return frozenset(WarehouseSuggestionSubjectKind)
        return frozenset(self._token_scopes_by_kind(level))

    @cached_property
    def _subject_kinds(self) -> SubjectKinds:
        return SubjectKinds(readable=self._kinds_reached("read"), actionable=self._kinds_reached("write"))

    @validated_request(
        query_serializer=WarehouseSuggestionListQuerySerializer,
        responses={200: OpenApiResponse(response=WarehouseSuggestionSerializer(many=True))},
    )
    def list(self, request: ValidatedRequest, **kwargs: Any) -> Response:
        kind = request.validated_query_data.get("kind")
        status = request.validated_query_data.get("status")
        return facade_page_response(
            self,
            lambda page: api.list_suggestions(
                self.team_id,
                self.user_access_control,
                kind=WarehouseSuggestionKind(kind) if kind else None,
                status=WarehouseSuggestionStatus(status) if status else None,
                subject_id=request.validated_query_data.get("subject_id"),
                assignee_id=request.validated_query_data.get("assignee_id"),
                subject_kinds=self._subject_kinds,
                page=page,
            ),
            serializer_class=WarehouseSuggestionSerializer,
        )

    @extend_schema(responses={200: WarehouseSuggestionStatusSerializer})
    @action(detail=False, methods=["get"], pagination_class=None)
    def status(self, request: Request, **kwargs: Any) -> Response:
        return Response(WarehouseSuggestionStatusSerializer(api.suggestion_status(self.team_id)).data)

    @extend_schema(responses={200: WarehouseSuggestionSerializer})
    def retrieve(self, request: Request, pk: str, **kwargs: Any) -> Response:
        return self._respond(
            lambda: api.get_suggestion(
                self.team_id, self.user_access_control, UUID(pk), subject_kinds=self._subject_kinds
            )
        )

    @validated_request(
        request_serializer=DismissWarehouseSuggestionSerializer,
        query_serializer=WarehouseSuggestionDecisionQuerySerializer,
        responses={200: OpenApiResponse(response=WarehouseSuggestionSerializer)},
    )
    @action(detail=True, methods=["post"])
    def dismiss(self, request: ValidatedRequest, pk: str, **kwargs: Any) -> Response:
        return self._respond(
            lambda: api.dismiss_suggestion(
                self.team,
                self.user_access_control,
                UUID(pk),
                user=cast(User, request.user),
                reason=request.validated_data["reason"],
                note=request.validated_data.get("note") or None,
                subject_kinds=self._subject_kinds,
                surface=_surface(request),
            )
        )

    @validated_request(
        request_serializer=AcceptWarehouseSuggestionSerializer,
        query_serializer=WarehouseSuggestionDecisionQuerySerializer,
        responses={200: OpenApiResponse(response=WarehouseSuggestionSerializer)},
    )
    @action(detail=True, methods=["post"])
    def accept(self, request: TypedRequest[AcceptOptions], pk: str, **kwargs: Any) -> Response:
        return self._respond(
            lambda: api.accept_suggestion(
                self.team,
                self.user_access_control,
                UUID(pk),
                user=cast(User, request.user),
                options=request.validated_data,
                was_impersonated=is_impersonated(request),
                subject_kinds=self._subject_kinds,
                surface=_surface(request),
            )
        )

    @validated_request(
        request_serializer=AssignWarehouseSuggestionSerializer,
        responses={200: OpenApiResponse(response=WarehouseSuggestionSerializer)},
    )
    @action(detail=True, methods=["post"])
    def assign(self, request: ValidatedRequest, pk: str, **kwargs: Any) -> Response:
        return self._respond(
            lambda: api.assign_suggestion(
                self.team,
                self.user_access_control,
                UUID(pk),
                user=cast(User, request.user),
                assignee_id=request.validated_data["assignee_id"],
                subject_kinds=self._subject_kinds,
            )
        )

    @extend_schema(request=None, responses={200: WarehouseSuggestionSerializer})
    @action(detail=True, methods=["post"])
    def resume(self, request: Request, pk: str, **kwargs: Any) -> Response:
        return self._respond(
            lambda: api.resume_suggestion(
                self.team,
                self.user_access_control,
                UUID(pk),
                user=cast(User, request.user),
                subject_kinds=self._subject_kinds,
            )
        )

    def _respond(self, decide_or_read: Callable[[], Suggestion]) -> Response:
        try:
            return Response(WarehouseSuggestionSerializer(decide_or_read()).data)
        except SuggestionNotFoundError:
            raise NotFound()
        except SubjectEditAccessRequiredError as error:
            raise PermissionDenied(EDIT_ACCESS_REQUIRED[error.subject_kind])
        except CatalogEditAccessRequiredError:
            raise PermissionDenied(CATALOG_EDIT_ACCESS_REQUIRED)
        except SuggestionAlreadyDecidedError as error:
            raise Conflict(str(error))
        except SubjectAlreadyCertifiedError as error:
            raise Conflict(ALREADY_CERTIFIED[error.subject_kind])
        except SuggestionSubjectGoneError as error:
            raise ValidationError({"subject_id": SUBJECT_GONE[error.subject_kind]})
        except AcceptOptionRefusedError as error:
            raise ValidationError({error.field: str(error)})
        except SuggestionOutdatedError as error:
            raise Conflict(str(error))
        except AcceptFailedError as error:
            raise APIException(str(error))
        except AssigneeCannotActError:
            raise ValidationError({"assignee_id": ASSIGNEE_CANNOT_ACT})
        except SuggestionNotOpenError:
            raise Conflict(ONLY_OPEN_ASSIGNABLE)