Ticket #5446: country_and_language_fields_trunk.3.patch

File country_and_language_fields_trunk.3.patch, 12.1 KB (added by lcordier, 16 years ago)

Make it patch against the latest trunk, some things moved around, and validator_list seems to be gone.

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

     
    431431        defaults.update(kwargs)
    432432        return super(CommaSeparatedIntegerField, self).formfield(**defaults)
    433433
     434class CountryField(CharField):
     435    def __init__(self, *args, **kwargs):
     436        from django.contrib.localflavor.generic import COUNTRIES
     437       
     438        kwargs.setdefault('max_length', 2)
     439        kwargs.setdefault('choices', COUNTRIES)
     440       
     441        super(CharField, self).__init__(*args, **kwargs)
     442
     443    def get_internal_type(self):
     444        return "CharField"
     445
    434446ansi_date_re = re.compile(r'^\d{4}-\d{1,2}-\d{1,2}$')
    435447
    436448class DateField(Field):
     
    701713        defaults.update(kwargs)
    702714        return super(IPAddressField, self).formfield(**defaults)
    703715
     716class LanguageField(CharField):
     717    def __init__(self, *args, **kwargs):
     718        kwargs.setdefault('max_length', 5)
     719        super(CharField, self).__init__(*args, **kwargs)
     720
     721    def get_internal_type(self):
     722        return "CharField"
     723
    704724class NullBooleanField(Field):
    705725    empty_strings_allowed = False
    706726    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 isValidLanguage(field_data, all_data):
     251    if not field_data in [lang[0] for lang in settings.LANGUAGES]:
     252        raise ValidationError, _("This value must be in LANGUAGES setting in settings.py module.")
     253
     254def isValidCountry(field_data, all_data):
     255    if not field_data in [lang[0] for lang in COUNTRIES]:
     256        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

     
    239239    Lau Bech Lauritzen
    240240    Rune Rønde Laursen <runerl@skjoldhoej.dk>
    241241    Eugene Lazutkin <http://lazutkin.com/blog/>
    242     lcordier@point45.com
     242    Louis Cordier <lcordier@gmail.com>
    243243    Jeong-Min Lee <falsetru@gmail.com>
    244244    Jannis Leidel <jl@websushi.org>
    245245    Christopher Lenz <http://www.cmlenz.net/>
  • docs/ref/models/fields.txt

     
    343343A field of integers separated by commas. As in :class:`CharField`, the
    344344:attr:`~CharField.max_length` argument is required.
    345345
     346
     347``CountryField``
     348----------------
     349
     350.. class:: CountryField(max_length=2, [**options])
     351
     352A country field, an uppercase two-letter ISO 3166-1 standard country code.
     353Countries are defined in `django.contrib.localflavor.generic.COUNTRIES`.
     354See also: `Country Code List`_
     355
     356.. _`Country Code List`: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
     357
     358
    346359``DateField``
    347360-------------
    348361
     
    631644An IP address, in string format (e.g. "192.0.2.30"). The admin represents this
    632645as an ``<input type="text">`` (a single-line input).
    633646
     647``LanguageField``
     648-----------------
     649
     650.. class:: LanguageField([**options])
     651
     652A language code. Generally you would check languages against
     653``settings.LANGUAGES``.
     654
     655
    634656``NullBooleanField``
    635657--------------------
    636658
Back to Top