[damned-lies] Show errors in module branch edit form



commit 3ea3dddb63f6d8395cb067709522e2ebe0a70459
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue Sep 15 09:02:22 2015 +0200

    Show errors in module branch edit form

 stats/forms.py                      |   11 +++++++++++
 stats/views.py                      |    9 ++++-----
 templates/module_edit_branches.html |    8 ++++++--
 3 files changed, 21 insertions(+), 7 deletions(-)
---
diff --git a/stats/forms.py b/stats/forms.py
index 3ea710d..873fce2 100644
--- a/stats/forms.py
+++ b/stats/forms.py
@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
 from django import forms
+from django.utils.translation import ugettext as _
+
 from stats.models import Branch, Category, CategoryName, Release
 
 
@@ -25,6 +27,7 @@ class ModuleBranchForm(forms.Form):
                                                             initial=cat.release.pk)
                     self.fields[str(cat.id)+'_cat'] = forms.ModelChoiceField(
                         queryset=CategoryName.objects.all(),
+                        required=False,
                         initial=cat.name
                     )
                     self.branch_fields.append((str(cat.id), str(cat.id)+'_cat'))
@@ -35,6 +38,7 @@ class ModuleBranchForm(forms.Form):
                                                         label=branch.name)
                 self.fields[branch.name + '_cat'] = forms.ModelChoiceField(
                     queryset=CategoryName.objects.all(),
+                    required=False,
                     initial=default_cat_name
                 )
                 self.branch_fields.append((branch.name, branch.name+'_cat'))
@@ -52,6 +56,13 @@ class ModuleBranchForm(forms.Form):
 
     def clean(self):
         cleaned_data = super(ModuleBranchForm, self).clean()
+        for field_name in cleaned_data.keys():
+            if (field_name.endswith('_cat') and cleaned_data[field_name] is None
+                    and cleaned_data[field_name[:-4]] is not None):
+                self.add_error(
+                    field_name,
+                    forms.ValidationError(_("You have to provide a category when a version is specified."))
+                )
         if cleaned_data['new_branch']:
             try:
                 branch = Branch.objects.get(module=self.module, name=cleaned_data['new_branch'])
diff --git a/stats/views.py b/stats/views.py
index 2a2bed3..6785e6c 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -98,16 +98,15 @@ def module_edit_branches(request, module_name):
                     Category.objects.get(pk=key).delete()
                     updated = True
                     continue
-                release_has_changed = field.initial != getattr(form.cleaned_data[key], 'pk', None)
-                category_has_changed = form.fields[key+'_cat'].initial != form.cleaned_data[key+'_cat']
+                release_has_changed = key in form.changed_data
+                category_has_changed = (key + '_cat') in form.changed_data
                 if form.cleaned_data[key] and (release_has_changed or category_has_changed):
                     if field.initial:
                         old_release = Release.objects.get(pk=field.initial)
                         cat = Category.objects.get(pk=key)
                         if release_has_changed:
-                            new_release = form.cleaned_data[key]
-                            cat.release = new_release
-                        cat.name = form.cleaned_data[key+'_cat']
+                            cat.release = form.cleaned_data[key]
+                        cat.name = form.cleaned_data[key + '_cat']
                     else:
                         rel = Release.objects.get(pk=form.cleaned_data[key].pk)
                         branch = Branch.objects.get(module=mod, name=key)
diff --git a/templates/module_edit_branches.html b/templates/module_edit_branches.html
index b941884..287cc12 100644
--- a/templates/module_edit_branches.html
+++ b/templates/module_edit_branches.html
@@ -17,10 +17,14 @@
 <table>
 <tr><td><h4>{% trans "Branch" %}</h4></td><td><h4>{% trans "Release" %}</h4></td><td><h4>{% trans "Category" 
%}</h4></td></tr>
 {% for field1, field2 in form.get_branches %}
-  <tr><th>{{ field1.label }}</th><td>{{ field1 }}</td><td>{{ field2 }}</td></tr>
+  <tr><th>{{ field1.label }}</th><td>{{ field1.errors }}{{ field1 }}</td><td>{{ field2.errors }}{{ field2 
}}</td></tr>
 {% endfor %}
 
-  <tr><td>{{ form.new_branch }}</td><td>{{ form.new_branch_release }}</td><td>{{ form.new_branch_category 
}}</td></tr>
+  <tr>
+      <td>{{ form.new_branch.errors }}{{ form.new_branch }}</td>
+      <td>{{ form.new_branch_release.errors }}{{ form.new_branch_release }}</td>
+      <td>{{ form.new_branch_category.errors }}{{ form.new_branch_category }}</td>
+  </tr>
   <tr><td colspan="3" align="right"><input type="submit" value="{% trans "Save" %}" /></td></tr>
 </table>
 </form>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]