Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overrides formfield method on Brazilian CPF, CNPJ and PostalCode model field. #490

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Authors
* Kevin Ramirez Zavalza
* László Ratskó
* Lefteris Nikoltsios
* Lucas Dantas Gueiros
* Luis Alberto Santana
* Łukasz Langa
* Luke Benstead
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Modifications to existing flavors:

- Fix Belarus passport field description punctuation
(`gh-484 <https://github.com/django/django-localflavor/pull/484>`_).
- Add min and max length to brazilian Postal Code form field
- Brazilian model fields CPF, CNPJ and PostalCode create correct form
field when used by DjangoModelForm

Other changes:

Expand Down
4 changes: 2 additions & 2 deletions localflavor/br/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class BRZipCodeField(CharField):
More details at: https://github.com/django/django-localflavor/issues/334
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
def __init__(self, max_length=9, min_length=8, **kwargs):
super().__init__(max_length=max_length, min_length=min_length, **kwargs)
self.validators.append(BRPostalCodeValidator())


Expand Down
29 changes: 28 additions & 1 deletion localflavor/br/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

from . import validators
from .br_states import STATE_CHOICES
from . import forms


class BRStateField(CharField):
"""A model field for states of Brazil."""
"""
A model field for states of Brazil.

Forms represent it as a :class:`~localflavor.br.forms.BRStateSelect` field.

"""

description = _("State of Brazil (two uppercase letters)")

Expand All @@ -25,6 +31,8 @@ class BRCPFField(CharField):
"""
A model field for the brazilian document named of CPF (Cadastro de Pessoa Física)

Forms represent it as a :class:`~localflavor.br.forms.BRCPFField` field.

.. versionadded:: 2.2
"""

Expand All @@ -39,12 +47,20 @@ def __init__(self, *args, **kwargs):
kwargs['max_length'] = 14
super().__init__(*args, **kwargs)
self.validators.append(validators.BRCPFValidator())

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



class BRCNPJField(CharField):
"""
A model field for the brazilian document named of CNPJ (Cadastro Nacional de Pessoa Jurídica)

Forms represent it as a :class:`~localflavor.br.forms.BRCNPJField` field.

.. versionadded:: 2.2
"""

Expand All @@ -55,11 +71,17 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.validators.append(validators.BRCNPJValidator())

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

class BRPostalCodeField(CharField):
"""
A model field for the brazilian zip code

Forms represent it as a :class:`~localflavor.br.forms.BRZipCOdeField` field.

.. versionadded:: 2.2
"""

Expand All @@ -69,3 +91,8 @@ def __init__(self, *args, **kwargs):
kwargs['max_length'] = 9
super().__init__(*args, **kwargs)
self.validators.append(validators.BRPostalCodeValidator())

def formfield(self, **kwargs):
defaults = {'form_class': forms.BRZipCodeField}
defaults.update(kwargs)
return super().formfield(**defaults)
41 changes: 36 additions & 5 deletions tests/test_br/test_br.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.test import SimpleTestCase
from django.test import SimpleTestCase, TestCase

from localflavor.br import models
from localflavor.br.forms import (BRCNPJField, BRCPFField, BRProcessoField, BRStateChoiceField, BRStateSelect,
Expand All @@ -16,18 +16,18 @@ def test_BRZipCodeField(self):
'12345_123': error_format,
'1234-123': error_format,
'abcde-abc': error_format,
'12345-': error_format,
'-123': error_format,
'12345-': ['Ensure this value has at least 8 characters (it has 6).'] + error_format,
'-123': ['Ensure this value has at least 8 characters (it has 4).'] + error_format,
}
self.assertFieldOutput(BRZipCodeField, valid, invalid)

for postal_code, _ in invalid.items():
for postal_code, error in invalid.items():
form = BRPersonProfileForm({
'postal_code': postal_code
})

self.assertFalse(form.is_valid())
self.assertEqual(form.errors['postal_code'], error_format)
self.assertEqual(form.errors['postal_code'], error)

def test_BRCNPJField(self):
error_format = {
Expand Down Expand Up @@ -224,6 +224,37 @@ def test_model_form_valid(self):
form = BRPersonProfileForm(case)
self.assertTrue(form.is_valid())

class BRLocalFlavorModelFormTests (TestCase):
def setUp(self):
self.form = BRPersonProfileForm({
'cpf':'111.111.111-11',
'cnpj':'64-132-916/0001-88',
'postal_code':'12345-123',
})

def test_BRCPFFiedlHtml(self):
name = 'cpf'
value = '111.111.111-11'
model_form_field = self.form.fields[name]
model_form_field_render = model_form_field.widget.render(name,value)
self.assertTrue('minlength="11"' in model_form_field_render)
self.assertTrue('maxlength="14"' in model_form_field_render)

def test_BRCNPJFiedlHtml(self):
name = 'cnpj'
value = '64-132-916/0001-88'
model_form_field = self.form.fields[name]
model_form_field_render = model_form_field.widget.render(name,value)
self.assertTrue('minlength="14"' in model_form_field_render)
self.assertTrue('maxlength="18"' in model_form_field_render)

def test_BRPostalCodeFiedlHtml(self):
name = 'postal_code'
value = '12345-123'
model_form_field = self.form.fields[name]
model_form_field_render = model_form_field.widget.render(name,value)
self.assertTrue('minlength="8"' in model_form_field_render)
self.assertTrue('maxlength="9"' in model_form_field_render)

class BRLocalFlavorModelTests(SimpleTestCase):

Expand Down