annotate gcalendar/forms.py @ 1179:55eb3620aa6c

Store page updates.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Apr 2019 15:01:42 -0500
parents f27986c11ddf
children
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)'),
bgneal@1113 39 ('12:00', '12:00 pm (12:00)'),
bgneal@1113 40 ('12:30', '12:30 pm (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):
bgneal@1094 67 what = forms.CharField(label='Event Title')
bgneal@1094 68 start_date = forms.DateField(label='Start Date')
bgneal@1094 69 start_time = forms.TimeField(label='Start Time', required=False,
bgneal@1094 70 widget=forms.Select(choices=TIME_CHOICES))
bgneal@1094 71 end_date = forms.DateField(label='End Date')
bgneal@1094 72 end_time = forms.TimeField(label='End Time', required=False,
bgneal@1094 73 widget=forms.Select(choices=TIME_CHOICES))
gremmie@1 74 time_zone = forms.CharField(required=False, widget=forms.HiddenInput())
bgneal@1094 75 where = forms.CharField(
bgneal@1094 76 label='Event Location',
bgneal@1094 77 required=False,
bgneal@1094 78 help_text='If applicable, please fill this out for an accurate Google map')
bgneal@1021 79 description = forms.CharField(
bgneal@1094 80 label='Event Details',
bgneal@1021 81 required=False,
bgneal@1021 82 widget=forms.Textarea(attrs={
bgneal@1021 83 'id': 'id_body', # needed for image related js
bgneal@1021 84 }))
gremmie@1 85
bgneal@198 86 DATE_FORMAT = '%m/%d/%Y' # must match the jQuery UI datepicker config
gremmie@1 87 TIME_FORMAT = '%H:%M'
gremmie@1 88 DEFAULT_START_TIME = '19:00'
gremmie@1 89 DEFAULT_END_TIME = '20:00'
gremmie@1 90
gremmie@1 91 class Meta:
gremmie@1 92 model = Event
gremmie@1 93 fields = ('what', 'start_date', 'start_time', 'end_date', 'end_time',
bgneal@228 94 'all_day', 'time_zone', 'where', 'description', 'create_forum_thread')
gremmie@1 95
gremmie@1 96 def __init__(self, *args, **kwargs):
gremmie@1 97 initial = kwargs.get('initial', {})
gremmie@1 98 instance = kwargs.get('instance', None)
gremmie@1 99
gremmie@1 100 if len(args) == 0: # no POST arguments
gremmie@1 101 if instance is None:
bgneal@198 102 init_day = datetime.date.today().strftime(self.DATE_FORMAT)
gremmie@1 103 if 'start_date' not in initial:
gremmie@1 104 initial['start_date'] = init_day
gremmie@1 105 if 'end_date' not in initial:
gremmie@1 106 initial['end_date'] = init_day
gremmie@1 107 if 'start_time' not in initial:
gremmie@1 108 initial['start_time'] = self.DEFAULT_START_TIME
gremmie@1 109 if 'end_time' not in initial:
gremmie@1 110 initial['end_time'] = self.DEFAULT_END_TIME
gremmie@1 111 else:
bgneal@198 112 initial['start_date'] = instance.start_date.strftime(self.DATE_FORMAT)
bgneal@198 113 initial['end_date'] = instance.end_date.strftime(self.DATE_FORMAT)
gremmie@1 114 if instance.all_day:
gremmie@1 115 initial['start_time'] = self.DEFAULT_START_TIME
gremmie@1 116 initial['end_time'] = self.DEFAULT_END_TIME
gremmie@1 117 else:
gremmie@1 118 if 'start_time' not in initial:
gremmie@1 119 initial['start_time'] = instance.start_time.strftime(self.TIME_FORMAT)
gremmie@1 120 if 'end_time' not in initial:
gremmie@1 121 initial['end_time'] = instance.end_time.strftime(self.TIME_FORMAT)
gremmie@1 122
gremmie@1 123 kwargs['initial'] = initial
gremmie@1 124
gremmie@1 125 super(EventEntryForm, self).__init__(*args, **kwargs)
gremmie@1 126
bgneal@228 127 # We don't want the user to create a forum thread on an existing event
bgneal@228 128 if instance is not None:
bgneal@228 129 del self.fields['create_forum_thread']
bgneal@228 130
bgneal@1021 131 def clean_description(self):
bgneal@1021 132 description = self.cleaned_data['description']
bgneal@1064 133 self.html = ''
bgneal@1021 134 if description:
bgneal@1021 135 self.html = site_markup(description, relative_urls=False)
bgneal@1021 136 try:
bgneal@1021 137 image_check(self.html)
bgneal@1021 138 except ImageCheckError as ex:
bgneal@1021 139 raise forms.ValidationError(str(ex))
bgneal@1021 140 return description
bgneal@1021 141
gremmie@1 142 def clean(self):
gremmie@1 143 start_date = self.cleaned_data.get('start_date')
gremmie@1 144 start_time = self.cleaned_data.get('start_time')
gremmie@1 145 all_day = self.cleaned_data.get('all_day')
gremmie@1 146 end_date = self.cleaned_data.get('end_date')
gremmie@1 147 end_time = self.cleaned_data.get('end_time')
gremmie@1 148
gremmie@1 149 if start_date and start_time and (all_day or (end_date and end_time)):
gremmie@1 150 if all_day:
gremmie@1 151 start = start_date
gremmie@1 152 end = end_date
gremmie@1 153 else:
gremmie@1 154 start = datetime.datetime.combine(start_date, start_time)
gremmie@1 155 end = datetime.datetime.combine(end_date, end_time)
gremmie@1 156 if start > end:
gremmie@1 157 raise forms.ValidationError("The start date of the event "
gremmie@1 158 "is after the ending time!")
gremmie@1 159
gremmie@1 160 return self.cleaned_data
gremmie@1 161
gremmie@1 162 def clean_time_zone(self):
gremmie@1 163 tz = self.cleaned_data['time_zone']
gremmie@1 164 try:
gremmie@1 165 pytz.timezone(tz)
gremmie@1 166 except pytz.UnknownTimeZoneError:
gremmie@1 167 raise forms.ValidationError("Invalid timezone.")
gremmie@1 168 return tz
gremmie@1 169