[TASK] Implement check of presentation type before validating event.

This commit is contained in:
Jan Philipp Timme 2014-12-11 15:47:47 +01:00
parent 44c4cd43c0
commit 3c183a256b
5 changed files with 81 additions and 48 deletions

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django import forms from django import forms
from frontend.models import Project, PersonGlobalIdentity, PresentationPerson, Presentation, PresentationEvent from frontend.models import Project, PersonGlobalIdentity, PresentationPerson, Presentation, PresentationEvent, PresentationSubmission
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout from crispy_forms.layout import Submit, Layout
@ -177,6 +177,26 @@ class PresentationEventForm(forms.ModelForm):
model = PresentationEvent model = PresentationEvent
fields = [u'date', u'name', u'workshop', u'place', u'start_date', u'end_date'] fields = [u'date', u'name', u'workshop', u'place', u'start_date', u'end_date']
class PresentationSubmissionForm(forms.ModelForm):
""" Form for the PresentationSubmission"""
def __init__(self, *args, **kwargs):
super(PresentationSubmissionForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.label_class = u'col-lg-2'
self.helper.field_class = u'col-lg-4'
self.helper.layout = Layout(
u'comment',
u'email',
)
comment = forms.CharField(label='Kommentar')
email = forms.EmailField(label='E-Mail Adresse')
class Meta:
model = PresentationSubmission
fields = [u'comment', u'email']
class PresentationForm(forms.ModelForm): class PresentationForm(forms.ModelForm):
@ -190,19 +210,13 @@ class PresentationForm(forms.ModelForm):
self.helper.layout = Layout( self.helper.layout = Layout(
u'title', u'title',
u'type', u'type',
u'abstract', u'abstract'
u'comments',
u'email'
) )
title = forms.CharField() title = forms.CharField(label='Titel')
type = forms.ChoiceField(choices=[(u'new', u'Hauptvortrag'), (u'existing', u'andere Veranstaltung')], initial=u'new', widget=forms.RadioSelect()) type = forms.ChoiceField(choices=[(u'main', u'Hauptvortrag'), (u'other', u'andere Veranstaltung')], initial=u'main', widget=forms.RadioSelect(), label='Veranstaltungstyp')
abstract = forms.TextInput() abstract = forms.TextInput()
# Event (other event)
comments = forms.CharField()
email = forms.EmailField()
class Meta: class Meta:
model = Presentation model = Presentation
fields = [u'title', u'type', u'abstract'] fields = [u'title', u'type', u'abstract']

View File

@ -18,26 +18,32 @@ class Project(models.Model):
class Presentation(models.Model): class Presentation(models.Model):
""" Presention Model for PresentationForm""" """ Presention Model for PresentationForm"""
title = models.CharField(max_length=255) title = models.CharField(max_length=255, verbose_name='Titel')
type = models.CharField(max_length=255) type = models.CharField(max_length=255, choices=((u'main', u'Hauptvortrag'), (u'other', u'andere Veranstaltung')), default=u'main', verbose_name='Veranstaltungstyp')
abstract = models.TextField() abstract = models.TextField(verbose_name=u'Zusammenfassung')
class PresentationSubmission(models.Model):
""" Submission containing a Presentation """
email = models.EmailField(max_length=255, verbose_name='E-Mail Adresse')
comment = models.TextField(verbose_name=u'Kommentar')
presentation = models.ForeignKey(Presentation, blank=False, null=False, db_index=True)
class PresentationEvent(models.Model): class PresentationEvent(models.Model):
"""Veranstaltung""" """Veranstaltung"""
date = models.DateField() date = models.DateField(verbose_name='Veranstaltungsdatum')
name = models.CharField(max_length=255) name = models.CharField(max_length=255, verbose_name='Name')
workshop = models.BooleanField() workshop = models.BooleanField(verbose_name='Workshop')
place = models.CharField(max_length=255) place = models.CharField(max_length=255, verbose_name='Ort')
start_date = models.DateField() start_date = models.DateField(verbose_name='Startdatum')
end_date = models.DateField() end_date = models.DateField(verbose_name='Enddatum')
presentatiom = models.ForeignKey(Presentation, blank=False, null=False, db_index=True) presentation = models.ForeignKey(Presentation, blank=False, null=False, db_index=True)
class PresentationPerson(models.Model): class PresentationPerson(models.Model):
""" Person Model for PresentationForm""" """ Person Model for PresentationForm"""
given_name = models.CharField(max_length=255) given_name = models.CharField(max_length=255, verbose_name='Vorname')
additional_name = models.CharField(max_length=255) additional_name = models.CharField(max_length=255, verbose_name='Namenszusatz', blank=True)
family_name = models.CharField(max_length=255) family_name = models.CharField(max_length=255, verbose_name='Nachname')
hshmembership = models.CharField(max_length=255, choices=((u'yes', u'Ja'), (u'no', u'Nein'), (u'maybe', u'Maybe')), default=u'maybe') hshmembership = models.CharField(max_length=255, choices=((u'yes', u'Ja'), (u'no', u'Nein'), (u'maybe', u'Maybe')), default=u'maybe', verbose_name='Hochschulangehörigkeit')
presentation = models.ForeignKey(Presentation, blank=False, null=False, db_index=True) presentation = models.ForeignKey(Presentation, blank=False, null=False, db_index=True)

View File

@ -5,7 +5,6 @@
<ul> <ul>
<li><a href="{% url 'frontend-projects' %}">Citavi projects</a></li> <li><a href="{% url 'frontend-projects' %}">Citavi projects</a></li>
<li><a href="{% url 'frontend-presentation-add' %}">Add presentations</a></li> <li><a href="{% url 'frontend-presentation-add' %}">Add presentations</a></li>
</ul> </ul>
Feel free to discover.<br> Feel free to discover.<br>
</p> </p>

View File

@ -68,6 +68,8 @@
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<hr>
{% crispy submission_form %}
<hr> <hr>
<input type="submit" class="btn btn-primary btn-default" value="Submit"> <input type="submit" class="btn btn-primary btn-default" value="Submit">
</form> </form>

View File

@ -15,7 +15,7 @@ from django.contrib.auth.views import login, logout
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm
from frontend.forms import PresentationForm, PresentationEventForm, PresentationPersonForm from frontend.forms import PresentationForm, PresentationEventForm, PresentationPersonForm, PresentationSubmissionForm
from frontend.forms import PresentationEventFormHelper, PresentationPersonFormHelper from frontend.forms import PresentationEventFormHelper, PresentationPersonFormHelper
from frontend.models import Presentation, PresentationEvent, PresentationPerson from frontend.models import Presentation, PresentationEvent, PresentationPerson
from frontend.formsets import PresentationEventInlineFormset, PresentationPersonInlineFormset from frontend.formsets import PresentationEventInlineFormset, PresentationPersonInlineFormset
@ -118,6 +118,7 @@ class PresentationAddView(ProtectedTemplateView):
""" These guys are passed through using get_context_data(). """ """ These guys are passed through using get_context_data(). """
presentation_form = None presentation_form = None
submission_form = None
person_formset = None person_formset = None
event_formset = None event_formset = None
person_formhelper = PresentationPersonFormHelper person_formhelper = PresentationPersonFormHelper
@ -142,12 +143,14 @@ class PresentationAddView(ProtectedTemplateView):
def create_blank_forms(self): def create_blank_forms(self):
""" Create blank form + formsets for the user to fill. """ """ Create blank form + formsets for the user to fill. """
self.presentation_form = PresentationForm(prefix='presentation') self.presentation_form = PresentationForm(prefix='presentation')
self.submission_form = PresentationSubmissionForm(prefix='submission')
self.person_formset = PresentationPersonInlineFormset(prefix='person') self.person_formset = PresentationPersonInlineFormset(prefix='person')
self.event_formset = PresentationEventInlineFormset(prefix='event') self.event_formset = PresentationEventInlineFormset(prefix='event')
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
""" Add form + formsets to template context. """ """ Add form + formsets to template context. """
kwargs[u'presentation_form'] = self.presentation_form kwargs[u'presentation_form'] = self.presentation_form
kwargs[u'submission_form'] = self.submission_form
kwargs[u'person_formset'] = self.person_formset kwargs[u'person_formset'] = self.person_formset
kwargs[u'event_formset'] = self.event_formset kwargs[u'event_formset'] = self.event_formset
kwargs[u'person_formhelper'] = self.person_formhelper kwargs[u'person_formhelper'] = self.person_formhelper
@ -159,30 +162,39 @@ class PresentationAddView(ProtectedTemplateView):
def handle_forms(self, request, *args, **kwargs): def handle_forms(self, request, *args, **kwargs):
""" Once a POST came in, try and validate the forms, saving the data. """ """ Once a POST came in, try and validate the forms, saving the data. """
presentation = None presentation = None
success = 0 presentation_ok = False
""" Check the PresentationForm first and save it. """ person_ok = False
event_ok = False
submission_ok = False
""" Create all the forms and formsets based on request params to do validation. """
self.presentation_form = PresentationForm(request.POST, request.FILES, prefix='presentation') self.presentation_form = PresentationForm(request.POST, request.FILES, prefix='presentation')
if self.presentation_form.is_valid(): self.person_formset = PresentationPersonInlineFormset(request.POST, request.FILES, prefix='person')
presentation = self.presentation_form.save() self.event_formset = PresentationEventInlineFormset(request.POST, request.FILES, prefix='event')
success += 1 self.submission_form = PresentationSubmissionForm(request.POST, request.FILES, prefix='submission')
""" If the PresentationForm was good, we now have a Presentation to use for the other guys. """ """ Check all for validity """
if presentation != None: presentation_ok = self.presentation_form.is_valid()
""" Get the person formset, validate and save. """ submission_ok = self.submission_form.is_valid()
person_formset = PresentationPersonInlineFormset(request.POST, request.FILES, instance=presentation, prefix='person') person_ok = self.person_formset.is_valid()
if person_formset.is_valid(): """ Only other event type needs a valid event formset. """
person_formset.save() print(presentation_ok, person_ok, event_ok, submission_ok)
success += 1 print(self.submission_form)
""" Get the event formset, validate and save. """ if presentation_ok and self.presentation_form.cleaned_data[u'type'] == 'other':
event_formset = PresentationEventInlineFormset(request.POST, request.FILES, instance=presentation, prefix='event') event_ok = self.event_formset.is_valid()
if event_formset.is_valid():
event_formset.save()
success += 1
else: else:
""" Create the formsets without the PresentationInstance, so their validation results can be displayed. """ event_ok = True
self.person_formset = PresentationPersonInlineFormset(request.POST, request.FILES, prefix='person') """ Only if everything is alright, store it."""
self.event_formset = PresentationEventInlineFormset(request.POST, request.FILES, prefix='event') if presentation_ok == True and person_ok == True and event_ok == True and submission_ok == True:
""" Everything was good, go to success URI or something, i don't know. """ presentation = self.presentation_form.save()
if success == 3: """ Now we can create the other ones based on the saved presentation to save them, too. """
submission_form = PresentationSubmissionForm(request.POST, request.FILES, instance=presentation, prefix='submission')
submission_form.save()
person_formset = PresentationPersonInlineFormset(request.POST, request.FILES, instance=presentation, prefix='person')
person_formset.save()
""" Conditionally save the events depending on whether they are needed. """
if presentation.type == 'other':
event_formset = PresentationEventInlineFormset(request.POST, request.FILES, instance=presentation, prefix='event')
event_formset.save()
""" we're done. """
self.success = True self.success = True
self.create_blank_forms() self.create_blank_forms()
else: else: