From f4860448ddad760172b4dde0323140e99d78a55f Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Tue, 3 May 2011 11:52:20 +0000 Subject: [PATCH] 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 --- django/contrib/localflavor/gb/__init__.py | 0 django/contrib/localflavor/gb/forms.py | 53 +++++++++ django/contrib/localflavor/gb/gb_regions.py | 97 ++++++++++++++++ django/contrib/localflavor/uk/forms.py | 59 ++-------- django/contrib/localflavor/uk/uk_regions.py | 105 ++---------------- docs/internals/deprecation.txt | 6 +- docs/ref/contrib/localflavor.txt | 10 +- .../forms/localflavor/{uk.py => gb.py} | 10 +- .../regressiontests/forms/localflavortests.py | 2 +- tests/regressiontests/forms/tests/__init__.py | 2 +- 10 files changed, 185 insertions(+), 159 deletions(-) create mode 100644 django/contrib/localflavor/gb/__init__.py create mode 100644 django/contrib/localflavor/gb/forms.py create mode 100644 django/contrib/localflavor/gb/gb_regions.py rename tests/regressiontests/forms/localflavor/{uk.py => gb.py} (73%) diff --git a/django/contrib/localflavor/gb/__init__.py b/django/contrib/localflavor/gb/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/localflavor/gb/forms.py b/django/contrib/localflavor/gb/forms.py new file mode 100644 index 0000000000..0f7d91a4a9 --- /dev/null +++ b/django/contrib/localflavor/gb/forms.py @@ -0,0 +1,53 @@ +""" +GB-specific Form helpers +""" + +import re + +from django.forms.fields import CharField, Select +from django.forms import ValidationError +from django.utils.translation import ugettext_lazy as _ + +class GBPostcodeField(CharField): + """ + A form field that validates its input is a UK postcode. + + The regular expression used is sourced from the schema for British Standard + BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd + + The value is uppercased and a space added in the correct place, if required. + """ + default_error_messages = { + 'invalid': _(u'Enter a valid postcode.'), + } + outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])' + incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}' + postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern)) + space_regex = re.compile(r' *(%s)$' % incode_pattern) + + def clean(self, value): + value = super(GBPostcodeField, self).clean(value) + if value == u'': + return value + postcode = value.upper().strip() + # Put a single space before the incode (second part). + postcode = self.space_regex.sub(r' \1', postcode) + if not self.postcode_regex.search(postcode): + raise ValidationError(self.error_messages['invalid']) + return postcode + +class GBCountySelect(Select): + """ + A Select widget that uses a list of UK Counties/Regions as its choices. + """ + def __init__(self, attrs=None): + from gb_regions import GB_REGION_CHOICES + super(GBCountySelect, self).__init__(attrs, choices=GB_REGION_CHOICES) + +class GBNationSelect(Select): + """ + A Select widget that uses a list of UK Nations as its choices. + """ + def __init__(self, attrs=None): + from gb_regions import GB_NATIONS_CHOICES + super(GBNationSelect, self).__init__(attrs, choices=GB_NATIONS_CHOICES) diff --git a/django/contrib/localflavor/gb/gb_regions.py b/django/contrib/localflavor/gb/gb_regions.py new file mode 100644 index 0000000000..c5f5dd7b76 --- /dev/null +++ b/django/contrib/localflavor/gb/gb_regions.py @@ -0,0 +1,97 @@ +""" +Sources: + English regions: http://www.statistics.gov.uk/geography/downloads/31_10_01_REGION_names_and_codes_12_00.xls + Northern Ireland regions: http://en.wikipedia.org/wiki/List_of_Irish_counties_by_area + Welsh regions: http://en.wikipedia.org/wiki/Preserved_counties_of_Wales + Scottish regions: http://en.wikipedia.org/wiki/Regions_and_districts_of_Scotland +""" +from django.utils.translation import ugettext_lazy as _ + +ENGLAND_REGION_CHOICES = ( + ("Bedfordshire", _("Bedfordshire")), + ("Buckinghamshire", _("Buckinghamshire")), + ("Cambridgeshire", ("Cambridgeshire")), + ("Cheshire", _("Cheshire")), + ("Cornwall and Isles of Scilly", _("Cornwall and Isles of Scilly")), + ("Cumbria", _("Cumbria")), + ("Derbyshire", _("Derbyshire")), + ("Devon", _("Devon")), + ("Dorset", _("Dorset")), + ("Durham", _("Durham")), + ("East Sussex", _("East Sussex")), + ("Essex", _("Essex")), + ("Gloucestershire", _("Gloucestershire")), + ("Greater London", _("Greater London")), + ("Greater Manchester", _("Greater Manchester")), + ("Hampshire", _("Hampshire")), + ("Hertfordshire", _("Hertfordshire")), + ("Kent", _("Kent")), + ("Lancashire", _("Lancashire")), + ("Leicestershire", _("Leicestershire")), + ("Lincolnshire", _("Lincolnshire")), + ("Merseyside", _("Merseyside")), + ("Norfolk", _("Norfolk")), + ("North Yorkshire", _("North Yorkshire")), + ("Northamptonshire", _("Northamptonshire")), + ("Northumberland", _("Northumberland")), + ("Nottinghamshire", _("Nottinghamshire")), + ("Oxfordshire", _("Oxfordshire")), + ("Shropshire", _("Shropshire")), + ("Somerset", _("Somerset")), + ("South Yorkshire", _("South Yorkshire")), + ("Staffordshire", _("Staffordshire")), + ("Suffolk", _("Suffolk")), + ("Surrey", _("Surrey")), + ("Tyne and Wear", _("Tyne and Wear")), + ("Warwickshire", _("Warwickshire")), + ("West Midlands", _("West Midlands")), + ("West Sussex", _("West Sussex")), + ("West Yorkshire", _("West Yorkshire")), + ("Wiltshire", _("Wiltshire")), + ("Worcestershire", _("Worcestershire")), +) + +NORTHERN_IRELAND_REGION_CHOICES = ( + ("County Antrim", _("County Antrim")), + ("County Armagh", _("County Armagh")), + ("County Down", _("County Down")), + ("County Fermanagh", _("County Fermanagh")), + ("County Londonderry", _("County Londonderry")), + ("County Tyrone", _("County Tyrone")), +) + +WALES_REGION_CHOICES = ( + ("Clwyd", _("Clwyd")), + ("Dyfed", _("Dyfed")), + ("Gwent", _("Gwent")), + ("Gwynedd", _("Gwynedd")), + ("Mid Glamorgan", _("Mid Glamorgan")), + ("Powys", _("Powys")), + ("South Glamorgan", _("South Glamorgan")), + ("West Glamorgan", _("West Glamorgan")), +) + +SCOTTISH_REGION_CHOICES = ( + ("Borders", _("Borders")), + ("Central Scotland", _("Central Scotland")), + ("Dumfries and Galloway", _("Dumfries and Galloway")), + ("Fife", _("Fife")), + ("Grampian", _("Grampian")), + ("Highland", _("Highland")), + ("Lothian", _("Lothian")), + ("Orkney Islands", _("Orkney Islands")), + ("Shetland Islands", _("Shetland Islands")), + ("Strathclyde", _("Strathclyde")), + ("Tayside", _("Tayside")), + ("Western Isles", _("Western Isles")), +) + +GB_NATIONS_CHOICES = ( + ("England", _("England")), + ("Northern Ireland", _("Northern Ireland")), + ("Scotland", _("Scotland")), + ("Wales", _("Wales")), +) + +GB_REGION_CHOICES = ENGLAND_REGION_CHOICES + NORTHERN_IRELAND_REGION_CHOICES + WALES_REGION_CHOICES + SCOTTISH_REGION_CHOICES + diff --git a/django/contrib/localflavor/uk/forms.py b/django/contrib/localflavor/uk/forms.py index aafe9734db..60e63fb490 100644 --- a/django/contrib/localflavor/uk/forms.py +++ b/django/contrib/localflavor/uk/forms.py @@ -1,53 +1,10 @@ -""" -UK-specific Form helpers -""" +from django.contrib.localflavor.gb import forms -import re +import warnings +warnings.warn( + 'The "UK" prefix for United Kingdom has been deprecated in favour of the ' + 'GB code. Please use the new GB-prefixed names.', PendingDeprecationWarning) -from django.forms.fields import CharField, Select -from django.forms import ValidationError -from django.utils.translation import ugettext_lazy as _ - -class UKPostcodeField(CharField): - """ - A form field that validates its input is a UK postcode. - - The regular expression used is sourced from the schema for British Standard - BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd - - The value is uppercased and a space added in the correct place, if required. - """ - default_error_messages = { - 'invalid': _(u'Enter a valid postcode.'), - } - outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])' - incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}' - postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern)) - space_regex = re.compile(r' *(%s)$' % incode_pattern) - - def clean(self, value): - value = super(UKPostcodeField, self).clean(value) - if value == u'': - return value - postcode = value.upper().strip() - # Put a single space before the incode (second part). - postcode = self.space_regex.sub(r' \1', postcode) - if not self.postcode_regex.search(postcode): - raise ValidationError(self.error_messages['invalid']) - return postcode - -class UKCountySelect(Select): - """ - A Select widget that uses a list of UK Counties/Regions as its choices. - """ - def __init__(self, attrs=None): - from uk_regions import UK_REGION_CHOICES - super(UKCountySelect, self).__init__(attrs, choices=UK_REGION_CHOICES) - -class UKNationSelect(Select): - """ - A Select widget that uses a list of UK Nations as its choices. - """ - def __init__(self, attrs=None): - from uk_regions import UK_NATIONS_CHOICES - super(UKNationSelect, self).__init__(attrs, choices=UK_NATIONS_CHOICES) +UKPostcodeField = forms.GBPostcodeField +UKCountySelect = forms.GBCountySelect +UKNationSelect = forms.GBNationSelect diff --git a/django/contrib/localflavor/uk/uk_regions.py b/django/contrib/localflavor/uk/uk_regions.py index dd92f5e758..25d056c653 100644 --- a/django/contrib/localflavor/uk/uk_regions.py +++ b/django/contrib/localflavor/uk/uk_regions.py @@ -1,97 +1,12 @@ -""" -Sources: - English regions: http://www.statistics.gov.uk/geography/downloads/31_10_01_REGION_names_and_codes_12_00.xls - Northern Ireland regions: http://en.wikipedia.org/wiki/List_of_Irish_counties_by_area - Welsh regions: http://en.wikipedia.org/wiki/Preserved_counties_of_Wales - Scottish regions: http://en.wikipedia.org/wiki/Regions_and_districts_of_Scotland -""" -from django.utils.translation import ugettext_lazy as _ +from django.contrib.localflavor.gb.gb_regions import ( + ENGLAND_REGION_CHOICES, NORTHERN_IRELAND_REGION_CHOICES, + WALES_REGION_CHOICES, SCOTTISH_REGION_CHOICES, GB_NATIONS_CHOICES, + GB_REGION_CHOICES) -ENGLAND_REGION_CHOICES = ( - ("Bedfordshire", _("Bedfordshire")), - ("Buckinghamshire", _("Buckinghamshire")), - ("Cambridgeshire", ("Cambridgeshire")), - ("Cheshire", _("Cheshire")), - ("Cornwall and Isles of Scilly", _("Cornwall and Isles of Scilly")), - ("Cumbria", _("Cumbria")), - ("Derbyshire", _("Derbyshire")), - ("Devon", _("Devon")), - ("Dorset", _("Dorset")), - ("Durham", _("Durham")), - ("East Sussex", _("East Sussex")), - ("Essex", _("Essex")), - ("Gloucestershire", _("Gloucestershire")), - ("Greater London", _("Greater London")), - ("Greater Manchester", _("Greater Manchester")), - ("Hampshire", _("Hampshire")), - ("Hertfordshire", _("Hertfordshire")), - ("Kent", _("Kent")), - ("Lancashire", _("Lancashire")), - ("Leicestershire", _("Leicestershire")), - ("Lincolnshire", _("Lincolnshire")), - ("Merseyside", _("Merseyside")), - ("Norfolk", _("Norfolk")), - ("North Yorkshire", _("North Yorkshire")), - ("Northamptonshire", _("Northamptonshire")), - ("Northumberland", _("Northumberland")), - ("Nottinghamshire", _("Nottinghamshire")), - ("Oxfordshire", _("Oxfordshire")), - ("Shropshire", _("Shropshire")), - ("Somerset", _("Somerset")), - ("South Yorkshire", _("South Yorkshire")), - ("Staffordshire", _("Staffordshire")), - ("Suffolk", _("Suffolk")), - ("Surrey", _("Surrey")), - ("Tyne and Wear", _("Tyne and Wear")), - ("Warwickshire", _("Warwickshire")), - ("West Midlands", _("West Midlands")), - ("West Sussex", _("West Sussex")), - ("West Yorkshire", _("West Yorkshire")), - ("Wiltshire", _("Wiltshire")), - ("Worcestershire", _("Worcestershire")), -) - -NORTHERN_IRELAND_REGION_CHOICES = ( - ("County Antrim", _("County Antrim")), - ("County Armagh", _("County Armagh")), - ("County Down", _("County Down")), - ("County Fermanagh", _("County Fermanagh")), - ("County Londonderry", _("County Londonderry")), - ("County Tyrone", _("County Tyrone")), -) - -WALES_REGION_CHOICES = ( - ("Clwyd", _("Clwyd")), - ("Dyfed", _("Dyfed")), - ("Gwent", _("Gwent")), - ("Gwynedd", _("Gwynedd")), - ("Mid Glamorgan", _("Mid Glamorgan")), - ("Powys", _("Powys")), - ("South Glamorgan", _("South Glamorgan")), - ("West Glamorgan", _("West Glamorgan")), -) - -SCOTTISH_REGION_CHOICES = ( - ("Borders", _("Borders")), - ("Central Scotland", _("Central Scotland")), - ("Dumfries and Galloway", _("Dumfries and Galloway")), - ("Fife", _("Fife")), - ("Grampian", _("Grampian")), - ("Highland", _("Highland")), - ("Lothian", _("Lothian")), - ("Orkney Islands", _("Orkney Islands")), - ("Shetland Islands", _("Shetland Islands")), - ("Strathclyde", _("Strathclyde")), - ("Tayside", _("Tayside")), - ("Western Isles", _("Western Isles")), -) - -UK_NATIONS_CHOICES = ( - ("England", _("England")), - ("Northern Ireland", _("Northern Ireland")), - ("Scotland", _("Scotland")), - ("Wales", _("Wales")), -) - -UK_REGION_CHOICES = ENGLAND_REGION_CHOICES + NORTHERN_IRELAND_REGION_CHOICES + WALES_REGION_CHOICES + SCOTTISH_REGION_CHOICES +import warnings +warnings.warn( + 'The "UK" prefix for United Kingdom has been deprecated in favour of the ' + 'GB code. Please use the new GB-prefixed names.', PendingDeprecationWarning) +UK_NATIONS_CHOICES = GB_NATIONS_CHOICES +UK_REGION_CHOICES = GB_REGION_CHOICES diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index a38823f409..98b2138bc1 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -175,7 +175,6 @@ their deprecation, as per the :ref:`Django deprecation policy combine paths in templates. * 1.6 - * The compatibility modules ``django.utils.copycompat`` and ``django.utils.hashcompat`` as well as the functions ``django.utils.itercompat.all`` and ``django.utils.itercompat.any`` @@ -195,6 +194,11 @@ their deprecation, as per the :ref:`Django deprecation policy is now an alias for :class:`~django.core.cache.backends.memcached.MemcachedCache`. In Django 1.6, the historical alias will be removed. + * The UK-prefixed objects of ``django.contrib.localflavor.uk`` will only + be accessible through their new GB-prefixed names (GB is the correct + ISO 3166 code for United Kingdom). They have been depreacted since the + 1.4 release. + * 2.0 * ``django.views.defaults.shortcut()``. This function has been moved to ``django.contrib.contenttypes.views.shortcut()`` as part of the diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index 4935382093..f6dab64217 100644 --- a/docs/ref/contrib/localflavor.txt +++ b/docs/ref/contrib/localflavor.txt @@ -123,7 +123,7 @@ Here's an example of how to use them:: .. _Sweden: `Sweden (se)`_ .. _Switzerland: `Switzerland (ch)`_ .. _Turkey: `Turkey (tr)`_ -.. _United Kingdom: `United Kingdom (uk)`_ +.. _United Kingdom: `United Kingdom (gb)`_ .. _United States of America: `United States of America (us)`_ .. _Uruguay: `Uruguay (uy)`_ @@ -1054,20 +1054,20 @@ Turkey (``tr``) A ``select`` widget that uses a list of Turkish provinces as its choices. -United Kingdom (``uk``) +United Kingdom (``gb``) ======================= -.. class:: uk.forms.UKPostcodeField +.. class:: gb.forms.GBPostcodeField A form field that validates input as a UK postcode. The regular expression used is sourced from the schema for British Standard BS7666 address types at http://www.cabinetoffice.gov.uk/media/291293/bs7666-v2-0.xml. -.. class:: uk.forms.UKCountySelect +.. class:: gb.forms.GBCountySelect A ``Select`` widget that uses a list of UK counties/regions as its choices. -.. class:: uk.forms.UKNationSelect +.. class:: gb.forms.GBNationSelect A ``Select`` widget that uses a list of UK nations as its choices. diff --git a/tests/regressiontests/forms/localflavor/uk.py b/tests/regressiontests/forms/localflavor/gb.py similarity index 73% rename from tests/regressiontests/forms/localflavor/uk.py rename to tests/regressiontests/forms/localflavor/gb.py index 6fd536f936..7f23ba58c9 100644 --- a/tests/regressiontests/forms/localflavor/uk.py +++ b/tests/regressiontests/forms/localflavor/gb.py @@ -1,10 +1,10 @@ -from django.contrib.localflavor.uk.forms import UKPostcodeField +from django.contrib.localflavor.gb.forms import GBPostcodeField from utils import LocalFlavorTestCase -class UKLocalFlavorTests(LocalFlavorTestCase): - def test_UKPostcodeField(self): +class GBLocalFlavorTests(LocalFlavorTestCase): + def test_GBPostcodeField(self): error_invalid = [u'Enter a valid postcode.'] valid = { 'BT32 4PX': 'BT32 4PX', @@ -21,10 +21,10 @@ class UKLocalFlavorTests(LocalFlavorTestCase): '1NV4L1D': error_invalid, ' b0gUS': error_invalid, } - self.assertFieldOutput(UKPostcodeField, valid, invalid) + self.assertFieldOutput(GBPostcodeField, valid, invalid) valid = {} invalid = { '1NV 4L1D': [u'Enter a bloody postcode!'], } kwargs = {'error_messages': {'invalid': 'Enter a bloody postcode!'}} - self.assertFieldOutput(UKPostcodeField, valid, invalid, field_kwargs=kwargs) + self.assertFieldOutput(GBPostcodeField, valid, invalid, field_kwargs=kwargs) diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py index 73f46a7201..fb501ab9d4 100644 --- a/tests/regressiontests/forms/localflavortests.py +++ b/tests/regressiontests/forms/localflavortests.py @@ -12,6 +12,7 @@ from localflavor.de import DELocalFlavorTests from localflavor.es import ESLocalFlavorTests from localflavor.fi import FILocalFlavorTests from localflavor.fr import FRLocalFlavorTests +from localflavor.gb import GBLocalFlavorTests from localflavor.generic import GenericLocalFlavorTests from localflavor.hr import HRLocalFlavorTests from localflavor.id import IDLocalFlavorTests @@ -29,7 +30,6 @@ from localflavor.ru import RULocalFlavorTests from localflavor.se import SELocalFlavorTests from localflavor.sk import SKLocalFlavorTests from localflavor.tr import TRLocalFlavorTests -from localflavor.uk import UKLocalFlavorTests from localflavor.us import USLocalFlavorTests from localflavor.uy import UYLocalFlavorTests from localflavor.za import ZALocalFlavorTests diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py index 2d8f9e9efe..6366a934cb 100644 --- a/tests/regressiontests/forms/tests/__init__.py +++ b/tests/regressiontests/forms/tests/__init__.py @@ -26,6 +26,7 @@ from regressiontests.forms.localflavortests import ( ESLocalFlavorTests, FILocalFlavorTests, FRLocalFlavorTests, + GBLocalFlavorTests, GenericLocalFlavorTests, HRLocalFlavorTests, IDLocalFlavorTests, @@ -43,7 +44,6 @@ from regressiontests.forms.localflavortests import ( SELocalFlavorTests, SKLocalFlavorTests, TRLocalFlavorTests, - UKLocalFlavorTests, USLocalFlavorTests, UYLocalFlavorTests, ZALocalFlavorTests