Source code for superdjango.contrib.i18n.timezones.forms
# Imports
from django import forms
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from .choices import ALL_TIMEZONE_CHOICES, COMMON_TIMEZONE_CHOICES
# Exports
__all__ = (
"TimeZoneField",
"TimeZoneSelectionForm",
"TimeZoneSettingsForm",
)
# Constants
DEFAULT_TIMEZONE = getattr(settings, "TIME_ZONE", "UTC")
# Fields
[docs]class TimeZoneField(forms.ChoiceField):
"""A form field which accepts valid time zone choices."""
[docs] def __init__(self, **kwargs):
"""``choices`` defaults to ``COMMON_TIMEZONE_CHOICES``."""
choices = kwargs.pop("choices", COMMON_TIMEZONE_CHOICES)
super().__init__(choices=choices, **kwargs)
# Forms
[docs]class TimeZoneSelectionForm(forms.Form):
"""Allows a user to select his or her timezone preference."""
timezone = TimeZoneField(
help_text=_("Please select your preferred time zone."),
label=_("time zone"),
required=True
)
[docs]class TimeZoneSettingsForm(forms.ModelForm):
"""Presents timezone settings with the default and default choices."""
default_timezone = TimeZoneField(
choices=ALL_TIMEZONE_CHOICES,
help_text=_("Select the default time zone."),
initial=DEFAULT_TIMEZONE,
label=_("default time zone"),
required=True
)