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', 'django.contrib.staticfiles',
'pipeline', 'pipeline',
'crispy_forms', 'crispy_forms',
'fancy_formsets',
'bootstrap3_datetime', 'bootstrap3_datetime',
'django_bootstrap_breadcrumbs', 'django_bootstrap_breadcrumbs',
'frontend', 'frontend',

View File

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

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 %} {% block content %}
hallo <br> hallo <br>
<a href="{% url 'frontend-index' %}">zurück</a> <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 %} {% endblock %}

View File

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