Skip to content

Decorators

application_kit.django.decorators

permissions_classes module-attribute

permissions_classes = cast(
    PermissionsClassesProtocol, _PermissionClasses
)

Original authentication decorator, use authenticate_key or authenticate_user.

authenticate_key module-attribute

authenticate_key = cast(
    AuthenticateKeyProtocol,
    partial(
        _PermissionClasses,
        classes=[],
        token_lambda=partial(
            get_authorization_token,
            [PUBLIC_KEY, PRIVATE_KEY],
        ),
    ),
)

The decorator to authenticate a request with a key.

authenticate_user module-attribute

authenticate_user = cast(
    AuthenticateUserProtocol,
    partial(
        _PermissionClasses,
        token_lambda=partial(
            get_authorization_token, [USER_TOKEN]
        ),
    ),
)

The decorator to authenticate users.

ProjectLambda

Bases: Protocol

__call__

__call__(get, meta)

Project lambda protocol definition.

Source code in application_kit/django/decorators.py
36
37
def __call__(self, get: dict[str, str], meta: dict[str, str]) -> tuple[str | None, TokenKind]:
    """Project lambda protocol definition."""

CounterNameLambda

Bases: Protocol

__call__

__call__(product, key, key_kind, **kwargs)

Counter name lambda protocol definition.

Source code in application_kit/django/decorators.py
41
42
def __call__(self, product: str, key: str, key_kind: TokenKind, **kwargs: Any) -> str:
    """Counter name lambda protocol definition."""

OrganizationLambda

Bases: Protocol

__call__

__call__(token, query_args)

Organization lambda protocol definition.

Source code in application_kit/django/decorators.py
46
47
def __call__(self, token: Token, query_args: dict[str, str]) -> CounterOrganizationDict | None:
    """Organization lambda protocol definition."""

PermissionsClassesProtocol

Bases: Protocol

__call__

__call__(classes, token_lambda=None, product=None)

Historic way of authenticating a view.

Supersed by authenticate_key, authenticate_user

PARAMETER DESCRIPTION
classes

A list of CheckablePermissions to execute on the request.

TYPE: list[type[CheckablePermission]]

token_lambda

The function to extract an authentication credential from request parameters and headers.

TYPE: TokenLambda | None DEFAULT: None

product

The products required by the endpoint.

TYPE: Products | None DEFAULT: None

Source code in application_kit/django/decorators.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def __call__(
    self,
    classes: Annotated[
        list[type[CheckablePermission]], Doc("A list of CheckablePermissions to execute on the request.")
    ],
    token_lambda: Annotated[
        TokenLambda | None,
        Doc("The function to extract an authentication credential from request parameters and headers."),
    ] = None,
    product: Annotated[Products | None, Doc("The products required by the endpoint.")] = None,
) -> Callable[[T], T]:
    """
    Historic way of authenticating a view.

    Supersed by [authenticate_key][application_kit.django.decorators.authenticate_key],
     [authenticate_user][application_kit.django.decorators.authenticate_user]
    """

AuthenticateKeyProtocol

Bases: Protocol

__call__

__call__(
    product=None,
    write_permission_needed=False,
    endpoint_mode=UNSET,
)

Authenticates a request with a public or a private key.

PARAMETER DESCRIPTION
product

The product required by the authenticate_key call.

Warning

If None is used, the default product set in the settings DEFAULT_PRODUCT will be used.

TYPE: Products | None DEFAULT: None

write_permission_needed

Deprecated

Use endpoint_mode instead.

TYPE: bool | None DEFAULT: False

endpoint_mode

Controls how the permissions should be considered for a request.

TYPE: EndpointMode DEFAULT: UNSET

Source code in application_kit/django/decorators.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
def __call__(
    self,
    product: Annotated[
        Products | None,
        Doc(
            """
            The product required by the authenticate_key call.

            !!! warning

                If None is used, the default product set in the settings `DEFAULT_PRODUCT` will be used.
            """
        ),
    ] = None,
    write_permission_needed: Annotated[
        bool | None,
        Doc(
            """
            !!! danger "Deprecated"

                Use endpoint_mode instead.
            """
        ),
    ] = False,
    endpoint_mode: Annotated[
        EndpointMode,
        Doc(
            """
            Controls how the permissions should be considered for a request.
            """
        ),
    ] = EndpointMode.UNSET,
) -> Callable[[T], T]:
    """Authenticates a request with a public or a private key."""

AuthenticateUserProtocol

Bases: Protocol

__call__

__call__(classes, product=None)

PermissionsClassesProtocol

PARAMETER DESCRIPTION
product

The product required by the authenticate_user call.

Note

If None is used, the default product set in the settings DEFAULT_PRODUCT will be used.

TYPE: Products | None DEFAULT: None

Source code in application_kit/django/decorators.py
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
def __call__(
    self,
    classes: list[type[CheckablePermission]],
    product: Annotated[
        Products | None,
        Doc(
            """
            The product required by the authenticate_user call.

            !!! note

                If None is used, the default product set in the settings `DEFAULT_PRODUCT` will be used.
            """
        ),
    ] = None,
) -> Callable[[T], T]:
    """PermissionsClassesProtocol"""

count_request

count_request(request_name, product, name_lambda=None)

Counts the request, must be placed after authenticate_key or authenticate_user decorator.

Source code in application_kit/django/decorators.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def count_request(
    request_name: str,
    product: str,
    name_lambda: CounterNameLambda | None = None,
) -> Callable[[T], T]:
    """Counts the request, must be placed after [authenticate_key][application_kit.django.decorators.authenticate_key] or
    [authenticate_user][application_kit.django.decorators.authenticate_user] decorator."""

    def _count_request(kwargs: Mapping[str, Any], request: HttpRequest) -> None:
        project_token = get_project_token(request)
        if project_token is not None:
            instance = project_token.instance
            kind = instance.kind
            request_kind = name_lambda(product, "", kind, **kwargs) if name_lambda else request_name
            add_job_to_queue_for_token(project_token, product, request_kind)
        else:
            warnings.warn(
                "Request was not authenticated, count_request decorator should be placed "
                "after a permission_classes decorator.",
                RuntimeWarning,
                stacklevel=2,
            )

    def wrapped(
        func: T,
    ) -> T:
        if is_coroutine_function(func):

            @wraps(func)
            async def wrapper(
                request: HttpRequest,
                *args: Any,
                **kwargs: Any,
            ) -> HttpResponseBase:
                _count_request(kwargs, request)

                return await func(request, *args, **kwargs)

            return wrapper  # type: ignore

        elif not_is_coroutine_function(func):

            @wraps(func)
            def wrapper(
                request: HttpRequest,
                *args: Any,
                **kwargs: Any,
            ) -> HttpResponseBase:
                _count_request(kwargs, request)

                return func(request, *args, **kwargs)

            return wrapper  # type: ignore
        else:
            raise RuntimeError(";_;")

    return wrapped