Scoping#

OEP-66 record-visibility building blocks for DRF list endpoints: a ScopingPolicy structural interface (a typing.Protocol), a ScopedQuerysetMixin that applies a configured policy in get_queryset(), and a FullScopePolicy for endpoints whose callers may see every row.

class FullScopePolicy#

A ScopingPolicy that leaves the queryset unchanged.

Use it on list endpoints whose permission_classes already restrict the endpoint to subjects that may see every row (for example platform admins), so the view is wired for record-visibility scoping without narrowing.

scope(queryset: QuerySet, subject: Any) → QuerySet#

Return queryset as-is: every row is within subject’s scope.

class ScopedQuerysetMixin#

Applies scoping_policy to a DRF view’s base queryset (OEP-66).

Mix into a GenericAPIView / ListAPIView (or GenericViewSet) and set scoping_policy to any object implementing the ScopingPolicy protocol. The mixin runs the policy on top of the view’s get_queryset() result so the list response contains only the rows within the requesting subject’s accessible scopes.

get_queryset() → QuerySet#

Return the base queryset scoped to the rows the requesting subject may see.

scoping_policy: Optional[ScopingPolicy] = None#

An object implementing the ScopingPolicy protocol.

class ScopingPolicy(*args, **kwargs)#

Structural interface for an OEP-66 record-visibility policy.

Any object exposing a compatible scope method satisfies this protocol – implementers need not import or inherit from it. It documents the expected interface for static type checkers; ScopedQuerysetMixin verifies a configured policy with a lightweight duck-typed check at runtime.

A policy must not re-implement access rules; it delegates to the platform’s authorization engine to resolve the subject’s accessible scopes and translates that answer into a queryset filter. Keeping it separate from the view lets the same visibility rule be reused and unit-tested on its own.

scope(queryset: QuerySet, subject: Any) → QuerySet#

Return queryset filtered to the rows visible to subject.