Ticket #10203: Ni-localflavor-3.diff

File Ni-localflavor-3.diff, 11.6 KB (added by fitoria, 16 years ago)

patch number 3 for Nicaragua Localflavor

  • django/contrib/localflavor/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
     8The numbers are used officialy in goverment institutions.
     9
     10This exists in this standalone file so that it's only imported into memory
     11when explicitly needed.
     12"""
     13from django.utils.translation import ugettext_lazy as _
     14
     15DEPARTAMENT_CHOICES = (
     16    (50, _(u'Boaco')),
     17    (75, _(u'Carazo')),
     18    (30, _(u'Chinandega')),
     19    (65, _(u'Chontales')),
     20    (25, _(u'Estelí')),
     21    (70, _(u'Granada')),
     22    (10, _(u'Jinotega')),
     23    (35, _(u'León')),
     24    (20, _(u'Madriz')),
     25    (55, _(u'Managua')),
     26    (60, _(u'Masaya')),
     27    (40, _(u'Matagalpa')),
     28    (5, _(u'Nueva Segovia')),
     29    (80, _(u'Rivas')),
     30    (85, _(u'Río San Juan')),
     31    (91, _(u'Región Autónoma del Atlántico Norte(RAAN)')),
     32    (93, _(u'Región Autónoma del Atlántico Sur(RAAS)')),
     33)
     34
  • django/contrib/localflavor/ni/forms.py

     
     1"""
     2Nicaragua-specific Form helpers
     3"""
     4
     5from django.forms import ValidationError
     6from django.forms.fields import  RegexField, Select, 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 have 8 digits number starting on April 1st so I added 8 digits.
     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'^[ 0-9\-]+$',
     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        if value in EMPTY_VALUES:
     31            return u''
     32
     33        v = super(NIPhoneNumberField, self).clean(value)
     34        v = v.replace('-', '')
     35        v = v.replace(' ', '')
     36        if (len(v) == 8):
     37            return v
     38        raise ValidationError(self.default_error_messages['invalid'])
     39   
     40class NIDepartamentSelect(Select):
     41    """
     42    A Select widget that uses a list of Nicaraguan departaments.
     43    """
     44    def __init__(self, attrs = None):
     45        from django.contrib.localflavor.ni.ni_departaments import DEPARTAMENT_CHOICES
     46        super(NIDepartamentSelect, self).__init__(attrs, choices=DEPARTAMENT_CHOICES)
     47
     48class NICedulaNumberField(RegexField):
     49    """
     50    Cedula number(cedula is the identification document to
     51    vote and make legal stuff in Nicaragua.
     52   
     53    The format is 999-999999-9999A where 9 is a number and A is a letter.
     54    """
     55    default_error_messages = {
     56        'invalid': _('Enter a valid Cedula number in XXX-XXXXXX-XXXXX format.'),
     57    }
     58   
     59    def __init__(self, *args, **kwargs):
     60        super(NICedulaNumberField, self).__init__(r'^(\d{3})-(\d{6})-(\d{4}[a-zA-z])$',
     61                max_length = None, min_length = None, *args, **kwargs)
     62
     63
     64    def clean(self, value):
     65        value = super(NICedulaNumberField, self).clean(value)
     66        if value in EMPTY_VALUES:
     67            return u''
     68        else:
     69            return value
  • tests/regressiontests/forms/localflavor/ni.py

     
     1# -*- coding: utf-8 -*-
     2# Test for the contrib/localflavor/ NI form fields
     3
     4tests = """
     5#NIPhoneNumberField ########################
     6
     7A form field that validates Nicaraguan Phone Number validation.
     8>>> from django.contrib.localflavor.ni.forms import NIPhoneNumberField
     9>>> phone = NIPhoneNumberField()
     10>>> phone.clean('98567489')
     11u'98567489'               
     12>>> phone.clean('9856-7489')
     13u'98567489'                 
     14>>> phone.clean('98-56-74-89')
     15u'98567489'   
     16>>> phone.clean('2344 5678')
     17u'23445678'
     18>>> phone.clean('2344 567 8')
     19u'23445678'
     20>>> phone.clean('9856-489')
     21Traceback (most recent call last):
     22  File "<console>", line 1, in <module>
     23  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 37, in clean
     24    raise ValidationError(self.default_error_messages['invalid'])                                 
     25ValidationError: [u'Phone numbers must be in the format  XXX-XXXX or XXXXXXX.']                   
     26>>> phone.clean('9856K489')                                                                       
     27Traceback (most recent call last):                                                               
     28  File "<console>", line 1, in <module>
     29  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 33, in clean
     30    v = super(NIPhoneNumberField, self).clean(value)
     31  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     32    raise ValidationError(self.error_messages['invalid'])
     33ValidationError: [u'Phone numbers must be in the format  XXX-XXXX or XXXXXXX.']
     34>>> phone.clean('9856*489')
     35Traceback (most recent call last):
     36  File "<console>", line 1, in <module>
     37  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 33, in clean
     38    v = super(NIPhoneNumberField, self).clean(value)
     39  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     40    raise ValidationError(self.error_messages['invalid'])
     41ValidationError: [u'Phone numbers must be in the format  XXX-XXXX or XXXXXXX.']
     42>>> phone.clean('')
     43u''
     44>>> phone.clean(None)
     45u''
     46
     47
     48#NICedulaNumberField ##########
     49
     50Personal identication number in Nicaragua. In format 999-999999-9999A where 9 is a number an A a letter
     51
     52>>> from django.contrib.localflavor.ni.forms import NICedulaNumberField
     53>>> c = NICedulaNumberField()                                         
     54>>> c.clean('001-090934-0002A')
     55u'001-090934-0002A'           
     56>>> c.clean('001-090934-00023')
     57Traceback (most recent call last):
     58  File "<stdin>", line 1, in <module>
     59  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     60    value = super(NICedulaNumberField, self).clean(value)                                         
     61  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean               
     62    raise ValidationError(self.error_messages['invalid'])                                         
     63django.forms.util.ValidationError>>> c.clean('001-0909A4-0002A')                                 
     64Traceback (most recent call last):                                                               
     65  File "<stdin>", line 1, in <module>                                                           
     66  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     67    value = super(NICedulaNumberField, self).clean(value)                                         
     68  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean               
     69    raise ValidationError(self.error_messages['invalid'])                                         
     70django.forms.util.ValidationError>>> c.clean('001090994-0002A')                                 
     71Traceback (most recent call last):
     72  File "<stdin>", line 1, in <module>
     73  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     74    value = super(NICedulaNumberField, self).clean(value)
     75  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     76    raise ValidationError(self.error_messages['invalid'])
     77django.forms.util.ValidationError
     78>>> c.clean('0a1-090934-0002A')
     79Traceback (most recent call last):
     80  File "<stdin>", line 1, in <module>
     81  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     82    value = super(NICedulaNumberField, self).clean(value)
     83  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     84    raise ValidationError(self.error_messages['invalid'])
     85django.forms.util.ValidationError
     86>>> c.clean('001-090994-0002a')
     87u'001-090994-0002a'
     88
     89# NIDepartamentSelect #############
     90
     91A choice field with the 15 departaments and 2 autonomic regions in Nicaragua.
     92>>> from django.contrib.localflavor.ni.forms import NIDepartamentSelect
     93>>> d = NIDepartamentSelect()
     94>>> print d.render('departaments', 'managua')                                                                                               
     95<select name="departaments">                                                                                                               
     96<option value="50">Boaco</option>                                                                                                           
     97<option value="75">Carazo</option>                                                                                                         
     98<option value="30">Chinandega</option>                                                                                                     
     99<option value="65">Chontales</option>                                                                                                       
     100<option value="25">Estelí</option>
     101<option value="70">Granada</option>
     102<option value="10">Jinotega</option>
     103<option value="35">León</option>
     104<option value="20">Madriz</option>
     105<option value="55">Managua</option>
     106<option value="60">Masaya</option>
     107<option value="40">Matagalpa</option>
     108<option value="5">Nueva Segovia</option>
     109<option value="80">Rivas</option>
     110<option value="85">Río San Juan</option>
     111<option value="91">Región Autónoma del Atlántico Norte(RAAN)</option>
     112<option value="93">Región Autónoma del Atlántico Sur(RAAS)</option>
     113</select>
     114 No newline at end of file
  • docs/ref/contrib/localflavor.txt

     
    5353    * Japan_
    5454    * Mexico_
    5555    * `The Netherlands`_
     56    * Nicaragua_
    5657    * Norway_
    5758    * Peru_
    5859    * Poland_
     
    9293.. _Italy: `Italy (it)`_
    9394.. _Japan: `Japan (jp)`_
    9495.. _Mexico: `Mexico (mx)`_
     96.. _Nicaragua: `Nicaragua (ni)`_
    9597.. _Norway: `Norway (no)`_
    9698.. _Peru: `Peru (pe)`_
    9799.. _Poland: `Poland (pl)`_
     
    390392
    391393    A ``Select`` widget that uses a list of Mexican states as its choices.
    392394
     395Nicaragua (``ni``)
     396===============
     397
     398.. class:: ni.forms.NIDepartamentSelect
     399
     400    A ``ChoiceField`` that uses a displays of Nicaraguan departaments and regions.
     401
     402.. class:: ni.forms.NICedulaNumberField
     403
     404    A form field that validates input as a Nicaraguan Cedula number
     405    (nicaraguan oficial identification document).
     406
     407.. class:: ni.forms.NIPhoneNumberField
     408
     409    A form field that validates input as a Nicaraguan Telephone number. Seven digits
     410    with or without a hypen.
     411
     412
    393413Norway (``no``)
    394414===============
    395415
Back to Top