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