Ticket #10969: localflavor.us.models.usstatefield.diff

File localflavor.us.models.usstatefield.diff, 6.2 KB (added by Paul McLanahan, 15 years ago)
  • django/contrib/localflavor/us/models.py

     
    11from django.conf import settings
    2 from django.db.models.fields import Field
     2from django.db.models.fields import Field, CharField
     3from django.contrib.localflavor.us.us_states import STATE_CHOICES
    34
    4 class USStateField(Field):
    5     def get_internal_type(self):
    6         return "USStateField"
    7        
    8     def db_type(self):
    9         if settings.DATABASE_ENGINE == 'oracle':
    10             return 'CHAR(2)'
    11         else:
    12             return 'varchar(2)'
    13    
    14     def formfield(self, **kwargs):
    15         from django.contrib.localflavor.us.forms import USStateSelect
    16         defaults = {'widget': USStateSelect}
    17         defaults.update(kwargs)
    18         return super(USStateField, self).formfield(**defaults)
     5class USStateField(CharField):
     6    def __init__(self, *args, **kwargs):
     7        kwargs['choices'] = STATE_CHOICES
     8        kwargs['max_length'] = 2
     9        super(USStateField, self).__init__(*args, **kwargs)
    1910
    2011class PhoneNumberField(Field):
    2112    def get_internal_type(self):
  • tests/regressiontests/localflavor/tests.py

     
     1from django.test import TestCase
     2from models import Place
     3from forms import PlaceForm
     4
     5class 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)
  • tests/regressiontests/localflavor/models.py

     
     1from django.db import models
     2from django.contrib.localflavor.us.models import USStateField
     3
     4class 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)
  • tests/regressiontests/localflavor/forms.py

     
     1from django.forms import ModelForm
     2from models import Place
     3
     4class PlaceForm(ModelForm):
     5    """docstring for PlaceForm"""
     6    class Meta:
     7        model = Place
  • AUTHORS

     
    300300    Jason McBrayer <http://www.carcosa.net/jason/>
    301301    Kevin McConnell <kevin.mcconnell@gmail.com>
    302302    mccutchen@gmail.com
     303    Paul McLanahan <paul@mclanahan.net>
    303304    Tobias McNulty <http://www.caktusgroup.com/blog>
    304305    Christian Metts
    305306    michael.mcewan@gmail.com
Back to Top