Ticket #7023: pesel-validation.diff

File pesel-validation.diff, 6.2 KB (added by Piotr Lewandowski <django@…>, 17 years ago)
  • tests/regressiontests/forms/localflavor/pl.py

     
    4747# PLNationalIdentificationNumberField ############################################
    4848
    4949>>> from django.contrib.localflavor.pl.forms import PLNationalIdentificationNumberField
     50>>> f =  PLNationalIdentificationNumberField(birthday_check = False)
     51>>> f.clean('20220412160')
     52u'20220412160'
     53>>> f.gender
     54'F'
    5055>>> f =  PLNationalIdentificationNumberField()
    5156>>> f.clean('80071610614')
    5257u'80071610614'
     58>>> f.gender
     59'M'
     60>>> f.birthday
     61datetime.date(1980, 7, 16)
     62>>> f.clean('84070712131')
     63u'84070712131'
     64>>> f.gender
     65'M'
     66>>> f.birthday
     67datetime.date(1984, 7, 7)
    5368>>> f.clean('80071610610')
    5469Traceback (most recent call last):
    5570...
     
    6277Traceback (most recent call last):
    6378...
    6479ValidationError: [u'National Identification Number consists of 11 digits.']
     80>>> f.clean('20220412153')
     81Traceback (most recent call last):
     82...
     83ValidationError: [u'Invalid National Identification Number (birthdate from the future).']
     84>>> f.clean('20333412154')
     85Traceback (most recent call last):
     86...
     87ValidationError: [u'Invalid National Identification Number (birthdate is not valid).']
    6588
    6689# PLNationalBusinessRegisterField ################################################
    6790
  • django/contrib/localflavor/pl/forms.py

     
    22Polish-specific form helpers
    33"""
    44
     5from datetime import date
    56import re
    67
    78from django.newforms import ValidationError
     
    3233    Checks the following rules:
    3334        * the length consist of 11 digits
    3435        * has a valid checksum
     36        * birthday is valid
     37        * birthday is from the past
    3538
     39    Calculates additional attributes for provided PESEL number:
     40        * self.gender ('F' or 'M'),
     41        * self.birthday (datetime.date) - if self.birthday_check is True.
     42
    3643    The algorithm is documented at http://en.wikipedia.org/wiki/PESEL.
    3744    """
    3845    default_error_messages = {
    3946        'invalid': _(u'National Identification Number consists of 11 digits.'),
    4047        'checksum': _(u'Wrong checksum for the National Identification Number.'),
     48        'invalid-birthday': _(u'Invalid National Identification Number (birthdate is not valid).'),
     49        'future-birthday': _(u'Invalid National Identification Number (birthdate from the future).'),
    4150    }
    4251
    43     def __init__(self, *args, **kwargs):
     52    def __init__(self, birthday_check = True, *args, **kwargs):
    4453        super(PLNationalIdentificationNumberField, self).__init__(r'^\d{11}$',
    4554            max_length=None, min_length=None, *args, **kwargs)
     55        self.birthday_check = birthday_check
    4656
    4757    def clean(self,value):
    4858        super(PLNationalIdentificationNumberField, self).clean(value)
    4959        if not self.has_valid_checksum(value):
    5060            raise ValidationError(self.error_messages['checksum'])
     61
     62        if int(value[9]) % 2:
     63            self.gender = 'M'
     64        else:
     65            self.gender = 'F'
     66
     67        if self.birthday_check:
     68            day = int(value[4:6])
     69            offset, month = divmod(int(value[2:4]), 20)
     70            year = 1900 + int(value[0:2]) + 100 * ((offset + 1) % 5 - 1)
     71            try:
     72                self.birthday = date(year, month, day)
     73            except ValueError:
     74                raise ValidationError(self.error_messages['invalid-birthday'])
     75            if self.birthday > date.today():
     76                raise ValidationError(self.error_messages['future-birthday'])
     77
    5178        return u'%s' % value
    5279
    5380    def has_valid_checksum(self, number):
  • django/conf/locale/pl/LC_MESSAGES/django.po

     
    55msgstr ""
    66"Project-Id-Version: Django\n"
    77"Report-Msgid-Bugs-To: \n"
    8 "POT-Creation-Date: 2008-04-14 18:02+0200\n"
     8"POT-Creation-Date: 2008-04-15 04:15+0200\n"
    99"PO-Revision-Date: 2008-02-25 15:53+0100\n"
    1010"Last-Translator: Piotr Lewandowski <django@icomputing.pl>\n"
    1111"Language-Team: Polish <pl@li.org>\n"
     
    27072707msgid "This field requires 11 digits."
    27082708msgstr "To pole musi zawierać 11 cyfr."
    27092709
    2710 #: contrib/localflavor/pl/forms.py:39
     2710#: contrib/localflavor/pl/forms.py:46
    27112711msgid "National Identification Number consists of 11 digits."
    27122712msgstr "Numer PESEL składa się z 11 cyfr."
    27132713
    2714 #: contrib/localflavor/pl/forms.py:40
     2714#: contrib/localflavor/pl/forms.py:47
    27152715msgid "Wrong checksum for the National Identification Number."
    27162716msgstr "Błędna suma kontrolna numeru PESEL."
    27172717
    2718 #: contrib/localflavor/pl/forms.py:72
     2718#: contrib/localflavor/pl/forms.py:48
     2719msgid "Invalid National Identification Number (birthdate is not valid)."
     2720msgstr "Nieprawidłowy numer PESEL (data urodzin nie jest prawidłowa)."
     2721
     2722#: contrib/localflavor/pl/forms.py:49
     2723msgid "Invalid National Identification Number (birthdate from the future)."
     2724msgstr "Nieprawidłowy Numer PESEL (przyszła data urodzin)."
     2725
     2726#: contrib/localflavor/pl/forms.py:99
    27192727msgid ""
    27202728"Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX."
    27212729msgstr "Wpisz numer NIP w formacie XXX-XXX-XX-XX or XX-XX-XXX-XXX."
    27222730
    2723 #: contrib/localflavor/pl/forms.py:73
     2731#: contrib/localflavor/pl/forms.py:100
    27242732msgid "Wrong checksum for the Tax Number (NIP)."
    27252733msgstr "Błędna suma kontrolna numeru NIP"
    27262734
    2727 #: contrib/localflavor/pl/forms.py:112
     2735#: contrib/localflavor/pl/forms.py:139
    27282736msgid "National Business Register Number (REGON) consists of 7 or 9 digits."
    27292737msgstr "Numer REGON składa się z 7 lub 9 cyfr."
    27302738
    2731 #: contrib/localflavor/pl/forms.py:113
     2739#: contrib/localflavor/pl/forms.py:140
    27322740msgid "Wrong checksum for the National Business Register Number (REGON)."
    27332741msgstr "Błędna suma kontrolna numeru REGON"
    27342742
    2735 #: contrib/localflavor/pl/forms.py:156
     2743#: contrib/localflavor/pl/forms.py:183
    27362744msgid "Enter a postal code in the format XX-XXX."
    27372745msgstr "Wpisz kod pocztowy w formacie XX-XXX."
    27382746
Back to Top