Ticket #10203: NI-localflavor.diff

File NI-localflavor.diff, 3.5 KB (added by fitoria, 11 years ago)

Nicaragua local flavor patch.

  • ni/ni_departaments.py

     
     1# -*- coding: utf-8 -*-
     2
     3"""
     4An alphabetical list of departaments for use as `choices`
     5in a formfield.,
     6
     7Source: http://es.wikipedia.org/wiki/Departamentos_de_Nicaragua
     8
     9This exists in this standalone file so that it's only imported into memory
     10when explicitly needed.
     11"""
     12from django.utils.translation import ugettext_lazy as _
     13
     14DEPARTAMENT_CHOICES = (
     15    ('BO', _(u'Boaco')),
     16    ('CZ', _(u'Carazo')),
     17    ('CH', _(u'Chinandega')),
     18    ('CT', _(u'Chontales')),
     19    ('ES', _(u'Estelí')),
     20    ('GR', _(u'Granada')),
     21    ('JI', _(u'Jinotega')),
     22    ('LE', _(u'León')),
     23    ('MZ', _(u'Madriz')),
     24    ('M', _(u'Managua')),
     25    ('MY', _(u'Masaya')),
     26    ('MT', _(u'Matagalpa')),
     27    ('NS', _(u'Nueva Segovia')),
     28    ('RI', _(u'Rivas')),
     29    ('RS', _(u'Río San Juan')),
     30    ('RAAN', _(u'Región Autónoma del Atlántico Norte(RAAN)')),
     31    ('RAAS', _(u'Región Autónoma del Atlántico Sur(RAAS)')),
     32)
     33
  • ni/forms.py

     
     1"""
     2Nicaragua-specific Form helpers
     3"""
     4
     5from django.forms import ValidationError
     6from django.forms.fields import  RegexField, ChoiceField, EMPTY_VALUES
     7from django.forms.util import smart_unicode
     8from django.utils.translation import ugettext_lazy as _
     9import re
     10
     11
     12class 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   
     33class 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
     41class 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
Back to Top