Skip to content

Commit

Permalink
raises error on nested override site
Browse files Browse the repository at this point in the history
  • Loading branch information
rapto committed Jun 12, 2024
1 parent e719286 commit fe73b85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit fe73b85

Please sign in to comment.