Code

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@…>, 7 years ago)

Just changed maxlength (deprecated) to max_length

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revisão 6786)
4+++ django/db/models/fields/__init__.py (cópia de trabalho)
5@@ -1072,3 +1072,31 @@
6 
7     def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=False, follow=True):
8         return [oldforms.HiddenField(name_prefix + self.name)]
9+
10+class LanguageField(CharField):
11+    def __init__(self, *args, **kwargs):
12+        from django.contrib.localflavor.generic import isValidLanguage
13+
14+        kwargs.setdefault('max_length', 5)
15+        kwargs.setdefault('choices', settings.LANGUAGES)
16+        kwargs.setdefault('validator_list', []).append(isValidLanguage)
17+       
18+        super(CharField, self).__init__(*args, **kwargs)
19+
20+    def get_internal_type(self):
21+        return "CharField"
22+
23+class CountryField(CharField):
24+    def __init__(self, *args, **kwargs):
25+        from django.contrib.localflavor.generic import COUNTRIES
26+        from django.contrib.localflavor.generic import isValidCountry
27+
28+        kwargs.setdefault('max_length', 2)
29+        kwargs.setdefault('choices', COUNTRIES)
30+        kwargs.setdefault('validator_list', []).append(isValidCountry)
31+
32+        super(CharField, self).__init__(*args, **kwargs)
33+
34+    def get_internal_type(self):
35+        return "CharField"
36+
37Index: django/contrib/localflavor/generic/__init__.py
38===================================================================
39--- django/contrib/localflavor/generic/__init__.py      (revisão 6786)
40+++ django/contrib/localflavor/generic/__init__.py      (cópia de trabalho)
41@@ -0,0 +1,256 @@
42+from django.utils.translation import ugettext as _
43+from django.core.validators import ValidationError
44+from django.conf import settings
45+
46+# Countries list - ISO 3166-1993 (E)
47+# http://xml.coverpages.org/country3166.html
48+COUNTRIES = (
49+    ('AD', _('Andorra')),
50+    ('AE', _('United Arab Emirates')),
51+    ('AF', _('Afghanistan')),
52+    ('AG', _('Antigua & Barbuda')),
53+    ('AI', _('Anguilla')),
54+    ('AL', _('Albania')),
55+    ('AM', _('Armenia')),
56+    ('AN', _('Netherlands Antilles')),
57+    ('AO', _('Angola')),
58+    ('AQ', _('Antarctica')),
59+    ('AR', _('Argentina')),
60+    ('AS', _('American Samoa')),
61+    ('AT', _('Austria')),
62+    ('AU', _('Australia')),
63+    ('AW', _('Aruba')),
64+    ('AZ', _('Azerbaijan')),
65+    ('BA', _('Bosnia and Herzegovina')),
66+    ('BB', _('Barbados')),
67+    ('BD', _('Bangladesh')),
68+    ('BE', _('Belgium')),
69+    ('BF', _('Burkina Faso')),
70+    ('BG', _('Bulgaria')),
71+    ('BH', _('Bahrain')),
72+    ('BI', _('Burundi')),
73+    ('BJ', _('Benin')),
74+    ('BM', _('Bermuda')),
75+    ('BN', _('Brunei Darussalam')),
76+    ('BO', _('Bolivia')),
77+    ('BR', _('Brazil')),
78+    ('BS', _('Bahama')),
79+    ('BT', _('Bhutan')),
80+    ('BV', _('Bouvet Island')),
81+    ('BW', _('Botswana')),
82+    ('BY', _('Belarus')),
83+    ('BZ', _('Belize')),
84+    ('CA', _('Canada')),
85+    ('CC', _('Cocos (Keeling) Islands')),
86+    ('CF', _('Central African Republic')),
87+    ('CG', _('Congo')),
88+    ('CH', _('Switzerland')),
89+    ('CI', _('Ivory Coast')),
90+    ('CK', _('Cook Iislands')),
91+    ('CL', _('Chile')),
92+    ('CM', _('Cameroon')),
93+    ('CN', _('China')),
94+    ('CO', _('Colombia')),
95+    ('CR', _('Costa Rica')),
96+    ('CU', _('Cuba')),
97+    ('CV', _('Cape Verde')),
98+    ('CX', _('Christmas Island')),
99+    ('CY', _('Cyprus')),
100+    ('CZ', _('Czech Republic')),
101+    ('DE', _('Germany')),
102+    ('DJ', _('Djibouti')),
103+    ('DK', _('Denmark')),
104+    ('DM', _('Dominica')),
105+    ('DO', _('Dominican Republic')),
106+    ('DZ', _('Algeria')),
107+    ('EC', _('Ecuador')),
108+    ('EE', _('Estonia')),
109+    ('EG', _('Egypt')),
110+    ('EH', _('Western Sahara')),
111+    ('ER', _('Eritrea')),
112+    ('ES', _('Spain')),
113+    ('ET', _('Ethiopia')),
114+    ('FI', _('Finland')),
115+    ('FJ', _('Fiji')),
116+    ('FK', _('Falkland Islands (Malvinas)')),
117+    ('FM', _('Micronesia')),
118+    ('FO', _('Faroe Islands')),
119+    ('FR', _('France')),
120+    ('FX', _('France, Metropolitan')),
121+    ('GA', _('Gabon')),
122+    ('GB', _('United Kingdom (Great Britain)')),
123+    ('GD', _('Grenada')),
124+    ('GE', _('Georgia')),
125+    ('GF', _('French Guiana')),
126+    ('GH', _('Ghana')),
127+    ('GI', _('Gibraltar')),
128+    ('GL', _('Greenland')),
129+    ('GM', _('Gambia')),
130+    ('GN', _('Guinea')),
131+    ('GP', _('Guadeloupe')),
132+    ('GQ', _('Equatorial Guinea')),
133+    ('GR', _('Greece')),
134+    ('GS', _('South Georgia and the South Sandwich Islands')),
135+    ('GT', _('Guatemala')),
136+    ('GU', _('Guam')),
137+    ('GW', _('Guinea-Bissau')),
138+    ('GY', _('Guyana')),
139+    ('HK', _('Hong Kong')),
140+    ('HM', _('Heard & McDonald Islands')),
141+    ('HN', _('Honduras')),
142+    ('HR', _('Croatia')),
143+    ('HT', _('Haiti')),
144+    ('HU', _('Hungary')),
145+    ('ID', _('Indonesia')),
146+    ('IE', _('Ireland')),
147+    ('IL', _('Israel')),
148+    ('IN', _('India')),
149+    ('IO', _('British Indian Ocean Territory')),
150+    ('IQ', _('Iraq')),
151+    ('IR', _('Islamic Republic of Iran')),
152+    ('IS', _('Iceland')),
153+    ('IT', _('Italy')),
154+    ('JM', _('Jamaica')),
155+    ('JO', _('Jordan')),
156+    ('JP', _('Japan')),
157+    ('KE', _('Kenya')),
158+    ('KG', _('Kyrgyzstan')),
159+    ('KH', _('Cambodia')),
160+    ('KI', _('Kiribati')),
161+    ('KM', _('Comoros')),
162+    ('KN', _('St. Kitts and Nevis')),
163+    ('KP', _('Korea, Democratic People\'s Republic of')),
164+    ('KR', _('Korea, Republic of')),
165+    ('KW', _('Kuwait')),
166+    ('KY', _('Cayman Islands')),
167+    ('KZ', _('Kazakhstan')),
168+    ('LA', _('Lao People\'s Democratic Republic')),
169+    ('LB', _('Lebanon')),
170+    ('LC', _('Saint Lucia')),
171+    ('LI', _('Liechtenstein')),
172+    ('LK', _('Sri Lanka')),
173+    ('LR', _('Liberia')),
174+    ('LS', _('Lesotho')),
175+    ('LT', _('Lithuania')),
176+    ('LU', _('Luxembourg')),
177+    ('LV', _('Latvia')),
178+    ('LY', _('Libyan Arab Jamahiriya')),
179+    ('MA', _('Morocco')),
180+    ('MC', _('Monaco')),
181+    ('MD', _('Moldova, Republic of')),
182+    ('MG', _('Madagascar')),
183+    ('MH', _('Marshall Islands')),
184+    ('ML', _('Mali')),
185+    ('MN', _('Mongolia')),
186+    ('MM', _('Myanmar')),
187+    ('MO', _('Macau')),
188+    ('MP', _('Northern Mariana Islands')),
189+    ('MQ', _('Martinique')),
190+    ('MR', _('Mauritania')),
191+    ('MS', _('Monserrat')),
192+    ('MT', _('Malta')),
193+    ('MU', _('Mauritius')),
194+    ('MV', _('Maldives')),
195+    ('MW', _('Malawi')),
196+    ('MX', _('Mexico')),
197+    ('MY', _('Malaysia')),
198+    ('MZ', _('Mozambique')),
199+    ('NA', _('Namibia')),
200+    ('NC', _('New Caledonia')),
201+    ('NE', _('Niger')),
202+    ('NF', _('Norfolk Island')),
203+    ('NG', _('Nigeria')),
204+    ('NI', _('Nicaragua')),
205+    ('NL', _('Netherlands')),
206+    ('NO', _('Norway')),
207+    ('NP', _('Nepal')),
208+    ('NR', _('Nauru')),
209+    ('NU', _('Niue')),
210+    ('NZ', _('New Zealand')),
211+    ('OM', _('Oman')),
212+    ('PA', _('Panama')),
213+    ('PE', _('Peru')),
214+    ('PF', _('French Polynesia')),
215+    ('PG', _('Papua New Guinea')),
216+    ('PH', _('Philippines')),
217+    ('PK', _('Pakistan')),
218+    ('PL', _('Poland')),
219+    ('PM', _('St. Pierre & Miquelon')),
220+    ('PN', _('Pitcairn')),
221+    ('PR', _('Puerto Rico')),
222+    ('PT', _('Portugal')),
223+    ('PW', _('Palau')),
224+    ('PY', _('Paraguay')),
225+    ('QA', _('Qatar')),
226+    ('RE', _('Reunion')),
227+    ('RO', _('Romania')),
228+    ('RU', _('Russian Federation')),
229+    ('RW', _('Rwanda')),
230+    ('SA', _('Saudi Arabia')),
231+    ('SB', _('Solomon Islands')),
232+    ('SC', _('Seychelles')),
233+    ('SD', _('Sudan')),
234+    ('SE', _('Sweden')),
235+    ('SG', _('Singapore')),
236+    ('SH', _('St. Helena')),
237+    ('SI', _('Slovenia')),
238+    ('SJ', _('Svalbard & Jan Mayen Islands')),
239+    ('SK', _('Slovakia')),
240+    ('SL', _('Sierra Leone')),
241+    ('SM', _('San Marino')),
242+    ('SN', _('Senegal')),
243+    ('SO', _('Somalia')),
244+    ('SR', _('Suriname')),
245+    ('ST', _('Sao Tome & Principe')),
246+    ('SV', _('El Salvador')),
247+    ('SY', _('Syrian Arab Republic')),
248+    ('SZ', _('Swaziland')),
249+    ('TC', _('Turks & Caicos Islands')),
250+    ('TD', _('Chad')),
251+    ('TF', _('French Southern Territories')),
252+    ('TG', _('Togo')),
253+    ('TH', _('Thailand')),
254+    ('TJ', _('Tajikistan')),
255+    ('TK', _('Tokelau')),
256+    ('TM', _('Turkmenistan')),
257+    ('TN', _('Tunisia')),
258+    ('TO', _('Tonga')),
259+    ('TP', _('East Timor')),
260+    ('TR', _('Turkey')),
261+    ('TT', _('Trinidad & Tobago')),
262+    ('TV', _('Tuvalu')),
263+    ('TW', _('Taiwan, Province of China')),
264+    ('TZ', _('Tanzania, United Republic of')),
265+    ('UA', _('Ukraine')),
266+    ('UG', _('Uganda')),
267+    ('UM', _('United States Minor Outlying Islands')),
268+    ('US', _('United States of America')),
269+    ('UY', _('Uruguay')),
270+    ('UZ', _('Uzbekistan')),
271+    ('VA', _('Vatican City State (Holy See)')),
272+    ('VC', _('St. Vincent & the Grenadines')),
273+    ('VE', _('Venezuela')),
274+    ('VG', _('British Virgin Islands')),
275+    ('VI', _('United States Virgin Islands')),
276+    ('VN', _('Viet Nam')),
277+    ('VU', _('Vanuatu')),
278+    ('WF', _('Wallis & Futuna Islands')),
279+    ('WS', _('Samoa')),
280+    ('YE', _('Yemen')),
281+    ('YT', _('Mayotte')),
282+    ('YU', _('Yugoslavia')),
283+    ('ZA', _('South Africa')),
284+    ('ZM', _('Zambia')),
285+    ('ZR', _('Zaire')),
286+    ('ZW', _('Zimbabwe')),
287+    ('ZZ', _('Unknown or unspecified country')),
288+)
289+
290+def isValidLanguage(field_data, all_data):
291+    if not field_data in [lang[0] for lang in settings.LANGUAGES]:
292+        raise ValidationError, _("This value must be in LANGUAGES setting in settings.py module.")
293+
294+def isValidCountry(field_data, all_data):
295+    if not field_data in [lang[0] for lang in COUNTRIES]:
296+        raise ValidationError, _("This value must be in COUNTRIES setting in localflavor.generic package.")
297+
298Index: tests/modeltests/countries/__init__.py
299===================================================================
300Index: tests/modeltests/countries/models.py
301===================================================================
302--- tests/modeltests/countries/models.py        (revisão 0)
303+++ tests/modeltests/countries/models.py        (revisão 0)
304@@ -0,0 +1,37 @@
305+"""
306+Test of LanguageField and CountryField fields.
307+Task #5446
308+"""
309+
310+from django.db import models
311+
312+class Person(models.Model):
313+    name = models.CharField(max_length=20)
314+    language = models.LanguageField()
315+    country = models.CountryField()
316+
317+    def __unicode__(self):
318+        return self.name
319+
320+__test__ = {'API_TESTS':"""
321+>>> tarsila = Person(name='Tarsila', language='pt-br', country='BR')
322+>>> tarsila.save()
323+>>> tarsila.language
324+'pt-br'
325+>>> tarsila.country
326+'BR'
327+>>> tarsila.get_language_display()
328+u'Brazilian'
329+>>> tarsila.get_country_display()
330+u'Brazil'
331+>>> mychelle = Person(name='Mychelle', language='en', country='US')
332+>>> mychelle.save()
333+>>> mychelle.language
334+'en'
335+>>> mychelle.country
336+'US'
337+>>> mychelle.get_language_display()
338+u'English'
339+>>> mychelle.get_country_display()
340+u'United States of America'
341+"""}
342Index: docs/model-api.txt
343===================================================================
344--- docs/model-api.txt  (revisão 6786)
345+++ docs/model-api.txt  (cópia de trabalho)
346@@ -156,6 +156,16 @@
347 A field of integers separated by commas. As in ``CharField``, the ``max_length``
348 argument is required.
349 
350+``CountryField``
351+~~~~~~~~~~~~~~~~~
352+
353+An uppercase two-letter ISO 3166-1993 (E) standard country code. You can see more
354+about these codes at this `Country Code List`_.
355+
356+.. _`Country Code List`: http://xml.coverpages.org/country3166.html
357+
358+The admin represents this as an ``<select>`` with a large list of valid countries.
359+
360 ``DateField``
361 ~~~~~~~~~~~~~
362 
363@@ -388,6 +398,14 @@
364 
365 The admin represents this as an ``<input type="text">`` (a single-line input).
366 
367+``LanguageField``
368+~~~~~~~~~~~~~~~~~
369+
370+A field that accept user put a language code. The languages list is taked from then
371+``LANGUAGES`` setting.
372+
373+The admin represents this as an ``<select>`` with a large list of valid languages.
374+
375 ``NullBooleanField``
376 ~~~~~~~~~~~~~~~~~~~~
377 
378Index: django/newforms/widgets.py
379===================================================================
380--- django/newforms/widgets.py  (revision 6914)
381+++ django/newforms/widgets.py  (working copy)
382@@ -426,7 +426,7 @@
383             if id_:
384                 final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
385             output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
386-        return self.format_output(output)
387+        return mark_safe(self.format_output(output))
388 
389     def id_for_label(self, id_):
390         # See the comment for RadioSelect.id_for_label()
391
392Index: django/newforms/widgets.py
393===================================================================
394--- django/newforms/widgets.py  (revision 6914)
395+++ django/newforms/widgets.py  (working copy)
396@@ -426,7 +426,7 @@
397             if id_:
398                 final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
399             output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
400-        return self.format_output(output)
401+        return mark_safe(self.format_output(output))
402 
403     def id_for_label(self, id_):
404         # See the comment for RadioSelect.id_for_label()