comparison gpp/gcalendar/forms.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children b6263ac72052
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 """
2 Forms for the gcalendar application.
3 """
4 import datetime
5 import pytz
6 from django import forms
7
8 from gcalendar.models import Event
9
10
11 TIME_CHOICES = (
12 ('00:00', '12:00 am (00:00)'),
13 ('00:30', '12:30 am (00:30)'),
14 ('01:00', '1:00 am (01:00)'),
15 ('01:30', '1:30 am (01:30)'),
16 ('02:00', '2:00 am (02:00)'),
17 ('02:30', '2:30 am (02:30)'),
18 ('03:00', '3:00 am (03:00)'),
19 ('03:30', '3:30 am (03:30)'),
20 ('04:00', '4:00 am (04:00)'),
21 ('04:30', '4:30 am (04:30)'),
22 ('05:00', '5:00 am (05:00)'),
23 ('05:30', '5:30 am (05:30)'),
24 ('06:00', '6:00 am (06:00)'),
25 ('06:30', '6:30 am (06:30)'),
26 ('07:00', '7:00 am (07:00)'),
27 ('07:30', '7:30 am (07:30)'),
28 ('08:00', '8:00 am (08:00)'),
29 ('08:30', '8:30 am (08:30)'),
30 ('09:00', '9:00 am (09:00)'),
31 ('09:30', '9:30 am (09:30)'),
32 ('10:00', '10:00 am (10:00)'),
33 ('10:30', '10:30 am (10:30)'),
34 ('11:00', '11:00 am (11:00)'),
35 ('11:30', '11:30 am (11:30)'),
36 ('12:00', '12:00 am (12:00)'),
37 ('12:30', '12:30 am (12:30)'),
38 ('13:00', '1:00 pm (13:00)'),
39 ('13:30', '1:30 pm (13:30)'),
40 ('14:00', '2:00 pm (14:00)'),
41 ('14:30', '2:30 pm (14:30)'),
42 ('15:00', '3:00 pm (15:00)'),
43 ('15:30', '3:30 pm (15:30)'),
44 ('16:00', '4:00 pm (16:00)'),
45 ('16:30', '4:30 pm (16:30)'),
46 ('17:00', '5:00 pm (17:00)'),
47 ('17:30', '5:30 pm (17:30)'),
48 ('18:00', '6:00 pm (18:00)'),
49 ('18:30', '6:30 pm (18:30)'),
50 ('19:00', '7:00 pm (19:00)'),
51 ('19:30', '7:30 pm (19:30)'),
52 ('20:00', '8:00 pm (20:00)'),
53 ('20:30', '8:30 pm (20:30)'),
54 ('21:00', '9:00 pm (21:00)'),
55 ('21:30', '9:30 pm (21:30)'),
56 ('22:00', '10:00 pm (22:00)'),
57 ('22:30', '10:30 pm (22:30)'),
58 ('23:00', '11:00 pm (23:00)'),
59 ('23:30', '11:30 pm (23:30)'),
60 )
61
62
63 class EventEntryForm(forms.ModelForm):
64 what = forms.CharField(widget=forms.TextInput(attrs={'size': 60}))
65 start_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10}))
66 start_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES))
67 end_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10}))
68 end_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES))
69 time_zone = forms.CharField(required=False, widget=forms.HiddenInput())
70 where = forms.CharField(required=False, widget=forms.TextInput(attrs={'size': 60}))
71
72 TIME_FORMAT = '%H:%M'
73 DEFAULT_START_TIME = '19:00'
74 DEFAULT_END_TIME = '20:00'
75
76 class Meta:
77 model = Event
78 fields = ('what', 'start_date', 'start_time', 'end_date', 'end_time',
79 'all_day', 'time_zone', 'where', 'description')
80
81 class Media:
82 css = {
83 'all': ('js/markitup/skins/markitup/style.css',
84 'js/markitup/sets/markdown/style.css',
85 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/redmond/jquery-ui.css',
86 'css/gcalendar.css',
87 )
88 }
89 js = (
90 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
91 'js/markitup/jquery.markitup.pack.js',
92 'js/markitup/sets/markdown/set.js',
93 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/jquery-ui.js',
94 'js/gcalendar.js',
95 )
96
97 def __init__(self, *args, **kwargs):
98 initial = kwargs.get('initial', {})
99 instance = kwargs.get('instance', None)
100
101 if len(args) == 0: # no POST arguments
102 if instance is None:
103 init_day = datetime.date.today().strftime('%m/%d/%Y')
104 if 'start_date' not in initial:
105 initial['start_date'] = init_day
106 if 'end_date' not in initial:
107 initial['end_date'] = init_day
108 if 'start_time' not in initial:
109 initial['start_time'] = self.DEFAULT_START_TIME
110 if 'end_time' not in initial:
111 initial['end_time'] = self.DEFAULT_END_TIME
112 else:
113 if instance.all_day:
114 initial['start_time'] = self.DEFAULT_START_TIME
115 initial['end_time'] = self.DEFAULT_END_TIME
116 else:
117 if 'start_time' not in initial:
118 initial['start_time'] = instance.start_time.strftime(self.TIME_FORMAT)
119 if 'end_time' not in initial:
120 initial['end_time'] = instance.end_time.strftime(self.TIME_FORMAT)
121
122 kwargs['initial'] = initial
123
124 super(EventEntryForm, self).__init__(*args, **kwargs)
125
126 def clean(self):
127 start_date = self.cleaned_data.get('start_date')
128 start_time = self.cleaned_data.get('start_time')
129 all_day = self.cleaned_data.get('all_day')
130 end_date = self.cleaned_data.get('end_date')
131 end_time = self.cleaned_data.get('end_time')
132
133 if start_date and start_time and (all_day or (end_date and end_time)):
134 if all_day:
135 start = start_date
136 end = end_date
137 else:
138 start = datetime.datetime.combine(start_date, start_time)
139 end = datetime.datetime.combine(end_date, end_time)
140 if start > end:
141 raise forms.ValidationError("The start date of the event "
142 "is after the ending time!")
143
144 return self.cleaned_data
145
146 def clean_time_zone(self):
147 tz = self.cleaned_data['time_zone']
148 try:
149 pytz.timezone(tz)
150 except pytz.UnknownTimeZoneError:
151 raise forms.ValidationError("Invalid timezone.")
152 return tz
153
154
155 class PasswordForm(forms.Form):
156 password = forms.CharField(widget=forms.PasswordInput())
157
158
159 # vim: ts=4 sw=4