Ticket #10203: Ni-localflavor-2.diff

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

Second Patch including documentation and test.

  • 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
     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
  • django/contrib/localflavor/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
  • 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('233-3023')
     11u'2333023'                 
     12>>> phone.clean('2333023')
     13u'2333023'               
     14>>> phone.clean('2333-023')
     15Traceback (most recent call last):
     16  File "<stdin>", line 1, in <module>
     17  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 30, in clean
     18    v = super(NIPhoneNumberField, self).clean(value)                                               
     19  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean               
     20    raise ValidationError(self.error_messages['invalid'])                                         
     21django.forms.util.ValidationError: [u'Phone numbers must be in the format  XXX-XXXX or XXXXXXX.'] 
     22>>> phone.clean('a33-3023')                                                                     
     23Traceback (most recent call last):                                                               
     24  File "<stdin>", line 1, in <module>                                                           
     25  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 30, in clean
     26    v = super(NIPhoneNumberField, self).clean(value)                                               
     27  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean               
     28    raise ValidationError(self.error_messages['invalid'])                                         
     29django.forms.util.ValidationError: [u'Phone numbers must be in the format  XXX-XXXX or XXXXXXX.'] 
     30>>> phone.clean('23334023')                                                                     
     31Traceback (most recent call last):                                                             
     32  File "<stdin>", line 1, in <module>                                                           
     33  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 30, in clean
     34    v = super(NIPhoneNumberField, self).clean(value)                                               
     35  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     36    raise ValidationError(self.error_messages['invalid'])
     37django.forms.util.ValidationError: [u'Phone numbers must be in the format  XXX-XXXX or XXXXXXX.']
     38>>> phone.clean('')
     39Traceback (most recent call last):
     40  File "<stdin>", line 1, in <module>
     41  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 30, in clean
     42    v = super(NIPhoneNumberField, self).clean(value)
     43  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 414, in clean
     44    value = super(RegexField, self).clean(value)
     45  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 148, in clean
     46    super(CharField, self).clean(value)
     47  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 119, in clean
     48    raise ValidationError(self.error_messages['required'])
     49django.forms.util.ValidationError>>>
     50>>> phone = NIPhoneNumberField(required = False)
     51>>> phone.clean('')
     52u''
     53>>> phone.clean(None)
     54u''
     55
     56
     57#NICedulaNumberField ##########
     58
     59Personal identication number in Nicaragua. In format 999-999999-9999A where 9 is a number an A a letter
     60
     61>>> from django.contrib.localflavor.ni.forms import NICedulaNumberField
     62>>> c = NICedulaNumberField()                                         
     63>>> c.clean('001-090934-0002A')
     64u'001-090934-0002A'           
     65>>> c.clean('001-090934-00023')
     66Traceback (most recent call last):
     67  File "<stdin>", line 1, in <module>
     68  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     69    value = super(NICedulaNumberField, self).clean(value)                                         
     70  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean               
     71    raise ValidationError(self.error_messages['invalid'])                                         
     72django.forms.util.ValidationError>>> c.clean('001-0909A4-0002A')                                 
     73Traceback (most recent call last):                                                               
     74  File "<stdin>", line 1, in <module>                                                           
     75  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     76    value = super(NICedulaNumberField, self).clean(value)                                         
     77  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean               
     78    raise ValidationError(self.error_messages['invalid'])                                         
     79django.forms.util.ValidationError>>> c.clean('001090994-0002A')                                 
     80Traceback (most recent call last):
     81  File "<stdin>", line 1, in <module>
     82  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     83    value = super(NICedulaNumberField, self).clean(value)
     84  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     85    raise ValidationError(self.error_messages['invalid'])
     86django.forms.util.ValidationError
     87>>> c.clean('0a1-090934-0002A')
     88Traceback (most recent call last):
     89  File "<stdin>", line 1, in <module>
     90  File "/usr/lib/python2.5/site-packages/django/contrib/localflavor/ni/forms.py", line 58, in clean
     91    value = super(NICedulaNumberField, self).clean(value)
     92  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 418, in clean
     93    raise ValidationError(self.error_messages['invalid'])
     94django.forms.util.ValidationError
     95>>> c.clean('001-090994-0002a')
     96u'001-090994-0002a'
     97
     98# NIDepartamentSelect #############
     99
     100A choice field with the 15 departaments and 2 autonomic regions in Nicaragua.
     101
     102>>>
     103>>> d = NIDepartamentSelect()
     104>>> d._get_choices()         
     105[('BO', <django.utils.functional.__proxy__ object at 0x7f4537003750>), ('CZ', <django.utils.functional.__proxy__ object at 0x7f4537003990>), ('CH', <django.utils.functional.__proxy__ object at 0x7f4537003410>), ('CT', <django.utils.functional.__proxy__ object at 0x7f4537003450>), ('ES', <django.utils.functional.__proxy__ object at 0x7f4537003490>), ('GR', <django.utils.functional.__proxy__ object at 0x7f45370035d0>), ('JI', <django.utils.functional.__proxy__ object at 0x7f4537003710>), ('LE', <django.utils.functional.__proxy__ object at 0x7f4537003890>), ('MZ', <django.utils.functional.__proxy__ object at 0x7f4537003910>), ('M', <django.utils.functional.__proxy__ object at 0x7f45365bfd50>), ('MY', <django.utils.functional.__proxy__ object at 0x7f45365bfdd0>), ('MT', <django.utils.functional.__proxy__ object at 0x7f45365bfe50>), ('NS', <django.utils.functional.__proxy__ object at 0x7f45365bfed0>), ('RI', <django.utils.functional.__proxy__ object at 0x7f45365bff10>), ('RS', <django.utils.functional.__proxy__ object at 0x7f45365bff90>), ('RAAN', <django.utils.functional.__proxy__ object at 0x7f453613e050>), ('RAAS', <django.utils.functional.__proxy__ object at 0x7f453613e0d0>)]                                                                   
     106>>> d.clean('M')
     107u'M'
     108>>> d.clean('FOO')
     109Traceback (most recent call last):
     110  File "<stdin>", line 1, in <module>
     111  File "/usr/lib/python2.5/site-packages/django/forms/fields.py", line 649, in clean
     112    raise ValidationError(self.error_messages['invalid_choice'] % {'value': value})
     113django.forms.util.ValidationError: [u'Escoja una opci\xf3n v\xe1lida. FOO no es una de las opciones disponibles.']
     114>>> d.valid_value('M')
     115True
     116>>> d.valid_value('FOO')
     117False
     118"""
     119
  • 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