[TASK] Try using inline form sets.

This commit is contained in:
Jan Philipp Timme 2014-11-26 11:56:18 +01:00
parent a7a8601566
commit 32f5915530
3 changed files with 37 additions and 43 deletions

View File

@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
from django import forms
from frontend.models import Project, PersonGlobalIdentity, PresentationPerson
from frontend.models import Project, PersonGlobalIdentity, PresentationPerson, Presentation
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout
from django.forms.formsets import BaseFormSet
from django.forms.models import inlineformset_factory
class NewProjectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
@ -84,27 +84,6 @@ class PersonMapForm(forms.Form):
preferred_identity = forms.BooleanField(initial=False, required=False)
class PresentationFormSet(BaseFormSet):
""" This can be done better, but i don't know how yet. """
def add_fields(self, form, index):
""" Add fields for Presentation to the FormSet. """
super(PresentationFormSet, self).add_fields(form, index)
# Additional fields for the actual presentation
form.fields["title"] = forms.CharField()
form.fields["event_type"] = forms.ChoiceField(choices=[(u'new', u'Hauptvortrag'), (u'existing', u'andere Veranstaltung')], initial=u'new', widget=forms.RadioSelect())
form.fields["abstract"] = forms.CharField()
# Event (other event)
form.fields["date"] = forms.DateField()
form.fields["rahmen"] = forms.CharField()
form.fields["workshop"] = forms.BooleanField()
form.fields["place"] = forms.CharField()
form.fields["startdate"] = forms.DateField()
form.fields["enddate"] = forms.DateField()
# additional comment + email
form.fields["comments"] = forms.CharField()
form.fields["email"] = forms.EmailField()
class PresentationPersonForm(forms.ModelForm):
""" Modelform for PresentationPerson """
def __init__(self, *args, **kwargs):
@ -154,6 +133,8 @@ class PresentationPersonForm(forms.ModelForm):
model = PresentationPerson
fields = [u'given_name', u'additional_name', u'family_name', u'hshmembership']
PresentationPersonFormSet = inlineformset_factory(Presentation, PresentationPerson)
class PresentationForm(forms.Form):
""" Big form containing all the stuff about the things. """
@ -168,10 +149,6 @@ class PresentationForm(forms.Form):
u'title',
u'event_type',
u'abstract',
u'given_name',
u'additional_name',
u'family_name',
u'hsh_membership',
u'date',
u'rahmen',
u'workshop',
@ -188,13 +165,6 @@ class PresentationForm(forms.Form):
event_type = forms.ChoiceField(choices=[(u'new', u'Hauptvortrag'), (u'existing', u'andere Veranstaltung')], initial=u'new', widget=forms.RadioSelect())
abstract = forms.CharField()
# Teilnehmer
given_name = forms.CharField()
additional_name = forms.CharField()
family_name = forms.CharField()
hsh_membership = forms.ChoiceField(choices=[(u'new', u'Yes'), (u'existing', u'No'), (u'idk', u'Maybe')], initial=u'new', widget=forms.RadioSelect())
# Event (other event)
date = forms.DateField()
rahmen = forms.CharField()

View File

@ -16,12 +16,18 @@ class Project(models.Model):
temp += u" (No project file)"
return temp
class Presentation(models.Model):
""" Presention Model for PresentationForm"""
title = models.CharField(max_length=255)
class PresentationPerson(models.Model):
""" Person Model for PresentationForm"""
given_name = models.CharField(max_length=255)
additional_name = models.CharField(max_length=255)
family_name = models.CharField(max_length=255)
hshmembership = models.CharField(max_length=255)
presentation = models.ForeignKey(Presentation, blank=False, null=False, db_index=True)
class PersonGlobalIdentity(models.Model):
""" Model representing a global person identity in django. Can be used to link any foreign identity to it. """

View File

@ -14,8 +14,10 @@ from django.contrib.auth.views import login, logout
from django.core.urlresolvers import reverse
from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm, PresentationPersonForm, PresentationFormSet
from django.forms.formsets import formset_factory
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.models import Project
@ -71,6 +73,9 @@ class MyTemplateView(MyViewMixin, TemplateView):
class MyFormView(MyViewMixin, FormView):
pass
class MyFormSetView(MyViewMixin, FormSetView):
pass
class MyCreateView(MyViewMixin, CreateView):
pass
@ -93,6 +98,9 @@ class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
class ProtectedFormView(LoggedInMixin, MyFormView):
pass
class ProtectedFormSetView(LoggedInMixin, MyFormSetView):
pass
class ProtectedCreateView(LoggedInMixin, MyCreateView):
pass
@ -105,17 +113,27 @@ class IndexView(ProtectedTemplateView):
template_name = u'index.html'
page_title = u'Index'
class HelloView(ProtectedFormView):
# TODO: Make this work!
class HelloView(ProtectedCreateView):
template_name = u'hello.html'
page_title = u'Hello'
form_class = None
model = Presentation
form_class = PresentationForm
success_url = u'/hello'
def __init__(self, *args, **kwargs):
super(HelloView, self).__init__()
PresentationPersonFormFactory = formset_factory(PresentationPersonForm, formset=PresentationFormSet)
self.form_class = PresentationPersonFormFactory(prefix='person')
print self.form_class
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 form_valid(self, form):
pass