Ticket #5446: country_and_language_fields_maxlength.diff

File country_and_language_fields_maxlength.diff, 12.8 KB (added by Martin Conte Mac Donell <Reflejo@…>, 17 years ago)

Just changed maxlength (deprecated) to max_length

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

     
    10721072
    10731073    def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
    10741074        return [oldforms.HiddenField(name_prefix + self.name)]
     1075
     1076class LanguageField(CharField):
     1077    def __init__(self, *args, **kwargs):
     1078        from django.contrib.localflavor.generic import isValidLanguage
     1079
     1080        kwargs.setdefault('max_length', 5)
     1081        kwargs.setdefault('choices', settings.LANGUAGES)
     1082        kwargs.setdefault('validator_list', []).append(isValidLanguage)
     1083       
     1084        super(CharField, self).__init__(*args, **kwargs)
     1085
     1086    def get_internal_type(self):
     1087        return "CharField"
     1088
     1089class CountryField(CharField):
     1090    def __init__(self, *args, **kwargs):
     1091        from django.contrib.localflavor.generic import COUNTRIES
     1092        from django.contrib.localflavor.generic import isValidCountry
     1093
     1094        kwargs.setdefault('max_length', 2)
     1095        kwargs.setdefault('choices', COUNTRIES)
     1096        kwargs.setdefault('validator_list', []).append(isValidCountry)
     1097
     1098        super(CharField, self).__init__(*args, **kwargs)
     1099
     1100    def get_internal_type(self):
     1101        return "CharField"
     1102
  • django/contrib/localflavor/generic/__init__.py

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

     
    156156A field of integers separated by commas. As in ``CharField``, the ``max_length``
    157157argument is required.
    158158
     159``CountryField``
     160~~~~~~~~~~~~~~~~~
     161
     162An uppercase two-letter ISO 3166-1993 (E) standard country code. You can see more
     163about these codes at this `Country Code List`_.
     164
     165.. _`Country Code List`: http://xml.coverpages.org/country3166.html
     166
     167The admin represents this as an ``<select>`` with a large list of valid countries.
     168
    159169``DateField``
    160170~~~~~~~~~~~~~
    161171
     
    388398
    389399The admin represents this as an ``<input type="text">`` (a single-line input).
    390400
     401``LanguageField``
     402~~~~~~~~~~~~~~~~~
     403
     404A field that accept user put a language code. The languages list is taked from then
     405``LANGUAGES`` setting.
     406
     407The admin represents this as an ``<select>`` with a large list of valid languages.
     408
    391409``NullBooleanField``
    392410~~~~~~~~~~~~~~~~~~~~
    393411
  • django/newforms/widgets.py

     
    426426            if id_:
    427427                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
    428428            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
    429         return self.format_output(output)
     429        return mark_safe(self.format_output(output))
    430430
    431431    def id_for_label(self, id_):
    432432        # See the comment for RadioSelect.id_for_label()
  • django/newforms/widgets.py

     
    426426            if id_:
    427427                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
    428428            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
    429         return self.format_output(output)
     429        return mark_safe(self.format_output(output))
    430430
    431431    def id_for_label(self, id_):
    432432        # See the comment for RadioSelect.id_for_label()
Back to Top