DevinG
DevinG

Reputation: 341

What is the best way to make sure the "django-timezone" session variable is set based on a user's profile preference each time they log in

Background

I read this section in Django's documentation titled "Selecting the current time zone,", and it's clear to me how a user can select their preferred timezone for their session (and then the middleware makes sure it is used throughout their session):

Question

If I store a user's timezone preference in a User model or User profile model, what is the best way to make sure the "django-timezone" session variable is always set for all their sessions (each time they log in)?

Thoughts/guesses

Upvotes: 1

Views: 56

Answers (1)

DevinG
DevinG

Reputation: 341

Extending LoginView

As suggested by Kevin Christopher Henry in the comments, I extended the built-in login view (decided to put the logic in the form_valid() method) after implementing what was suggested in Django's documentation titled "Selecting the current time zone." I'll show the relevant parts of my custom user model as well so it's clear how I'm storing users' time zone preferences.

models.py

from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
from django.contrib.auth.validators import ASCIIUsernameValidator
# third-party package
from timezone_field import TimeZoneField

TZ_CHOICES = [
    (ZoneInfo('Pacific/Honolulu'), 'Pacific/Honolulu'),
    (ZoneInfo('America/Anchorage'), 'America/Anchorage'),
    (ZoneInfo('America/Los_Angeles'), 'America/Los_Angeles'),
    (ZoneInfo('US/Arizona'), 'US/Arizona'),
    (ZoneInfo('America/Denver'), 'America/Denver'),
    (ZoneInfo('America/Chicago'), 'America/Chicago'),
    (ZoneInfo('America/New_York'), 'America/New_York'),
]

class CustomUser(AbstractBaseUser, PermissionsMixin):
    username_validator = ASCIIUsernameValidator()

    """A bunch of fields (username, email, etc.)"""

    tz_preference = TimeZoneField(
        use_pytz=False, default="America/Denver", choices=TZ_CHOICES, choices_display="STANDARD", verbose_name=_('Timezone Preference')
    )

views.py

from django.contrib.auth.views import LoginView
from django.contrib.auth import login as auth_login
import zoneinfo
from django.utils import timezone

class CustomLoginView(LoginView):
    def form_valid(self, form):
        """Security check complete. Log the user in."""
        auth_login(self.request, form.get_user())
        # now set session timezone according to user's preference
        tzname = self.request.user.tz_preference.key
        self.request.session["django_timezone"] = tzname
        # and activate it (after this view, middleware will activate it)
        timezone.activate(zoneinfo.ZoneInfo(tzname))
        return HttpResponseRedirect(self.get_success_url())

Upvotes: 1

Related Questions