Projet

Général

Profil

0001-lingo-add-default-values-for-TIPI-reference-fields-2.patch

Serghei Mihai (congés, retour 15/05), 05 décembre 2018 09:50

Télécharger (13,1 ko)

Voir les différences:

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
combo/apps/lingo/forms.py
1
# -*- coding: utf-8 -*-
2
#
3
# lingo - basket and payment system
4
# Copyright (C) 2015  Entr'ouvert
5
#
6
# This program is free software: you can redistribute it and/or modify it
7
# under the terms of the GNU Affero General Public License as published
8
# by the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18

  
19
from django import forms
20
from django.utils.safestring import mark_safe
21

  
22
from .models import TipiPaymentFormCell
23

  
24

  
25
class TipiCellForm(forms.ModelForm):
26
    class Meta:
27
        model = TipiPaymentFormCell
28
        fields = ('title', 'url', 'regies', 'control_protocol',
29
                  'exer', 'idpce', 'idligne', 'rolrec', 'roldeb',
30
                  'roldet', 'test_mode')
31

  
32
    def as_p(self, *args, **kwargs):
33
        rendered = super(TipiCellForm, self).as_p(*args, **kwargs)
34
        prefix = 'c%s' % self.instance.get_reference()
35
        return mark_safe('''%s
36
        <script type="text/javascript">handle_tipi_protocol_fields_display('%s', 'control_protocol');</script>
37
        ''' % (rendered, prefix))
combo/apps/lingo/migrations/0033_auto_20181204_2241.py
1
# -*- coding: utf-8 -*-
2
from __future__ import unicode_literals
3

  
4
from django.db import migrations, models
5

  
6

  
7
class Migration(migrations.Migration):
8

  
9
    dependencies = [
10
        ('lingo', '0032_basketitem_capture_date'),
11
    ]
12

  
13
    operations = [
14
        migrations.AddField(
15
            model_name='tipipaymentformcell',
16
            name='exer',
17
            field=models.CharField(help_text='Default value to be used in form', max_length=4, verbose_name='Exer', blank=True),
18
        ),
19
        migrations.AddField(
20
            model_name='tipipaymentformcell',
21
            name='idligne',
22
            field=models.CharField(help_text='Default value to be used in form', max_length=6, verbose_name='IDLIGNE', blank=True),
23
        ),
24
        migrations.AddField(
25
            model_name='tipipaymentformcell',
26
            name='idpce',
27
            field=models.CharField(help_text='Default value to be used in form', max_length=8, verbose_name='IDPCE', blank=True),
28
        ),
29
        migrations.AddField(
30
            model_name='tipipaymentformcell',
31
            name='roldeb',
32
            field=models.CharField(help_text='Default value to be used in form', max_length=2, verbose_name='ROLDEB', blank=True),
33
        ),
34
        migrations.AddField(
35
            model_name='tipipaymentformcell',
36
            name='roldet',
37
            field=models.CharField(help_text='Default value to be used in form', max_length=13, verbose_name='ROLDET', blank=True),
38
        ),
39
        migrations.AddField(
40
            model_name='tipipaymentformcell',
41
            name='rolrec',
42
            field=models.CharField(help_text='Default value to be used in form', max_length=2, verbose_name='ROLREC', blank=True),
43
        ),
44
    ]
combo/apps/lingo/models.py
712 712
    url = models.URLField(_('TIPI payment service URL'), default='https://www.tipi.budget.gouv.fr/tpa/paiement.web')
713 713
    regies = models.CharField(_('Regies'), help_text=_('separated by commas'), max_length=256)
714 714
    control_protocol = models.CharField(_('Control protocol'), max_length=8, choices=TIPI_CONTROL_PROCOTOLS, default='pesv2')
715
    exer = models.CharField(_('Exer'), max_length=4, blank=True, help_text=_('Default value to be used in form'))
716
    idpce = models.CharField(_('IDPCE'), max_length=8, blank=True, help_text=_('Default value to be used in form'))
717
    idligne = models.CharField(_('IDLIGNE'), max_length=6, blank=True, help_text=_('Default value to be used in form'))
718
    rolrec = models.CharField(_('ROLREC'), max_length=2, blank=True, help_text=_('Default value to be used in form'))
719
    roldeb = models.CharField(_('ROLDEB'), max_length=2, blank=True, help_text=_('Default value to be used in form'))
720
    roldet = models.CharField(_('ROLDET'), max_length=13, blank=True, help_text=_('Default value to be used in form'))
715 721
    test_mode = models.BooleanField(_('Test mode'), default=False)
716 722
    template_name = 'lingo/tipi_form.html'
717 723

  
......
721 727
    class Media:
722 728
        js = ('js/tipi.js',)
723 729

  
730
    def get_default_form_class(self):
731
        from .forms import TipiCellForm
732
        return TipiCellForm
733

  
724 734
    def get_cell_extra_context(self, context):
725 735
        extra_context = super(TipiPaymentFormCell, self).get_cell_extra_context(context)
736
        form_fields = self.get_default_form_class().base_fields
737
        field_definitions = ({'protocol': 'any', 'fields': ['exer']},
738
                  {'protocol': 'pesv2', 'fields': ['idligne', 'idpce']},
739
                  {'protocol': 'rolmre', 'fields': ['rolrec', 'roldeb', 'roldet']}
740
        )
741
        reference_fields = []
742
        for definition in field_definitions:
743
            for field in definition['fields']:
744
                field_pattern = '[0-9]+'
745
                # special pattern for rolrec
746
                if field == 'rolrec':
747
                    field_pattern = '[A-Z0-9]+'
748
                reference_fields.append({'name': field, 'length': form_fields[field].max_length,
749
                                         'placeholder': '0'*form_fields[field].max_length,
750
                                         'pattern': field_pattern,
751
                                         'protocol': definition['protocol']})
726 752
        context['title'] = self.title
727 753
        context['url'] = self.url
728 754
        context['mode'] = 'T' if self.test_mode else 'M'
729
        context['pesv2'] = (self.control_protocol == 'pesv2')
755
        context['control_protocol'] = self.control_protocol
730 756
        context['regies'] = []
757
        for field in reference_fields:
758
            if getattr(self, field['name']):
759
                field['default'] = getattr(self, field['name'])
760
        context['reference_fields'] = reference_fields
731 761
        for regie in self.regies.split(','):
732 762
            regie_id = regie.strip()
733 763
            if not regie_id:
combo/apps/lingo/templates/lingo/tipi_form.html
24 24
    </ul>
25 25
    <p>
26 26
      <label>{% trans "Reference" %}</label>
27
      <input type="text" id="exer" required pattern="[0-9]+" maxlength="4" size="4" placeholder="0000" /> -
28
      {% if pesv2 %}
29
      <input type="text" id="idpce" required pattern="[0-9]+" maxlength="8" size="8" placeholder="00000000" /> -
30
      <input type="text" id="idligne" required pattern="[0-9]+" maxlength="6" size="6" placeholder="000000" />
31
      {% else %}
32
      <input type="text" id="rolrec" required pattern="[A-Z0-9]+" maxlength="2" size="2" placeholder="00" /> -
33
      <input type="text" id="roldeb" required pattern="[0-9]+" maxlength="2" size="2" placeholder="00" /> -
34
      <input type="text" id="roldet" required pattern="[0-9]+" maxlength="13" size="13" placeholder="0000000000000" />
27
      {% regroup reference_fields by protocol as fields %}
28
      {% for field in fields %}
29
      {% for f in field.list %}
30
      {% if field.grouper == control_protocol or field.grouper == 'any' %}
31
      <input type="text" id="{{ f.name }}" required pattern="{{ f.pattern }}" maxlength="{{ f.length }}" size="{{ f.length }}" placeholder="{{ f.placeholder }}" {% if f.default %}value="{{ f.default }}" readonly {% endif %}/>{% if field.grouper == 'any' or not forloop.last %} - {% endif %}
35 32
      {% endif %}
33
      {% endfor %}
34
      {% endfor %}
36 35
    </p>
37 36
    <ul class="errorlist" id="montant_error" style="display: none">
38 37
      <li>{% trans "invalid amount" %}</li>
combo/manager/static/js/combo.manager.js
128 128
  });
129 129
}
130 130

  
131
function handle_tipi_protocol_fields_display(prefix, field)
132
{
133
    var $control_element = $("[name=" + prefix + "-" + field + "]");
134
    function hide_fields(fields) {
135
        fields.forEach(function(f) {
136
            $("[name=" + prefix + "-" + f + "]").parent().hide();
137
        });
138
    }
139

  
140
    function show_fields(fields) {
141
        fields.forEach(function(f) {
142
            $("[name=" + prefix + "-" + f + "]").parent().show();
143
        })
144
    }
145
    $control_element.on('change', function() {
146
        if ($control_element.val() == 'pesv2') {
147
            show_fields(['idpce', 'idligne']);
148
            hide_fields(['rolrec', 'roldeb', 'roldet']);
149
        } else {
150
            show_fields(['rolrec', 'roldeb', 'roldet']);
151
            hide_fields(['idpce', 'idligne']);
152
        }
153
    });
154
    $control_element.trigger('change');
155
}
156

  
131 157
$(function() {
132 158
  $('div.cell-list h3').on('click', function() {
133 159
    $(this).parent().toggleClass('toggled').toggleClass('untoggled');
tests/test_lingo_cells.py
199 199
    html = cell.render({})
200 200
    assert "Community identifier" in html
201 201
    assert '<select id="numcli">' in html
202

  
203
    # set reference default values and check they are filled and readonly
204
    cell.exer = '1234'
205
    cell.rolrec = '00'
206
    cell.save()
207
    html = cell.render({})
208
    assert 'value="1234" readonly' in html
209
    assert 'value="00" readonly' in html
tests/test_lingo_manager.py
10 10

  
11 11
import eopayment
12 12
from combo.data.models import Page
13
from combo.apps.lingo.models import Regie, BasketItem, Transaction, ActiveItems
13
from combo.apps.lingo.models import Regie, BasketItem, Transaction, ActiveItems, TipiPaymentFormCell
14 14
from decimal import Decimal
15 15

  
16 16
pytestmark = pytest.mark.django_db
......
155 155
    assert resp.text.count('<tr') == 0
156 156
    assert 'No transactions found matching' in resp.text
157 157

  
158
def test_configure_tipi_cell(app, admin_user):
159
    page = Page(title='tipi', slug='tipi', template_name='standard')
160
    page.save()
161

  
162
    app = login(app)
163
    resp = app.get('/manage/pages/%s/' % page.id, status=200)
164

  
165
    cell = TipiPaymentFormCell(title='Test payment', page=page, placeholder='content', order=0)
166
    cell.save()
167
    resp = app.get('/manage/pages/%s/' % page.id, status=200)
168
    assert resp.text.count('Exer:') == 1
169
    assert resp.text.count('IDPCE:') == 1
170
    assert resp.text.count('IDLIGNE:') == 1
171
    assert resp.text.count('ROLREC:') == 1
172
    assert resp.text.count('ROLDEB:') == 1
173
    assert resp.text.count('ROLDET:') == 1
174
    assert resp.text.count('handle_tipi_protocol_fields_display') == 1
175

  
158 176
def test_configure_invoices_cell(app, admin_user):
159 177
    page = Page(title='xxx', slug='test', template_name='standard')
160 178
    page.save()
......
165 183
    # 2 occurences of tipi payment form should be present, and no other cells
166 184
    assert resp.text.count('lingo') == 2
167 185
    assert resp.text.count('lingo_tipipaymentformcell') == 2
186
    assert resp.text.count('lingo_tipipaymentformcell') == 2
168 187

  
169 188
    regie = Regie()
170 189
    regie.label = 'Test'
171
-