Hide keyboard shortcuts

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 

2 

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 

7 

8# Exports 

9 

10__all__ = ( 

11 "TimeZoneField", 

12 "TimeZoneSettingsForm", 

13) 

14 

15# Constants 

16 

17DEFAULT_TIMEZONE = getattr(settings, "TIME_ZONE", "UTC") 

18 

19# Fields 

20 

21 

22class TimeZoneField(forms.ChoiceField): 

23 """A form field which accepts valid time zone choices.""" 

24 

25 def __init__(self, **kwargs): 

26 """``choices`` defaults to ``COMMON_TIMEZONE_CHOICES``.""" 

27 choices = kwargs.pop("choices", COMMON_TIMEZONE_CHOICES) 

28 

29 super().__init__(choices=choices, **kwargs) 

30 

31# Forms 

32 

33 

34class TimeZoneSelectionForm(forms.Form): 

35 """Allows a user to select his or her timezone preference.""" 

36 

37 timezone = TimeZoneField( 

38 help_text=_("Please select your preferred time zone."), 

39 label=_("time zone"), 

40 required=True 

41 ) 

42 

43 

44class TimeZoneSettingsForm(forms.ModelForm): 

45 """Presents timezone settings with the default and default choices.""" 

46 

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 )