Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.conf import settings
4from django.contrib import messages
5from django.shortcuts import redirect, render
6from django.utils.translation import ugettext_lazy as _
7from superdjango.shortcuts import get_model
8from .choices import COMMON_TIMEZONE_CHOICES
9from .descriptors import TimeZone
11# Exports
13__all__ = (
14 "set_my_timezone",
15)
17# Constants
19DEFAULT_TIMEZONE = getattr(settings, "TIME_ZONE", "UTC")
20TIMEZONE_KEY = getattr(settings, "SUPERDJANGO_TIMEZONE_KEY", "django_timezone")
21TIMEZONE_SETTINGS_MODEL = getattr(settings, "SUPERDJANGO_TIMEZONE_SETTINGS_MODEL", None)
23# Views
26def set_my_timezone(request):
27 """Allows the current user to set his or her timezone."""
29 if request.method == "POST":
30 new_tz = request.POST['timezone']
32 request.session[TIMEZONE_KEY] = new_tz
34 timezone = TimeZone(new_tz)
35 messages.success(request, _("You time zone has been set to %s." % timezone.label))
37 return redirect('/')
38 else:
39 if TIMEZONE_SETTINGS_MODEL:
40 config = get_model(TIMEZONE_SETTINGS_MODEL)
41 choices = config.get_timezone_choices()
42 else:
43 choices = COMMON_TIMEZONE_CHOICES
45 context = {
46 'current_tz': request.session.get(TIMEZONE_KEY, DEFAULT_TIMEZONE),
47 'timezones': choices,
48 }
49 return render(request, 'i18n/set_my_timezone.html', context)