From 3c60c2b5882f6bc73784251f90a451665ebee45b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 24 Jan 2020 10:20:24 +0100
Subject: [PATCH 1/2] lingo: add labels to TIPI regies (#18320)

---
 combo/apps/lingo/models.py                    | 15 +++++++++----
 .../apps/lingo/templates/lingo/tipi_form.html |  6 +++---
 tests/test_lingo_cells.py                     | 21 ++++++++++++++++---
 3 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py
index 1e86e99..ad33e23 100644
--- a/combo/apps/lingo/models.py
+++ b/combo/apps/lingo/models.py
@@ -19,6 +19,7 @@
 import datetime
 import json
 import logging
+import re
 
 from decimal import Decimal
 
@@ -737,7 +738,11 @@ TIPI_CONTROL_PROCOTOLS = (
 class TipiPaymentFormCell(CellBase):
     title = models.CharField(_('Title'), max_length=150, blank=True)
     url = models.URLField(_('TIPI payment service URL'), default='https://www.tipi.budget.gouv.fr/tpa/paiement.web')
-    regies = models.CharField(_('Regies'), help_text=_('separated by commas'), max_length=256)
+    regies = models.CharField(
+        _('Regies'),
+        help_text=_('Values separated by commas. It is possible to add a label after a regie identifier. '
+                    'Example: "1234 - Regie A,5678 - Regie B"'),
+        max_length=256)
     control_protocol = models.CharField(_('Control protocol'), max_length=8, choices=TIPI_CONTROL_PROCOTOLS,
                                         default='pesv2')
     exer = models.CharField('Exer', max_length=4, blank=True, help_text=_('Default value to be used in form'))
@@ -797,8 +802,10 @@ class TipiPaymentFormCell(CellBase):
                 field['default'] = getattr(self, field['name'])
         context['reference_fields'] = reference_fields
         for regie in self.regies.split(','):
-            regie_id = regie.strip()
-            if not regie_id:
+            regie = regie.strip()
+            id_search = re.search(r'(\d+)', regie)
+            if not id_search:
                 continue
-            context['regies'].append(regie_id)
+            regie_id = id_search.group(1)
+            context['regies'].append((regie_id, regie))
         return extra_context
diff --git a/combo/apps/lingo/templates/lingo/tipi_form.html b/combo/apps/lingo/templates/lingo/tipi_form.html
index 04ae77a..0816b5e 100644
--- a/combo/apps/lingo/templates/lingo/tipi_form.html
+++ b/combo/apps/lingo/templates/lingo/tipi_form.html
@@ -11,13 +11,13 @@
     {% if regies|length > 1 %}
     <p><label>{% trans "Community identifier" %}</label>
       <select id="numcli">
-        {% for id in regies %}
-        <option value="{{ id }}">{{ id }}</option>
+        {% for regie in regies %}
+        <option value="{{ regie.0 }}">{{ regie.1 }}</option>
         {% endfor %}
       </select>
     </p>
     {% else %}
-    <input type="hidden" id="numcli" value="{{ regies.0 }}" />
+    <input type="hidden" id="numcli" value="{{ regies.0.0 }}" />
     {% endif %}
     <ul class="errorlist" id="refdet_error" style="display: none">
       <li>{% trans "invalid reference" %}</li>
diff --git a/tests/test_lingo_cells.py b/tests/test_lingo_cells.py
index 3b5135b..744fecb 100644
--- a/tests/test_lingo_cells.py
+++ b/tests/test_lingo_cells.py
@@ -178,7 +178,7 @@ def test_tipi_cell():
     cell = TipiPaymentFormCell()
     cell.page = page
     cell.title = 'TIPI Payment'
-    cell.regies = "test regie"
+    cell.regies = "1234"
     cell.order = 0
     cell.save()
     assert cell.control_protocol == 'pesv2'
@@ -187,7 +187,7 @@ def test_tipi_cell():
     html = cell.render({})
     assert "<h2>TIPI Payment</h2>" in html
     assert "Community identifier" not in html
-    assert '<input type="hidden" id="numcli" value="test regie" />' in html
+    assert '<input type="hidden" id="numcli" value="1234" />' in html
     assert 'id="exer"' in html
     assert 'id="idpce"' in html
     assert 'id="idligne"' in html
@@ -197,6 +197,16 @@ def test_tipi_cell():
     assert 'data-saisie="M"' in html
     assert 'data-pesv2="True"' in html
 
+    cell.regies = "1234 - test regie"
+    cell.save()
+    html = cell.render({})
+    assert '<input type="hidden" id="numcli" value="1234" />' in html
+
+    cell.regies = "test regie"
+    cell.save()
+    html = cell.render({})
+    assert '<input type="hidden" id="numcli" value="" />' in html
+
     cell.control_protocol = 'rolmre'
     cell.test_mode = True
     cell.save()
@@ -212,11 +222,16 @@ def test_tipi_cell():
     cell_media = str(cell.media)
     assert "js/tipi.js" in cell_media
 
-    cell.regies = 'regie1, regie2'
+    cell.regies = '1 regie1, 2- regie2,3 : regie3,4,5regie5 ,bad-format-regie 6'
     cell.save()
     html = cell.render({})
     assert "Community identifier" in html
     assert '<select id="numcli">' in html
+    assert '<option value="1">1 regie1</option>' in html
+    assert '<option value="2">2- regie2</option>' in html
+    assert '<option value="3">3 : regie3</option>' in html
+    assert '<option value="4">4</option>' in html
+    assert '<option value="5">5regie5</option>' in html
 
     # set reference default values and check they are filled and readonly
     cell.exer = '1234'
-- 
2.20.1

