Projet

Général

Profil

0001-templatetags-add-is_valid_xxx-filters-30849.patch

Nicolas Roche, 25 février 2019 16:21

Télécharger (7,48 ko)

Voir les différences:

Subject: [PATCH] templatetags: add is_valid_xxx filters (#30849)

 tests/test_formdata.py            |  19 +++++
 tests/test_templates.py           |  31 ++++++++
 wcs/qommon/templatetags/qommon.py | 115 ++++++++++++++++++++++++++++++
 3 files changed, 165 insertions(+)
tests/test_formdata.py
1336 1336

  
1337 1337
        tmpl = Template('{{ 4.12|decimal:form_var_arg }}')
1338 1338
        assert tmpl.render(context) == '4.120'
1339

  
1340
def test_lazy_formdata_is_valid_filters(pub, variable_test_data):
1341
    formdef = FormDef.select()[0]
1342
    formdata = formdef.data_class().select()[0]
1343
    for mode in (None, 'lazy'):
1344
        pub.substitutions.reset()
1345
        pub.substitutions.feed(formdef)
1346

  
1347
        nir = '1 78 05 75 114 287 79'
1348
        rib = '2004 1010 1247 3252 4N03 321'
1349
        iban = 'FR77 2004 1010 1247 3252 4N03 321'
1350

  
1351
        with pub.substitutions.temporary_feed(formdata, force_mode=mode):
1352
            tmpl = Template('{{ nir|is_valid_nir }}')
1353
            assert tmpl.render({'nir': nir}) == nir
1354
            tmpl = Template('{{ rib|is_valid_rib }}')
1355
            assert tmpl.render({'rib': rib}) == rib
1356
            tmpl = Template('{{ iban|is_valid_iban }}')
1357
            assert tmpl.render({'iban': iban}) == iban
tests/test_templates.py
323 323
    assert tmpl.render() == 'hello'
324 324
    tmpl = Template('{% if 3|decimal|decimal == 3 %}hello{% endif %}')
325 325
    assert tmpl.render() == 'hello'
326

  
327
def test_is_valid_templatetag():
328
    nir = '1 78 05 75 114 287 79'
329
    rib = '3000 6000 0112 3456 7890 189'
330
    wrong_nir = '1 78 05 75 114 287 78'
331
    wrong_rib = '2004 1010 1247 3352 4N03 321'
332
    wrong_iban = 'FR77 2004 1010 1247 3352 4N03 321'
333

  
334
    ibans = [
335
        'BE71 0961 2345 6769',                # Belgium
336
        'FR76 3000 6000 0112 3456 7890 189',  # France
337
        'DE91 1000 0000 0123 4567 89',        # Germany
338
        'GR96 0810 0010 0000 0123 4567 890',  # Greece
339
        'RO09 BCYP 0000 0012 3456 7890',      # Romania
340
        'SA44 2000 0001 2345 6789 1234',      # Saudi Arabia
341
        'ES79 2100 0813 6101 2345 6789',      # Spain
342
        'CH56 0483 5012 3456 7800 9',         # Switzerland
343
        'GB98 MIDL 0700 9312 3456 78']        # United Kingdom
344

  
345
    tmpl = Template('{{ nir|is_valid_nir }}')
346
    assert tmpl.render({'nir': nir}) == nir
347
    assert tmpl.render({'nir': wrong_nir}) == 'Invalid NIR: %s' % wrong_nir
348

  
349
    tmpl = Template('{{ rib|is_valid_rib }}')
350
    assert tmpl.render({'rib': rib}) == rib
351
    assert tmpl.render({'rib': wrong_rib}) == 'Invalid RIB: %s' % wrong_rib
352

  
353
    tmpl = Template('{{ iban|is_valid_iban }}')
354
    for iban in ibans:
355
        assert tmpl.render({'iban': iban}) == iban
356
    assert tmpl.render({'iban': wrong_iban}) == 'Invalid IBAN: %s' % wrong_iban
wcs/qommon/templatetags/qommon.py
24 24
from wcs.qommon import evalutils
25 25
from wcs.qommon import tokens
26 26
from wcs.qommon.admin.texts import TextsDirectory
27
from wcs.qommon import _
27 28

  
28 29
register = template.Library()
29 30

  
......
227 228
    }
228 229
    token.store()
229 230
    return '---===BUTTON:%s:%s===---' % (token.id, label)
231

  
232
def compute_big_modulo_97(string_number):
233
    '''https://en.wikipedia.org/wiki/International_Bank_Account_Number'''
234
    tmp = string_number[0:9]
235
    string_number = string_number[9:]
236
    modulo = int(tmp) % 97
237

  
238
    while string_number != '':
239
        tmp = "%s%s" % (modulo, string_number[0:9])
240
        string_number = string_number[9:]
241
        modulo = int(tmp) % 97
242

  
243
    return modulo
244

  
245
def char_to_rib_char(car):
246
    if '0' <= car <= '9':
247
        return car
248
    if car >= 'A' and car <= 'I':
249
        return chr(ord(car) - ord('A') + ord('1'))
250
    if car >= 'J' and car <= 'R':
251
        return chr(ord(car) - ord('J') + ord('1'))
252
    if car >= 'S' and car <= 'Z':
253
        return chr(ord(car) - ord('S') + ord('2'))  # yes 2, not an error
254
    error = "unexpected '%c' char for rib number trnaslation" % (car)
255
    raise Exception(error)
256

  
257
def char_to_iban_char(car):
258
    if '0' <= car <= '9':
259
        return car
260
    if car >= 'A' and car <= 'Z':
261
        return str(ord(car) - ord('A') + 10)
262
    error = "unexpected '%c' char for iban number translation" % (car)
263
    raise Exception(error)
264

  
265
def compute_key_rib(bank_code, office_code, account_number):
266
    '''https://fr.wikipedia.org/wiki/Cl%C3%A9_RIB'''
267
    account_number_converted = ''
268
    for car in account_number:
269
        account_number_converted += char_to_rib_char(car)
270

  
271
    account_number_part1 = account_number_converted[0:6]
272
    account_number_part2 = account_number_converted[6:11]
273
    assert account_number_converted == "%s%s" % (account_number_part1, account_number_part2)
274

  
275
    return str(97 - ((89 * int(bank_code)
276
                    + 15 * int(office_code)
277
                    + 76 * int(account_number_part1)
278
                    + 3 * int(account_number_part2)) % 97))
279

  
280
def compute_key_iban(country_code, bban):
281
    '''https://fr.wikipedia.org/wiki/International_Bank_Account_Number'''
282
    dummy_iban = "%s%s00" % (bban, country_code)
283
    dummy_iban_converted = ''
284

  
285
    for car in dummy_iban:
286
        dummy_iban_converted += char_to_iban_char(car)
287

  
288
    return "%02i" % (98 - compute_big_modulo_97(dummy_iban_converted))
289

  
290
def compute_key_nir(sex, year, month, dept, comm, order):
291
    '''https://fr.wikipedia.org/wiki/Num%C3%A9ro_de_s%C3%A9curit%C3%A9_sociale_en_France'''
292
    if dept == '2A':
293
        dept = '19'
294
    if dept == '2B':
295
        dept = 18
296
    return str((97 - int("%s%s%s%s%s%s" % (sex, year, month, dept, comm, order)) % 97))
297

  
298
def parse_identifier_number(value):
299
    if hasattr(value, 'get_value'):
300
        value = value.get_value()  # unlazy
301
    value = value.replace(' ', '')
302
    value = value.replace('-', '')
303
    return value
304

  
305
@register.filter
306
def is_valid_rib(value):
307
    rib = parse_identifier_number(value)
308
    bank_code = rib[0:5]
309
    office_code = rib[5:10]
310
    account_number = rib[10:21]
311
    rib_key = rib[21:23]
312

  
313
    my_rib_key = compute_key_rib(bank_code, office_code, account_number)
314
    if my_rib_key != rib_key:
315
        return _('Invalid RIB: %s') % value
316
    return value
317

  
318
@register.filter
319
def is_valid_iban(value):
320
    iban = parse_identifier_number(value)
321
    country_code = iban[0:2]
322
    iban_key = iban[2:4]
323
    bban = iban[4:27]
324

  
325
    my_iban_key = compute_key_iban(country_code, bban)
326
    if my_iban_key != iban_key:
327
        return _('Invalid IBAN: %s') % value
328
    return value
329

  
330
@register.filter
331
def is_valid_nir(value):
332
    nir = parse_identifier_number(value)
333
    sex = nir[0]
334
    year = nir[1:3]
335
    month = nir[3:5]
336
    dept = nir[5:7]
337
    comm = nir[7:10]
338
    order = nir[10:13]
339
    nir_key = nir[13:15]
340

  
341
    my_nir_key = compute_key_nir(sex, year, month, dept, comm, order)
342
    if my_nir_key != nir_key:
343
        return _('Invalid NIR: %s') % value
344
    return value
230
-