Skip to content

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

6.3 KB
Raw Blame
import logging

from api_v2.deployment_helper import DeploymentHelper
from api_v2.exceptions import APINotFound
from configuration.enums import ConfigKey
from configuration.models import Configuration
from pipeline_v2.exceptions import PipelineNotFound
from pipeline_v2.models import Pipeline
from pipeline_v2.pipeline_processor import PipelineProcessor
from platform_api.permissions import IsOrganizationAdmin
from platform_audit.actions import Action, TargetType
from platform_audit.recorder import (
    BEFORE_UNAVAILABLE,
    best_effort,
    record_platform_event,
)
from rest_framework import status, viewsets
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 utils.user_context import UserContext

from notification_v2.constants import NotificationUrlConstant

from .models import Notification
from .serializers import NotificationSerializer, NotificationSettingsSerializer

logger = logging.getLogger(__name__)


class NotificationViewSet(viewsets.ModelViewSet):
    serializer_class = NotificationSerializer
    org_filter_paths = [
        "pipeline__workflow__organization",
        "api__workflow__organization",
    ]

    def get_queryset(self):
        queryset = Notification.objects.all()
        pipeline_uuid = self.kwargs.get(NotificationUrlConstant.PIPELINE_UID)
        api_uuid = self.kwargs.get(NotificationUrlConstant.API_UID)

        if pipeline_uuid:
            try:
                pipeline = PipelineProcessor.fetch_pipeline(
                    pipeline_id=pipeline_uuid, check_active=False
                )
                queryset = queryset.filter(pipeline=pipeline)
            except Pipeline.DoesNotExist:
                raise PipelineNotFound()

        elif api_uuid:
            api = DeploymentHelper.get_api_by_id(api_id=api_uuid)
            if not api:
                raise APINotFound()
            queryset = queryset.filter(api=api)

        return queryset


class CanChangePlatformDefaults(BasePermission):
    message = "Change platform defaults is restricted to organisation admins."

    def has_permission(self, request, view):
        from tenant_account_v2.capabilities import Capability, opened_to_members

        return IsOrganizationAdmin().has_permission(request, view) or opened_to_members(
            request.user,
            UserContext.get_organization(),
            Capability.CHANGE_PLATFORM_DEFAULTS,
        )


class NotificationSettingsView(APIView):
    """Org-scoped notification batching settings — currently just the club interval.

    GET returns the org's effective interval (override or env-derived default).
    PATCH writes/updates the override via the generic configuration KV table.

    Read-at-enqueue contract: updates take effect for notifications enqueued
    after the change. Existing PENDING buffer rows keep their original
    flush_after.
    """

    # Admins, or members when the org lets members change platform defaults
    # (UN-4399). Platform defaults are otherwise per-user; this is the only
    # organisation-level setting the capability governs.
    permission_classes = [IsAuthenticated, CanChangePlatformDefaults]

    def get(self, request: Request) -> Response:
        organization = UserContext.get_organization()
        value = Configuration.get_value_by_organization(
            ConfigKey.NOTIFICATION_CLUB_INTERVAL, organization
        )
        return Response({"club_interval_seconds": int(value)})

    def patch(self, request: Request) -> Response:
        serializer = NotificationSettingsSerializer(data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        organization = UserContext.get_organization()
        new_value = serializer.validated_data.get("club_interval_seconds")
        if new_value is None:
            return Response(
                {"detail": "club_interval_seconds is required."},
                status=status.HTTP_400_BAD_REQUEST,
            )
        # ConfigKey.cast_value enforces type + any future bounds; bubble its
        # ValueError up as a 400 instead of letting it 500.
        try:
            ConfigKey.NOTIFICATION_CLUB_INTERVAL.cast_value(new_value)
        except ValueError as exc:
            return Response({"detail": str(exc)}, status=status.HTTP_400_BAD_REQUEST)
        before = _club_interval(organization)
        Configuration.objects.update_or_create(
            organization=organization,
            key=ConfigKey.NOTIFICATION_CLUB_INTERVAL.name,
            defaults={"value": str(new_value), "enabled": True},
        )
        # ``before`` is None only when it could not be read: record the change
        # without a before side rather than claim the setting was unset.
        if before != int(new_value):
            record_platform_event(
                action=Action.PLATFORM_SETTING_CHANGED,
                target_type=TargetType.PLATFORM_SETTING,
                target_id="club_interval_seconds",
                target_name="club_interval_seconds",
                changes={
                    "before": None
                    if before is None
                    else {"club_interval_seconds": before},
                    "after": {"club_interval_seconds": int(new_value)},
                },
                summary=f"club_interval_seconds: {before} → {int(new_value)}"
                if before is not None
                else f"club_interval_seconds → {int(new_value)} {BEFORE_UNAVAILABLE}",
                request=request,
            )
        return Response({"club_interval_seconds": int(new_value)})


@best_effort
def _club_interval(organization) -> int:
    """The interval in effect, or ``None`` (via ``best_effort``) if unreadable.

    Reads the override itself: ``get_value_by_organization`` returns the
    default on any error, a failed query included, which would be recorded as
    the previous value.
    """
    key = ConfigKey.NOTIFICATION_CLUB_INTERVAL
    override = Configuration.objects.filter(
        organization=organization, key=key.name
    ).first()
    if override is not None and override.enabled and override.typed_value is not None:
        return int(override.typed_value)
    # No usable override: the default is what was in effect.
    return int(Configuration.get_value_by_organization(key, None))