class ScheduledEventBaseForm(KanisaBaseModelForm): kanisa_form_class = 'scheduledevent' start_time = BootstrapTimeField() date = BootstrapDateField() end_date = BootstrapDateField(required=False) is_multi_day = forms.BooleanField( label='Multi-day event', required=False, help_text=('Check this box if this event spans multiple days.')) def __init__(self, *args, **kwargs): super(ScheduledEventBaseForm, self).__init__(*args, **kwargs) fields = OrderedDict() multi_day = self.fields.pop('is_multi_day') for key, value in self.fields.items(): fields[key] = value if key == 'start_time': fields['is_multi_day'] = multi_day self.fields = fields def clean(self): super(ScheduledEventBaseForm, self).clean() cleaned_data = self.cleaned_data if cleaned_data['date']: sd = cleaned_data['date'] ed = cleaned_data['end_date'] multiday = cleaned_data['is_multi_day'] if multiday: if not ed: errors = ErrorList( ['Multi-day events must have an end ' 'date']) self._errors["is_multi_day"] = errors del cleaned_data["is_multi_day"] if ed and ed < sd: errors = ErrorList( ['The event cannot end before it ' 'starts.']) self._errors["end_date"] = errors del cleaned_data["end_date"] return cleaned_data
class ServiceForm(KanisaBaseModelForm): def __init__(self, *args, **kwargs): super(ServiceForm, self).__init__(*args, **kwargs) if self.instance.pk: del self.fields['event'] del self.fields['date'] del self.fields['band'] else: url = reverse('kanisa_xhr_bandinformation') self.helper.attrs["data-band-info-url"] = url url = reverse('kanisa_xhr_eventinformation') self.helper.attrs["data-event-info-url"] = url date = BootstrapDateField() # The list of events in the field events is largely ignored. We # need to specify .all() to ensure that any event which is # selected (after the user has selected a date) is accepted. With # ScheduledEvent.objects.none(), the view renders more quickly, # but no events are accepted when the user hits submit. event = EventChoiceField(ScheduledEvent.bare_objects.all()) band = forms.ModelChoiceField(Band.objects.all(), required=False) band_leader = AccountChoiceField(get_user_model().objects.all()) musicians = MultipleAccountChoiceField(get_user_model().objects.all(), required=False) class Meta: fields = ( 'date', 'event', 'band', 'band_leader', 'musicians', ) model = Service
class SiteWideNoticeForm(KanisaBaseModelForm): publish_until = BootstrapDateField() class Meta: fields = [ 'headline', 'publish_until', 'contents', ] model = SiteWideNotice widgets = {'contents': KanisaMainInputWidget()}
class BlogPostForm(KanisaBaseModelForm): author = AccountChoiceField( get_user_model().objects.all(), required=False ) publish_date = BootstrapDateField( help_text=('Blog posts are published on the site at 00:00 on the ' 'publish date.') ) class Meta: model = BlogPost widgets = { 'main_text': KanisaMainInputWidget(), 'teaser_text': KanisaBlogTeaserInputWidget(), } fields = ( 'title', 'author', 'publish_date', 'teaser_text', 'main_text', 'enable_comments', )
class SermonForm(KanisaBaseModelForm): date = BootstrapDateField() no_mp3 = forms.BooleanField( initial=False, required=False, widget=forms.HiddenInput ) class Meta: model = Sermon widgets = {'details': KanisaMainInputWidget(), 'transcript': KanisaMainInputWidget()} def apply_id3(self, cleaned_data): try: audio = EasyID3(self.files['mp3'].temporary_file_path()) except ID3NoHeaderError: audio = MP3(self.files['mp3'].temporary_file_path()) audio["TIT2"] = TIT2(encoding=3, text=[cleaned_data['title']]) audio.save() audio = EasyID3(self.files['mp3'].temporary_file_path()) audio = EasyID3(self.files['mp3'].temporary_file_path()) audio['title'] = cleaned_data['title'] audio['artist'] = unicode(cleaned_data['speaker']) if not cleaned_data['series']: album_title = 'Sermons from %s' % conf.KANISA_CHURCH_NAME else: album_title = unicode(cleaned_data['series']) audio['album'] = album_title audio['albumartistsort'] = conf.KANISA_CHURCH_NAME audio['organization'] = conf.KANISA_CHURCH_NAME audio['genre'] = 'Speech' # Not sure if this date format is right - the MP3 players I've # got to test with don't show anything more than the year. if 'date' in cleaned_data: audio['date'] = cleaned_data['date'].strftime('%Y%m%d') audio.save() def clean(self): super(SermonForm, self).clean() cleaned_data = self.cleaned_data if 'mp3' in self.files: if hasattr(self.files['mp3'], 'temporary_file_path'): audio = MP3(self.files['mp3'].temporary_file_path()) else: # You probably need to set FILE_UPLOAD_HANDLERS to # django.core.files.uploadhandler.TemporaryFileUploadHandler audio = None if audio is None or not audio.info or audio.info.sketchy: errors = ErrorList(['Please upload a valid MP3.']) self._errors["mp3"] = errors del cleaned_data["mp3"] else: self.apply_id3(cleaned_data) else: show_mp3_warning = not self.cleaned_data.get("no_mp3", False) if not self.instance.pk and show_mp3_warning: # We've got no MP3 file, and we've not seen this error # before - let's check that was intentional. self.data["no_mp3"] = True raise forms.ValidationError( 'No MP3 was uploaded - if that was intentional, please ' 'click \'Save Sermon\' again.') return cleaned_data