summaryrefslogtreecommitdiffstats
path: root/activities/middleware.py
diff options
context:
space:
mode:
Diffstat (limited to 'activities/middleware.py')
-rw-r--r--activities/middleware.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/activities/middleware.py b/activities/middleware.py
new file mode 100644
index 0000000..1ed2219
--- /dev/null
+++ b/activities/middleware.py
@@ -0,0 +1,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