Code

Ticket #6564: usstate.diff

File usstate.diff, 2.4 KB (added by purdeaandrei, 6 years ago)
Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 7092)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -1055,8 +1055,8 @@
6         return [oldforms.USStateField]
7 
8     def formfield(self, **kwargs):
9-        from django.contrib.localflavor.us.forms import USStateSelect
10-        defaults = {'widget': USStateSelect}
11+        from django.contrib.localflavor.us.forms import USStateField as USStateFormField
12+        defaults = {'form_class': USStateFormField}
13         defaults.update(kwargs)
14         return super(USStateField, self).formfield(**defaults)
15 
16Index: django/contrib/localflavor/us/forms.py
17===================================================================
18--- django/contrib/localflavor/us/forms.py      (revision 7092)
19+++ django/contrib/localflavor/us/forms.py      (working copy)
20@@ -3,7 +3,7 @@
21 """
22 
23 from django.newforms import ValidationError
24-from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES
25+from django.newforms.fields import Field, RegexField, Select, EMPTY_VALUES, ChoiceField
26 from django.utils.encoding import smart_unicode
27 from django.utils.translation import ugettext
28 import re
29@@ -77,7 +77,7 @@
30             raise ValidationError(self.error_messages['invalid'])
31         return u'%s-%s-%s' % (area, group, serial)
32 
33-class USStateField(Field):
34+class USStateField(ChoiceField):
35     """
36     A form field that validates its input is a U.S. state name or abbreviation.
37     It normalizes the input to the standard two-leter postal service
38@@ -86,7 +86,11 @@
39     default_error_messages = {
40         'invalid': u'Enter a U.S. state or territory.',
41     }
42-
43+   
44+    def __init__(self, empty_label=u'---------', *args, **kwargs):
45+        from us_states import STATE_CHOICES
46+       ChoiceField.__init__(self, (('', empty_label),) + STATE_CHOICES, *args, **kwargs)
47+   
48     def clean(self, value):
49         from us_states import STATES_NORMALIZED
50         super(USStateField, self).clean(value)
51@@ -102,11 +106,3 @@
52             except KeyError:
53                 pass
54         raise ValidationError(self.error_messages['invalid'])
55-
56-class USStateSelect(Select):
57-    """
58-    A Select widget that uses a list of U.S. states/territories as its choices.
59-    """
60-    def __init__(self, attrs=None):
61-        from us_states import STATE_CHOICES
62-        super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)