Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8210 and #8664: moved `USStateField` and `PhoneNumberField` to…

… `django.contrib.localflavor.us`. This is a backwards-incompatible change, albeit a minor one; just add an aditional `import` and go on your way.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@8819 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3a18002bbd3b08fb3ee40d681bdda1a39eb0ad9c 1 parent 4f225d4
Jacob Kaplan-Moss authored September 01, 2008
3  django/contrib/gis/tests/relatedapp/models.py
... ...
@@ -1,4 +1,5 @@
1 1
 from django.contrib.gis.db import models
  2
+from django.contrib.localflavor.us.models import USStateField
2 3
 
3 4
 class Location(models.Model):
4 5
     name = models.CharField(max_length=50)
@@ -7,6 +8,6 @@ class Location(models.Model):
7 8
 
8 9
 class City(models.Model):
9 10
     name = models.CharField(max_length=50)
10  
-    state = models.USStateField()
  11
+    state = USStateField()
11 12
     location = models.ForeignKey(Location)
12 13
     objects = models.GeoManager()
3  django/contrib/gis/utils/layermapping.py
@@ -118,6 +118,7 @@ def __str__(self):
118 118
     OFTDate, OFTDateTime, OFTInteger, OFTReal, OFTString, OFTTime
119 119
 from django.contrib.gis.models import GeometryColumns, SpatialRefSys
120 120
 from django.db import models, transaction
  121
+from django.contrib.localflavor.us.models import USStateField
121 122
 
122 123
 # LayerMapping exceptions.
123 124
 class LayerMapError(Exception): pass
@@ -150,7 +151,7 @@ class LayerMapping(object):
150 151
         models.SlugField : OFTString,
151 152
         models.TextField : OFTString,
152 153
         models.URLField : OFTString,
153  
-        models.USStateField : OFTString,
  154
+        USStateField : OFTString,
154 155
         models.XMLField : OFTString,
155 156
         models.SmallIntegerField : (OFTInteger, OFTReal, OFTString),
156 157
         models.PositiveSmallIntegerField : (OFTInteger, OFTReal, OFTString),
35  django/contrib/localflavor/us/models.py
... ...
@@ -0,0 +1,35 @@
  1
+from django.conf import settings
  2
+from django.db.models.fields import Field
  3
+
  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)
  19
+
  20
+class PhoneNumberField(Field):
  21
+    def get_internal_type(self):
  22
+        return "PhoneNumberField"
  23
+
  24
+    def db_type(self):
  25
+        if settings.DATABASE_ENGINE == 'oracle':
  26
+            return 'VARCHAR2(20)'
  27
+        else:
  28
+            return 'varchar(20)'
  29
+
  30
+    def formfield(self, **kwargs):
  31
+        from django.contrib.localflavor.us.forms import USPhoneNumberField
  32
+        defaults = {'form_class': USPhoneNumberField}
  33
+        defaults.update(kwargs)
  34
+        return super(PhoneNumberField, self).formfield(**defaults)
  35
+
2  django/db/backends/mysql/creation.py
@@ -21,14 +21,12 @@ class DatabaseCreation(BaseDatabaseCreation):
21 21
         'IPAddressField':    'char(15)',
22 22
         'NullBooleanField':  'bool',
23 23
         'OneToOneField':     'integer',
24  
-        'PhoneNumberField':  'varchar(20)',
25 24
         'PositiveIntegerField': 'integer UNSIGNED',
26 25
         'PositiveSmallIntegerField': 'smallint UNSIGNED',
27 26
         'SlugField':         'varchar(%(max_length)s)',
28 27
         'SmallIntegerField': 'smallint',
29 28
         'TextField':         'longtext',
30 29
         'TimeField':         'time',
31  
-        'USStateField':      'varchar(2)',
32 30
     }
33 31
 
34 32
     def sql_table_creation_suffix(self):
2  django/db/backends/oracle/creation.py
@@ -30,7 +30,6 @@ class DatabaseCreation(BaseDatabaseCreation):
30 30
         'IPAddressField':               'VARCHAR2(15)',
31 31
         'NullBooleanField':             'NUMBER(1) CHECK ((%(qn_column)s IN (0,1)) OR (%(qn_column)s IS NULL))',
32 32
         'OneToOneField':                'NUMBER(11)',
33  
-        'PhoneNumberField':             'VARCHAR2(20)',
34 33
         'PositiveIntegerField':         'NUMBER(11) CHECK (%(qn_column)s >= 0)',
35 34
         'PositiveSmallIntegerField':    'NUMBER(11) CHECK (%(qn_column)s >= 0)',
36 35
         'SlugField':                    'NVARCHAR2(50)',
@@ -38,7 +37,6 @@ class DatabaseCreation(BaseDatabaseCreation):
38 37
         'TextField':                    'NCLOB',
39 38
         'TimeField':                    'TIMESTAMP',
40 39
         'URLField':                     'VARCHAR2(%(max_length)s)',
41  
-        'USStateField':                 'CHAR(2)',
42 40
     }
43 41
 
44 42
     remember = {}
2  django/db/backends/postgresql/creation.py
@@ -21,14 +21,12 @@ class DatabaseCreation(BaseDatabaseCreation):
21 21
         'IPAddressField':    'inet',
22 22
         'NullBooleanField':  'boolean',
23 23
         'OneToOneField':     'integer',
24  
-        'PhoneNumberField':  'varchar(20)',
25 24
         'PositiveIntegerField': 'integer CHECK ("%(column)s" >= 0)',
26 25
         'PositiveSmallIntegerField': 'smallint CHECK ("%(column)s" >= 0)',
27 26
         'SlugField':         'varchar(%(max_length)s)',
28 27
         'SmallIntegerField': 'smallint',
29 28
         'TextField':         'text',
30 29
         'TimeField':         'time',
31  
-        'USStateField':      'varchar(2)',
32 30
     }
33 31
 
34 32
     def sql_table_creation_suffix(self):
2  django/db/backends/sqlite3/creation.py
@@ -22,14 +22,12 @@ class DatabaseCreation(BaseDatabaseCreation):
22 22
         'IPAddressField':               'char(15)',
23 23
         'NullBooleanField':             'bool',
24 24
         'OneToOneField':                'integer',
25  
-        'PhoneNumberField':             'varchar(20)',
26 25
         'PositiveIntegerField':         'integer unsigned',
27 26
         'PositiveSmallIntegerField':    'smallint unsigned',
28 27
         'SlugField':                    'varchar(%(max_length)s)',
29 28
         'SmallIntegerField':            'smallint',
30 29
         'TextField':                    'text',
31 30
         'TimeField':                    'time',
32  
-        'USStateField':                 'varchar(2)',
33 31
     }
34 32
     
35 33
     def sql_for_pending_references(self, model, style, pending_references):
20  django/db/models/fields/__init__.py
@@ -741,16 +741,6 @@ def formfield(self, **kwargs):
741 741
         defaults.update(kwargs)
742 742
         return super(NullBooleanField, self).formfield(**defaults)
743 743
 
744  
-class PhoneNumberField(Field):
745  
-    def get_internal_type(self):
746  
-        return "PhoneNumberField"
747  
-
748  
-    def formfield(self, **kwargs):
749  
-        from django.contrib.localflavor.us.forms import USPhoneNumberField
750  
-        defaults = {'form_class': USPhoneNumberField}
751  
-        defaults.update(kwargs)
752  
-        return super(PhoneNumberField, self).formfield(**defaults)
753  
-
754 744
 class PositiveIntegerField(IntegerField):
755 745
     def get_internal_type(self):
756 746
         return "PositiveIntegerField"
@@ -876,16 +866,6 @@ def formfield(self, **kwargs):
876 866
         defaults.update(kwargs)
877 867
         return super(URLField, self).formfield(**defaults)
878 868
 
879  
-class USStateField(Field):
880  
-    def get_internal_type(self):
881  
-        return "USStateField"
882  
-
883  
-    def formfield(self, **kwargs):
884  
-        from django.contrib.localflavor.us.forms import USStateSelect
885  
-        defaults = {'widget': USStateSelect}
886  
-        defaults.update(kwargs)
887  
-        return super(USStateField, self).formfield(**defaults)
888  
-
889 869
 class XMLField(TextField):
890 870
     def __init__(self, verbose_name=None, name=None, schema_path=None, **kwargs):
891 871
         self.schema_path = schema_path
5  docs/ref/contrib/localflavor.txt
@@ -649,3 +649,8 @@ United States of America (``us``)
649 649
 
650 650
     A form ``Select`` widget that uses a list of U.S. states/territories as its
651 651
     choices.
  652
+
  653
+.. class:: us.models.USStateField
  654
+
  655
+    A model field that forms represent as a ``forms.USStateField`` field and
  656
+    stores the two-letter U.S. state abbreviation in the database.
8  docs/ref/models/fields.txt
@@ -705,14 +705,6 @@ Like all ::class:`CharField` subclasses, :class:`URLField` takes the optional
705 705
 :attr:`~CharField.max_length`argument. If you don't specify
706 706
 :attr:`~CharField.max_length`, a default of 200 is used.
707 707
 
708  
-``USStateField``
709  
-----------------
710  
-
711  
-.. class:: USStateField([**options])
712  
-
713  
-A two-letter U.S. state abbreviation. The admin represents this as an ``<input
714  
-type="text">`` (a single-line input).
715  
-
716 708
 ``XMLField``
717 709
 ------------
718 710
 
3  docs/topics/db/models.txt
@@ -625,13 +625,14 @@ model.
625 625
 
626 626
 For example, this model has a few custom methods::
627 627
 
  628
+    from django.contrib.localflavor.us.models import USStateField
628 629
     class Person(models.Model):
629 630
         first_name = models.CharField(max_length=50)
630 631
         last_name = models.CharField(max_length=50)
631 632
         birth_date = models.DateField()
632 633
         address = models.CharField(max_length=100)
633 634
         city = models.CharField(max_length=50)
634  
-        state = models.USStateField() # Yes, this is America-centric...
  635
+        state = USStateField() # Yes, this is America-centric...
635 636
 
636 637
         def baby_boomer_status(self):
637 638
             "Returns the person's baby-boomer status."
4  docs/topics/forms/modelforms.txt
@@ -73,10 +73,6 @@ the full list of conversions:
73 73
     ``TimeField``                    ``TimeField``
74 74
     ``URLField``                     ``URLField`` with ``verify_exists`` set
75 75
                                      to the model field's ``verify_exists``
76  
-    ``USStateField``                 ``CharField`` with
77  
-                                     ``widget=USStateSelect``
78  
-                                     (``USStateSelect`` is from
79  
-                                     ``django.contrib.localflavor.us``)
80 76
     ``XMLField``                     ``CharField`` with ``widget=Textarea``
81 77
     ===============================  ========================================
82 78
 
3  tests/modeltests/model_forms/models.py
@@ -82,8 +82,9 @@ class WriterProfile(models.Model):
82 82
     def __unicode__(self):
83 83
         return "%s is %s" % (self.writer, self.age)
84 84
 
  85
+from django.contrib.localflavor.us.models import PhoneNumberField
85 86
 class PhoneNumber(models.Model):
86  
-    phone = models.PhoneNumberField()
  87
+    phone = PhoneNumberField()
87 88
     description = models.CharField(max_length=20)
88 89
 
89 90
     def __unicode__(self):
9  tests/regressiontests/serializers_regress/models.py
@@ -8,6 +8,7 @@
8 8
 from django.db import models
9 9
 from django.contrib.contenttypes import generic
10 10
 from django.contrib.contenttypes.models import ContentType
  11
+from django.contrib.localflavor.us.models import USStateField, PhoneNumberField
11 12
 
12 13
 # The following classes are for testing basic data
13 14
 # marshalling, including NULL values.
@@ -52,7 +53,7 @@ class NullBooleanData(models.Model):
52 53
     data = models.NullBooleanField(null=True)
53 54
 
54 55
 class PhoneData(models.Model):
55  
-    data = models.PhoneNumberField(null=True)
  56
+    data = PhoneNumberField(null=True)
56 57
 
57 58
 class PositiveIntegerData(models.Model):
58 59
     data = models.PositiveIntegerField(null=True)
@@ -73,7 +74,7 @@ class TimeData(models.Model):
73 74
     data = models.TimeField(null=True)
74 75
 
75 76
 class USStateData(models.Model):
76  
-    data = models.USStateField(null=True)
  77
+    data = USStateField(null=True)
77 78
 
78 79
 class XMLData(models.Model):
79 80
     data = models.XMLField(null=True)
@@ -188,7 +189,7 @@ class IPAddressPKData(models.Model):
188 189
 #     data = models.NullBooleanField(primary_key=True)
189 190
 
190 191
 class PhonePKData(models.Model):
191  
-    data = models.PhoneNumberField(primary_key=True)
  192
+    data = PhoneNumberField(primary_key=True)
192 193
 
193 194
 class PositiveIntegerPKData(models.Model):
194 195
     data = models.PositiveIntegerField(primary_key=True)
@@ -209,7 +210,7 @@ class SmallPKData(models.Model):
209 210
 #    data = models.TimeField(primary_key=True)
210 211
 
211 212
 class USStatePKData(models.Model):
212  
-    data = models.USStateField(primary_key=True)
  213
+    data = USStateField(primary_key=True)
213 214
 
214 215
 # class XMLPKData(models.Model):
215 216
 #     data = models.XMLField(primary_key=True)

0 notes on commit 3a18002

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