Skip to content

Commit

Permalink
Try harder to restore original settings. Fixes #991
Browse files Browse the repository at this point in the history
root cause is that settings are cached and after override
MENU_ITEMS is modified. In case of intermediary failures it was
not restored and other pages were showing bogus menu items.

This seems to resolve the problem although it's now very hard to
reproduce this problem reliably, hence no actual test.
  • Loading branch information
atodorov committed May 3, 2020
1 parent 1824081 commit 178a575
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions tcms/core/response.py
@@ -1,7 +1,8 @@
# Copyright (c) 2019 Alexander Todorov <atodorov@MrSenko.com>
# Copyright (c) 2019-2020 Alexander Todorov <atodorov@MrSenko.com>

# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html

from django.conf import settings
from django.template.response import TemplateResponse


Expand Down Expand Up @@ -33,5 +34,22 @@ def render_to_response(self, context, **response_kwargs):
modify_settings = None

def render(self):
with self.modify_settings: # pylint: disable=not-context-manager
return super().render()
try:
with self.modify_settings: # pylint: disable=not-context-manager
return super().render()
finally:
# if this attribute is still set that means disabling settings override
# failed which leads to navbar showing bogus menu items, see
# https://github.com/kiwitcms/Kiwi/issues/991
# => try to restore the original unmodified settings, see
# django.test.utils.override_settings().disable()
#
# NOTE: the only way to reproduce this reliably ATM is by raising exception
# in .disable() before restoring the original settings although I can see
# the problem on tcms.kiwitcms.org after the last restart 1 week ago!
# Maybe the switch to CBV and this response class made #991 harder to
# reproduce. It was easier to reproduce in the past by triggering some kind
# of exception in the FBV which used modify_settings() !!!
if hasattr(self.modify_settings, 'wrapped'):
settings._wrapped = self.modify_settings.wrapped # pylint: disable=protected-access
del self.modify_settings.wrapped

0 comments on commit 178a575

Please sign in to comment.