From 2135befd64ce6c5c1ba765337e078a6cce7ae0f2 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sun, 13 Dec 2009 21:56:18 +0000 Subject: [PATCH] Fixed #10969: Made US State field a CharField, fixing a few oddities in its behavior. Thanks Paul McLanahan. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11857 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/contrib/localflavor/us/models.py | 27 +-- tests/regressiontests/localflavor/__init__.py | 0 tests/regressiontests/localflavor/forms.py | 14 ++ tests/regressiontests/localflavor/models.py | 16 ++ tests/regressiontests/localflavor/tests.py | 166 ++++++++++++++++++ 6 files changed, 206 insertions(+), 18 deletions(-) create mode 100644 tests/regressiontests/localflavor/__init__.py create mode 100644 tests/regressiontests/localflavor/forms.py create mode 100644 tests/regressiontests/localflavor/models.py create mode 100644 tests/regressiontests/localflavor/tests.py diff --git a/AUTHORS b/AUTHORS index 6840e0d2be..585b1c8db6 100644 --- a/AUTHORS +++ b/AUTHORS @@ -303,6 +303,7 @@ answer newbie questions, and generally made Django that much better: Jason McBrayer Kevin McConnell mccutchen@gmail.com + Paul McLanahan Tobias McNulty Christian Metts michael.mcewan@gmail.com diff --git a/django/contrib/localflavor/us/models.py b/django/contrib/localflavor/us/models.py index 9465126db7..3e755da306 100644 --- a/django/contrib/localflavor/us/models.py +++ b/django/contrib/localflavor/us/models.py @@ -1,23 +1,14 @@ from django.conf import settings -from django.db.models.fields import Field - -class USStateField(Field): +from django.db.models.fields import Field, CharField +from django.contrib.localflavor.us.us_states import STATE_CHOICES + +class USStateField(CharField): """U.S. state (two uppercase letters)""" - def get_internal_type(self): - return "USStateField" - - def db_type(self): - if settings.DATABASE_ENGINE == 'oracle': - return 'CHAR(2)' - else: - return 'varchar(2)' - - def formfield(self, **kwargs): - from django.contrib.localflavor.us.forms import USStateSelect - defaults = {'widget': USStateSelect} - defaults.update(kwargs) - return super(USStateField, self).formfield(**defaults) - + def __init__(self, *args, **kwargs): + kwargs['choices'] = STATE_CHOICES + kwargs['max_length'] = 2 + super(USStateField, self).__init__(*args, **kwargs) + class PhoneNumberField(Field): """Phone number""" def get_internal_type(self): diff --git a/tests/regressiontests/localflavor/__init__.py b/tests/regressiontests/localflavor/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/localflavor/forms.py b/tests/regressiontests/localflavor/forms.py new file mode 100644 index 0000000000..49635b02fb --- /dev/null +++ b/tests/regressiontests/localflavor/forms.py @@ -0,0 +1,14 @@ +from django.forms import ModelForm +from models import Place + +class PlaceForm(ModelForm): + """docstring for PlaceForm""" + class Meta: + model = Place +from django.forms import ModelForm +from models import Place + +class PlaceForm(ModelForm): + """docstring for PlaceForm""" + class Meta: + model = Place diff --git a/tests/regressiontests/localflavor/models.py b/tests/regressiontests/localflavor/models.py new file mode 100644 index 0000000000..079c7bd982 --- /dev/null +++ b/tests/regressiontests/localflavor/models.py @@ -0,0 +1,16 @@ +from django.db import models +from django.contrib.localflavor.us.models import USStateField + +class Place(models.Model): + state = USStateField(blank=True) + state_req = USStateField() + state_default = USStateField(default="CA", blank=True) + name = models.CharField(max_length=20) +from django.db import models +from django.contrib.localflavor.us.models import USStateField + +class Place(models.Model): + state = USStateField(blank=True) + state_req = USStateField() + state_default = USStateField(default="CA", blank=True) + name = models.CharField(max_length=20) diff --git a/tests/regressiontests/localflavor/tests.py b/tests/regressiontests/localflavor/tests.py new file mode 100644 index 0000000000..61e0e5b5e6 --- /dev/null +++ b/tests/regressiontests/localflavor/tests.py @@ -0,0 +1,166 @@ +from django.test import TestCase +from models import Place +from forms import PlaceForm + +class USLocalflavorTests(TestCase): + def setUp(self): + self.form = PlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'}) + + 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_state_display(), 'Georgia') + self.assertEqual(place.get_state_req_display(), 'North Carolina') + + def test_required(self): + """Test that required USStateFields throw appropriate errors.""" + form = PlaceForm({'state':'GA', 'name':'Place in GA'}) + self.assertFalse(form.is_valid()) + self.assertEqual(form.errors['state_req'], [u'This field is required.']) + + def test_field_blank_option(self): + """Test that the empty option is there.""" + state_select_html = """\ +""" + self.assertEqual(str(self.form['state']), state_select_html) +from django.test import TestCase +from models import Place +from forms import PlaceForm + +class USLocalflavorTests(TestCase): + def setUp(self): + self.form = PlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'}) + + 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_state_display(), 'Georgia') + self.assertEqual(place.get_state_req_display(), 'North Carolina') + + def test_required(self): + """Test that required USStateFields throw appropriate errors.""" + form = PlaceForm({'state':'GA', 'name':'Place in GA'}) + self.assertFalse(form.is_valid()) + self.assertEqual(form.errors['state_req'], [u'This field is required.']) + + def test_field_blank_option(self): + """Test that the empty option is there.""" + state_select_html = """\ +""" + self.assertEqual(str(self.form['state']), state_select_html)