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

Added a model field for Ecuadorian provinces #138

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion localflavor/ec/ec_provinces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

#: A list of Ecuador departaments as `choices` in a formfield.
#: A list of Ecuador provinces as `choices` in a formfield.
PROVINCE_CHOICES = (
('A', 'Azuay'),
('B', 'Bolívar'),
Expand Down
17 changes: 17 additions & 0 deletions localflavor/ec/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.utils.translation import ugettext_lazy as _
from django.db.models.fields import CharField

from .ec_provinces import PROVINCE_CHOICES


class ECProvinceField(CharField):
"""
A model field that represents an Ecuadorian province and stores its
abbreviation.
"""
description = _("Ecuadorian province")

def __init__(self, *args, **kwargs):
kwargs['choices'] = PROVINCE_CHOICES
kwargs['max_length'] = 2
super(ECProvinceField, self).__init__(*args, **kwargs)
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
INSTALLED_APPS = [
'localflavor',
'tests.test_au',
'tests.test_ec',
'tests.test_mk',
'tests.test_mx',
'tests.test_us',
Expand Down
Empty file added tests/test_ec/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/test_ec/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import absolute_import

from django.forms import ModelForm

from .models import ECPlace


class ECPlaceForm(ModelForm):
class Meta:
model = ECPlace
fields = ('province',)
7 changes: 7 additions & 0 deletions tests/test_ec/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models

from localflavor.ec.models import ECProvinceField


class ECPlace(models.Model):
province = ECProvinceField(blank=True)
13 changes: 13 additions & 0 deletions tests/test_ec.py → tests/test_ec/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@

from localflavor.ec.forms import ECProvinceSelect

from .forms import ECPlaceForm

class ECLocalFlavorTests(SimpleTestCase):

def setUp(self):
self.form = ECPlaceForm({
'province':'SD'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding a space after the colon here?

})

def test_ECProvinceSelect(self):
p = ECProvinceSelect()
out = """<select name="province">
Expand Down Expand Up @@ -35,3 +42,9 @@ def test_ECProvinceSelect(self):
<option value="Z">Zamora Chinchipe</option>
</select>"""
self.assertHTMLEqual(p.render('province', 'U'), out)

def test_get_display_methods(self):
"""Test that the get_*_display() methods are added to the model instances."""
place = self.form.save()
self.assertEqual(place.get_province_display(),
"Santo Domingo de los Ts\xe1chilas")