comparison gcalendar/forms.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/gcalendar/forms.py@bbbc357ac5f3
children 21c592cac71c
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Forms for the gcalendar application.
3 """
4 import datetime
5 import pytz
6 from django import forms
7 from django.conf import settings
8
9 from gcalendar.models import Event
10
11
12 TIME_CHOICES = (
13 ('00:00', '12:00 am (00:00)'),
14 ('00:30', '12:30 am (00:30)'),
15 ('01:00', '1:00 am (01:00)'),
16 ('01:30', '1:30 am (01:30)'),
17 ('02:00', '2:00 am (02:00)'),
18 ('02:30', '2:30 am (02:30)'),
19 ('03:00', '3:00 am (03:00)'),
20 ('03:30', '3:30 am (03:30)'),
21 ('04:00', '4:00 am (04:00)'),
22 ('04:30', '4:30 am (04:30)'),
23 ('05:00', '5:00 am (05:00)'),
24 ('05:30', '5:30 am (05:30)'),
25 ('06:00', '6:00 am (06:00)'),
26 ('06:30', '6:30 am (06:30)'),
27 ('07:00', '7:00 am (07:00)'),
28 ('07:30', '7:30 am (07:30)'),
29 ('08:00', '8:00 am (08:00)'),
30 ('08:30', '8:30 am (08:30)'),
31 ('09:00', '9:00 am (09:00)'),
32 ('09:30', '9:30 am (09:30)'),
33 ('10:00', '10:00 am (10:00)'),
34 ('10:30', '10:30 am (10:30)'),
35 ('11:00', '11:00 am (11:00)'),
36 ('11:30', '11:30 am (11:30)'),
37 ('12:00', '12:00 am (12:00)'),
38 ('12:30', '12:30 am (12:30)'),
39 ('13:00', '1:00 pm (13:00)'),
40 ('13:30', '1:30 pm (13:30)'),
41 ('14:00', '2:00 pm (14:00)'),
42 ('14:30', '2:30 pm (14:30)'),
43 ('15:00', '3:00 pm (15:00)'),
44 ('15:30', '3:30 pm (15:30)'),
45 ('16:00', '4:00 pm (16:00)'),
46 ('16:30', '4:30 pm (16:30)'),
47 ('17:00', '5:00 pm (17:00)'),
48 ('17:30', '5:30 pm (17:30)'),
49 ('18:00', '6:00 pm (18:00)'),
50 ('18:30', '6:30 pm (18:30)'),
51 ('19:00', '7:00 pm (19:00)'),
52 ('19:30', '7:30 pm (19:30)'),
53 ('20:00', '8:00 pm (20:00)'),
54 ('20:30', '8:30 pm (20:30)'),
55 ('21:00', '9:00 pm (21:00)'),
56 ('21:30', '9:30 pm (21:30)'),
57 ('22:00', '10:00 pm (22:00)'),
58 ('22:30', '10:30 pm (22:30)'),
59 ('23:00', '11:00 pm (23:00)'),
60 ('23:30', '11:30 pm (23:30)'),
61 )
62
63
64 class EventEntryForm(forms.ModelForm):
65 what = forms.CharField(widget=forms.TextInput(attrs={'size': 60}))
66 start_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10}))
67 start_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES))
68 end_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10}))
69 end_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES))
70 time_zone = forms.CharField(required=False, widget=forms.HiddenInput())
71 where = forms.CharField(required=False, widget=forms.TextInput(attrs={'size': 60}))
72 description = forms.CharField(required=False,
73 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
74
75 DATE_FORMAT = '%m/%d/%Y' # must match the jQuery UI datepicker config
76 TIME_FORMAT = '%H:%M'
77 DEFAULT_START_TIME = '19:00'
78 DEFAULT_END_TIME = '20:00'
79
80 class Meta:
81 model = Event
82 fields = ('what', 'start_date', 'start_time', 'end_date', 'end_time',
83 'all_day', 'time_zone', 'where', 'description', 'create_forum_thread')
84
85 class Media:
86 css = {
87 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
88 settings.GPP_THIRD_PARTY_CSS['jquery-ui'] +
89 ['css/gcalendar.css'])
90 }
91 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
92 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
93 ['js/timezone.js', 'js/gcalendar.js'])
94
95 def __init__(self, *args, **kwargs):
96 initial = kwargs.get('initial', {})
97 instance = kwargs.get('instance', None)
98
99 if len(args) == 0: # no POST arguments
100 if instance is None:
101 init_day = datetime.date.today().strftime(self.DATE_FORMAT)
102 if 'start_date' not in initial:
103 initial['start_date'] = init_day
104 if 'end_date' not in initial:
105 initial['end_date'] = init_day
106 if 'start_time' not in initial:
107 initial['start_time'] = self.DEFAULT_START_TIME
108 if 'end_time' not in initial:
109 initial['end_time'] = self.DEFAULT_END_TIME
110 else:
111 initial['start_date'] = instance.start_date.strftime(self.DATE_FORMAT)
112 initial['end_date'] = instance.end_date.strftime(self.DATE_FORMAT)
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 # We don't want the user to create a forum thread on an existing event
127 if instance is not None:
128 del self.fields['create_forum_thread']
129
130 def clean(self):
131 start_date = self.cleaned_data.get('start_date')
132 start_time = self.cleaned_data.get('start_time')
133 all_day = self.cleaned_data.get('all_day')
134 end_date = self.cleaned_data.get('end_date')
135 end_time = self.cleaned_data.get('end_time')
136
137 if start_date and start_time and (all_day or (end_date and end_time)):
138 if all_day:
139 start = start_date
140 end = end_date
141 else:
142 start = datetime.datetime.combine(start_date, start_time)
143 end = datetime.datetime.combine(end_date, end_time)
144 if start > end:
145 raise forms.ValidationError("The start date of the event "
146 "is after the ending time!")
147
148 return self.cleaned_data
149
150 def clean_time_zone(self):
151 tz = self.cleaned_data['time_zone']
152 try:
153 pytz.timezone(tz)
154 except pytz.UnknownTimeZoneError:
155 raise forms.ValidationError("Invalid timezone.")
156 return tz
157