Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raises error on nested override site #2073

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mezzanine/utils/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def override_current_site_id(site_id):
Context manager that overrides the current site id for code executed
within it. Used to access SiteRelated objects outside the current site.
"""
if hasattr(override_current_site_id.thread_local,'site_id'):
raise RecursionError(f'''override_current_site_id can't be nested''')
override_current_site_id.thread_local.site_id = site_id
try:
yield
Expand Down
10 changes: 10 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@ def test_override_site_id(self):
self.assertEqual(current_site_id(), 2)
self.assertEqual(current_site_id(), 1)

def test_nested_override_site_id(self):
self.assertEqual(current_site_id(), 1)
with override_current_site_id(2):
self.assertEqual(current_site_id(), 2)
with self.assertRaises(RecursionError):
with override_current_site_id(3):
self.assertEqual(current_site_id(), 3)
self.assertEqual(current_site_id(), 2)
self.assertEqual(current_site_id(), 1)


class CSRFTestViews:
def nevercache_view(request):
Expand Down
Loading