Use a custom approach with inline formsets.

This commit is contained in:
Jan Philipp Timme 2014-12-03 11:27:10 +01:00
parent 23139fbcca
commit a374ba960a
5 changed files with 60 additions and 47 deletions

View File

@ -47,6 +47,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'pipeline',
'crispy_forms',
'fancy_formsets',
'bootstrap3_datetime',
'django_bootstrap_breadcrumbs',
'frontend',

View File

@ -5,9 +5,6 @@ from frontend.models import Project, PersonGlobalIdentity, PresentationPerson, P
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout
from django.forms.models import inlineformset_factory
class NewProjectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(NewProjectForm, self).__init__(*args, **kwargs)
@ -153,38 +150,34 @@ class PresentationEventForm(forms.ModelForm):
Submit(u'send', u'Create', css_class=u'btn-default')
)
date = forms.DateField(
label = u"Datum",
required = True ,
label=u"Datum",
required=True ,
)
name = forms.CharField(
label = u"Rahmen der Veranstaltung",
label=u"Rahmen der Veranstaltung",
max_length=255,
required = True,
required=True,
)
workshop = forms.BooleanField(
label = u"Workshop?",
required = False,
label=u"Workshop?",
required=False,
)
place = forms.CharField(
label = u"Ort",
place = forms.CharField(
label=u"Ort",
max_length=255 ,
required = True,
required=True,
)
start_date = forms.DateField(
label = u"Startdatum",
max_length=255,
required = False,
label=u"Startdatum"
)
end_date = forms.DateField(
label = u"Enddatum",
max_length=255,
required = False,
label=u"Enddatum"
)
class Meta:
model = PresentationEvent
fields = [u'date', u'name', u'workshop', u'place', u'start_date', u'end_date']
class PresentationForm(forms.ModelForm):
""" Big form containing all the stuff about the things. """
@ -201,7 +194,6 @@ class PresentationForm(forms.ModelForm):
u'abstract',
Submit(u'send', u'Abschicken', css_class=u'btn-default')
)
pass
title = forms.CharField()
type = forms.ChoiceField(choices=[(u'new', u'Hauptvortrag'), (u'existing', u'andere Veranstaltung')], initial=u'new', widget=forms.RadioSelect())
@ -210,7 +202,7 @@ class PresentationForm(forms.ModelForm):
# Event (other event)
comments = forms.CharField()
email = forms.EmailField()
class Meta:
model = Presentation
fields = [u'title', u'type', u'abstract']

19
frontend/formsets.py Normal file
View File

@ -0,0 +1,19 @@
from fancy_formsets.forms import FancyBaseInlineFormSet
from frontend.models import Presentation, PresentationEvent, PresentationPerson
from django.forms.models import inlineformset_factory
PresentationPersonInlineFormset = inlineformset_factory(
Presentation,
PresentationPerson,
formset=FancyBaseInlineFormSet,
extra=1
)
PresentationEventInlineFormset = inlineformset_factory(
Presentation,
PresentationEvent,
formset=FancyBaseInlineFormSet,
extra=1
)

View File

@ -5,6 +5,8 @@
{% block content %}
hallo <br>
<a href="{% url 'frontend-index' %}">zurück</a>
<p>{% crispy form %}</p>
<p>{% crispy presentation_form %}</p>
<p>{% crispy person_formset %}</p>
<p>{% crispy event_formset %}</p>
{% endblock %}

View File

@ -15,14 +15,15 @@ from django.contrib.auth.views import login, logout
from django.core.urlresolvers import reverse
from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm
from frontend.forms import PresentationPersonFormSet, PresentationForm
from frontend.models import Presentation
from extra_views import FormSetView
from frontend.forms import PresentationForm, PresentationEventForm, PresentationPersonForm
from frontend.models import Presentation, PresentationEvent, PresentationPerson
from frontend.formsets import PresentationEventInlineFormset, PresentationPersonInlineFormset
from fancy_formsets.views import FormsetsView
from frontend.models import Project
FRONTEND_PAGE_NAME = u'Citavi Mapper'
from service.Mapper import person_mapper
@ -73,9 +74,6 @@ class MyTemplateView(MyViewMixin, TemplateView):
class MyFormView(MyViewMixin, FormView):
pass
class MyFormSetView(MyViewMixin, FormSetView):
pass
class MyCreateView(MyViewMixin, CreateView):
pass
@ -98,7 +96,7 @@ class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
class ProtectedFormView(LoggedInMixin, MyFormView):
pass
class ProtectedFormSetView(LoggedInMixin, MyFormSetView):
class ProtectedFormSetView(LoggedInMixin, MyTemplateView):
pass
class ProtectedCreateView(LoggedInMixin, MyCreateView):
@ -114,28 +112,29 @@ class IndexView(ProtectedTemplateView):
page_title = u'Index'
# TODO: Make this work!
class HelloView(ProtectedCreateView):
"""
class PresentationPersonInline(InlineFormSet):
model = PresentationPerson
class PresentationEventInline(InlineFormSet):
model = PresentationEvent
"""
class HelloView(ProtectedFormSetView):
template_name = u'hello.html'
page_title = u'Hello'
model = Presentation
form_class = PresentationForm
success_url = u'/hello'
def get(self, request, *args, **kwargs):
"""
Handles GET requests and instantiates blank versions of the form
and its inline formsets.
"""
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
presentationperson_form = PresentationPersonFormSet()
return self.render_to_response(
self.get_context_data(form=form,
presentationperson_form=presentationperson_form))
def __init__(self, *args, **kwargs):
super(HelloView, self).__init__(*args, **kwargs)
def get_context_data(self, **kwargs):
""" Add form + formsets to template context. """
kwargs[u'presentation_form'] = PresentationForm();
kwargs[u'person_formset'] = PresentationPersonInlineFormset(prefix='person');
kwargs[u'event_formset'] = PresentationEventInlineFormset(prefix='event');
return super(HelloView, self).get_context_data(**kwargs)
def form_valid(self, form):
pass
class ProjectView(ProtectedFormView):
template_name = u'projects.html'