Ticket #5446: country_and_language_fields_trunk.4.patch

File country_and_language_fields_trunk.4.patch, 12.4 KB (added by lcordier, 15 years ago)

Patch with form-presentation improvements, countries are now sorted.

  • django/db/models/fields/__init__.py

     
    432432        defaults.update(kwargs)
    433433        return super(CharField, self).formfield(**defaults)
    434434
     435class CountryField(CharField):
     436    def __init__(self, *args, **kwargs):
     437        from django.contrib.localflavor.generic import COUNTRIES
     438       
     439        kwargs.setdefault('max_length', 2)
     440        kwargs.setdefault('choices', COUNTRIES)
     441       
     442        super(CharField, self).__init__(*args, **kwargs)
     443
     444    def get_internal_type(self):
     445        return "CharField"
     446
    435447# TODO: Maybe move this into contrib, because it's specialized.
    436448class CommaSeparatedIntegerField(CharField):
    437449    def formfield(self, **kwargs):
     
    728740        defaults.update(kwargs)
    729741        return super(IPAddressField, self).formfield(**defaults)
    730742
     743class LanguageField(CharField):
     744    def __init__(self, *args, **kwargs):
     745        kwargs.setdefault('max_length', 5)
     746        super(CharField, self).__init__(*args, **kwargs)
     747
     748    def get_internal_type(self):
     749        return "CharField"
     750
    731751class NullBooleanField(Field):
    732752    empty_strings_allowed = False
    733753    def __init__(self, *args, **kwargs):
  • django/contrib/localflavor/generic/__init__.py

     
     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.")
  • tests/modeltests/countries/models.py

     
     1"""
     2Test of LanguageField and CountryField fields.
     3Task #5446
     4"""
     5
     6from django.db import models
     7
     8class Person(models.Model):
     9    name = models.CharField(max_length=20)
     10    language = models.LanguageField()
     11    country = models.CountryField()
     12
     13    def __unicode__(self):
     14        return self.name
     15
     16__test__ = {'API_TESTS':"""
     17>>> tarsila = Person(name='Tarsila', language='pt-br', country='BR')
     18>>> tarsila.save()
     19>>> tarsila.language
     20'pt-br'
     21>>> tarsila.country
     22'BR'
     23>>> tarsila.get_language_display()
     24u'Brazilian'
     25>>> tarsila.get_country_display()
     26u'Brazil'
     27>>> mychelle = Person(name='Mychelle', language='en', country='US')
     28>>> mychelle.save()
     29>>> mychelle.language
     30'en'
     31>>> mychelle.country
     32'US'
     33>>> mychelle.get_language_display()
     34u'English'
     35>>> mychelle.get_country_display()
     36u'United States of America'
     37"""}
  • AUTHORS

     
    259259    Lau Bech Lauritzen
    260260    Rune Rønde Laursen <runerl@skjoldhoej.dk>
    261261    Eugene Lazutkin <http://lazutkin.com/blog/>
    262     lcordier@point45.com
     262    Louis Cordier <lcordier@gmail.com>
    263263    Jeong-Min Lee <falsetru@gmail.com>
    264264    Tai Lee <real.human@mrmachine.net>
    265265    Jannis Leidel <jl@websushi.org>
  • docs/ref/models/fields.txt

     
    363363:attr:`~CharField.max_length` argument is required and the note about database
    364364portability mentioned there should be heeded.
    365365
     366``CountryField``
     367----------------
     368
     369.. class:: CountryField(max_length=2, [**options])
     370
     371A country field, an uppercase two-letter ISO 3166-1 standard country code.
     372Countries are defined in `django.contrib.localflavor.generic.COUNTRIES`.
     373See also: `Country Code List`_
     374
     375.. _`Country Code List`: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
     376
    366377``DateField``
    367378-------------
    368379
     
    649660An IP address, in string format (e.g. "192.0.2.30"). The admin represents this
    650661as an ``<input type="text">`` (a single-line input).
    651662
     663``LanguageField``
     664-----------------
     665
     666.. class:: LanguageField([**options])
     667
     668A language code. Generally you would check languages against
     669``settings.LANGUAGES``.
     670
    652671``NullBooleanField``
    653672--------------------
    654673
Back to Top