Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 2135befd64ce6c5c1ba765337e078a6cce7ae0f2 1 parent 07ba0db
Karen Tracey authored December 13, 2009
1  AUTHORS
@@ -303,6 +303,7 @@ answer newbie questions, and generally made Django that much better:
303 303
     Jason McBrayer <http://www.carcosa.net/jason/>
304 304
     Kevin McConnell <kevin.mcconnell@gmail.com>
305 305
     mccutchen@gmail.com
  306
+    Paul McLanahan <paul@mclanahan.net>
306 307
     Tobias McNulty <http://www.caktusgroup.com/blog>
307 308
     Christian Metts
308 309
     michael.mcewan@gmail.com
27  django/contrib/localflavor/us/models.py
... ...
@@ -1,23 +1,14 @@
1 1
 from django.conf import settings
2  
-from django.db.models.fields import Field
3  
-
4  
-class USStateField(Field): 
  2
+from django.db.models.fields import Field, CharField
  3
+from django.contrib.localflavor.us.us_states import STATE_CHOICES
  4
+  
  5
+class USStateField(CharField):
5 6
     """U.S. state (two uppercase letters)"""
6  
-    def get_internal_type(self): 
7  
-        return "USStateField" 
8  
-        
9  
-    def db_type(self):
10  
-        if settings.DATABASE_ENGINE == 'oracle':
11  
-            return 'CHAR(2)'
12  
-        else:
13  
-            return 'varchar(2)'
14  
-    
15  
-    def formfield(self, **kwargs): 
16  
-        from django.contrib.localflavor.us.forms import USStateSelect 
17  
-        defaults = {'widget': USStateSelect} 
18  
-        defaults.update(kwargs) 
19  
-        return super(USStateField, self).formfield(**defaults)
20  
-
  7
+    def __init__(self, *args, **kwargs):
  8
+        kwargs['choices'] = STATE_CHOICES
  9
+        kwargs['max_length'] = 2
  10
+        super(USStateField, self).__init__(*args, **kwargs)
  11
+  
21 12
 class PhoneNumberField(Field):
22 13
     """Phone number"""
23 14
     def get_internal_type(self):
0  tests/regressiontests/localflavor/__init__.py
No changes.
14  tests/regressiontests/localflavor/forms.py
... ...
@@ -0,0 +1,14 @@
  1
+from django.forms import ModelForm
  2
+from models import Place
  3
+
  4
+class PlaceForm(ModelForm):
  5
+    """docstring for PlaceForm"""
  6
+    class Meta:
  7
+        model = Place
  8
+from django.forms import ModelForm
  9
+from models import Place
  10
+
  11
+class PlaceForm(ModelForm):
  12
+    """docstring for PlaceForm"""
  13
+    class Meta:
  14
+        model = Place
16  tests/regressiontests/localflavor/models.py
... ...
@@ -0,0 +1,16 @@
  1
+from django.db import models
  2
+from django.contrib.localflavor.us.models import USStateField
  3
+
  4
+class Place(models.Model):
  5
+    state = USStateField(blank=True)
  6
+    state_req = USStateField()
  7
+    state_default = USStateField(default="CA", blank=True)
  8
+    name = models.CharField(max_length=20)
  9
+from django.db import models
  10
+from django.contrib.localflavor.us.models import USStateField
  11
+
  12
+class Place(models.Model):
  13
+    state = USStateField(blank=True)
  14
+    state_req = USStateField()
  15
+    state_default = USStateField(default="CA", blank=True)
  16
+    name = models.CharField(max_length=20)
166  tests/regressiontests/localflavor/tests.py
... ...
@@ -0,0 +1,166 @@
  1
+from django.test import TestCase
  2
+from models import Place
  3
+from forms import PlaceForm
  4
+
  5
+class USLocalflavorTests(TestCase):
  6
+    def setUp(self):
  7
+        self.form = PlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'})
  8
+        
  9
+    def test_get_display_methods(self):
  10
+        """Test that the get_*_display() methods are added to the model instances."""
  11
+        place = self.form.save()
  12
+        self.assertEqual(place.get_state_display(), 'Georgia')
  13
+        self.assertEqual(place.get_state_req_display(), 'North Carolina')
  14
+    
  15
+    def test_required(self):
  16
+        """Test that required USStateFields throw appropriate errors."""
  17
+        form = PlaceForm({'state':'GA', 'name':'Place in GA'})
  18
+        self.assertFalse(form.is_valid())
  19
+        self.assertEqual(form.errors['state_req'], [u'This field is required.'])
  20
+    
  21
+    def test_field_blank_option(self):
  22
+        """Test that the empty option is there."""
  23
+        state_select_html = """\
  24
+<select name="state" id="id_state">
  25
+<option value="">---------</option>
  26
+<option value="AL">Alabama</option>
  27
+<option value="AK">Alaska</option>
  28
+<option value="AS">American Samoa</option>
  29
+<option value="AZ">Arizona</option>
  30
+<option value="AR">Arkansas</option>
  31
+<option value="CA">California</option>
  32
+<option value="CO">Colorado</option>
  33
+<option value="CT">Connecticut</option>
  34
+<option value="DE">Delaware</option>
  35
+<option value="DC">District of Columbia</option>
  36
+<option value="FL">Florida</option>
  37
+<option value="GA" selected="selected">Georgia</option>
  38
+<option value="GU">Guam</option>
  39
+<option value="HI">Hawaii</option>
  40
+<option value="ID">Idaho</option>
  41
+<option value="IL">Illinois</option>
  42
+<option value="IN">Indiana</option>
  43
+<option value="IA">Iowa</option>
  44
+<option value="KS">Kansas</option>
  45
+<option value="KY">Kentucky</option>
  46
+<option value="LA">Louisiana</option>
  47
+<option value="ME">Maine</option>
  48
+<option value="MD">Maryland</option>
  49
+<option value="MA">Massachusetts</option>
  50
+<option value="MI">Michigan</option>
  51
+<option value="MN">Minnesota</option>
  52
+<option value="MS">Mississippi</option>
  53
+<option value="MO">Missouri</option>
  54
+<option value="MT">Montana</option>
  55
+<option value="NE">Nebraska</option>
  56
+<option value="NV">Nevada</option>
  57
+<option value="NH">New Hampshire</option>
  58
+<option value="NJ">New Jersey</option>
  59
+<option value="NM">New Mexico</option>
  60
+<option value="NY">New York</option>
  61
+<option value="NC">North Carolina</option>
  62
+<option value="ND">North Dakota</option>
  63
+<option value="MP">Northern Mariana Islands</option>
  64
+<option value="OH">Ohio</option>
  65
+<option value="OK">Oklahoma</option>
  66
+<option value="OR">Oregon</option>
  67
+<option value="PA">Pennsylvania</option>
  68
+<option value="PR">Puerto Rico</option>
  69
+<option value="RI">Rhode Island</option>
  70
+<option value="SC">South Carolina</option>
  71
+<option value="SD">South Dakota</option>
  72
+<option value="TN">Tennessee</option>
  73
+<option value="TX">Texas</option>
  74
+<option value="UT">Utah</option>
  75
+<option value="VT">Vermont</option>
  76
+<option value="VI">Virgin Islands</option>
  77
+<option value="VA">Virginia</option>
  78
+<option value="WA">Washington</option>
  79
+<option value="WV">West Virginia</option>
  80
+<option value="WI">Wisconsin</option>
  81
+<option value="WY">Wyoming</option>
  82
+</select>"""
  83
+        self.assertEqual(str(self.form['state']), state_select_html)
  84
+from django.test import TestCase
  85
+from models import Place
  86
+from forms import PlaceForm
  87
+
  88
+class USLocalflavorTests(TestCase):
  89
+    def setUp(self):
  90
+        self.form = PlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'})
  91
+        
  92
+    def test_get_display_methods(self):
  93
+        """Test that the get_*_display() methods are added to the model instances."""
  94
+        place = self.form.save()
  95
+        self.assertEqual(place.get_state_display(), 'Georgia')
  96
+        self.assertEqual(place.get_state_req_display(), 'North Carolina')
  97
+    
  98
+    def test_required(self):
  99
+        """Test that required USStateFields throw appropriate errors."""
  100
+        form = PlaceForm({'state':'GA', 'name':'Place in GA'})
  101
+        self.assertFalse(form.is_valid())
  102
+        self.assertEqual(form.errors['state_req'], [u'This field is required.'])
  103
+    
  104
+    def test_field_blank_option(self):
  105
+        """Test that the empty option is there."""
  106
+        state_select_html = """\
  107
+<select name="state" id="id_state">
  108
+<option value="">---------</option>
  109
+<option value="AL">Alabama</option>
  110
+<option value="AK">Alaska</option>
  111
+<option value="AS">American Samoa</option>
  112
+<option value="AZ">Arizona</option>
  113
+<option value="AR">Arkansas</option>
  114
+<option value="CA">California</option>
  115
+<option value="CO">Colorado</option>
  116
+<option value="CT">Connecticut</option>
  117
+<option value="DE">Delaware</option>
  118
+<option value="DC">District of Columbia</option>
  119
+<option value="FL">Florida</option>
  120
+<option value="GA" selected="selected">Georgia</option>
  121
+<option value="GU">Guam</option>
  122
+<option value="HI">Hawaii</option>
  123
+<option value="ID">Idaho</option>
  124
+<option value="IL">Illinois</option>
  125
+<option value="IN">Indiana</option>
  126
+<option value="IA">Iowa</option>
  127
+<option value="KS">Kansas</option>
  128
+<option value="KY">Kentucky</option>
  129
+<option value="LA">Louisiana</option>
  130
+<option value="ME">Maine</option>
  131
+<option value="MD">Maryland</option>
  132
+<option value="MA">Massachusetts</option>
  133
+<option value="MI">Michigan</option>
  134
+<option value="MN">Minnesota</option>
  135
+<option value="MS">Mississippi</option>
  136
+<option value="MO">Missouri</option>
  137
+<option value="MT">Montana</option>
  138
+<option value="NE">Nebraska</option>
  139
+<option value="NV">Nevada</option>
  140
+<option value="NH">New Hampshire</option>
  141
+<option value="NJ">New Jersey</option>
  142
+<option value="NM">New Mexico</option>
  143
+<option value="NY">New York</option>
  144
+<option value="NC">North Carolina</option>
  145
+<option value="ND">North Dakota</option>
  146
+<option value="MP">Northern Mariana Islands</option>
  147
+<option value="OH">Ohio</option>
  148
+<option value="OK">Oklahoma</option>
  149
+<option value="OR">Oregon</option>
  150
+<option value="PA">Pennsylvania</option>
  151
+<option value="PR">Puerto Rico</option>
  152
+<option value="RI">Rhode Island</option>
  153
+<option value="SC">South Carolina</option>
  154
+<option value="SD">South Dakota</option>
  155
+<option value="TN">Tennessee</option>
  156
+<option value="TX">Texas</option>
  157
+<option value="UT">Utah</option>
  158
+<option value="VT">Vermont</option>
  159
+<option value="VI">Virgin Islands</option>
  160
+<option value="VA">Virginia</option>
  161
+<option value="WA">Washington</option>
  162
+<option value="WV">West Virginia</option>
  163
+<option value="WI">Wisconsin</option>
  164
+<option value="WY">Wyoming</option>
  165
+</select>"""
  166
+        self.assertEqual(str(self.form['state']), state_select_html)

0 notes on commit 2135bef

Please sign in to comment.
Something went wrong with that request. Please try again.