diff options
| author | Jannis Leidel <jannis@leidel.info> | 2011-05-03 11:52:20 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2011-05-03 11:52:20 +0000 |
| commit | f4860448ddad760172b4dde0323140e99d78a55f (patch) | |
| tree | b3a5d6c02c646bb96e43e16223e35a7f04bd6366 /django | |
| parent | 41a1a89e4ede058f35f2ff6da18a828ac059d785 (diff) | |
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
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/localflavor/gb/__init__.py | 0 | ||||
| -rw-r--r-- | django/contrib/localflavor/gb/forms.py | 53 | ||||
| -rw-r--r-- | django/contrib/localflavor/gb/gb_regions.py | 97 | ||||
| -rw-r--r-- | django/contrib/localflavor/uk/forms.py | 59 | ||||
| -rw-r--r-- | django/contrib/localflavor/uk/uk_regions.py | 105 |
5 files changed, 168 insertions, 146 deletions
diff --git a/django/contrib/localflavor/gb/__init__.py b/django/contrib/localflavor/gb/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/django/contrib/localflavor/gb/__init__.py 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 |
