[TASK] Rename hello view, add the actual form handling stuff.
This commit is contained in:
parent
5839ec3bbc
commit
44c4cd43c0
|
@ -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<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'),
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<p>
|
||||
<ul>
|
||||
<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>
|
||||
Feel free to discover.<br>
|
||||
|
|
|
@ -25,9 +25,19 @@
|
|||
</script>
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<a href="{% url 'frontend-index' %}">zurück</a><br>
|
||||
|
||||
<h2>Vortrag anlegen</h2>
|
||||
{% if success %}
|
||||
<div class="alert alert-success" role="alert">
|
||||
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">
|
||||
{% crispy presentation_form %}
|
||||
<hr>
|
||||
|
@ -42,9 +52,9 @@
|
|||
<div class="person_form">
|
||||
{% crispy person_form person_formhelper %}
|
||||
</div>
|
||||
<hr>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<hr>
|
||||
<h3>Veranstaltungen</h3>
|
||||
<div>
|
||||
<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">
|
||||
{% crispy event_form event_formhelper %}
|
||||
</div>
|
||||
<hr>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<br>
|
||||
<hr>
|
||||
<input type="submit" class="btn btn-primary btn-default" value="Submit">
|
||||
</form>
|
||||
<hr>
|
||||
<a href="{% url 'frontend-index' %}">zurück zum Index</a><br>
|
||||
{% endblock %}
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue