gremmie@1: """ gremmie@1: Forms for the gcalendar application. gremmie@1: """ gremmie@1: import datetime gremmie@1: import pytz gremmie@1: from django import forms gremmie@1: gremmie@1: from gcalendar.models import Event gremmie@1: gremmie@1: gremmie@1: TIME_CHOICES = ( gremmie@1: ('00:00', '12:00 am (00:00)'), gremmie@1: ('00:30', '12:30 am (00:30)'), gremmie@1: ('01:00', '1:00 am (01:00)'), gremmie@1: ('01:30', '1:30 am (01:30)'), gremmie@1: ('02:00', '2:00 am (02:00)'), gremmie@1: ('02:30', '2:30 am (02:30)'), gremmie@1: ('03:00', '3:00 am (03:00)'), gremmie@1: ('03:30', '3:30 am (03:30)'), gremmie@1: ('04:00', '4:00 am (04:00)'), gremmie@1: ('04:30', '4:30 am (04:30)'), gremmie@1: ('05:00', '5:00 am (05:00)'), gremmie@1: ('05:30', '5:30 am (05:30)'), gremmie@1: ('06:00', '6:00 am (06:00)'), gremmie@1: ('06:30', '6:30 am (06:30)'), gremmie@1: ('07:00', '7:00 am (07:00)'), gremmie@1: ('07:30', '7:30 am (07:30)'), gremmie@1: ('08:00', '8:00 am (08:00)'), gremmie@1: ('08:30', '8:30 am (08:30)'), gremmie@1: ('09:00', '9:00 am (09:00)'), gremmie@1: ('09:30', '9:30 am (09:30)'), gremmie@1: ('10:00', '10:00 am (10:00)'), gremmie@1: ('10:30', '10:30 am (10:30)'), gremmie@1: ('11:00', '11:00 am (11:00)'), gremmie@1: ('11:30', '11:30 am (11:30)'), gremmie@1: ('12:00', '12:00 am (12:00)'), gremmie@1: ('12:30', '12:30 am (12:30)'), gremmie@1: ('13:00', '1:00 pm (13:00)'), gremmie@1: ('13:30', '1:30 pm (13:30)'), gremmie@1: ('14:00', '2:00 pm (14:00)'), gremmie@1: ('14:30', '2:30 pm (14:30)'), gremmie@1: ('15:00', '3:00 pm (15:00)'), gremmie@1: ('15:30', '3:30 pm (15:30)'), gremmie@1: ('16:00', '4:00 pm (16:00)'), gremmie@1: ('16:30', '4:30 pm (16:30)'), gremmie@1: ('17:00', '5:00 pm (17:00)'), gremmie@1: ('17:30', '5:30 pm (17:30)'), gremmie@1: ('18:00', '6:00 pm (18:00)'), gremmie@1: ('18:30', '6:30 pm (18:30)'), gremmie@1: ('19:00', '7:00 pm (19:00)'), gremmie@1: ('19:30', '7:30 pm (19:30)'), gremmie@1: ('20:00', '8:00 pm (20:00)'), gremmie@1: ('20:30', '8:30 pm (20:30)'), gremmie@1: ('21:00', '9:00 pm (21:00)'), gremmie@1: ('21:30', '9:30 pm (21:30)'), gremmie@1: ('22:00', '10:00 pm (22:00)'), gremmie@1: ('22:30', '10:30 pm (22:30)'), gremmie@1: ('23:00', '11:00 pm (23:00)'), gremmie@1: ('23:30', '11:30 pm (23:30)'), gremmie@1: ) gremmie@1: gremmie@1: gremmie@1: class EventEntryForm(forms.ModelForm): gremmie@1: what = forms.CharField(widget=forms.TextInput(attrs={'size': 60})) gremmie@1: start_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10})) gremmie@1: start_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES)) gremmie@1: end_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10})) gremmie@1: end_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES)) gremmie@1: time_zone = forms.CharField(required=False, widget=forms.HiddenInput()) gremmie@1: where = forms.CharField(required=False, widget=forms.TextInput(attrs={'size': 60})) gremmie@1: gremmie@1: TIME_FORMAT = '%H:%M' gremmie@1: DEFAULT_START_TIME = '19:00' gremmie@1: DEFAULT_END_TIME = '20:00' gremmie@1: gremmie@1: class Meta: gremmie@1: model = Event gremmie@1: fields = ('what', 'start_date', 'start_time', 'end_date', 'end_time', gremmie@1: 'all_day', 'time_zone', 'where', 'description') gremmie@1: gremmie@1: class Media: gremmie@1: css = { gremmie@1: 'all': ('js/markitup/skins/markitup/style.css', gremmie@1: 'js/markitup/sets/markdown/style.css', gremmie@1: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/redmond/jquery-ui.css', gremmie@1: 'css/gcalendar.css', gremmie@1: ) gremmie@1: } gremmie@1: js = ( gremmie@1: 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', gremmie@1: 'js/markitup/jquery.markitup.pack.js', gremmie@1: 'js/markitup/sets/markdown/set.js', gremmie@1: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/jquery-ui.js', gremmie@1: 'js/gcalendar.js', gremmie@1: ) gremmie@1: gremmie@1: def __init__(self, *args, **kwargs): gremmie@1: initial = kwargs.get('initial', {}) gremmie@1: instance = kwargs.get('instance', None) gremmie@1: gremmie@1: if len(args) == 0: # no POST arguments gremmie@1: if instance is None: gremmie@1: init_day = datetime.date.today().strftime('%m/%d/%Y') gremmie@1: if 'start_date' not in initial: gremmie@1: initial['start_date'] = init_day gremmie@1: if 'end_date' not in initial: gremmie@1: initial['end_date'] = init_day gremmie@1: if 'start_time' not in initial: gremmie@1: initial['start_time'] = self.DEFAULT_START_TIME gremmie@1: if 'end_time' not in initial: gremmie@1: initial['end_time'] = self.DEFAULT_END_TIME gremmie@1: else: gremmie@1: if instance.all_day: gremmie@1: initial['start_time'] = self.DEFAULT_START_TIME gremmie@1: initial['end_time'] = self.DEFAULT_END_TIME gremmie@1: else: gremmie@1: if 'start_time' not in initial: gremmie@1: initial['start_time'] = instance.start_time.strftime(self.TIME_FORMAT) gremmie@1: if 'end_time' not in initial: gremmie@1: initial['end_time'] = instance.end_time.strftime(self.TIME_FORMAT) gremmie@1: gremmie@1: kwargs['initial'] = initial gremmie@1: gremmie@1: super(EventEntryForm, self).__init__(*args, **kwargs) gremmie@1: gremmie@1: def clean(self): gremmie@1: start_date = self.cleaned_data.get('start_date') gremmie@1: start_time = self.cleaned_data.get('start_time') gremmie@1: all_day = self.cleaned_data.get('all_day') gremmie@1: end_date = self.cleaned_data.get('end_date') gremmie@1: end_time = self.cleaned_data.get('end_time') gremmie@1: gremmie@1: if start_date and start_time and (all_day or (end_date and end_time)): gremmie@1: if all_day: gremmie@1: start = start_date gremmie@1: end = end_date gremmie@1: else: gremmie@1: start = datetime.datetime.combine(start_date, start_time) gremmie@1: end = datetime.datetime.combine(end_date, end_time) gremmie@1: if start > end: gremmie@1: raise forms.ValidationError("The start date of the event " gremmie@1: "is after the ending time!") gremmie@1: gremmie@1: return self.cleaned_data gremmie@1: gremmie@1: def clean_time_zone(self): gremmie@1: tz = self.cleaned_data['time_zone'] gremmie@1: try: gremmie@1: pytz.timezone(tz) gremmie@1: except pytz.UnknownTimeZoneError: gremmie@1: raise forms.ValidationError("Invalid timezone.") gremmie@1: return tz gremmie@1: gremmie@1: gremmie@1: class PasswordForm(forms.Form): gremmie@1: password = forms.CharField(widget=forms.PasswordInput()) gremmie@1: gremmie@1: gremmie@1: # vim: ts=4 sw=4