Ticket #7023: pesel-validation.diff
File pesel-validation.diff, 6.2 KB (added by , 17 years ago) |
---|
-
tests/regressiontests/forms/localflavor/pl.py
47 47 # PLNationalIdentificationNumberField ############################################ 48 48 49 49 >>> from django.contrib.localflavor.pl.forms import PLNationalIdentificationNumberField 50 >>> f = PLNationalIdentificationNumberField(birthday_check = False) 51 >>> f.clean('20220412160') 52 u'20220412160' 53 >>> f.gender 54 'F' 50 55 >>> f = PLNationalIdentificationNumberField() 51 56 >>> f.clean('80071610614') 52 57 u'80071610614' 58 >>> f.gender 59 'M' 60 >>> f.birthday 61 datetime.date(1980, 7, 16) 62 >>> f.clean('84070712131') 63 u'84070712131' 64 >>> f.gender 65 'M' 66 >>> f.birthday 67 datetime.date(1984, 7, 7) 53 68 >>> f.clean('80071610610') 54 69 Traceback (most recent call last): 55 70 ... … … 62 77 Traceback (most recent call last): 63 78 ... 64 79 ValidationError: [u'National Identification Number consists of 11 digits.'] 80 >>> f.clean('20220412153') 81 Traceback (most recent call last): 82 ... 83 ValidationError: [u'Invalid National Identification Number (birthdate from the future).'] 84 >>> f.clean('20333412154') 85 Traceback (most recent call last): 86 ... 87 ValidationError: [u'Invalid National Identification Number (birthdate is not valid).'] 65 88 66 89 # PLNationalBusinessRegisterField ################################################ 67 90 -
django/contrib/localflavor/pl/forms.py
2 2 Polish-specific form helpers 3 3 """ 4 4 5 from datetime import date 5 6 import re 6 7 7 8 from django.newforms import ValidationError … … 32 33 Checks the following rules: 33 34 * the length consist of 11 digits 34 35 * has a valid checksum 36 * birthday is valid 37 * birthday is from the past 35 38 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 36 43 The algorithm is documented at http://en.wikipedia.org/wiki/PESEL. 37 44 """ 38 45 default_error_messages = { 39 46 'invalid': _(u'National Identification Number consists of 11 digits.'), 40 47 '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).'), 41 50 } 42 51 43 def __init__(self, *args, **kwargs):52 def __init__(self, birthday_check = True, *args, **kwargs): 44 53 super(PLNationalIdentificationNumberField, self).__init__(r'^\d{11}$', 45 54 max_length=None, min_length=None, *args, **kwargs) 55 self.birthday_check = birthday_check 46 56 47 57 def clean(self,value): 48 58 super(PLNationalIdentificationNumberField, self).clean(value) 49 59 if not self.has_valid_checksum(value): 50 60 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 51 78 return u'%s' % value 52 79 53 80 def has_valid_checksum(self, number): -
django/conf/locale/pl/LC_MESSAGES/django.po
5 5 msgstr "" 6 6 "Project-Id-Version: Django\n" 7 7 "Report-Msgid-Bugs-To: \n" 8 "POT-Creation-Date: 2008-04-1 4 18:02+0200\n"8 "POT-Creation-Date: 2008-04-15 04:15+0200\n" 9 9 "PO-Revision-Date: 2008-02-25 15:53+0100\n" 10 10 "Last-Translator: Piotr Lewandowski <django@icomputing.pl>\n" 11 11 "Language-Team: Polish <pl@li.org>\n" … … 2707 2707 msgid "This field requires 11 digits." 2708 2708 msgstr "To pole musi zawierać 11 cyfr." 2709 2709 2710 #: contrib/localflavor/pl/forms.py: 392710 #: contrib/localflavor/pl/forms.py:46 2711 2711 msgid "National Identification Number consists of 11 digits." 2712 2712 msgstr "Numer PESEL składa się z 11 cyfr." 2713 2713 2714 #: contrib/localflavor/pl/forms.py:4 02714 #: contrib/localflavor/pl/forms.py:47 2715 2715 msgid "Wrong checksum for the National Identification Number." 2716 2716 msgstr "Błędna suma kontrolna numeru PESEL." 2717 2717 2718 #: contrib/localflavor/pl/forms.py:72 2718 #: contrib/localflavor/pl/forms.py:48 2719 msgid "Invalid National Identification Number (birthdate is not valid)." 2720 msgstr "Nieprawidłowy numer PESEL (data urodzin nie jest prawidłowa)." 2721 2722 #: contrib/localflavor/pl/forms.py:49 2723 msgid "Invalid National Identification Number (birthdate from the future)." 2724 msgstr "Nieprawidłowy Numer PESEL (przyszła data urodzin)." 2725 2726 #: contrib/localflavor/pl/forms.py:99 2719 2727 msgid "" 2720 2728 "Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX." 2721 2729 msgstr "Wpisz numer NIP w formacie XXX-XXX-XX-XX or XX-XX-XXX-XXX." 2722 2730 2723 #: contrib/localflavor/pl/forms.py: 732731 #: contrib/localflavor/pl/forms.py:100 2724 2732 msgid "Wrong checksum for the Tax Number (NIP)." 2725 2733 msgstr "Błędna suma kontrolna numeru NIP" 2726 2734 2727 #: contrib/localflavor/pl/forms.py:1 122735 #: contrib/localflavor/pl/forms.py:139 2728 2736 msgid "National Business Register Number (REGON) consists of 7 or 9 digits." 2729 2737 msgstr "Numer REGON składa się z 7 lub 9 cyfr." 2730 2738 2731 #: contrib/localflavor/pl/forms.py:1 132739 #: contrib/localflavor/pl/forms.py:140 2732 2740 msgid "Wrong checksum for the National Business Register Number (REGON)." 2733 2741 msgstr "Błędna suma kontrolna numeru REGON" 2734 2742 2735 #: contrib/localflavor/pl/forms.py:1 562743 #: contrib/localflavor/pl/forms.py:183 2736 2744 msgid "Enter a postal code in the format XX-XXX." 2737 2745 msgstr "Wpisz kod pocztowy w formacie XX-XXX." 2738 2746