Skip to content

Commit

Permalink
Added models for ES-ID document and ES-PostalCodes (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
benkonrath authored Apr 30, 2019
1 parent 27b01b6 commit 2e27476
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Authors
* Gary Wilson Jr
* Gerardo Orozco
* Ghassen Telmoudi
* Gil Obradors
* Grzes Furga
* Hamad AlGhanim
* Honza Král
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ New fields for existing flavors:
- Added `EircodeField` in IE flavor
(`gh-366 <https://github.com/django/django-localflavor/pull/366>`_).

- Added Models for Spain (`ESPostalCodeField` and `ESIdentityCardNumberField`)
(`gh-357 <https://github.com/django/django-localflavor/pull/357>`_)

Modifications to existing flavors:

- Deprecated `generic.checksums.luhn` and `generic.checksums.ean`. Please use the python-stdnum library instead.
Expand Down
6 changes: 6 additions & 0 deletions docs/localflavor/es.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Forms
.. automodule:: localflavor.es.forms
:members:

Models
------

.. automodule:: localflavor.es.models
:members:

Data
----

Expand Down
54 changes: 54 additions & 0 deletions localflavor/es/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- encoding: utf-8 -*-

from django.db.models import CharField
from django.utils.translation import ugettext_lazy as _

from .forms import ESIdentityCardNumberField as ESIdentityCardNumberFormField
from .forms import ESPostalCodeField as ESPostalCodeFormField


class ESPostalCodeField(CharField):
"""
A model field that stores the five numbers (XXXXX) of Spain Postal Codes
Forms represent it as ``form.ESPostalCodeField``
.. versionadded:: 2.2
"""

description = _("Spain postal code ( five numbers)")

def __init__(self, *args, **kwargs):
kwargs['max_length'] = 5
super(ESPostalCodeField, self).__init__(*args, **kwargs)

def formfield(self, **kwargs):
defaults = {'form_class': ESPostalCodeFormField}
defaults.update(kwargs)
return super(ESPostalCodeField, self).formfield(**defaults)


class ESIdentityCardNumberField(CharField):
"""A model field that stores Spanish NIF/NIE/CIF in format ``XXXXXXXXX``
Forms represent it as ``form.ESIdentityCardNumberField`` field.
.. versionadded:: 2.2
"""

description = _("Identification National Document")

def __init__(self, *args, **kwargs):
kwargs['max_length'] = 9
super(ESIdentityCardNumberField, self).__init__(*args, **kwargs)

def to_python(self, value):
value = super(ESIdentityCardNumberField, self).to_python(value)
if value is not None:
return value.upper().replace(' ', '').replace('-', '')
return value

def formfield(self, **kwargs):
defaults = {'form_class': ESIdentityCardNumberFormField}
defaults.update(kwargs)
return super(ESIdentityCardNumberField, self).formfield(**defaults)
18 changes: 18 additions & 0 deletions tests/test_es.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import unicode_literals

from django import forms
from django.db import models
from django.test import SimpleTestCase

from localflavor.es.forms import (ESCCCField, ESIdentityCardNumberField, ESPostalCodeField, ESProvinceSelect,
ESRegionSelect)
from localflavor.es.models import ESIdentityCardNumberField as ESIdentityCardNumberModelField
from localflavor.es.models import ESPostalCodeField as ESPostalCodeModelField


class ESLocalFlavorTests(SimpleTestCase):
Expand Down Expand Up @@ -106,6 +110,10 @@ def test_ESPostalCodeField(self):
}
self.assertFieldOutput(ESPostalCodeField, valid, invalid)

def test_ESPostalCodeField_model_field_default_form(self):
field = ESPostalCodeModelField()
self.assertEqual(type(field.formfield()), type(ESPostalCodeField()))

def test_ESIdentityCardNumberField(self):
error_invalid = ['Please enter a valid NIF, NIE, or CIF.']
error_checksum_nif = ['Invalid checksum for NIF.']
Expand Down Expand Up @@ -153,6 +161,16 @@ def test_ESIdentityCardNumberField_invalid_error_override(self):
form = ESIdentityCardNumberField(error_messages={'invalid': invalid_override_message})
self.assertEqual(form.error_messages['invalid'], invalid_override_message)

def test_ESIdentityCardNumberField_model_field_to_python_strips_whitespace(self):
field = ESIdentityCardNumberModelField()

self.assertEqual('X6124387Q', field.to_python('X-6124387-Q'))
self.assertEqual('P39008008', field.to_python('P 39008008'))

def test_ESIdentityCardNumberField_model_field_default_form(self):
field = ESIdentityCardNumberModelField()
self.assertEqual(type(field.formfield()), type(ESIdentityCardNumberField()))

def test_ESCCCField(self):
error_invalid = ['Please enter a valid bank account number in format XXXX-XXXX-XX-XXXXXXXXXX.']
error_checksum = ['Invalid checksum for bank account number.']
Expand Down

0 comments on commit 2e27476

Please sign in to comment.