Skip to content

Commit

Permalink
Fix time zone activation leaking into subsequent requests in require_…
Browse files Browse the repository at this point in the history
…admin_access()

The timezone of an admin user was only activated, but never deactivated,
so it leaks into following requests of the current thread.

For example, this affected tests executed AFTER
wagtail.admin.tests.test_account_management (see #9628).

This commit changes the timezone activation to use the override()
context manager from django.utils.timezone, which calls deactivate()
when the context manager is closed.

This is similar to how we use the override() from
django.utils.translation for locale activation in the same decorator.
  • Loading branch information
th3hamm0r authored and laymonage committed Mar 1, 2023
1 parent b7c7f68 commit ec6397a
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions wagtail/admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from functools import wraps

import l18n
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.timezone import activate as activate_tz
from django.utils.timezone import override as override_tz
from django.utils.translation import gettext as _
from django.utils.translation import override

Expand Down Expand Up @@ -175,8 +176,9 @@ def decorated_view(request, *args, **kwargs):
)
l18n.set_language(preferred_language)
time_zone = user.wagtail_userprofile.get_current_time_zone()
activate_tz(time_zone)
with LogContext(user=user):
else:
time_zone = settings.TIME_ZONE
with override_tz(time_zone), LogContext(user=user):
if preferred_language:
with override(preferred_language):
response = view_func(request, *args, **kwargs)
Expand Down

0 comments on commit ec6397a

Please sign in to comment.