From 9f2d71dd2e5ec2f6afb60390359b4d0997b43a24 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Sun, 25 Nov 2018 20:09:02 +0100 Subject: [PATCH 2/2] lingo: add default values for TIPI reference fields (#26057) Make fields readonly if default value defined. --- ...aymentformcell_default_reference_values.py | 20 +++++++++++++++++++ combo/apps/lingo/models.py | 9 ++++++++- .../apps/lingo/templates/lingo/tipi_form.html | 2 +- tests/test_lingo_cells.py | 7 +++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 combo/apps/lingo/migrations/0033_tipipaymentformcell_default_reference_values.py diff --git a/combo/apps/lingo/migrations/0033_tipipaymentformcell_default_reference_values.py b/combo/apps/lingo/migrations/0033_tipipaymentformcell_default_reference_values.py new file mode 100644 index 00000000..1e868d23 --- /dev/null +++ b/combo/apps/lingo/migrations/0033_tipipaymentformcell_default_reference_values.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import jsonfield.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('lingo', '0032_basketitem_capture_date'), + ] + + operations = [ + migrations.AddField( + model_name='tipipaymentformcell', + name='default_reference_values', + field=jsonfield.fields.JSONField(default=dict, help_text='Example: {"exer": "1234", "idligne": "345678"}', verbose_name="Default reference fields values", blank=True), + ), + ] diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py index f2eb8f3f..fa95bfa9 100644 --- a/combo/apps/lingo/models.py +++ b/combo/apps/lingo/models.py @@ -712,6 +712,8 @@ class TipiPaymentFormCell(CellBase): 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) control_protocol = models.CharField(_('Control protocol'), max_length=8, choices=TIPI_CONTROL_PROCOTOLS, default='pesv2') + default_reference_values = JSONField(blank=True, verbose_name=_('Default reference fields values'), + help_text=_('Example: {"exer": "1234", "idligne": "345678"}')) test_mode = models.BooleanField(_('Test mode'), default=False) template_name = 'lingo/tipi_form.html' @@ -723,7 +725,7 @@ class TipiPaymentFormCell(CellBase): def get_cell_extra_context(self, context): extra_context = super(TipiPaymentFormCell, self).get_cell_extra_context(context) - context['reference_fields'] = ( + reference_fields = ( {'name': 'exer', 'pattern': '[0-9]+', 'length': '4', 'placeholder': '0'*4, 'protocol': 'any'}, {'name': 'idpce', 'pattern': '[0-9]+', 'length': '8', 'placeholder': '0'*8, 'protocol': 'pesv2'}, {'name': 'idligne', 'pattern': '[0-9]+', 'length': '6', 'placeholder': '0'*6, 'protocol': 'pesv2'}, @@ -737,6 +739,11 @@ class TipiPaymentFormCell(CellBase): context['pesv2'] = (self.control_protocol == 'pesv2') context['control_protocol'] = self.control_protocol context['regies'] = [] + if self.default_reference_values: + for field in reference_fields: + if field['name'] in self.default_reference_values: + field['default'] = self.default_reference_values[field['name']] + context['reference_fields'] = reference_fields for regie in self.regies.split(','): regie_id = regie.strip() if not regie_id: diff --git a/combo/apps/lingo/templates/lingo/tipi_form.html b/combo/apps/lingo/templates/lingo/tipi_form.html index 39b3c480..d0f87bc4 100644 --- a/combo/apps/lingo/templates/lingo/tipi_form.html +++ b/combo/apps/lingo/templates/lingo/tipi_form.html @@ -28,7 +28,7 @@ {% for field in fields %} {% for f in field.list %} {% if field.grouper == control_protocol or field.grouper == 'any' %} - {% if field.grouper == 'any' or not forloop.last %} - {% endif %} + {% if field.grouper == 'any' or not forloop.last %} - {% endif %} {% endif %} {% endfor %} {% endfor %} diff --git a/tests/test_lingo_cells.py b/tests/test_lingo_cells.py index 9f525b22..28fb3412 100644 --- a/tests/test_lingo_cells.py +++ b/tests/test_lingo_cells.py @@ -199,3 +199,10 @@ def test_tipi_cell(): html = cell.render({}) assert "Community identifier" in html assert '