annotate gcalendar/forms.py @ 1041:565c31469bea

Added Bob Dalley's book to store page.
author Brian Neal <bgneal@gmail.com>
date Fri, 15 Jan 2016 21:29:51 -0600
parents 68c3343f3318
children f0ac48aa8c64
rev   line source
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
gremmie@1 7
bgneal@1021 8 from core.html import ImageCheckError
bgneal@1021 9 from core.html import image_check
bgneal@1021 10 from core.markup import site_markup
gremmie@1 11 from gcalendar.models import Event
gremmie@1 12
gremmie@1 13
gremmie@1 14 TIME_CHOICES = (
gremmie@1 15 ('00:00', '12:00 am (00:00)'),
gremmie@1 16 ('00:30', '12:30 am (00:30)'),
gremmie@1 17 ('01:00', '1:00 am (01:00)'),
gremmie@1 18 ('01:30', '1:30 am (01:30)'),
gremmie@1 19 ('02:00', '2:00 am (02:00)'),
gremmie@1 20 ('02:30', '2:30 am (02:30)'),
gremmie@1 21 ('03:00', '3:00 am (03:00)'),
gremmie@1 22 ('03:30', '3:30 am (03:30)'),
gremmie@1 23 ('04:00', '4:00 am (04:00)'),
gremmie@1 24 ('04:30', '4:30 am (04:30)'),
gremmie@1 25 ('05:00', '5:00 am (05:00)'),
gremmie@1 26 ('05:30', '5:30 am (05:30)'),
gremmie@1 27 ('06:00', '6:00 am (06:00)'),
gremmie@1 28 ('06:30', '6:30 am (06:30)'),
gremmie@1 29 ('07:00', '7:00 am (07:00)'),
gremmie@1 30 ('07:30', '7:30 am (07:30)'),
gremmie@1 31 ('08:00', '8:00 am (08:00)'),
gremmie@1 32 ('08:30', '8:30 am (08:30)'),
gremmie@1 33 ('09:00', '9:00 am (09:00)'),
gremmie@1 34 ('09:30', '9:30 am (09:30)'),
gremmie@1 35 ('10:00', '10:00 am (10:00)'),
gremmie@1 36 ('10:30', '10:30 am (10:30)'),
gremmie@1 37 ('11:00', '11:00 am (11:00)'),
gremmie@1 38 ('11:30', '11:30 am (11:30)'),
gremmie@1 39 ('12:00', '12:00 am (12:00)'),
gremmie@1 40 ('12:30', '12:30 am (12:30)'),
gremmie@1 41 ('13:00', '1:00 pm (13:00)'),
gremmie@1 42 ('13:30', '1:30 pm (13:30)'),
gremmie@1 43 ('14:00', '2:00 pm (14:00)'),
gremmie@1 44 ('14:30', '2:30 pm (14:30)'),
gremmie@1 45 ('15:00', '3:00 pm (15:00)'),
gremmie@1 46 ('15:30', '3:30 pm (15:30)'),
gremmie@1 47 ('16:00', '4:00 pm (16:00)'),
gremmie@1 48 ('16:30', '4:30 pm (16:30)'),
gremmie@1 49 ('17:00', '5:00 pm (17:00)'),
gremmie@1 50 ('17:30', '5:30 pm (17:30)'),
gremmie@1 51 ('18:00', '6:00 pm (18:00)'),
gremmie@1 52 ('18:30', '6:30 pm (18:30)'),
gremmie@1 53 ('19:00', '7:00 pm (19:00)'),
gremmie@1 54 ('19:30', '7:30 pm (19:30)'),
gremmie@1 55 ('20:00', '8:00 pm (20:00)'),
gremmie@1 56 ('20:30', '8:30 pm (20:30)'),
gremmie@1 57 ('21:00', '9:00 pm (21:00)'),
gremmie@1 58 ('21:30', '9:30 pm (21:30)'),
gremmie@1 59 ('22:00', '10:00 pm (22:00)'),
gremmie@1 60 ('22:30', '10:30 pm (22:30)'),
gremmie@1 61 ('23:00', '11:00 pm (23:00)'),
gremmie@1 62 ('23:30', '11:30 pm (23:30)'),
gremmie@1 63 )
gremmie@1 64
gremmie@1 65
gremmie@1 66 class EventEntryForm(forms.ModelForm):
gremmie@1 67 what = forms.CharField(widget=forms.TextInput(attrs={'size': 60}))
gremmie@1 68 start_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10}))
gremmie@1 69 start_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES))
gremmie@1 70 end_date = forms.DateField(widget=forms.TextInput(attrs={'size': 10}))
gremmie@1 71 end_time = forms.TimeField(required=False, widget=forms.Select(choices=TIME_CHOICES))
gremmie@1 72 time_zone = forms.CharField(required=False, widget=forms.HiddenInput())
gremmie@1 73 where = forms.CharField(required=False, widget=forms.TextInput(attrs={'size': 60}))
bgneal@1021 74 description = forms.CharField(
bgneal@1021 75 required=False,
bgneal@1021 76 widget=forms.Textarea(attrs={
bgneal@1021 77 'class': 'markItUp smileyTarget',
bgneal@1021 78 'id': 'id_body', # needed for image related js
bgneal@1021 79 }))
gremmie@1 80
bgneal@198 81 DATE_FORMAT = '%m/%d/%Y' # must match the jQuery UI datepicker config
gremmie@1 82 TIME_FORMAT = '%H:%M'
gremmie@1 83 DEFAULT_START_TIME = '19:00'
gremmie@1 84 DEFAULT_END_TIME = '20:00'
gremmie@1 85
gremmie@1 86 class Meta:
gremmie@1 87 model = Event
gremmie@1 88 fields = ('what', 'start_date', 'start_time', 'end_date', 'end_time',
bgneal@228 89 'all_day', 'time_zone', 'where', 'description', 'create_forum_thread')
gremmie@1 90
gremmie@1 91 def __init__(self, *args, **kwargs):
gremmie@1 92 initial = kwargs.get('initial', {})
gremmie@1 93 instance = kwargs.get('instance', None)
gremmie@1 94
gremmie@1 95 if len(args) == 0: # no POST arguments
gremmie@1 96 if instance is None:
bgneal@198 97 init_day = datetime.date.today().strftime(self.DATE_FORMAT)
gremmie@1 98 if 'start_date' not in initial:
gremmie@1 99 initial['start_date'] = init_day
gremmie@1 100 if 'end_date' not in initial:
gremmie@1 101 initial['end_date'] = init_day
gremmie@1 102 if 'start_time' not in initial:
gremmie@1 103 initial['start_time'] = self.DEFAULT_START_TIME
gremmie@1 104 if 'end_time' not in initial:
gremmie@1 105 initial['end_time'] = self.DEFAULT_END_TIME
gremmie@1 106 else:
bgneal@198 107 initial['start_date'] = instance.start_date.strftime(self.DATE_FORMAT)
bgneal@198 108 initial['end_date'] = instance.end_date.strftime(self.DATE_FORMAT)
gremmie@1 109 if instance.all_day:
gremmie@1 110 initial['start_time'] = self.DEFAULT_START_TIME
gremmie@1 111 initial['end_time'] = self.DEFAULT_END_TIME
gremmie@1 112 else:
gremmie@1 113 if 'start_time' not in initial:
gremmie@1 114 initial['start_time'] = instance.start_time.strftime(self.TIME_FORMAT)
gremmie@1 115 if 'end_time' not in initial:
gremmie@1 116 initial['end_time'] = instance.end_time.strftime(self.TIME_FORMAT)
gremmie@1 117
gremmie@1 118 kwargs['initial'] = initial
gremmie@1 119
gremmie@1 120 super(EventEntryForm, self).__init__(*args, **kwargs)
gremmie@1 121
bgneal@228 122 # We don't want the user to create a forum thread on an existing event
bgneal@228 123 if instance is not None:
bgneal@228 124 del self.fields['create_forum_thread']
bgneal@228 125
bgneal@1021 126 def clean_description(self):
bgneal@1021 127 description = self.cleaned_data['description']
bgneal@1021 128 self.html = None
bgneal@1021 129 if description:
bgneal@1021 130 self.html = site_markup(description, relative_urls=False)
bgneal@1021 131 try:
bgneal@1021 132 image_check(self.html)
bgneal@1021 133 except ImageCheckError as ex:
bgneal@1021 134 raise forms.ValidationError(str(ex))
bgneal@1021 135 return description
bgneal@1021 136
gremmie@1 137 def clean(self):
gremmie@1 138 start_date = self.cleaned_data.get('start_date')
gremmie@1 139 start_time = self.cleaned_data.get('start_time')
gremmie@1 140 all_day = self.cleaned_data.get('all_day')
gremmie@1 141 end_date = self.cleaned_data.get('end_date')
gremmie@1 142 end_time = self.cleaned_data.get('end_time')
gremmie@1 143
gremmie@1 144 if start_date and start_time and (all_day or (end_date and end_time)):
gremmie@1 145 if all_day:
gremmie@1 146 start = start_date
gremmie@1 147 end = end_date
gremmie@1 148 else:
gremmie@1 149 start = datetime.datetime.combine(start_date, start_time)
gremmie@1 150 end = datetime.datetime.combine(end_date, end_time)
gremmie@1 151 if start > end:
gremmie@1 152 raise forms.ValidationError("The start date of the event "
gremmie@1 153 "is after the ending time!")
gremmie@1 154
gremmie@1 155 return self.cleaned_data
gremmie@1 156
gremmie@1 157 def clean_time_zone(self):
gremmie@1 158 tz = self.cleaned_data['time_zone']
gremmie@1 159 try:
gremmie@1 160 pytz.timezone(tz)
gremmie@1 161 except pytz.UnknownTimeZoneError:
gremmie@1 162 raise forms.ValidationError("Invalid timezone.")
gremmie@1 163 return tz
gremmie@1 164