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 import forms
4from django.conf import settings
5from django.utils.translation import ugettext_lazy as _
6from .choices import ALL_TIMEZONE_CHOICES, COMMON_TIMEZONE_CHOICES
8# Exports
10__all__ = (
11 "TimeZoneField",
12 "TimeZoneSettingsForm",
13)
15# Constants
17DEFAULT_TIMEZONE = getattr(settings, "TIME_ZONE", "UTC")
19# Fields
22class TimeZoneField(forms.ChoiceField):
23 """A form field which accepts valid time zone choices."""
25 def __init__(self, **kwargs):
26 """``choices`` defaults to ``COMMON_TIMEZONE_CHOICES``."""
27 choices = kwargs.pop("choices", COMMON_TIMEZONE_CHOICES)
29 super().__init__(choices=choices, **kwargs)
31# Forms
34class TimeZoneSelectionForm(forms.Form):
35 """Allows a user to select his or her timezone preference."""
37 timezone = TimeZoneField(
38 help_text=_("Please select your preferred time zone."),
39 label=_("time zone"),
40 required=True
41 )
44class TimeZoneSettingsForm(forms.ModelForm):
45 """Presents timezone settings with the default and default choices."""
47 default_timezone = TimeZoneField(
48 choices=ALL_TIMEZONE_CHOICES,
49 help_text=_("Select the default time zone."),
50 initial=DEFAULT_TIMEZONE,
51 label=_("default time zone"),
52 required=True
53 )