summaryrefslogtreecommitdiffstats
path: root/core/decorators.py
blob: dc8d4d2f8d4e11a817baa5f2ab5a6576c5f71a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from collections.abc import Callable
from functools import partial, wraps
from typing import ParamSpecArgs, ParamSpecKwargs

from django.http import HttpRequest
from django.views.decorators.cache import cache_page as dj_cache_page

from core.models import Config

VaryByFunc = Callable[[HttpRequest, ParamSpecArgs, ParamSpecKwargs], str]


def vary_by_ap_json(request, *args, **kwargs) -> str:
    """
    Return a cache usable string token that is different based upon Accept
    header.
    """
    if request.ap_json:
        return "ap_json"
    return "not_ap"


def vary_by_identity(request, *args, **kwargs) -> str:
    """
    Return a cache usable string token that is different based upon the
    request.identity
    """
    if request.identity:
        return f"ident{request.identity.pk}"
    return "identNone"


def cache_page(
    timeout: int | str = "cache_timeout_page_default",
    *,
    key_prefix: str = "",
    public_only: bool = False,
    vary_by: VaryByFunc | list[VaryByFunc] | None = None,
):
    """
    Decorator for views that caches the page result.
    timeout can either be the number of seconds or the name of a SystemOptions
    value.
    If public_only is True, requests with an identity are not cached.
    """
    _timeout = timeout
    _prefix = key_prefix
    if callable(vary_by):
        vary_by = [vary_by]

    def decorator(function):
        @wraps(function)
        def inner(request, *args, **kwargs):
            if public_only:
                if request.user.is_authenticated:
                    return function(request, *args, **kwargs)

            prefix = [_prefix]

            if isinstance(vary_by, list):
                prefix.extend([vfunc(request, *args, **kwargs) for vfunc in vary_by])

            prefix = "".join(prefix)

            if isinstance(_timeout, str):
                timeout = getattr(Config.system, _timeout)
            else:
                timeout = _timeout

            return dj_cache_page(timeout=timeout, key_prefix=prefix)(function)(
                request, *args, **kwargs
            )

        return inner

    return decorator


cache_page_by_ap_json = partial(cache_page, vary_by=[vary_by_ap_json])