[TASK] Rename hello view, add the actual form handling stuff.

This commit is contained in:
Jan Philipp Timme 2014-12-11 11:45:48 +01:00
parent 5839ec3bbc
commit 44c4cd43c0
5 changed files with 95 additions and 24 deletions

View File

@ -7,13 +7,13 @@ admin.autodiscover()
from frontend.views import login_wrap, logout_wrap from frontend.views import login_wrap, logout_wrap
from frontend.views import IndexView, ProjectView, ProjectUpdateView, ProjectPersonView, ProjectMapPersonView from frontend.views import IndexView, ProjectView, ProjectUpdateView, ProjectPersonView, ProjectMapPersonView
from frontend.views import HelloView from frontend.views import PresentationAddView
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^$', IndexView.as_view(), name=u'frontend-index'), url(r'^$', IndexView.as_view(), name=u'frontend-index'),
url(r'^project/$', ProjectView.as_view(), name=u'frontend-projects'), 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<project_id>\d+)/update$', ProjectUpdateView.as_view(), name=u'frontend-project-update'), url(r'^project/(?P<project_id>\d+)/update$', ProjectUpdateView.as_view(), name=u'frontend-project-update'),
url(r'^project/(?P<project_id>\d+)/map/person$', ProjectPersonView.as_view(), name=u'frontend-project-view-person'), url(r'^project/(?P<project_id>\d+)/map/person$', ProjectPersonView.as_view(), name=u'frontend-project-view-person'),

View File

@ -159,7 +159,7 @@ class PresentationEventForm(forms.ModelForm):
required=True, required=True,
) )
workshop = forms.BooleanField( workshop = forms.BooleanField(
label=u"Workshop?", label=u"Veranstaltung ist ein Workshop",
required=False, required=False,
) )
place = forms.CharField( place = forms.CharField(
@ -190,7 +190,9 @@ 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()

View File

@ -4,7 +4,7 @@
<p> <p>
<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-hello' %}">hello</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>

View File

@ -25,9 +25,19 @@
</script> </script>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<a href="{% url 'frontend-index' %}">zurück</a><br> {% if success %}
<div class="alert alert-success" role="alert">
<h2>Vortrag anlegen</h2> Vortrag erfolgreich gespeichert!
</div>
<hr>
{% endif %}
{% if error %}
<div class="alert alert-danger" role="alert">
Unvollständige oder fehlerhafte Angaben!<br>
Bitte vervollständigen Sie Ihre Angaben und versuchen Sie es erneut!
</div>
<hr>
{% endif %}
<form method="POST" action="" class="form-horizontal" id="presentationform"> <form method="POST" action="" class="form-horizontal" id="presentationform">
{% crispy presentation_form %} {% crispy presentation_form %}
<hr> <hr>
@ -42,9 +52,9 @@
<div class="person_form"> <div class="person_form">
{% crispy person_form person_formhelper %} {% crispy person_form person_formhelper %}
</div> </div>
<hr>
{% endfor %} {% endfor %}
</div> </div>
<hr>
<h3>Veranstaltungen</h3> <h3>Veranstaltungen</h3>
<div> <div>
<input id="id_{{ event_formset.prefix }}-TOTAL_FORMS" name="{{ event_formset.prefix }}-TOTAL_FORMS" type="hidden" value="1" /> <input id="id_{{ event_formset.prefix }}-TOTAL_FORMS" name="{{ event_formset.prefix }}-TOTAL_FORMS" type="hidden" value="1" />
@ -56,10 +66,11 @@
<div class="event_form"> <div class="event_form">
{% crispy event_form event_formhelper %} {% crispy event_form event_formhelper %}
</div> </div>
<hr>
{% endfor %} {% endfor %}
</div> </div>
<br> <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>
<hr>
<a href="{% url 'frontend-index' %}">zurück zum Index</a><br>
{% endblock %} {% endblock %}

View File

@ -111,24 +111,82 @@ class IndexView(ProtectedTemplateView):
page_title = u'Index' page_title = u'Index'
class HelloView(ProtectedTemplateView): class PresentationAddView(ProtectedTemplateView):
template_name = u'hello.html' template_name = u'presentation-add.html'
page_title = u'Hello' page_title = u'Vortrag anlegen'
success_url = u'/hello' 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): 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): def get_context_data(self, **kwargs):
""" Add form + formsets to template context. """ """ Add form + formsets to template context. """
kwargs[u'presentation_form'] = PresentationForm() kwargs[u'presentation_form'] = self.presentation_form
person_formset = PresentationPersonInlineFormset(prefix='person') kwargs[u'person_formset'] = self.person_formset
event_formset = PresentationEventInlineFormset(prefix='event') kwargs[u'event_formset'] = self.event_formset
kwargs[u'person_formset'] = person_formset kwargs[u'person_formhelper'] = self.person_formhelper
kwargs[u'event_formset'] = event_formset kwargs[u'event_formhelper'] = self.event_formhelper
kwargs[u'person_formhelper'] = PresentationPersonFormHelper; kwargs[u'success'] = self.success
kwargs[u'event_formhelper'] = PresentationEventFormHelper; kwargs[u'error'] = self.error
return super(HelloView, self).get_context_data(**kwargs) 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): class ProjectView(ProtectedFormView):