|
1 """ |
|
2 Chile specific form helpers. |
|
3 """ |
|
4 |
|
5 from django.core.validators import EMPTY_VALUES |
|
6 from django.forms import ValidationError |
|
7 from django.forms.fields import RegexField, Select |
|
8 from django.utils.translation import ugettext_lazy as _ |
|
9 from django.utils.encoding import smart_unicode |
|
10 |
|
11 |
|
12 class CLRegionSelect(Select): |
|
13 """ |
|
14 A Select widget that uses a list of Chilean Regions (Regiones) |
|
15 as its choices. |
|
16 """ |
|
17 def __init__(self, attrs=None): |
|
18 from cl_regions import REGION_CHOICES |
|
19 super(CLRegionSelect, self).__init__(attrs, choices=REGION_CHOICES) |
|
20 |
|
21 class CLRutField(RegexField): |
|
22 """ |
|
23 Chilean "Rol Unico Tributario" (RUT) field. This is the Chilean national |
|
24 identification number. |
|
25 |
|
26 Samples for testing are available from |
|
27 https://palena.sii.cl/cvc/dte/ee_empresas_emisoras.html |
|
28 """ |
|
29 default_error_messages = { |
|
30 'invalid': _('Enter a valid Chilean RUT.'), |
|
31 'strict': _('Enter a valid Chilean RUT. The format is XX.XXX.XXX-X.'), |
|
32 'checksum': _('The Chilean RUT is not valid.'), |
|
33 } |
|
34 |
|
35 def __init__(self, *args, **kwargs): |
|
36 if 'strict' in kwargs: |
|
37 del kwargs['strict'] |
|
38 super(CLRutField, self).__init__(r'^(\d{1,2}\.)?\d{3}\.\d{3}-[\dkK]$', |
|
39 error_message=self.default_error_messages['strict'], *args, **kwargs) |
|
40 else: |
|
41 # In non-strict mode, accept RUTs that validate but do not exist in |
|
42 # the real world. |
|
43 super(CLRutField, self).__init__(r'^[\d\.]{1,11}-?[\dkK]$', *args, **kwargs) |
|
44 |
|
45 def clean(self, value): |
|
46 """ |
|
47 Check and clean the Chilean RUT. |
|
48 """ |
|
49 super(CLRutField, self).clean(value) |
|
50 if value in EMPTY_VALUES: |
|
51 return u'' |
|
52 rut, verificador = self._canonify(value) |
|
53 if self._algorithm(rut) == verificador: |
|
54 return self._format(rut, verificador) |
|
55 else: |
|
56 raise ValidationError(self.error_messages['checksum']) |
|
57 |
|
58 def _algorithm(self, rut): |
|
59 """ |
|
60 Takes RUT in pure canonical form, calculates the verifier digit. |
|
61 """ |
|
62 suma = 0 |
|
63 multi = 2 |
|
64 for r in rut[::-1]: |
|
65 suma += int(r) * multi |
|
66 multi += 1 |
|
67 if multi == 8: |
|
68 multi = 2 |
|
69 return u'0123456789K0'[11 - suma % 11] |
|
70 |
|
71 def _canonify(self, rut): |
|
72 """ |
|
73 Turns the RUT into one normalized format. Returns a (rut, verifier) |
|
74 tuple. |
|
75 """ |
|
76 rut = smart_unicode(rut).replace(' ', '').replace('.', '').replace('-', '') |
|
77 return rut[:-1], rut[-1] |
|
78 |
|
79 def _format(self, code, verifier=None): |
|
80 """ |
|
81 Formats the RUT from canonical form to the common string representation. |
|
82 If verifier=None, then the last digit in 'code' is the verifier. |
|
83 """ |
|
84 if verifier is None: |
|
85 verifier = code[-1] |
|
86 code = code[:-1] |
|
87 while len(code) > 3 and '.' not in code[:3]: |
|
88 pos = code.find('.') |
|
89 if pos == -1: |
|
90 new_dot = -3 |
|
91 else: |
|
92 new_dot = pos - 3 |
|
93 code = code[:new_dot] + '.' + code[new_dot:] |
|
94 return u'%s-%s' % (code, verifier) |
|
95 |