From 94ab8fbcc35969f7b659bd1e7810074cd7f51fb4 Mon Sep 17 00:00:00 2001 From: "A.Avenel" Date: Wed, 2 Nov 2011 14:35:38 +0100 Subject: [PATCH 2/4] Part of a fix to #62 : french numbers should use commas rather than dots --- budget/forms.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/budget/forms.py b/budget/forms.py index 7342762..713bcfb 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -45,6 +45,11 @@ def get_billform_for(project, set_default=True, **kwargs): form.set_default() return form +class CommaDecimalField(DecimalField): + """A class to deal with comma in Decimal Field""" + def process_formdata(self, value): + value[0] = str(value[0]).replace(',', '.') + return super(CommaDecimalField, self).process_formdata(value) class EditProjectForm(Form): @@ -102,7 +107,7 @@ class BillForm(Form): date = DateField(_("Date"), validators=[Required()], default=datetime.now) what = TextField(_("What?"), validators=[Required()]) payer = SelectField(_("Payer"), validators=[Required()], coerce=int) - amount = DecimalField(_("Amount paid"), validators=[Required()]) + amount = CommaDecimalField(_("Amount paid"), validators=[Required()]) payed_for = SelectMultipleField(_("For whom?"), validators=[Required()], widget=select_multi_checkbox, coerce=int) submit = SubmitField(_("Send the bill")) @@ -129,7 +134,9 @@ class BillForm(Form): def validate_amount(self, field): if field.data < 0: - raise ValidationError(_("Bills can't be negative")) + field.data = abs(field.data) + elif field.data == 0: + raise ValidationError(_("Bills can't be null")) class MemberForm(Form): From 6df3bcdf1385a17e9d3d3dff6ba9278cb4e57247 Mon Sep 17 00:00:00 2001 From: "A.Avenel" Date: Wed, 2 Nov 2011 15:26:35 +0100 Subject: [PATCH 3/4] Oops, forgot to commit tests --- budget/tests.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/budget/tests.py b/budget/tests.py index 3f261fa..de6317b 100644 --- a/budget/tests.py +++ b/budget/tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import os import tempfile -import unittest +import unittest2 as unittest import base64 import json @@ -283,15 +283,6 @@ class BudgetTestCase(TestCase): bill = models.Bill.query.one() self.assertEqual(bill.amount, 25) - self.app.post("/raclette/add", data={ - 'date': '2011-08-10', - 'what': u'fromage à raclette', - 'payer': members_ids[0], - 'payed_for': members_ids, - 'amount': '-25', # bill with a negative value is not possible - }) - self.assertEqual(1, models.Bill.query.count()) - # edit the bill resp = self.app.post("/raclette/edit/%s" % bill.id, data={ 'date': '2011-08-10', @@ -336,6 +327,28 @@ class BudgetTestCase(TestCase): balance = models.Project.query.get("raclette").balance self.assertEqual(set(balance.values()), set([19.0, -19.0])) + #Bill with negative amount + self.app.post("/raclette/add", data={ + 'date': '2011-08-12', + 'what': u'fromage à raclette', + 'payer': members_ids[0], + 'payed_for': members_ids, + 'amount': '-25', # bill with a negative value should be converted to a positive value + }) + bill = models.Bill.query.filter(models.Bill.date=='2011-08-12')[0] + self.assertEqual(bill.amount, 25) + + #add a bill with a comma + self.app.post("/raclette/add", data={ + 'date': '2011-08-01', + 'what': u'fromage à raclette', + 'payer': members_ids[0], + 'payed_for': members_ids, + 'amount': '25,02', + }) + bill = models.Bill.query.filter(models.Bill.date=='2011-08-01')[0] + self.assertEqual(bill.amount, 25.02) + def test_rounding(self): self.post_project("raclette") From e20c3bdb6bea10d56e72d903952875da53cabd90 Mon Sep 17 00:00:00 2001 From: "A.Avenel" Date: Thu, 3 Nov 2011 13:33:11 +0100 Subject: [PATCH 4/4] Small fix in CommaDecimalField class --- budget/forms.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/budget/forms.py b/budget/forms.py index 8db90be..f5c3fb5 100644 --- a/budget/forms.py +++ b/budget/forms.py @@ -48,7 +48,8 @@ def get_billform_for(project, set_default=True, **kwargs): class CommaDecimalField(DecimalField): """A class to deal with comma in Decimal Field""" def process_formdata(self, value): - value[0] = str(value[0]).replace(',', '.') + if value: + value[0] = str(value[0]).replace(',', '.') return super(CommaDecimalField, self).process_formdata(value)