Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13729 -- Renamed UK localflavor to GB to correctly follow ISO …

…3166. Thanks, Claude Paroz.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16147 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f4860448ddad760172b4dde0323140e99d78a55f 1 parent 41a1a89
Jannis Leidel authored May 03, 2011
53  django/contrib/localflavor/gb/forms.py
... ...
@@ -0,0 +1,53 @@
  1
+"""
  2
+GB-specific Form helpers
  3
+"""
  4
+
  5
+import re
  6
+
  7
+from django.forms.fields import CharField, Select
  8
+from django.forms import ValidationError
  9
+from django.utils.translation import ugettext_lazy as _
  10
+
  11
+class GBPostcodeField(CharField):
  12
+    """
  13
+    A form field that validates its input is a UK postcode.
  14
+
  15
+    The regular expression used is sourced from the schema for British Standard
  16
+    BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
  17
+
  18
+    The value is uppercased and a space added in the correct place, if required.
  19
+    """
  20
+    default_error_messages = {
  21
+        'invalid': _(u'Enter a valid postcode.'),
  22
+    }
  23
+    outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])'
  24
+    incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}'
  25
+    postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern))
  26
+    space_regex = re.compile(r' *(%s)$' % incode_pattern)
  27
+
  28
+    def clean(self, value):
  29
+        value = super(GBPostcodeField, self).clean(value)
  30
+        if value == u'':
  31
+            return value
  32
+        postcode = value.upper().strip()
  33
+        # Put a single space before the incode (second part).
  34
+        postcode = self.space_regex.sub(r' \1', postcode)
  35
+        if not self.postcode_regex.search(postcode):
  36
+            raise ValidationError(self.error_messages['invalid'])
  37
+        return postcode
  38
+
  39
+class GBCountySelect(Select):
  40
+    """
  41
+    A Select widget that uses a list of UK Counties/Regions as its choices.
  42
+    """
  43
+    def __init__(self, attrs=None):
  44
+        from gb_regions import GB_REGION_CHOICES
  45
+        super(GBCountySelect, self).__init__(attrs, choices=GB_REGION_CHOICES)
  46
+
  47
+class GBNationSelect(Select):
  48
+    """
  49
+    A Select widget that uses a list of UK Nations as its choices.
  50
+    """
  51
+    def __init__(self, attrs=None):
  52
+        from gb_regions import GB_NATIONS_CHOICES
  53
+        super(GBNationSelect, self).__init__(attrs, choices=GB_NATIONS_CHOICES)
97  django/contrib/localflavor/gb/gb_regions.py
... ...
@@ -0,0 +1,97 @@
  1
+"""
  2
+Sources:
  3
+    English regions: http://www.statistics.gov.uk/geography/downloads/31_10_01_REGION_names_and_codes_12_00.xls
  4
+    Northern Ireland regions: http://en.wikipedia.org/wiki/List_of_Irish_counties_by_area
  5
+    Welsh regions: http://en.wikipedia.org/wiki/Preserved_counties_of_Wales
  6
+    Scottish regions: http://en.wikipedia.org/wiki/Regions_and_districts_of_Scotland
  7
+"""
  8
+from django.utils.translation import ugettext_lazy as _
  9
+
  10
+ENGLAND_REGION_CHOICES = (
  11
+    ("Bedfordshire", _("Bedfordshire")),
  12
+    ("Buckinghamshire", _("Buckinghamshire")),
  13
+    ("Cambridgeshire", ("Cambridgeshire")),
  14
+    ("Cheshire", _("Cheshire")),
  15
+    ("Cornwall and Isles of Scilly", _("Cornwall and Isles of Scilly")),
  16
+    ("Cumbria", _("Cumbria")),
  17
+    ("Derbyshire", _("Derbyshire")),
  18
+    ("Devon", _("Devon")),
  19
+    ("Dorset", _("Dorset")),
  20
+    ("Durham", _("Durham")),
  21
+    ("East Sussex", _("East Sussex")),
  22
+    ("Essex", _("Essex")),
  23
+    ("Gloucestershire", _("Gloucestershire")),
  24
+    ("Greater London", _("Greater London")),
  25
+    ("Greater Manchester", _("Greater Manchester")),
  26
+    ("Hampshire", _("Hampshire")),
  27
+    ("Hertfordshire", _("Hertfordshire")),
  28
+    ("Kent", _("Kent")),
  29
+    ("Lancashire", _("Lancashire")),
  30
+    ("Leicestershire", _("Leicestershire")),
  31
+    ("Lincolnshire", _("Lincolnshire")),
  32
+    ("Merseyside", _("Merseyside")),
  33
+    ("Norfolk", _("Norfolk")),
  34
+    ("North Yorkshire", _("North Yorkshire")),
  35
+    ("Northamptonshire", _("Northamptonshire")),
  36
+    ("Northumberland", _("Northumberland")),
  37
+    ("Nottinghamshire", _("Nottinghamshire")),
  38
+    ("Oxfordshire", _("Oxfordshire")),
  39
+    ("Shropshire", _("Shropshire")),
  40
+    ("Somerset", _("Somerset")),
  41
+    ("South Yorkshire", _("South Yorkshire")),
  42
+    ("Staffordshire", _("Staffordshire")),
  43
+    ("Suffolk", _("Suffolk")),
  44
+    ("Surrey", _("Surrey")),
  45
+    ("Tyne and Wear", _("Tyne and Wear")),
  46
+    ("Warwickshire", _("Warwickshire")),
  47
+    ("West Midlands", _("West Midlands")),
  48
+    ("West Sussex", _("West Sussex")),
  49
+    ("West Yorkshire", _("West Yorkshire")),
  50
+    ("Wiltshire", _("Wiltshire")),
  51
+    ("Worcestershire", _("Worcestershire")),
  52
+)
  53
+
  54
+NORTHERN_IRELAND_REGION_CHOICES = (
  55
+    ("County Antrim", _("County Antrim")),
  56
+    ("County Armagh", _("County Armagh")),
  57
+    ("County Down", _("County Down")),
  58
+    ("County Fermanagh", _("County Fermanagh")),
  59
+    ("County Londonderry", _("County Londonderry")),
  60
+    ("County Tyrone", _("County Tyrone")),
  61
+)
  62
+
  63
+WALES_REGION_CHOICES = (
  64
+    ("Clwyd", _("Clwyd")),
  65
+    ("Dyfed", _("Dyfed")),
  66
+    ("Gwent", _("Gwent")),
  67
+    ("Gwynedd", _("Gwynedd")),
  68
+    ("Mid Glamorgan", _("Mid Glamorgan")),
  69
+    ("Powys", _("Powys")),
  70
+    ("South Glamorgan", _("South Glamorgan")),
  71
+    ("West Glamorgan", _("West Glamorgan")),
  72
+)
  73
+
  74
+SCOTTISH_REGION_CHOICES = (
  75
+    ("Borders", _("Borders")),
  76
+    ("Central Scotland", _("Central Scotland")),
  77
+    ("Dumfries and Galloway", _("Dumfries and Galloway")),
  78
+    ("Fife", _("Fife")),
  79
+    ("Grampian", _("Grampian")),
  80
+    ("Highland", _("Highland")),
  81
+    ("Lothian", _("Lothian")),
  82
+    ("Orkney Islands", _("Orkney Islands")),
  83
+    ("Shetland Islands", _("Shetland Islands")),
  84
+    ("Strathclyde", _("Strathclyde")),
  85
+    ("Tayside", _("Tayside")),
  86
+    ("Western Isles", _("Western Isles")),
  87
+)
  88
+
  89
+GB_NATIONS_CHOICES = (
  90
+    ("England", _("England")),
  91
+    ("Northern Ireland", _("Northern Ireland")),
  92
+    ("Scotland", _("Scotland")),
  93
+    ("Wales", _("Wales")),
  94
+)
  95
+
  96
+GB_REGION_CHOICES = ENGLAND_REGION_CHOICES + NORTHERN_IRELAND_REGION_CHOICES + WALES_REGION_CHOICES + SCOTTISH_REGION_CHOICES
  97
+
59  django/contrib/localflavor/uk/forms.py
... ...
@@ -1,53 +1,10 @@
1  
-"""
2  
-UK-specific Form helpers
3  
-"""
  1
+from django.contrib.localflavor.gb import forms
4 2
 
5  
-import re
  3
+import warnings
  4
+warnings.warn(
  5
+    'The "UK" prefix for United Kingdom has been deprecated in favour of the '
  6
+    'GB code. Please use the new GB-prefixed names.', PendingDeprecationWarning)
6 7
 
7  
-from django.forms.fields import CharField, Select
8  
-from django.forms import ValidationError
9  
-from django.utils.translation import ugettext_lazy as _
10  
-
11  
-class UKPostcodeField(CharField):
12  
-    """
13  
-    A form field that validates its input is a UK postcode.
14  
-
15  
-    The regular expression used is sourced from the schema for British Standard
16  
-    BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd
17  
-
18  
-    The value is uppercased and a space added in the correct place, if required.
19  
-    """
20  
-    default_error_messages = {
21  
-        'invalid': _(u'Enter a valid postcode.'),
22  
-    }
23  
-    outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])'
24  
-    incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}'
25  
-    postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern))
26  
-    space_regex = re.compile(r' *(%s)$' % incode_pattern)
27  
-
28  
-    def clean(self, value):
29  
-        value = super(UKPostcodeField, self).clean(value)
30  
-        if value == u'':
31  
-            return value
32  
-        postcode = value.upper().strip()
33  
-        # Put a single space before the incode (second part).
34  
-        postcode = self.space_regex.sub(r' \1', postcode)
35  
-        if not self.postcode_regex.search(postcode):
36  
-            raise ValidationError(self.error_messages['invalid'])
37  
-        return postcode
38  
-
39  
-class UKCountySelect(Select):
40  
-    """
41  
-    A Select widget that uses a list of UK Counties/Regions as its choices.
42  
-    """
43  
-    def __init__(self, attrs=None):
44  
-        from uk_regions import UK_REGION_CHOICES
45  
-        super(UKCountySelect, self).__init__(attrs, choices=UK_REGION_CHOICES)
46  
-
47  
-class UKNationSelect(Select):
48  
-    """
49  
-    A Select widget that uses a list of UK Nations as its choices.
50  
-    """
51  
-    def __init__(self, attrs=None):
52  
-        from uk_regions import UK_NATIONS_CHOICES
53  
-        super(UKNationSelect, self).__init__(attrs, choices=UK_NATIONS_CHOICES)
  8
+UKPostcodeField = forms.GBPostcodeField
  9
+UKCountySelect = forms.GBCountySelect
  10
+UKNationSelect = forms.GBNationSelect
109  django/contrib/localflavor/uk/uk_regions.py
... ...
@@ -1,97 +1,12 @@
1  
-"""
2  
-Sources:
3  
-    English regions: http://www.statistics.gov.uk/geography/downloads/31_10_01_REGION_names_and_codes_12_00.xls
4  
-    Northern Ireland regions: http://en.wikipedia.org/wiki/List_of_Irish_counties_by_area
5  
-    Welsh regions: http://en.wikipedia.org/wiki/Preserved_counties_of_Wales
6  
-    Scottish regions: http://en.wikipedia.org/wiki/Regions_and_districts_of_Scotland
7  
-"""
8  
-from django.utils.translation import ugettext_lazy as _
9  
-
10  
-ENGLAND_REGION_CHOICES = (
11  
-    ("Bedfordshire", _("Bedfordshire")),
12  
-    ("Buckinghamshire", _("Buckinghamshire")),
13  
-    ("Cambridgeshire", ("Cambridgeshire")),
14  
-    ("Cheshire", _("Cheshire")),
15  
-    ("Cornwall and Isles of Scilly", _("Cornwall and Isles of Scilly")),
16  
-    ("Cumbria", _("Cumbria")),
17  
-    ("Derbyshire", _("Derbyshire")),
18  
-    ("Devon", _("Devon")),
19  
-    ("Dorset", _("Dorset")),
20  
-    ("Durham", _("Durham")),
21  
-    ("East Sussex", _("East Sussex")),
22  
-    ("Essex", _("Essex")),
23  
-    ("Gloucestershire", _("Gloucestershire")),
24  
-    ("Greater London", _("Greater London")),
25  
-    ("Greater Manchester", _("Greater Manchester")),
26  
-    ("Hampshire", _("Hampshire")),
27  
-    ("Hertfordshire", _("Hertfordshire")),
28  
-    ("Kent", _("Kent")),
29  
-    ("Lancashire", _("Lancashire")),
30  
-    ("Leicestershire", _("Leicestershire")),
31  
-    ("Lincolnshire", _("Lincolnshire")),
32  
-    ("Merseyside", _("Merseyside")),
33  
-    ("Norfolk", _("Norfolk")),
34  
-    ("North Yorkshire", _("North Yorkshire")),
35  
-    ("Northamptonshire", _("Northamptonshire")),
36  
-    ("Northumberland", _("Northumberland")),
37  
-    ("Nottinghamshire", _("Nottinghamshire")),
38  
-    ("Oxfordshire", _("Oxfordshire")),
39  
-    ("Shropshire", _("Shropshire")),
40  
-    ("Somerset", _("Somerset")),
41  
-    ("South Yorkshire", _("South Yorkshire")),
42  
-    ("Staffordshire", _("Staffordshire")),
43  
-    ("Suffolk", _("Suffolk")),
44  
-    ("Surrey", _("Surrey")),
45  
-    ("Tyne and Wear", _("Tyne and Wear")),
46  
-    ("Warwickshire", _("Warwickshire")),
47  
-    ("West Midlands", _("West Midlands")),
48  
-    ("West Sussex", _("West Sussex")),
49  
-    ("West Yorkshire", _("West Yorkshire")),
50  
-    ("Wiltshire", _("Wiltshire")),
51  
-    ("Worcestershire", _("Worcestershire")),
52  
-)
53  
-
54  
-NORTHERN_IRELAND_REGION_CHOICES = (
55  
-    ("County Antrim", _("County Antrim")),
56  
-    ("County Armagh", _("County Armagh")),
57  
-    ("County Down", _("County Down")),
58  
-    ("County Fermanagh", _("County Fermanagh")),
59  
-    ("County Londonderry", _("County Londonderry")),
60  
-    ("County Tyrone", _("County Tyrone")),
61  
-)
62  
-
63  
-WALES_REGION_CHOICES = (
64  
-    ("Clwyd", _("Clwyd")),
65  
-    ("Dyfed", _("Dyfed")),
66  
-    ("Gwent", _("Gwent")),
67  
-    ("Gwynedd", _("Gwynedd")),
68  
-    ("Mid Glamorgan", _("Mid Glamorgan")),
69  
-    ("Powys", _("Powys")),
70  
-    ("South Glamorgan", _("South Glamorgan")),
71  
-    ("West Glamorgan", _("West Glamorgan")),
72  
-)
73  
-
74  
-SCOTTISH_REGION_CHOICES = (
75  
-    ("Borders", _("Borders")),
76  
-    ("Central Scotland", _("Central Scotland")),
77  
-    ("Dumfries and Galloway", _("Dumfries and Galloway")),
78  
-    ("Fife", _("Fife")),
79  
-    ("Grampian", _("Grampian")),
80  
-    ("Highland", _("Highland")),
81  
-    ("Lothian", _("Lothian")),
82  
-    ("Orkney Islands", _("Orkney Islands")),
83  
-    ("Shetland Islands", _("Shetland Islands")),
84  
-    ("Strathclyde", _("Strathclyde")),
85  
-    ("Tayside", _("Tayside")),
86  
-    ("Western Isles", _("Western Isles")),
87  
-)
88  
-
89  
-UK_NATIONS_CHOICES = (
90  
-    ("England", _("England")),
91  
-    ("Northern Ireland", _("Northern Ireland")),
92  
-    ("Scotland", _("Scotland")),
93  
-    ("Wales", _("Wales")),
94  
-)
95  
-
96  
-UK_REGION_CHOICES = ENGLAND_REGION_CHOICES + NORTHERN_IRELAND_REGION_CHOICES + WALES_REGION_CHOICES + SCOTTISH_REGION_CHOICES
97  
-
  1
+from django.contrib.localflavor.gb.gb_regions import (
  2
+    ENGLAND_REGION_CHOICES, NORTHERN_IRELAND_REGION_CHOICES,
  3
+    WALES_REGION_CHOICES, SCOTTISH_REGION_CHOICES, GB_NATIONS_CHOICES,
  4
+    GB_REGION_CHOICES)
  5
+
  6
+import warnings
  7
+warnings.warn(
  8
+    'The "UK" prefix for United Kingdom has been deprecated in favour of the '
  9
+    'GB code. Please use the new GB-prefixed names.', PendingDeprecationWarning)
  10
+
  11
+UK_NATIONS_CHOICES = GB_NATIONS_CHOICES
  12
+UK_REGION_CHOICES  = GB_REGION_CHOICES
6  docs/internals/deprecation.txt
@@ -175,7 +175,6 @@ their deprecation, as per the :ref:`Django deprecation policy
175 175
           combine paths in templates.
176 176
 
177 177
     * 1.6
178  
-
179 178
         * The compatibility modules ``django.utils.copycompat`` and
180 179
           ``django.utils.hashcompat`` as well as the functions
181 180
           ``django.utils.itercompat.all`` and ``django.utils.itercompat.any``
@@ -195,6 +194,11 @@ their deprecation, as per the :ref:`Django deprecation policy
195 194
           is now an alias for :class:`~django.core.cache.backends.memcached.MemcachedCache`.
196 195
           In Django 1.6, the historical alias will be removed.
197 196
 
  197
+        * The UK-prefixed objects of ``django.contrib.localflavor.uk`` will only
  198
+          be accessible through their new GB-prefixed names (GB is the correct
  199
+          ISO 3166 code for United Kingdom). They have been depreacted since the
  200
+          1.4 release.
  201
+
198 202
     * 2.0
199 203
         * ``django.views.defaults.shortcut()``. This function has been moved
200 204
           to ``django.contrib.contenttypes.views.shortcut()`` as part of the
10  docs/ref/contrib/localflavor.txt
@@ -123,7 +123,7 @@ Here's an example of how to use them::
123 123
 .. _Sweden: `Sweden (se)`_
124 124
 .. _Switzerland: `Switzerland (ch)`_
125 125
 .. _Turkey: `Turkey (tr)`_
126  
-.. _United Kingdom: `United Kingdom (uk)`_
  126
+.. _United Kingdom: `United Kingdom (gb)`_
127 127
 .. _United States of America: `United States of America (us)`_
128 128
 .. _Uruguay: `Uruguay (uy)`_
129 129
 
@@ -1054,20 +1054,20 @@ Turkey (``tr``)
1054 1054
 
1055 1055
     A ``select`` widget that uses a list of Turkish provinces as its choices.
1056 1056
 
1057  
-United Kingdom (``uk``)
  1057
+United Kingdom (``gb``)
1058 1058
 =======================
1059 1059
 
1060  
-.. class:: uk.forms.UKPostcodeField
  1060
+.. class:: gb.forms.GBPostcodeField
1061 1061
 
1062 1062
     A form field that validates input as a UK postcode. The regular
1063 1063
     expression used is sourced from the schema for British Standard BS7666
1064 1064
     address types at http://www.cabinetoffice.gov.uk/media/291293/bs7666-v2-0.xml.
1065 1065
 
1066  
-.. class:: uk.forms.UKCountySelect
  1066
+.. class:: gb.forms.GBCountySelect
1067 1067
 
1068 1068
     A ``Select`` widget that uses a list of UK counties/regions as its choices.
1069 1069
 
1070  
-.. class:: uk.forms.UKNationSelect
  1070
+.. class:: gb.forms.GBNationSelect
1071 1071
 
1072 1072
     A ``Select`` widget that uses a list of UK nations as its choices.
1073 1073
 
0  localflavor/gb/__init__.py b/django/contrib/localflavor/gb/__init__.py
No changes.
10  tests/regressiontests/forms/localflavor/uk.py → tests/regressiontests/forms/localflavor/gb.py
... ...
@@ -1,10 +1,10 @@
1  
-from django.contrib.localflavor.uk.forms import UKPostcodeField
  1
+from django.contrib.localflavor.gb.forms import GBPostcodeField
2 2
 
3 3
 from utils import LocalFlavorTestCase
4 4
 
5 5
 
6  
-class UKLocalFlavorTests(LocalFlavorTestCase):
7  
-    def test_UKPostcodeField(self):
  6
+class GBLocalFlavorTests(LocalFlavorTestCase):
  7
+    def test_GBPostcodeField(self):
8 8
         error_invalid = [u'Enter a valid postcode.']
9 9
         valid = {
10 10
             'BT32 4PX': 'BT32 4PX',
@@ -21,10 +21,10 @@ def test_UKPostcodeField(self):
21 21
             '1NV4L1D': error_invalid,
22 22
             ' b0gUS': error_invalid,
23 23
         }
24  
-        self.assertFieldOutput(UKPostcodeField, valid, invalid)
  24
+        self.assertFieldOutput(GBPostcodeField, valid, invalid)
25 25
         valid = {}
26 26
         invalid = {
27 27
             '1NV 4L1D': [u'Enter a bloody postcode!'],
28 28
         }
29 29
         kwargs = {'error_messages': {'invalid': 'Enter a bloody postcode!'}}
30  
-        self.assertFieldOutput(UKPostcodeField, valid, invalid, field_kwargs=kwargs)
  30
+        self.assertFieldOutput(GBPostcodeField, valid, invalid, field_kwargs=kwargs)
2  tests/regressiontests/forms/localflavortests.py
@@ -12,6 +12,7 @@
12 12
 from localflavor.es import ESLocalFlavorTests
13 13
 from localflavor.fi import FILocalFlavorTests
14 14
 from localflavor.fr import FRLocalFlavorTests
  15
+from localflavor.gb import GBLocalFlavorTests
15 16
 from localflavor.generic import GenericLocalFlavorTests
16 17
 from localflavor.hr import HRLocalFlavorTests
17 18
 from localflavor.id import IDLocalFlavorTests
@@ -29,7 +30,6 @@
29 30
 from localflavor.se import SELocalFlavorTests
30 31
 from localflavor.sk import SKLocalFlavorTests
31 32
 from localflavor.tr import TRLocalFlavorTests
32  
-from localflavor.uk import UKLocalFlavorTests
33 33
 from localflavor.us import USLocalFlavorTests
34 34
 from localflavor.uy import UYLocalFlavorTests
35 35
 from localflavor.za import ZALocalFlavorTests
2  tests/regressiontests/forms/tests/__init__.py
@@ -26,6 +26,7 @@
26 26
     ESLocalFlavorTests,
27 27
     FILocalFlavorTests,
28 28
     FRLocalFlavorTests,
  29
+    GBLocalFlavorTests,
29 30
     GenericLocalFlavorTests,
30 31
     HRLocalFlavorTests,
31 32
     IDLocalFlavorTests,
@@ -43,7 +44,6 @@
43 44
     SELocalFlavorTests,
44 45
     SKLocalFlavorTests,
45 46
     TRLocalFlavorTests,
46  
-    UKLocalFlavorTests,
47 47
     USLocalFlavorTests,
48 48
     UYLocalFlavorTests,
49 49
     ZALocalFlavorTests

0 notes on commit f486044

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