From 99a0441505a93e6318dd5434c3f461c19f66bb4d Mon Sep 17 00:00:00 2001 From: Ed Rivas Date: Tue, 20 Jul 2021 16:26:50 -0600 Subject: [PATCH] fix: make `settings._editable_caches` more robust Fixes #1989 --- mezzanine/conf/__init__.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/mezzanine/conf/__init__.py b/mezzanine/conf/__init__.py index 2cc1c1c26a..066b3aebf9 100644 --- a/mezzanine/conf/__init__.py +++ b/mezzanine/conf/__init__.py @@ -119,15 +119,6 @@ class Placeholder: bytes: partial(bytes, encoding="utf8"), } - def __init__(self): - """ - The ``_editable_caches`` attribute maps Request objects to dicts of - editable settings loaded from the database. We cache settings per- - request to ensure that the database is hit at most once per request, - and that each request sees the same settings for its duration. - """ - self._editable_caches = WeakKeyDictionary() - @property def _current_request(self): return current_request() or self.NULL_REQUEST @@ -159,6 +150,12 @@ def _get_editable(self, request): ``_editable_caches``, a WeakKeyDictionary that will automatically discard each entry when no more references to the request exist. """ + if not hasattr(self, "_editable_caches"): + # The ``_editable_caches`` attribute maps Request objects to dicts of + # editable settings loaded from the database. We cache settings per-request + # to ensure that the database is hit at most once per request, and that each + # request sees the same settings for its duration. + self._editable_caches = WeakKeyDictionary() try: editable_settings = self._editable_caches[request] except KeyError: