Source code for superdjango.contrib.i18n.timezones.views

# Imports

from django.conf import settings
from django.contrib import messages
from django.shortcuts import redirect, render
from django.utils.translation import ugettext_lazy as _
from superdjango.shortcuts import get_model
from .choices import COMMON_TIMEZONE_CHOICES
from .descriptors import TimeZone

# Exports

__all__ = (
    "set_my_timezone",
)

# Constants

DEFAULT_TIMEZONE = getattr(settings, "TIME_ZONE", "UTC")
TIMEZONE_KEY = getattr(settings, "SUPERDJANGO_TIMEZONE_KEY", "django_timezone")
TIMEZONE_SETTINGS_MODEL = getattr(settings, "SUPERDJANGO_TIMEZONE_SETTINGS_MODEL", None)

# Views


[docs]def set_my_timezone(request): """Allows the current user to set his or her timezone.""" if request.method == "POST": new_tz = request.POST['timezone'] request.session[TIMEZONE_KEY] = new_tz timezone = TimeZone(new_tz) messages.success(request, _("You time zone has been set to %s." % timezone.label)) return redirect('/') else: if TIMEZONE_SETTINGS_MODEL: config = get_model(TIMEZONE_SETTINGS_MODEL) choices = config.get_timezone_choices() else: choices = COMMON_TIMEZONE_CHOICES context = { 'current_tz': request.session.get(TIMEZONE_KEY, DEFAULT_TIMEZONE), 'timezones': choices, } return render(request, 'i18n/set_my_timezone.html', context)