From 44c4cd43c0a765c1e7d0aeae8f4cbbb3b1b3efc4 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 11 Dec 2014 11:45:48 +0100 Subject: [PATCH] [TASK] Rename hello view, add the actual form handling stuff. --- citavi_mapper/urls.py | 4 +- frontend/forms.py | 6 +- frontend/templates/index.html | 2 +- .../{hello.html => presentation-add.html} | 23 +++-- frontend/views.py | 84 ++++++++++++++++--- 5 files changed, 95 insertions(+), 24 deletions(-) rename frontend/templates/{hello.html => presentation-add.html} (83%) diff --git a/citavi_mapper/urls.py b/citavi_mapper/urls.py index 43bebb0..674f646 100644 --- a/citavi_mapper/urls.py +++ b/citavi_mapper/urls.py @@ -7,13 +7,13 @@ admin.autodiscover() from frontend.views import login_wrap, logout_wrap from frontend.views import IndexView, ProjectView, ProjectUpdateView, ProjectPersonView, ProjectMapPersonView -from frontend.views import HelloView +from frontend.views import PresentationAddView urlpatterns = patterns( '', url(r'^$', IndexView.as_view(), name=u'frontend-index'), url(r'^project/$', ProjectView.as_view(), name=u'frontend-projects'), - url(r'^hello/$', HelloView.as_view(), name=u'frontend-hello'), + url(r'^presentation/add/$', PresentationAddView.as_view(), name=u'frontend-presentation-add'), url(r'^project/(?P\d+)/update$', ProjectUpdateView.as_view(), name=u'frontend-project-update'), url(r'^project/(?P\d+)/map/person$', ProjectPersonView.as_view(), name=u'frontend-project-view-person'), diff --git a/frontend/forms.py b/frontend/forms.py index 6a9a3c2..6a4b4d7 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -159,7 +159,7 @@ class PresentationEventForm(forms.ModelForm): required=True, ) workshop = forms.BooleanField( - label=u"Workshop?", + label=u"Veranstaltung ist ein Workshop", required=False, ) place = forms.CharField( @@ -190,7 +190,9 @@ class PresentationForm(forms.ModelForm): self.helper.layout = Layout( u'title', u'type', - u'abstract' + u'abstract', + u'comments', + u'email' ) title = forms.CharField() diff --git a/frontend/templates/index.html b/frontend/templates/index.html index f638241..a3cbb37 100644 --- a/frontend/templates/index.html +++ b/frontend/templates/index.html @@ -4,7 +4,7 @@

Feel free to discover.
diff --git a/frontend/templates/hello.html b/frontend/templates/presentation-add.html similarity index 83% rename from frontend/templates/hello.html rename to frontend/templates/presentation-add.html index ebee25d..9fec6a6 100644 --- a/frontend/templates/hello.html +++ b/frontend/templates/presentation-add.html @@ -25,9 +25,19 @@ {% endblock %} {% block content %} - zurück
- -

Vortrag anlegen

+ {% if success %} + +
+ {% endif %} + {% if error %} + +
+ {% endif %}
{% crispy presentation_form %}
@@ -42,9 +52,9 @@
{% crispy person_form person_formhelper %}
-
{% endfor %} +

Veranstaltungen

@@ -56,10 +66,11 @@
{% crispy event_form event_formhelper %}
-
{% endfor %}
-
+
+
+ zurück zum Index
{% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index 88d24e9..9992a7c 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -111,24 +111,82 @@ class IndexView(ProtectedTemplateView): page_title = u'Index' -class HelloView(ProtectedTemplateView): - template_name = u'hello.html' - page_title = u'Hello' - success_url = u'/hello' +class PresentationAddView(ProtectedTemplateView): + template_name = u'presentation-add.html' + page_title = u'Vortrag anlegen' + success_url = u'presentation-add' + + """ These guys are passed through using get_context_data(). """ + presentation_form = None + person_formset = None + event_formset = None + person_formhelper = PresentationPersonFormHelper + event_formhelper = PresentationEventFormHelper + success = False + error = False def __init__(self, *args, **kwargs): - super(HelloView, self).__init__(*args, **kwargs) + super(PresentationAddView, self).__init__(*args, **kwargs) + + def get(self, request, *args, **kwargs): + """ Create empty form + formsets by default. """ + self.create_blank_forms() + return super(PresentationAddView, self).get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + """ Just use the same template and go on by default. Also, trigger handling the forms. """ + self.handle_forms(request, *args, **kwargs) + context = self.get_context_data(**kwargs) + return self.render_to_response(context) + + def create_blank_forms(self): + """ Create blank form + formsets for the user to fill. """ + self.presentation_form = PresentationForm(prefix='presentation') + self.person_formset = PresentationPersonInlineFormset(prefix='person') + self.event_formset = PresentationEventInlineFormset(prefix='event') def get_context_data(self, **kwargs): """ Add form + formsets to template context. """ - kwargs[u'presentation_form'] = PresentationForm() - person_formset = PresentationPersonInlineFormset(prefix='person') - event_formset = PresentationEventInlineFormset(prefix='event') - kwargs[u'person_formset'] = person_formset - kwargs[u'event_formset'] = event_formset - kwargs[u'person_formhelper'] = PresentationPersonFormHelper; - kwargs[u'event_formhelper'] = PresentationEventFormHelper; - return super(HelloView, self).get_context_data(**kwargs) + kwargs[u'presentation_form'] = self.presentation_form + kwargs[u'person_formset'] = self.person_formset + kwargs[u'event_formset'] = self.event_formset + kwargs[u'person_formhelper'] = self.person_formhelper + kwargs[u'event_formhelper'] = self.event_formhelper + kwargs[u'success'] = self.success + kwargs[u'error'] = self.error + return super(PresentationAddView, self).get_context_data(**kwargs) + + def handle_forms(self, request, *args, **kwargs): + """ Once a POST came in, try and validate the forms, saving the data. """ + presentation = None + success = 0 + """ Check the PresentationForm first and save it. """ + self.presentation_form = PresentationForm(request.POST, request.FILES, prefix='presentation') + if self.presentation_form.is_valid(): + presentation = self.presentation_form.save() + success += 1 + """ If the PresentationForm was good, we now have a Presentation to use for the other guys. """ + if presentation != None: + """ Get the person formset, validate and save. """ + person_formset = PresentationPersonInlineFormset(request.POST, request.FILES, instance=presentation, prefix='person') + if person_formset.is_valid(): + person_formset.save() + success += 1 + """ Get the event formset, validate and save. """ + event_formset = PresentationEventInlineFormset(request.POST, request.FILES, instance=presentation, prefix='event') + if event_formset.is_valid(): + event_formset.save() + success += 1 + else: + """ Create the formsets without the PresentationInstance, so their validation results can be displayed. """ + self.person_formset = PresentationPersonInlineFormset(request.POST, request.FILES, prefix='person') + self.event_formset = PresentationEventInlineFormset(request.POST, request.FILES, prefix='event') + """ Everything was good, go to success URI or something, i don't know. """ + if success == 3: + self.success = True + self.create_blank_forms() + else: + self.error = True class ProjectView(ProtectedFormView):