From 72104e93ffc2b166d29b372debba1de908820f9f Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Sun, 25 Nov 2018 20:09:25 +0100 Subject: [PATCH] lingo: add default values for TIPI reference fields (#26057) Make fields readonly if default value defined. --- combo/apps/lingo/forms.py | 37 ++++++++++++++++ .../migrations/0033_auto_20181204_2241.py | 44 +++++++++++++++++++ combo/apps/lingo/models.py | 32 +++++++++++++- .../apps/lingo/templates/lingo/tipi_form.html | 15 +++---- combo/manager/static/js/combo.manager.js | 26 +++++++++++ tests/test_lingo_cells.py | 8 ++++ tests/test_lingo_manager.py | 21 ++++++++- 7 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 combo/apps/lingo/forms.py create mode 100644 combo/apps/lingo/migrations/0033_auto_20181204_2241.py diff --git a/combo/apps/lingo/forms.py b/combo/apps/lingo/forms.py new file mode 100644 index 00000000..5a212fad --- /dev/null +++ b/combo/apps/lingo/forms.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# +# lingo - basket and payment system +# Copyright (C) 2015 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from django import forms +from django.utils.safestring import mark_safe + +from .models import TipiPaymentFormCell + + +class TipiCellForm(forms.ModelForm): + class Meta: + model = TipiPaymentFormCell + fields = ('title', 'url', 'regies', 'control_protocol', + 'exer', 'idpce', 'idligne', 'rolrec', 'roldeb', + 'roldet', 'test_mode') + + def as_p(self, *args, **kwargs): + rendered = super(TipiCellForm, self).as_p(*args, **kwargs) + prefix = 'c%s' % self.instance.get_reference() + return mark_safe('''%s + + ''' % (rendered, prefix)) diff --git a/combo/apps/lingo/migrations/0033_auto_20181204_2241.py b/combo/apps/lingo/migrations/0033_auto_20181204_2241.py new file mode 100644 index 00000000..9326248d --- /dev/null +++ b/combo/apps/lingo/migrations/0033_auto_20181204_2241.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('lingo', '0032_basketitem_capture_date'), + ] + + operations = [ + migrations.AddField( + model_name='tipipaymentformcell', + name='exer', + field=models.CharField(help_text='Default value to be used in form', max_length=4, verbose_name='Exer', blank=True), + ), + migrations.AddField( + model_name='tipipaymentformcell', + name='idligne', + field=models.CharField(help_text='Default value to be used in form', max_length=6, verbose_name='IDLIGNE', blank=True), + ), + migrations.AddField( + model_name='tipipaymentformcell', + name='idpce', + field=models.CharField(help_text='Default value to be used in form', max_length=8, verbose_name='IDPCE', blank=True), + ), + migrations.AddField( + model_name='tipipaymentformcell', + name='roldeb', + field=models.CharField(help_text='Default value to be used in form', max_length=2, verbose_name='ROLDEB', blank=True), + ), + migrations.AddField( + model_name='tipipaymentformcell', + name='roldet', + field=models.CharField(help_text='Default value to be used in form', max_length=13, verbose_name='ROLDET', blank=True), + ), + migrations.AddField( + model_name='tipipaymentformcell', + name='rolrec', + field=models.CharField(help_text='Default value to be used in form', max_length=2, verbose_name='ROLREC', blank=True), + ), + ] diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py index 5cdf143c..1b849762 100644 --- a/combo/apps/lingo/models.py +++ b/combo/apps/lingo/models.py @@ -712,6 +712,12 @@ 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') + exer = models.CharField(_('Exer'), max_length=4, blank=True, help_text=_('Default value to be used in form')) + idpce = models.CharField(_('IDPCE'), max_length=8, blank=True, help_text=_('Default value to be used in form')) + idligne = models.CharField(_('IDLIGNE'), max_length=6, blank=True, help_text=_('Default value to be used in form')) + rolrec = models.CharField(_('ROLREC'), max_length=2, blank=True, help_text=_('Default value to be used in form')) + roldeb = models.CharField(_('ROLDEB'), max_length=2, blank=True, help_text=_('Default value to be used in form')) + roldet = models.CharField(_('ROLDET'), max_length=13, blank=True, help_text=_('Default value to be used in form')) test_mode = models.BooleanField(_('Test mode'), default=False) template_name = 'lingo/tipi_form.html' @@ -721,13 +727,37 @@ class TipiPaymentFormCell(CellBase): class Media: js = ('js/tipi.js',) + def get_default_form_class(self): + from .forms import TipiCellForm + return TipiCellForm + def get_cell_extra_context(self, context): extra_context = super(TipiPaymentFormCell, self).get_cell_extra_context(context) + form_fields = self.get_default_form_class().base_fields + field_definitions = ({'protocol': 'any', 'fields': ['exer']}, + {'protocol': 'pesv2', 'fields': ['idligne', 'idpce']}, + {'protocol': 'rolmre', 'fields': ['rolrec', 'roldeb', 'roldet']} + ) + reference_fields = [] + for definition in field_definitions: + for field in definition['fields']: + field_pattern = '[0-9]+' + # special pattern for rolrec + if field == 'rolrec': + field_pattern = '[A-Z0-9]+' + reference_fields.append({'name': field, 'length': form_fields[field].max_length, + 'placeholder': '0'*form_fields[field].max_length, + 'pattern': field_pattern, + 'protocol': definition['protocol']}) context['title'] = self.title context['url'] = self.url context['mode'] = 'T' if self.test_mode else 'M' - context['pesv2'] = (self.control_protocol == 'pesv2') + context['control_protocol'] = self.control_protocol context['regies'] = [] + for field in reference_fields: + if getattr(self, field['name']): + 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: diff --git a/combo/apps/lingo/templates/lingo/tipi_form.html b/combo/apps/lingo/templates/lingo/tipi_form.html index 98dfc575..d0f87bc4 100644 --- a/combo/apps/lingo/templates/lingo/tipi_form.html +++ b/combo/apps/lingo/templates/lingo/tipi_form.html @@ -24,15 +24,14 @@

- - - {% if pesv2 %} - - - - {% else %} - - - - - + {% regroup reference_fields by protocol as fields %} + {% 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 %} {% endif %} + {% endfor %} + {% endfor %}