Ticket #5446: country_and_language_fields_trunk.20100409.patch

File country_and_language_fields_trunk.20100409.patch, 22.2 KB (added by Hector Lecuanda, 15 years ago)

Apply against latest trunk, moved field definitions to localflavor so as to keep contrib as 'rm -rf -able', added working tests

  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index b06bbd6..dc9d5dd 100644
    a b answer newbie questions, and generally made Django that much better:  
    113113    colin@owlfish.com
    114114    crankycoder@gmail.com
    115115    Paul Collier <paul@paul-collier.com>
     116    Louis Cordier <lcordier@gmail.com>
    116117    Robert Coup
    117118    Pete Crosier <pete.crosier@gmail.com>
    118119    Matt Croydon <http://www.postneo.com/>
    answer newbie questions, and generally made Django that much better:  
    285286    Rune Rønde Laursen <runerl@skjoldhoej.dk>
    286287    Mark Lavin <markdlavin@gmail.com>
    287288    Eugene Lazutkin <http://lazutkin.com/blog/>
    288     lcordier@point45.com
     289    Hector Lecuanda <hector+djdev@lecuanda.com>
    289290    Jeong-Min Lee <falsetru@gmail.com>
    290291    Tai Lee <real.human@mrmachine.net>
    291292    Christopher Lenz <http://www.cmlenz.net/>
  • django/contrib/localflavor/generic/__init__.py

    diff --git a/django/contrib/localflavor/generic/__init__.py b/django/contrib/localflavor/generic/__init__.py
    index e69de29..8b1f47f 100644
    a b  
     1from django.utils.translation import ugettext as _
     2from django.core.exceptions import ValidationError
     3from django.conf import settings
     4
     5# Countries list - ISO 3166-1
     6# http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
     7
     8COUNTRIES = (
     9    ('AD', _('Andorra')),
     10    ('AE', _('United Arab Emirates')),
     11    ('AF', _('Afghanistan')),
     12    ('AG', _('Antigua & Barbuda')),
     13    ('AI', _('Anguilla')),
     14    ('AL', _('Albania')),
     15    ('AM', _('Armenia')),
     16    ('AN', _('Netherlands Antilles')),
     17    ('AO', _('Angola')),
     18    ('AQ', _('Antarctica')),
     19    ('AR', _('Argentina')),
     20    ('AS', _('American Samoa')),
     21    ('AT', _('Austria')),
     22    ('AU', _('Australia')),
     23    ('AW', _('Aruba')),
     24    ('AZ', _('Azerbaijan')),
     25    ('BA', _('Bosnia and Herzegovina')),
     26    ('BB', _('Barbados')),
     27    ('BD', _('Bangladesh')),
     28    ('BE', _('Belgium')),
     29    ('BF', _('Burkina Faso')),
     30    ('BG', _('Bulgaria')),
     31    ('BH', _('Bahrain')),
     32    ('BI', _('Burundi')),
     33    ('BJ', _('Benin')),
     34    ('BM', _('Bermuda')),
     35    ('BN', _('Brunei Darussalam')),
     36    ('BO', _('Bolivia')),
     37    ('BR', _('Brazil')),
     38    ('BS', _('Bahama')),
     39    ('BT', _('Bhutan')),
     40    ('BV', _('Bouvet Island')),
     41    ('BW', _('Botswana')),
     42    ('BY', _('Belarus')),
     43    ('BZ', _('Belize')),
     44    ('CA', _('Canada')),
     45    ('CC', _('Cocos (Keeling) Islands')),
     46    ('CF', _('Central African Republic')),
     47    ('CG', _('Congo')),
     48    ('CH', _('Switzerland')),
     49    ('CI', _('Ivory Coast')),
     50    ('CK', _('Cook Iislands')),
     51    ('CL', _('Chile')),
     52    ('CM', _('Cameroon')),
     53    ('CN', _('China')),
     54    ('CO', _('Colombia')),
     55    ('CR', _('Costa Rica')),
     56    ('CU', _('Cuba')),
     57    ('CV', _('Cape Verde')),
     58    ('CX', _('Christmas Island')),
     59    ('CY', _('Cyprus')),
     60    ('CZ', _('Czech Republic')),
     61    ('DE', _('Germany')),
     62    ('DJ', _('Djibouti')),
     63    ('DK', _('Denmark')),
     64    ('DM', _('Dominica')),
     65    ('DO', _('Dominican Republic')),
     66    ('DZ', _('Algeria')),
     67    ('EC', _('Ecuador')),
     68    ('EE', _('Estonia')),
     69    ('EG', _('Egypt')),
     70    ('EH', _('Western Sahara')),
     71    ('ER', _('Eritrea')),
     72    ('ES', _('Spain')),
     73    ('ET', _('Ethiopia')),
     74    ('FI', _('Finland')),
     75    ('FJ', _('Fiji')),
     76    ('FK', _('Falkland Islands (Malvinas)')),
     77    ('FM', _('Micronesia')),
     78    ('FO', _('Faroe Islands')),
     79    ('FR', _('France')),
     80    ('FX', _('France, Metropolitan')),
     81    ('GA', _('Gabon')),
     82    ('GB', _('United Kingdom (Great Britain)')),
     83    ('GD', _('Grenada')),
     84    ('GE', _('Georgia')),
     85    ('GF', _('French Guiana')),
     86    ('GH', _('Ghana')),
     87    ('GI', _('Gibraltar')),
     88    ('GL', _('Greenland')),
     89    ('GM', _('Gambia')),
     90    ('GN', _('Guinea')),
     91    ('GP', _('Guadeloupe')),
     92    ('GQ', _('Equatorial Guinea')),
     93    ('GR', _('Greece')),
     94    ('GS', _('South Georgia and the South Sandwich Islands')),
     95    ('GT', _('Guatemala')),
     96    ('GU', _('Guam')),
     97    ('GW', _('Guinea-Bissau')),
     98    ('GY', _('Guyana')),
     99    ('HK', _('Hong Kong')),
     100    ('HM', _('Heard & McDonald Islands')),
     101    ('HN', _('Honduras')),
     102    ('HR', _('Croatia')),
     103    ('HT', _('Haiti')),
     104    ('HU', _('Hungary')),
     105    ('ID', _('Indonesia')),
     106    ('IE', _('Ireland')),
     107    ('IL', _('Israel')),
     108    ('IN', _('India')),
     109    ('IO', _('British Indian Ocean Territory')),
     110    ('IQ', _('Iraq')),
     111    ('IR', _('Islamic Republic of Iran')),
     112    ('IS', _('Iceland')),
     113    ('IT', _('Italy')),
     114    ('JM', _('Jamaica')),
     115    ('JO', _('Jordan')),
     116    ('JP', _('Japan')),
     117    ('KE', _('Kenya')),
     118    ('KG', _('Kyrgyzstan')),
     119    ('KH', _('Cambodia')),
     120    ('KI', _('Kiribati')),
     121    ('KM', _('Comoros')),
     122    ('KN', _('St. Kitts and Nevis')),
     123    ('KP', _('Korea, Democratic People\'s Republic of')),
     124    ('KR', _('Korea, Republic of')),
     125    ('KW', _('Kuwait')),
     126    ('KY', _('Cayman Islands')),
     127    ('KZ', _('Kazakhstan')),
     128    ('LA', _('Lao People\'s Democratic Republic')),
     129    ('LB', _('Lebanon')),
     130    ('LC', _('Saint Lucia')),
     131    ('LI', _('Liechtenstein')),
     132    ('LK', _('Sri Lanka')),
     133    ('LR', _('Liberia')),
     134    ('LS', _('Lesotho')),
     135    ('LT', _('Lithuania')),
     136    ('LU', _('Luxembourg')),
     137    ('LV', _('Latvia')),
     138    ('LY', _('Libyan Arab Jamahiriya')),
     139    ('MA', _('Morocco')),
     140    ('MC', _('Monaco')),
     141    ('MD', _('Moldova, Republic of')),
     142    ('MG', _('Madagascar')),
     143    ('MH', _('Marshall Islands')),
     144    ('ML', _('Mali')),
     145    ('MN', _('Mongolia')),
     146    ('MM', _('Myanmar')),
     147    ('MO', _('Macau')),
     148    ('MP', _('Northern Mariana Islands')),
     149    ('MQ', _('Martinique')),
     150    ('MR', _('Mauritania')),
     151    ('MS', _('Monserrat')),
     152    ('MT', _('Malta')),
     153    ('MU', _('Mauritius')),
     154    ('MV', _('Maldives')),
     155    ('MW', _('Malawi')),
     156    ('MX', _('Mexico')),
     157    ('MY', _('Malaysia')),
     158    ('MZ', _('Mozambique')),
     159    ('NA', _('Namibia')),
     160    ('NC', _('New Caledonia')),
     161    ('NE', _('Niger')),
     162    ('NF', _('Norfolk Island')),
     163    ('NG', _('Nigeria')),
     164    ('NI', _('Nicaragua')),
     165    ('NL', _('Netherlands')),
     166    ('NO', _('Norway')),
     167    ('NP', _('Nepal')),
     168    ('NR', _('Nauru')),
     169    ('NU', _('Niue')),
     170    ('NZ', _('New Zealand')),
     171    ('OM', _('Oman')),
     172    ('PA', _('Panama')),
     173    ('PE', _('Peru')),
     174    ('PF', _('French Polynesia')),
     175    ('PG', _('Papua New Guinea')),
     176    ('PH', _('Philippines')),
     177    ('PK', _('Pakistan')),
     178    ('PL', _('Poland')),
     179    ('PM', _('St. Pierre & Miquelon')),
     180    ('PN', _('Pitcairn')),
     181    ('PR', _('Puerto Rico')),
     182    ('PT', _('Portugal')),
     183    ('PW', _('Palau')),
     184    ('PY', _('Paraguay')),
     185    ('QA', _('Qatar')),
     186    ('RE', _('Reunion')),
     187    ('RO', _('Romania')),
     188    ('RU', _('Russian Federation')),
     189    ('RW', _('Rwanda')),
     190    ('SA', _('Saudi Arabia')),
     191    ('SB', _('Solomon Islands')),
     192    ('SC', _('Seychelles')),
     193    ('SD', _('Sudan')),
     194    ('SE', _('Sweden')),
     195    ('SG', _('Singapore')),
     196    ('SH', _('St. Helena')),
     197    ('SI', _('Slovenia')),
     198    ('SJ', _('Svalbard & Jan Mayen Islands')),
     199    ('SK', _('Slovakia')),
     200    ('SL', _('Sierra Leone')),
     201    ('SM', _('San Marino')),
     202    ('SN', _('Senegal')),
     203    ('SO', _('Somalia')),
     204    ('SR', _('Suriname')),
     205    ('ST', _('Sao Tome & Principe')),
     206    ('SV', _('El Salvador')),
     207    ('SY', _('Syrian Arab Republic')),
     208    ('SZ', _('Swaziland')),
     209    ('TC', _('Turks & Caicos Islands')),
     210    ('TD', _('Chad')),
     211    ('TF', _('French Southern Territories')),
     212    ('TG', _('Togo')),
     213    ('TH', _('Thailand')),
     214    ('TJ', _('Tajikistan')),
     215    ('TK', _('Tokelau')),
     216    ('TM', _('Turkmenistan')),
     217    ('TN', _('Tunisia')),
     218    ('TO', _('Tonga')),
     219    ('TP', _('East Timor')),
     220    ('TR', _('Turkey')),
     221    ('TT', _('Trinidad & Tobago')),
     222    ('TV', _('Tuvalu')),
     223    ('TW', _('Taiwan, Province of China')),
     224    ('TZ', _('Tanzania, United Republic of')),
     225    ('UA', _('Ukraine')),
     226    ('UG', _('Uganda')),
     227    ('UM', _('United States Minor Outlying Islands')),
     228    ('US', _('United States of America')),
     229    ('UY', _('Uruguay')),
     230    ('UZ', _('Uzbekistan')),
     231    ('VA', _('Vatican City State (Holy See)')),
     232    ('VC', _('St. Vincent & the Grenadines')),
     233    ('VE', _('Venezuela')),
     234    ('VG', _('British Virgin Islands')),
     235    ('VI', _('United States Virgin Islands')),
     236    ('VN', _('Viet Nam')),
     237    ('VU', _('Vanuatu')),
     238    ('WF', _('Wallis & Futuna Islands')),
     239    ('WS', _('Samoa')),
     240    ('YE', _('Yemen')),
     241    ('YT', _('Mayotte')),
     242    ('YU', _('Yugoslavia')),
     243    ('ZA', _('South Africa')),
     244    ('ZM', _('Zambia')),
     245    ('ZR', _('Zaire')),
     246    ('ZW', _('Zimbabwe')),
     247    ('ZZ', _('Unknown or unspecified country')),
     248)
     249
     250def sorted_countries(countries):
     251    """ Sort countries for a given language.
     252        Assume ZZ is the last entry, keep it last.
     253    """
     254    c = list(countries[:-1])
     255    c.sort(key=lambda x: x[1])
     256    c.append(countries[-1])
     257    return(tuple(c))
     258
     259COUNTRIES = sorted_countries(COUNTRIES)
     260
     261def isValidLanguage(field_data, all_data):
     262    if not field_data in [lang[0] for lang in settings.LANGUAGES]:
     263        raise ValidationError, _("This value must be in LANGUAGES setting in settings.py module.")
     264
     265def isValidCountry(field_data, all_data):
     266    if not field_data in [lang[0] for lang in COUNTRIES]:
     267        raise ValidationError, _("This value must be in COUNTRIES setting in localflavor.generic package.")
  • new file django/contrib/localflavor/generic/models.py

    diff --git a/django/contrib/localflavor/generic/models.py b/django/contrib/localflavor/generic/models.py
    new file mode 100644
    index 0000000..92618d3
    - +  
     1from django.db.models.fields import CharField
     2
     3class CountryField(CharField):
     4    def __init__(self, *args, **kwargs):
     5        from django.contrib.localflavor.generic import COUNTRIES
     6
     7        kwargs.setdefault('max_length', 2)
     8        kwargs.setdefault('choices', COUNTRIES)
     9
     10        super(CharField, self).__init__(*args, **kwargs)
     11
     12    def get_internal_type(self):
     13        return "CharField"
     14
     15class LanguageField(CharField):
     16    def __init__(self, *args, **kwargs):
     17        kwargs.setdefault('max_length', 5)
     18        super(CharField, self).__init__(*args, **kwargs)
     19
     20    def get_internal_type(self):
     21        return "CharField"
  • docs/ref/contrib/localflavor.txt

    diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt
    index 1c58e2d..7d7d29e 100644
    a b Here's an example of how to use them::  
    117117.. _United States of America: `United States of America (us)`_
    118118.. _Uruguay: `Uruguay (uy)`_
    119119
     120The generic subpackage also defines a `CountryField` and a `LanguageField` that
     121you can use in your models. They render as a Drop Down and store the ISO-3166
     122code in your database.
     123
     124``CountryField``
     125----------------
     126
     127.. class:: CountryField(max_length=2, [**options])
     128
     129A country field, an uppercase two-letter ISO 3166-1 standard country code.
     130Countries are defined in `django.contrib.localflavor.generic.COUNTRIES`.
     131See also: `Country Code List`_
     132
     133.. _`Country Code List`: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
     134
     135``LanguageField``
     136-----------------
     137
     138.. class:: LanguageField([**options])
     139
     140A language code. Generally you would check languages against
     141``settings.LANGUAGES``.
     142
     143
    120144Adding flavors
    121145==============
    122146
  • tests/regressiontests/forms/localflavor/generic.py

    diff --git a/tests/regressiontests/forms/localflavor/generic.py b/tests/regressiontests/forms/localflavor/generic.py
    index 0dbe30d..2c143fb 100644
    a b ValidationError: [u'Enter a valid date/time.']  
    160160>>> repr(f.clean(''))
    161161'None'
    162162
     163# CountryField ################################################################
     164Create a Model and a Form Model that uses CountryField from localflavor.generic
     165then check the widget and choices
     166
     167>>> from django.db import models
     168>>> from django.forms import ModelForm
     169>>> from django.contrib.localflavor.us.models import USStateField
     170>>> from django.contrib.localflavor.generic.models import CountryField
     171>>> class WorldPlace(models.Model):
     172...     country = CountryField(blank=True)
     173...     country_req = CountryField()
     174...     country_default = CountryField(default="MX", blank=True)
     175...     name = models.CharField(max_length=20)
     176...
     177...     class Meta:
     178...         app_label = 'someapp'
     179...
     180>>> class WorldPlaceForm(ModelForm):
     181...     class Meta:
     182...         model = WorldPlace
     183...
     184>>> form = WorldPlaceForm()
     185>>> form.fields['country'].widget.__class__
     186<class 'django.forms.widgets.Select'>
     187>>> form.fields['country']._choices
     188[('', '---------'), ('AF', u'Afghanistan'), ('AL', u'Albania'), ('DZ', u'Algeria'), ('AS', u'American Samoa'), ('AD', u'Andorra'), ('AO', u'Angola'), ('AI', u'Anguilla'), ('AQ', u'Antarctica'), ('AG', u'Antigua & Barbuda'), ('AR', u'Argentina'), ('AM', u'Armenia'), ('AW', u'Aruba'), ('AU', u'Australia'), ('AT', u'Austria'), ('AZ', u'Azerbaijan'), ('BS', u'Bahama'), ('BH', u'Bahrain'), ('BD', u'Bangladesh'), ('BB', u'Barbados'), ('BY', u'Belarus'), ('BE', u'Belgium'), ('BZ', u'Belize'), ('BJ', u'Benin'), ('BM', u'Bermuda'), ('BT', u'Bhutan'), ('BO', u'Bolivia'), ('BA', u'Bosnia and Herzegovina'), ('BW', u'Botswana'), ('BV', u'Bouvet Island'), ('BR', u'Brazil'), ('IO', u'British Indian Ocean Territory'), ('VG', u'British Virgin Islands'), ('BN', u'Brunei Darussalam'), ('BG', u'Bulgaria'), ('BF', u'Burkina Faso'), ('BI', u'Burundi'), ('KH', u'Cambodia'), ('CM', u'Cameroon'), ('CA', u'Canada'), ('CV', u'Cape Verde'), ('KY', u'Cayman Islands'), ('CF', u'Central African Republic'), ('TD', u'Chad'), ('CL', u'Chile'), ('CN', u'China'), ('CX', u'Christmas Island'), ('CC', u'Cocos (Keeling) Islands'), ('CO', u'Colombia'), ('KM', u'Comoros'), ('CG', u'Congo'), ('CK', u'Cook Iislands'), ('CR', u'Costa Rica'), ('HR', u'Croatia'), ('CU', u'Cuba'), ('CY', u'Cyprus'), ('CZ', u'Czech Republic'), ('DK', u'Denmark'), ('DJ', u'Djibouti'), ('DM', u'Dominica'), ('DO', u'Dominican Republic'), ('TP', u'East Timor'), ('EC', u'Ecuador'), ('EG', u'Egypt'), ('SV', u'El Salvador'), ('GQ', u'Equatorial Guinea'), ('ER', u'Eritrea'), ('EE', u'Estonia'), ('ET', u'Ethiopia'), ('FK', u'Falkland Islands (Malvinas)'), ('FO', u'Faroe Islands'), ('FJ', u'Fiji'), ('FI', u'Finland'), ('FR', u'France'), ('FX', u'France, Metropolitan'), ('GF', u'French Guiana'), ('PF', u'French Polynesia'), ('TF', u'French Southern Territories'), ('GA', u'Gabon'), ('GM', u'Gambia'), ('GE', u'Georgia'), ('DE', u'Germany'), ('GH', u'Ghana'), ('GI', u'Gibraltar'), ('GR', u'Greece'), ('GL', u'Greenland'), ('GD', u'Grenada'), ('GP', u'Guadeloupe'), ('GU', u'Guam'), ('GT', u'Guatemala'), ('GN', u'Guinea'), ('GW', u'Guinea-Bissau'), ('GY', u'Guyana'), ('HT', u'Haiti'), ('HM', u'Heard & McDonald Islands'), ('HN', u'Honduras'), ('HK', u'Hong Kong'), ('HU', u'Hungary'), ('IS', u'Iceland'), ('IN', u'India'), ('ID', u'Indonesia'), ('IQ', u'Iraq'), ('IE', u'Ireland'), ('IR', u'Islamic Republic of Iran'), ('IL', u'Israel'), ('IT', u'Italy'), ('CI', u'Ivory Coast'), ('JM', u'Jamaica'), ('JP', u'Japan'), ('JO', u'Jordan'), ('KZ', u'Kazakhstan'), ('KE', u'Kenya'), ('KI', u'Kiribati'), ('KP', u"Korea, Democratic People's Republic of"), ('KR', u'Korea, Republic of'), ('KW', u'Kuwait'), ('KG', u'Kyrgyzstan'), ('LA', u"Lao People's Democratic Republic"), ('LV', u'Latvia'), ('LB', u'Lebanon'), ('LS', u'Lesotho'), ('LR', u'Liberia'), ('LY', u'Libyan Arab Jamahiriya'), ('LI', u'Liechtenstein'), ('LT', u'Lithuania'), ('LU', u'Luxembourg'), ('MO', u'Macau'), ('MG', u'Madagascar'), ('MW', u'Malawi'), ('MY', u'Malaysia'), ('MV', u'Maldives'), ('ML', u'Mali'), ('MT', u'Malta'), ('MH', u'Marshall Islands'), ('MQ', u'Martinique'), ('MR', u'Mauritania'), ('MU', u'Mauritius'), ('YT', u'Mayotte'), ('MX', u'Mexico'), ('FM', u'Micronesia'), ('MD', u'Moldova, Republic of'), ('MC', u'Monaco'), ('MN', u'Mongolia'), ('MS', u'Monserrat'), ('MA', u'Morocco'), ('MZ', u'Mozambique'), ('MM', u'Myanmar'), ('NA', u'Namibia'), ('NR', u'Nauru'), ('NP', u'Nepal'), ('NL', u'Netherlands'), ('AN', u'Netherlands Antilles'), ('NC', u'New Caledonia'), ('NZ', u'New Zealand'), ('NI', u'Nicaragua'), ('NE', u'Niger'), ('NG', u'Nigeria'), ('NU', u'Niue'), ('NF', u'Norfolk Island'), ('MP', u'Northern Mariana Islands'), ('NO', u'Norway'), ('OM', u'Oman'), ('PK', u'Pakistan'), ('PW', u'Palau'), ('PA', u'Panama'), ('PG', u'Papua New Guinea'), ('PY', u'Paraguay'), ('PE', u'Peru'), ('PH', u'Philippines'), ('PN', u'Pitcairn'), ('PL', u'Poland'), ('PT', u'Portugal'), ('PR', u'Puerto Rico'), ('QA', u'Qatar'), ('RE', u'Reunion'), ('RO', u'Romania'), ('RU', u'Russian Federation'), ('RW', u'Rwanda'), ('LC', u'Saint Lucia'), ('WS', u'Samoa'), ('SM', u'San Marino'), ('ST', u'Sao Tome & Principe'), ('SA', u'Saudi Arabia'), ('SN', u'Senegal'), ('SC', u'Seychelles'), ('SL', u'Sierra Leone'), ('SG', u'Singapore'), ('SK', u'Slovakia'), ('SI', u'Slovenia'), ('SB', u'Solomon Islands'), ('SO', u'Somalia'), ('ZA', u'South Africa'), ('GS', u'South Georgia and the South Sandwich Islands'), ('ES', u'Spain'), ('LK', u'Sri Lanka'), ('SH', u'St. Helena'), ('KN', u'St. Kitts and Nevis'), ('PM', u'St. Pierre & Miquelon'), ('VC', u'St. Vincent & the Grenadines'), ('SD', u'Sudan'), ('SR', u'Suriname'), ('SJ', u'Svalbard & Jan Mayen Islands'), ('SZ', u'Swaziland'), ('SE', u'Sweden'), ('CH', u'Switzerland'), ('SY', u'Syrian Arab Republic'), ('TW', u'Taiwan, Province of China'), ('TJ', u'Tajikistan'), ('TZ', u'Tanzania, United Republic of'), ('TH', u'Thailand'), ('TG', u'Togo'), ('TK', u'Tokelau'), ('TO', u'Tonga'), ('TT', u'Trinidad & Tobago'), ('TN', u'Tunisia'), ('TR', u'Turkey'), ('TM', u'Turkmenistan'), ('TC', u'Turks & Caicos Islands'), ('TV', u'Tuvalu'), ('UG', u'Uganda'), ('UA', u'Ukraine'), ('AE', u'United Arab Emirates'), ('GB', u'United Kingdom (Great Britain)'), ('UM', u'United States Minor Outlying Islands'), ('VI', u'United States Virgin Islands'), ('US', u'United States of America'), ('UY', u'Uruguay'), ('UZ', u'Uzbekistan'), ('VU', u'Vanuatu'), ('VA', u'Vatican City State (Holy See)'), ('VE', u'Venezuela'), ('VN', u'Viet Nam'), ('WF', u'Wallis & Futuna Islands'), ('EH', u'Western Sahara'), ('YE', u'Yemen'), ('YU', u'Yugoslavia'), ('ZR', u'Zaire'), ('ZM', u'Zambia'), ('ZW', u'Zimbabwe'), ('ZZ', u'Unknown or unspecified country')]
    163189"""
  • tests/regressiontests/localflavor/forms.py

    diff --git a/tests/regressiontests/localflavor/forms.py b/tests/regressiontests/localflavor/forms.py
    index 2dd1da6..f0ca6c9 100644
    a b  
    11from django.forms import ModelForm
    2 from models import Place
     2from models import Place, WorldPlace
    33
    44class PlaceForm(ModelForm):
    55    """docstring for PlaceForm"""
    66    class Meta:
    77        model = Place
     8
     9class WorldPlaceForm(ModelForm):
     10    """docstring for WorldPlaceForm"""
     11    class Meta:
     12        model = WorldPlace
  • tests/regressiontests/localflavor/models.py

    diff --git a/tests/regressiontests/localflavor/models.py b/tests/regressiontests/localflavor/models.py
    index f74a505..c3ca518 100644
    a b  
    11from django.db import models
    22from django.contrib.localflavor.us.models import USStateField
     3from django.contrib.localflavor.generic.models import CountryField
    34
    45class Place(models.Model):
    56    state = USStateField(blank=True)
    67    state_req = USStateField()
    78    state_default = USStateField(default="CA", blank=True)
    89    name = models.CharField(max_length=20)
     10
     11class WorldPlace(models.Model):
     12    country = CountryField(blank=True)
     13    country_req = CountryField()
     14    country_default = CountryField(default="MX", blank=True)
     15    name = models.CharField(max_length=20)
     16 
     17    class Meta:
     18        app_label = 'someapp'
  • tests/regressiontests/localflavor/tests.py

    diff --git a/tests/regressiontests/localflavor/tests.py b/tests/regressiontests/localflavor/tests.py
    index 0ea3c52..4b43ec5 100644
    a b class USLocalflavorTests(TestCase):  
    77        self.form = PlaceForm({'state':'GA', 'state_req':'NC', 'name':'impossible'})
    88       
    99    def test_get_display_methods(self):
    10         """Test that the get_*_display() methods are added to the model instances."""
    1110        place = self.form.save()
    1211        self.assertEqual(place.get_state_display(), 'Georgia')
    1312        self.assertEqual(place.get_state_req_display(), 'North Carolina')
    1413   
    1514    def test_required(self):
    16         """Test that required USStateFields throw appropriate errors."""
    1715        form = PlaceForm({'state':'GA', 'name':'Place in GA'})
    1816        self.assertFalse(form.is_valid())
    1917        self.assertEqual(form.errors['state_req'], [u'This field is required.'])
    2018   
    2119    def test_field_blank_option(self):
    22         """Test that the empty option is there."""
    2320        state_select_html = """\
    2421<select name="state" id="id_state">
    2522<option value="">---------</option>
    class USLocalflavorTests(TestCase):  
    8178<option value="WY">Wyoming</option>
    8279</select>"""
    8380        self.assertEqual(str(self.form['state']), state_select_html)
     81
     82class GenericLocalFlavorTests(TestCase):
     83    def setUp(self):
     84        self.form = WorldPlaceForm({'country':'MX', 'country_req':'DE', 'name':'whynot'})
     85        worldplace = self.form.save()
     86        self.assertEqual(place.get_state_display(), 'Mexico')
     87        self.assertEqual(place.get_state_req_display(), 'Germany')
     88
     89    def test_required(self):
     90        form = PlaceForm({'country':'MX', 'name':'Place in Mexico'})
     91        self.assertFalse(form.is_valid())
     92        self.assertEqual(form.errors['country_req'], [u'This field is required.'])
  • tests/regressiontests/serializers_regress/models.py

    diff --git a/tests/regressiontests/serializers_regress/models.py b/tests/regressiontests/serializers_regress/models.py
    index bec0a98..5b62aec 100644
    a b from django.db import models  
    99from django.contrib.contenttypes import generic
    1010from django.contrib.contenttypes.models import ContentType
    1111from django.contrib.localflavor.us.models import USStateField, PhoneNumberField
     12from django.contrib.localflavor.generic.models import CountryField
    1213
    1314# The following classes are for testing basic data
    1415# marshalling, including NULL values, where allowed.
    class TimeData(models.Model):  
    7980class USStateData(models.Model):
    8081    data = USStateField(null=True)
    8182
     83class CountryData(models.Model):
     84    data = CountryField(null=True)
     85
    8286class XMLData(models.Model):
    8387    data = models.XMLField(null=True)
    8488
Back to Top