Projet

Général

Profil

0001-template-filter-for-Luhn-algorithm-35013.patch

Benjamin Dauvergne, 24 juillet 2019 06:27

Télécharger (3,15 ko)

Voir les différences:

Subject: [PATCH] template filter for Luhn algorithm (#35013)

 tests/test_templates.py           | 22 +++++++++++++++++++++
 wcs/qommon/templatetags/qommon.py | 32 +++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
tests/test_templates.py
522 522
    lazy_formdata = LazyFormData(MockFormData())
523 523
    tmpl = Template('{% with form_geoloc_base|reproj:"EPSG:3946" as c %}{{c.0}}/{{c.1}}{% endwith %}')
524 524
    assert tmpl.render(CompatibilityNamesDict({'form': lazy_formdata})) == '1625337.15483/5422836.71627'
525

  
526
def test_luhn():
527
    # EO Siret
528
    tmpl = Template('{% if  "44317013900036"|is_valid_siret%}yes{% endif %}')
529
    assert tmpl.render() == 'yes'
530
    # with spaces
531
    tmpl = Template('{% if  " 443 170\t139 00 036 "|is_valid_siret %}yes{% endif %}')
532
    assert tmpl.render() == 'yes'
533
    for i in range(10):
534
        if i == 6:
535
            continue
536
        tmpl = Template('{% if  "4431701390003' + str(i) + '"|is_valid_siret %}yes{% endif %}')
537
        assert tmpl.render() == ''
538

  
539
    # EO Siren
540
    tmpl = Template('{% if  "443170139"|is_valid_siren %}yes{% endif %}')
541
    assert tmpl.render() == 'yes'
542
    for i in range(10):
543
        if i == 9:
544
            continue
545
        tmpl = Template('{% if  "44317013' + str(i) + '"|is_valid_siren %}yes{% endif %}')
546
        assert tmpl.render() == ''
wcs/qommon/templatetags/qommon.py
20 20
from decimal import DivisionByZero as DecimalDivisionByZero
21 21
import hashlib
22 22
import math
23
import re
23 24
import string
24 25
import random
25 26

  
......
348 349
    proj = pyproj.Proj(init='EPSG:4326')
349 350
    target_proj = pyproj.Proj(init=projection_name)
350 351
    return pyproj.transform(proj, target_proj, coords['lon'], coords['lat'])
352

  
353
def is_valid_luhn(value):
354
    '''Verify Luhn checksum on a string representing a number'''
355
    if not value:
356
        return False
357
    string_value = unicode(value)
358
    string_value = re.sub(r'\s', '', string_value)
359
    if not string_value.isdigit():
360
        return False
361

  
362
    # take all digits couting from the right, double value for digits pair
363
    # index (counting from 1), if double has 2 digits take their sum
364
    checksum = 0
365
    for i, x in enumerate(reversed(string_value)):
366
        if i % 2 == 0:
367
            checksum += int(x)
368
        else:
369
            checksum += sum(int(y) for y in str(2 * int(x)))
370
    return checksum % 10 == 0
371

  
372
@register.filter
373
def is_valid_siren(value):
374
    return is_valid_luhn(value)
375

  
376
@register.filter
377
def is_valid_siret(value):
378
    return is_valid_luhn(value)
379

  
380
@register.filter
381
def is_valid_cb(value):
382
    return is_valid_luhn(value)
351
-