| 37 | | def has_valid_checksum(self, number): |
|---|
| 38 | | """ |
|---|
| 39 | | Calculates a checksum with the provided algorithm. |
|---|
| 40 | | """ |
|---|
| 41 | | multiple_table = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1) |
|---|
| 42 | | result = 0 |
|---|
| 43 | | for i in range(len(number)): |
|---|
| 44 | | result += int(number[i])*multiple_table[i] |
|---|
| 45 | | |
|---|
| 46 | | if result % 10 == 0: |
|---|
| 47 | | return True |
|---|
| 48 | | else: |
|---|
| 49 | | return False |
|---|
| 50 | | |
|---|
| | 50 | def has_valid_checksum(self, number): |
|---|
| | 51 | """ |
|---|
| | 52 | Calculates a checksum with the provided algorithm. |
|---|
| | 53 | """ |
|---|
| | 54 | multiple_table = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1) |
|---|
| | 55 | result = 0 |
|---|
| | 56 | for i in range(len(number)): |
|---|
| | 57 | result += int(number[i]) * multiple_table[i] |
|---|
| | 58 | return result % 10 == 0 |
|---|
| | 74 | def clean(self,value): |
|---|
| | 75 | super(PLTaxNumberField, self).clean(value) |
|---|
| | 76 | value = re.sub("[-]", "", value) |
|---|
| | 77 | if not self.has_valid_checksum(value): |
|---|
| | 78 | raise ValidationError(_(u'Wrong checksum for the Tax Number (NIP).')) |
|---|
| | 79 | return u'%s' % value |
|---|
| | 80 | |
|---|
| | 81 | def has_valid_checksum(self, number): |
|---|
| | 82 | """ |
|---|
| | 83 | Calculates a checksum with the provided algorithm. |
|---|
| | 84 | """ |
|---|
| | 85 | multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7) |
|---|
| | 86 | result = 0 |
|---|
| | 87 | for i in range(len(number)-1): |
|---|
| | 88 | result += int(number[i]) * multiple_table[i] |
|---|
| | 89 | |
|---|
| | 90 | result %= 11 |
|---|
| | 91 | if result == int(number[-1]): |
|---|
| | 92 | return True |
|---|
| | 93 | else: |
|---|
| | 94 | return False |
|---|
| | 95 | |
|---|
| | 96 | class PLNationalBusinessRegisterField(RegexField): |
|---|
| | 97 | """ |
|---|
| | 98 | A form field that validated as Polish National Official Business Register Number (REGON) |
|---|
| | 99 | Valid forms are: 7 or 9 digits number |
|---|
| | 100 | |
|---|
| | 101 | More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm |
|---|
| | 102 | |
|---|
| | 103 | The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html |
|---|
| | 104 | """ |
|---|
| | 105 | def __init__(self, *args, **kwargs): |
|---|
| | 106 | super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$', |
|---|
| | 107 | max_length=None, min_length=None, error_message=_(u'National Business Register Number (REGON) consists of 7 or 9 digits.'), |
|---|
| | 108 | *args, **kwargs) |
|---|
| | 109 | |
|---|
| | 110 | def clean(self,value): |
|---|
| | 111 | super(PLNationalBusinessRegisterField, self).clean(value) |
|---|
| | 112 | if not self.has_valid_checksum(value): |
|---|
| | 113 | raise ValidationError(_(u'Wrong checksum for the National Business Register Number (REGON).')) |
|---|
| | 114 | return u'%s' % value |
|---|
| | 115 | |
|---|
| | 116 | def has_valid_checksum(self, number): |
|---|
| | 117 | """ |
|---|
| | 118 | Calculates a checksum with the provided algorithm. |
|---|
| | 119 | """ |
|---|
| | 120 | multiple_table_7 = (2, 3, 4, 5, 6, 7) |
|---|
| | 121 | multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7) |
|---|
| | 122 | result = 0 |
|---|
| | 123 | |
|---|
| | 124 | if len(number) == 7: |
|---|
| | 125 | multiple_table = multiple_table_7 |
|---|
| | 126 | else: |
|---|
| | 127 | multiple_table = multiple_table_9 |
|---|
| | 128 | |
|---|
| | 129 | for i in range(len(number)-1): |
|---|
| | 130 | result += int(number[i]) * multiple_table[i] |
|---|
| | 131 | |
|---|
| | 132 | result %= 11 |
|---|
| | 133 | if result == 10: |
|---|
| | 134 | result = 0 |
|---|
| | 135 | if result == int(number[-1]): |
|---|
| | 136 | return True |
|---|
| | 137 | else: |
|---|
| | 138 | return False |
|---|