Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#19078 closed New feature (wontfix)

Polish IBAN field

Reported by: bartosak@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: polish IBAN
Cc: Łukasz Rekucki Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I would like to add polish IBAN account number field

from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError, RegexField
from django.utils.translation import ugettext_lazy as _

class PLIBANField(RegexField):
    """
    Polish International Bank Account Number (IBAN) field

    For Polish IBAN validation algorithm see http://pl.wikipedia.org/wiki/International_Bank_Account_Number
    """
    default_error_messages = {
        'invalid': _('Enter a valid IBAN in PLXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX format'),
    }

    def __init__(self, max_length=40, min_length=28, *args, **kwargs):
        super(PLIBANField, self).__init__(r'^[0-9A-Za-z\-\s]{28,40}$',
                max_length, min_length, *args, **kwargs)

    def clean(self, value):
        """
        Strips - and spaces, performs country code and checksum validation
        """
        value = super(PLIBANField, self).clean(value)
        if value in EMPTY_VALUES:
            return u''
        value = value.replace('-','')
        value = value.replace(' ','')
        value = value.upper()
        if value[0:2] != 'PL':
            raise ValidationError(self.error_messages[_('Unknown country code')])
        numeric_format = ''
        for char in value[4:] + value[0:4]:
            if char.isalpha():
                numeric_format += str(ord(char) - 55)
            else:
                numeric_format += char
        if int(numeric_format) % 97 != 1:
            raise ValidationError(self.error_messages[_('Invalid checksum')])
        return value

Change History (3)

comment:1 Changed 4 years ago by Wojciech Bartosiak

comment:2 Changed 4 years ago by Łukasz Rekucki

Cc: Łukasz Rekucki added
Resolution: wontfix
Status: newclosed

Hi,

Thanks for your contribution, but localflavor packages have been recently split into seperate repositiories on Github and their developement will continue there.

Please post this issue on https://github.com/django/django-localflavor-pl. Thanks :)

Either way, for the change to be accepted it needs to come in a form of a patch (or a github pull request), needs to contain tests and (in case of new features like) documentation.

comment:3 Changed 4 years ago by Ben Kornrath

Hi,

For anybody who's following this, you don't need a specific country validation for IBAN. There's a generic validation algorithm all valid IBAN countries which I implemented in this package:

https://github.com/benkonrath/django-iban

I hope this is useful for anybody looking to use IBANs in Django.

Note: See TracTickets for help on using tickets.
Back to Top