summaryrefslogtreecommitdiffstats
path: root/activities/middleware.py
blob: 1ed2219e2566367f66c7fca4bf15009891db1d14 (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
from time import time

from activities.models import Emoji


class EmojiDefaultsLoadingMiddleware:
    """
    Caches the default Emoji
    """

    refresh_interval: float = 30.0

    def __init__(self, get_response):
        self.get_response = get_response
        self.loaded_ts: float = 0.0

    def __call__(self, request):
        # Allow test fixtures to force and lock the Emojis
        if not getattr(Emoji, "__forced__", False):
            if (
                not getattr(Emoji, "locals", None)
                or (time() - self.loaded_ts) >= self.refresh_interval
            ):
                Emoji.locals = Emoji.load_locals()
                self.loaded_ts = time()
        response = self.get_response(request)
        return response