summaryrefslogtreecommitdiff
path: root/ishtar
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2011-01-06 18:11:41 +0100
committerÉtienne Loks <etienne.loks@peacefrogs.net>2011-01-06 18:11:41 +0100
commit000359b99c3a39be870fe744df295e3e39db9d7f (patch)
treed728d9b3b4aba2b25ca6e13697802492d16f535e /ishtar
parentadbfac4fe875b013c2613dddadd0c1c59e0cc660 (diff)
downloadIshtar-000359b99c3a39be870fe744df295e3e39db9d7f.tar.bz2
Ishtar-000359b99c3a39be870fe744df295e3e39db9d7f.zip
Prevent duplicate towns (refs #14)
Diffstat (limited to 'ishtar')
-rw-r--r--ishtar/furnitures/forms.py22
-rw-r--r--ishtar/templates/default_wizard.html3
2 files changed, 22 insertions, 3 deletions
diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py
index e771fe595..12c9ae9c0 100644
--- a/ishtar/furnitures/forms.py
+++ b/ishtar/furnitures/forms.py
@@ -26,7 +26,7 @@ from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from django.template import Context, RequestContext
from django.shortcuts import render_to_response
-from django.forms.formsets import formset_factory
+from django.forms.formsets import formset_factory, BaseFormSet
from django import forms
from formwizard.forms import NamedUrlSessionFormWizard
@@ -43,6 +43,9 @@ class FinalForm(forms.Form):
final = True
form_label = _("Confirm")
+class FormSet(BaseFormSet):
+ pass
+
class Wizard(NamedUrlSessionFormWizard):
def get_template(self, request, storage):
templates = ['default_wizard.html']
@@ -230,7 +233,22 @@ class TownForm(forms.Form):
'autocomplete-town', associated_model=models.Town),
validators=[models.Town.valid_id], required=False)
-TownFormSet = formset_factory(TownForm, can_delete=True)
+class TownFormSet(FormSet):
+ def clean(self):
+ """Checks that no towns are duplicated."""
+ if any(self.errors):
+ return
+ towns = []
+ for i in range(0, self.total_form_count()):
+ form = self.forms[i]
+ if 'town' not in form.cleaned_data:
+ continue
+ town = form.cleaned_data['town']
+ if town in towns:
+ raise forms.ValidationError, _("There are identical towns.")
+ towns.append(town)
+
+TownFormSet = formset_factory(TownForm, can_delete=True, formset=TownFormSet)
TownFormSet.form_label = _("Towns")
class FileForm3(forms.Form):
diff --git a/ishtar/templates/default_wizard.html b/ishtar/templates/default_wizard.html
index 59389719e..83f02539b 100644
--- a/ishtar/templates/default_wizard.html
+++ b/ishtar/templates/default_wizard.html
@@ -12,11 +12,12 @@
<div class='form'>
{% if form.forms %}
<table class='formset'>
+ {%if form.non_form_errors%}<tr class='error'><th colspan='2'>{{form.non_form_errors}}</th></tr>{%endif%}
{{ form.management_form }}
{% for formsetform in form.forms %}
{{ formsetform.as_table }}
{% endfor %}
- <tr><td rowspan="2"><button name="formset_modify" value="{{form_step}}">{% trans "Modify" %}</button></td></tr></li>
+ <tr class='modify'><td colspan="2"><button name="formset_modify" value="{{form_step}}">{% trans "Modify" %}</button></td></tr></li>
</table>
{% else %}
<table>