From 3f747a442730774ebedd88e9255db1a84539a4b7 Mon Sep 17 00:00:00 2001 From: Pelle Koster Date: Wed, 11 Dec 2024 14:00:01 +0100 Subject: [PATCH] Prevent another database query during htmx requests --- src/argus/auth/context_processors.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/argus/auth/context_processors.py b/src/argus/auth/context_processors.py index 49e792650..226d5a12e 100644 --- a/src/argus/auth/context_processors.py +++ b/src/argus/auth/context_processors.py @@ -7,9 +7,14 @@ See django settings for ``TEMPLATES``. """ +import functools from argus.auth.models import Preferences +# When ``render`` is called multiple times during a request (such as by HtmxMessageMiddleware), +# that also results in this function being called more than once for a request. That's unnecessary. +# Luckily we can cache the result for a request. +@functools.lru_cache(maxsize=10) def preferences(request): preferences_choices = {} for namespace, cls in Preferences.NAMESPACES.items():