| 1 | """ |
| 2 | Nicaragua-specific Form helpers |
| 3 | """ |
| 4 | |
| 5 | from django.forms import ValidationError |
| 6 | from django.forms.fields import RegexField, ChoiceField, EMPTY_VALUES |
| 7 | from django.forms.util import smart_unicode |
| 8 | from django.utils.translation import ugettext_lazy as _ |
| 9 | import re |
| 10 | |
| 11 | |
| 12 | class NIPhoneNumberField(RegexField): |
| 13 | """ |
| 14 | Nicaraguan phone number field. |
| 15 | NOTE: Nicaragua will add another digit from April 1st 2009. |
| 16 | """ |
| 17 | default_error_messages = { |
| 18 | 'invalid': u'Phone numbers must be in the format XXX-XXXX or XXXXXXX.', |
| 19 | } |
| 20 | |
| 21 | def __init__(self, *args, **kwargs): |
| 22 | super(NIPhoneNumberField, self).__init__(r'^\d{3}-\d{4}|^\d{7}$', |
| 23 | max_length = None, min_length = None, *args, **kwargs) |
| 24 | |
| 25 | def clean(self, value): |
| 26 | """ |
| 27 | Validates the input and returns a string with only numbers. |
| 28 | Returns an empty string for empty values |
| 29 | """ |
| 30 | v = super(NIPhoneNumberField, self).clean(value) |
| 31 | return v.replace('-', '') |
| 32 | |
| 33 | class NIDepartamentSelect(ChoiceField): |
| 34 | """ |
| 35 | A Select widget that uses a list of Nicaraguan departaments. |
| 36 | """ |
| 37 | def __init__(self): |
| 38 | from django.contrib.localflavor.ni.ni_departaments import DEPARTAMENT_CHOICES # relative import |
| 39 | super(NIDepartamentSelect, self).__init__(choices=DEPARTAMENT_CHOICES) |
| 40 | |
| 41 | class NICedulaNumberField(RegexField): |
| 42 | """ |
| 43 | Cedula number(cedula is the identification document to |
| 44 | vote and make legal stuff in Nicaragua. |
| 45 | |
| 46 | The format is 999-999999-9999A where 9 is a number and A is a letter. |
| 47 | """ |
| 48 | default_error_messages = { |
| 49 | 'invalid': _('Enter a valid Cedula number in XXX-XXXXXX-XXXXX format.'), |
| 50 | } |
| 51 | |
| 52 | def __init__(self, *args, **kwargs): |
| 53 | super(NICedulaNumberField, self).__init__(r'^(\d{3})-(\d{6})-(\d{4}[a-zA-z])$', |
| 54 | max_length = None, min_length = None, *args, **kwargs) |
| 55 | |
| 56 | |
| 57 | def clean(self, value): |
| 58 | value = super(NICedulaNumberField, self).clean(value) |
| 59 | if value in EMPTY_VALUES: |
| 60 | return u'' |
| 61 | else: |
| 62 | return value |