From e7fc6dcb8a9ec9188ce879adf123f04b9acda753 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 10 Dec 2014 16:28:21 +0100 Subject: [PATCH] Integrate jquery formset.js properly. --- .gitignore | 2 +- frontend/forms.py | 64 +++++----- frontend/formsets.py | 5 +- frontend/models.py | 8 +- frontend/static/css/frontend.css | 44 +++++++ frontend/static/js/jquery.formset.js | 171 +++++++++++++++++++++++++++ frontend/templates/hello.html | 38 ++++-- frontend/views.py | 15 +-- 8 files changed, 292 insertions(+), 55 deletions(-) create mode 100644 frontend/static/css/frontend.css create mode 100644 frontend/static/js/jquery.formset.js diff --git a/.gitignore b/.gitignore index 2220269..18df25f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ *.pyc citavi_mapper/private_settings.py -static/ +/static/ media/* diff --git a/frontend/forms.py b/frontend/forms.py index ee0761e..6a9a3c2 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -81,21 +81,23 @@ class PersonMapForm(forms.Form): global_identity = forms.ModelChoiceField(queryset=PersonGlobalIdentity.objects.all(), required=False) preferred_identity = forms.BooleanField(initial=False, required=False) +""" FormHelper for the PresentationPersonForm""" +PresentationPersonFormHelper = FormHelper() +PresentationPersonFormHelper.form_tag = False +PresentationPersonFormHelper.label_class = u'col-lg-2' +PresentationPersonFormHelper.field_class = u'col-lg-4' +PresentationPersonFormHelper.layout = Layout( + u'given_name', + u'additional_name', + u'family_name', + u'hshmembership' +) class PresentationPersonForm(forms.ModelForm): """ Modelform for PresentationPerson """ def __init__(self, *args, **kwargs): super(PresentationPersonForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.form_tag = False - self.helper.label_class = u'col-lg-2' - self.helper.field_class = u'col-lg-4' - self.helper.layout = Layout( - u'given_name', - u'additional_name', - u'family_name', - u'hshmembership' - ) + self.helper = PresentationPersonFormHelper given_name = forms.CharField( label=u"Vorname", @@ -115,13 +117,10 @@ class PresentationPersonForm(forms.ModelForm): required=True, ) - hshmembership = forms.ChoiceField( - choices=[ - (u'yes', u'Ja'), # - (u'no', u'Nein'), - (u'maybe', u'Unbekannt'), - ], - initial=u'yes', + hshmembership = forms.TypedChoiceField( + choices=((u'yes', u'Ja'), (u'no', u'Nein'), (u'maybe', u'Maybe')), + initial=u'maybe', + required=True, widget=forms.RadioSelect() ) @@ -129,22 +128,27 @@ class PresentationPersonForm(forms.ModelForm): model = PresentationPerson fields = [u'given_name', u'additional_name', u'family_name', u'hshmembership'] + +""" FormHelper for the PresentationEventForm""" +PresentationEventFormHelper = FormHelper() +PresentationEventFormHelper.form_tag = False +PresentationEventFormHelper.label_class = u'col-lg-2' +PresentationEventFormHelper.field_class = u'col-lg-4' +PresentationEventFormHelper.layout = Layout( + u'date', + u'name', + u'workshop', + u'place', + u'start_date', + u'end_date' +) + class PresentationEventForm(forms.ModelForm): """ Form for the PresentationEventModel""" def __init__(self, *args, **kwargs): super(PresentationEventForm, self).__init__(*args, **kwargs) - self.helper = FormHelper() - self.helper.form_tag = False - self.helper.label_class = u'col-lg-2' - self.helper.field_class = u'col-lg-4' - self.helper.layout = Layout( - u'date', - u'name', - u'workshop', - u'place', - u'start_date', - u'end_date' - ) + self.helper = PresentationEventFormHelper + date = forms.DateField( label=u"Datum", required=True , @@ -191,7 +195,7 @@ class PresentationForm(forms.ModelForm): title = forms.CharField() type = forms.ChoiceField(choices=[(u'new', u'Hauptvortrag'), (u'existing', u'andere Veranstaltung')], initial=u'new', widget=forms.RadioSelect()) - abstract = forms.CharField() + abstract = forms.TextInput() # Event (other event) comments = forms.CharField() diff --git a/frontend/formsets.py b/frontend/formsets.py index eaf793c..7c365cf 100644 --- a/frontend/formsets.py +++ b/frontend/formsets.py @@ -1,11 +1,14 @@ +from django import forms from frontend.models import Presentation, PresentationEvent, PresentationPerson from django.forms.models import inlineformset_factory + PresentationPersonInlineFormset = inlineformset_factory( Presentation, PresentationPerson, extra=1, - can_delete=False + can_delete=False, + widgets={'hshmembership': forms.RadioSelect} ) PresentationEventInlineFormset = inlineformset_factory( diff --git a/frontend/models.py b/frontend/models.py index 83dbc43..7d6a823 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*-2 - from django.db import models + class Project(models.Model): """ Model representing a citavi project. """ name = models.CharField(max_length=255) @@ -20,7 +20,7 @@ class Presentation(models.Model): """ Presention Model for PresentationForm""" title = models.CharField(max_length=255) type = models.CharField(max_length=255) - abstract = models.CharField(max_length=255) + abstract = models.TextField() class PresentationEvent(models.Model): """Veranstaltung""" @@ -31,13 +31,13 @@ class PresentationEvent(models.Model): start_date = models.DateField() end_date = models.DateField() presentatiom = models.ForeignKey(Presentation, blank=False, null=False, db_index=True) - + 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) + hshmembership = models.CharField(max_length=255, choices=((u'yes', u'Ja'), (u'no', u'Nein'), (u'maybe', u'Maybe')), default=u'maybe') presentation = models.ForeignKey(Presentation, blank=False, null=False, db_index=True) diff --git a/frontend/static/css/frontend.css b/frontend/static/css/frontend.css new file mode 100644 index 0000000..7b81eee --- /dev/null +++ b/frontend/static/css/frontend.css @@ -0,0 +1,44 @@ +/* logo alignment stuff*/ +a.navbar-brand { + margin-top: -11px; +} + + +/* style for login form */ +#frontend-loginform { + width: 300px; + margin-left: auto; + margin-right: auto; + margin-top: 50px; +} + +.container { + width: auto; + /*max-width: 680px;*/ + padding: 0 15px; +} + +/* Footer text align */ +.container .text-muted { + margin: 20px 0; +} + +/* sticky footer */ +html { + position: relative; + min-height: 100%; +} + +body { + /* sticky footer height */ + margin-bottom: 60px; +} + +.footer { + position: absolute; + bottom: 0; + width: 100%; + /* sticky footer height */ + height: 60px; + background-color: #f5f5f5; +} diff --git a/frontend/static/js/jquery.formset.js b/frontend/static/js/jquery.formset.js new file mode 100644 index 0000000..9b12367 --- /dev/null +++ b/frontend/static/js/jquery.formset.js @@ -0,0 +1,171 @@ +/** + * jQuery Formset 1.2 + * @author Stanislaus Madueke (stan DOT madueke AT gmail DOT com) + * @requires jQuery 1.2.6 or later + * + * Copyright (c) 2009, Stanislaus Madueke + * All rights reserved. + * + * Licensed under the New BSD License + * See: http://www.opensource.org/licenses/bsd-license.php + */ +;(function($) { + $.fn.formset = function(opts) + { + var options = $.extend({}, $.fn.formset.defaults, opts), + flatExtraClasses = options.extraClasses.join(' '), + $$ = $(this), + + applyExtraClasses = function(row, ndx) { + if (options.extraClasses) { + row.removeClass(flatExtraClasses); + row.addClass(options.extraClasses[ndx % options.extraClasses.length]); + } + }, + + updateElementIndex = function(elem, prefix, ndx) { + var idRegex = new RegExp('(' + prefix + '-\\d+-)|(^)'), + replacement = prefix + '-' + ndx + '-'; + if (elem.attr("for")) elem.attr("for", elem.attr("for").replace(idRegex, replacement)); + if (elem.attr('id')) elem.attr('id', elem.attr('id').replace(idRegex, replacement)); + if (elem.attr('name')) elem.attr('name', elem.attr('name').replace(idRegex, replacement)); + }, + + hasChildElements = function(row) { + return row.find('input,select,textarea,label').length > 0; + }, + + insertDeleteLink = function(row) { + if (row.is('TR')) { + // If the forms are laid out in table rows, insert + // the remove button into the last table cell: + row.children(':last').append('' + options.deleteText + ''); + } else if (row.is('UL') || row.is('OL')) { + // If they're laid out as an ordered/unordered list, + // insert an
  • after the last list item: + row.append('
  • ' + options.deleteText +'
  • '); + } else { + // Otherwise, just insert the remove button as the + // last child element of the form's container: + row.append('' + options.deleteText +''); + } + row.find('a.' + options.deleteCssClass).click(function() { + var row = $(this).parents('.' + options.formCssClass), + del = row.find('input:hidden[id $= "-DELETE"]'); + if (del.length) { + // We're dealing with an inline formset; rather than remove + // this form from the DOM, we'll mark it as deleted and hide + // it, then let Django handle the deleting: + del.val('on'); + row.hide(); + } else { + row.remove(); + // Update the TOTAL_FORMS form count. + // Also update names and IDs for all remaining form controls so they remain in sequence: + var forms = $('.' + options.formCssClass).not('.formset-custom-template'); + $('#id_' + options.prefix + '-TOTAL_FORMS').val(forms.length); + for (var i=0, formCount=forms.length; i