From 5622aaeaedb982f936e1ba8451c0d7bd024325f7 Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Thu, 25 Jul 2019 15:20:41 +0200
Subject: [PATCH] lingo: accept french amount format (#35052)

---
 combo/apps/lingo/models.py  |  4 +---
 combo/apps/lingo/views.py   |  4 ++--
 combo/utils/__init__.py     | 13 +++++++++++++
 tests/test_lingo_payment.py | 11 ++++++++++-
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py
index 0619967..3a11ab8 100644
--- a/combo/apps/lingo/models.py
+++ b/combo/apps/lingo/models.py
@@ -20,8 +20,6 @@ import datetime
 import json
 import logging
 
-from decimal import Decimal
-
 from dateutil import parser
 import eopayment
 from jsonfield import JSONField
@@ -46,7 +44,7 @@ from django.template.loader import render_to_string
 from combo.data.fields import RichTextField
 from combo.data.models import CellBase
 from combo.data.library import register_cell_class
-from combo.utils import NothingInCacheException, aes_hex_encrypt, requests
+from combo.utils import NothingInCacheException, aes_hex_encrypt, requests, Decimal
 from combo.apps.notifications.models import Notification
 
 try:
diff --git a/combo/apps/lingo/views.py b/combo/apps/lingo/views.py
index 4150d79..55a8f98 100644
--- a/combo/apps/lingo/views.py
+++ b/combo/apps/lingo/views.py
@@ -15,7 +15,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from decimal import Decimal, ROUND_HALF_UP
+from decimal import ROUND_HALF_UP
 import json
 import logging
 import requests
@@ -39,7 +39,7 @@ from django.utils.encoding import smart_text
 import eopayment
 
 from combo.data.models import Page
-from combo.utils import check_request_signature, aes_hex_decrypt, DecryptionError
+from combo.utils import check_request_signature, aes_hex_decrypt, DecryptionError, Decimal
 from combo.profile.utils import get_user_from_name_id
 
 from .models import (Regie, BasketItem, Transaction, TransactionOperation,
diff --git a/combo/utils/__init__.py b/combo/utils/__init__.py
index 0bec5db..0998b20 100644
--- a/combo/utils/__init__.py
+++ b/combo/utils/__init__.py
@@ -14,6 +14,10 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+from decimal import Decimal as PythonDecimal
+
+from django.utils import six
+
 # import specific symbols for compatibility
 from .cache import cache_during_request
 from .crypto import aes_hex_decrypt, aes_hex_encrypt, DecryptionError
@@ -21,3 +25,12 @@ from .misc import ellipsize, flatten_context
 from .requests_wrapper import requests, NothingInCacheException
 from .signature import check_query, check_request_signature, sign_url
 from .urls import get_templated_url, TemplateError
+
+
+class Decimal(PythonDecimal):
+
+    def __new__(cls, value="0", *args, **kwargs):
+        if isinstance(value, six.string_types):
+            # replace , by . for French users comfort
+            value = value.replace(',', '.')
+        return super(Decimal, cls).__new__(cls, value, *args, **kwargs)
diff --git a/tests/test_lingo_payment.py b/tests/test_lingo_payment.py
index 902578b..deedf0c 100644
--- a/tests/test_lingo_payment.py
+++ b/tests/test_lingo_payment.py
@@ -236,6 +236,15 @@ def test_add_amount_to_basket(app, key, regie, user):
     assert json.loads(resp.content)['result'] == 'success'
     assert BasketItem.objects.filter(amount=Decimal('81.22')).exists()
 
+    data['amount'] = '11,11'  # french notation
+    data['extra'] = {}
+    url = '%s?email=%s&orig=wcs' % (reverse('api-add-basket-item'), user_email)
+    url = sign_url(url, key)
+    resp = app.post_json(url, params=data)
+    assert resp.status_code == 200
+    assert json.loads(resp.content)['result'] == 'success'
+    assert BasketItem.objects.filter(amount=Decimal('11.11')).exists()
+
     other_regie.is_default = True
     other_regie.save()
     data['amount'] = []
@@ -406,7 +415,7 @@ def test_pay_multiple_regies(app, key, regie, user):
     resp = resp.forms[0].submit()
     assert resp.location.startswith('http://dummy-payment.demo.entrouvert.com/')
     qs = urlparse.parse_qs(urlparse.urlparse(resp.location).query)
-    assert qs['amount'] == ['223.35']
+    assert qs['amount'] == ['234.46']
 
     resp = login(app).get(page.get_online_url())
     resp = resp.forms[1].submit()
-- 
2.20.1

