From a374ba960ab635b95250ac795939bc441eb03b64 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 3 Dec 2014 11:27:10 +0100 Subject: [PATCH] Use a custom approach with inline formsets. --- citavi_mapper/settings.py | 1 + frontend/forms.py | 34 ++++++++++-------------- frontend/formsets.py | 19 ++++++++++++++ frontend/templates/hello.html | 4 ++- frontend/views.py | 49 +++++++++++++++++------------------ 5 files changed, 60 insertions(+), 47 deletions(-) create mode 100644 frontend/formsets.py diff --git a/citavi_mapper/settings.py b/citavi_mapper/settings.py index a7e5938..4788c8a 100644 --- a/citavi_mapper/settings.py +++ b/citavi_mapper/settings.py @@ -47,6 +47,7 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', 'pipeline', 'crispy_forms', + 'fancy_formsets', 'bootstrap3_datetime', 'django_bootstrap_breadcrumbs', 'frontend', diff --git a/frontend/forms.py b/frontend/forms.py index 4953769..cb23e8b 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -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'] diff --git a/frontend/formsets.py b/frontend/formsets.py new file mode 100644 index 0000000..1124ab7 --- /dev/null +++ b/frontend/formsets.py @@ -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 +) diff --git a/frontend/templates/hello.html b/frontend/templates/hello.html index 1cce49e..ce06c09 100644 --- a/frontend/templates/hello.html +++ b/frontend/templates/hello.html @@ -5,6 +5,8 @@ {% block content %} hallo
zurück -

{% crispy form %}

+

{% crispy presentation_form %}

+

{% crispy person_formset %}

+

{% crispy event_formset %}

{% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index e2374fb..d613e2b 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -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'