Ticket #5526: localflavor_datetimefields.patch
File localflavor_datetimefields.patch, 12.6 KB (added by , 17 years ago) |
---|
-
django/contrib/localflavor/nl/__init__.py
1 LOCALES = ( 2 'nl_NL', 3 'nl_NL.UTF-8', 'nl_NL.ISO8859-1', # Some OSes need these (e.g., FreeBSD) 4 ) -
django/contrib/localflavor/nl/forms.py
8 8 from django.newforms.fields import Field, Select, EMPTY_VALUES 9 9 from django.utils.translation import ugettext as _ 10 10 from django.utils.encoding import smart_unicode 11 from django.contrib.localflavor.generic import forms 12 from django.contrib.localflavor.nl.widgets import DateTimeInput 13 from django.contrib.localflavor.nl import LOCALES 11 14 15 DATE_INPUT_FORMATS = ( 16 '%d-%m-%Y', '%d/%m/%Y', # '25-10-2006', '25/10/2006' 17 '%d/%m/%y', # '25/10/06' 18 '%d %b %Y', '%d %B %Y', # '25 okt 2006', '25 oktober 2006' 19 ) 20 21 DATETIME_INPUT_FORMATS = ( 22 '%d-%m-%Y %H:%M:%S', # '25-10-2006 14:30:59' 23 '%d-%m-%Y %H:%M', # '25-10-2006 14:30' 24 '%d-%m-%Y', # '25-10-2006' 25 '%d/%m/%Y %H:%M:%S', # '25/10/2006 14:30:59' 26 '%d/%m/%Y %H:%M', # '25/10/2006 14:30' 27 '%d/%m/%Y', # '25/10/2006' 28 '%d-%m-%y %H:%M:%S', # '25-10-06 14:30:59' 29 '%d-%m-%y %H:%M', # '25-10-06 14:30' 30 '%d-%m-%y', # '25-10-06' 31 '%d/%m/%y %H:%M:%S', # '25/10/06 14:30:59' 32 '%d/%m/%y %H:%M', # '25/10/06 14:30' 33 '%d/%m/%y', # '25/10/06' 34 ) 35 36 DATE_OUTPUT_FORMAT = '%d-%m-%Y' 37 DATETIME_OUTPUT_FORMAT = '%d-%m-%Y %H:%M:%S' 38 12 39 pc_re = re.compile('^\d{4}[A-Z]{2}$') 13 40 sofi_re = re.compile('^\d{9}$') 14 41 numeric_re = re.compile('^\d+$') … … 90 117 raise ValidationError(msg) 91 118 92 119 return value 120 121 class DateField(forms.DateField): 122 """ 123 A DateField using a Dutch locale. 124 """ 125 widget = DateTimeInput(DATE_OUTPUT_FORMAT) 126 locales = LOCALES 127 def __init__(self, input_formats=DATE_INPUT_FORMATS, *args, **kwargs): 128 super(DateField, self).__init__(input_formats=input_formats, *args, **kwargs) 129 130 class DateTimeField(forms.DateTimeField): 131 """ 132 A DateTimeField using a Dutch locale. 133 """ 134 widget = DateTimeInput(DATETIME_OUTPUT_FORMAT) 135 locales = LOCALES 136 def __init__(self, input_formats=DATETIME_INPUT_FORMATS, *args, **kwargs): 137 super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs) -
django/contrib/localflavor/nl/widgets.py
1 from django.contrib.localflavor.generic import widgets 2 from django.contrib.localflavor.nl import LOCALES 3 4 class DateTimeInput(widgets.DateTimeInput): 5 locales = LOCALES -
django/contrib/localflavor/generic/forms.py
1 import locale 2 1 3 from django import newforms as forms 2 4 3 5 DEFAULT_DATE_INPUT_FORMATS = ( … … 20 22 '%d/%m/%y', # '25/10/06' 21 23 ) 22 24 23 class DateField(forms.DateField):25 def try_locales(locales): 24 26 """ 27 Try to set the locale to one of the specified locales. Stop as soon as 28 one of the locales is available. 29 """ 30 for _locale in locales: 31 try: 32 locale.setlocale(locale.LC_TIME, _locale) 33 break 34 except locale.Error: 35 pass 36 37 class LocalizedDateTimeField(object): 38 locales = () 39 40 def clean(self, value): 41 """ 42 Temporarily replace the current locale with the locale necessary to 43 properly validate given value. 44 """ 45 cur_locale = locale.setlocale(locale.LC_TIME) 46 try: 47 try_locales(self.locales) 48 return super(LocalizedDateTimeField, self).clean(value) 49 finally: 50 locale.setlocale(locale.LC_TIME, cur_locale) 51 52 class DateField(LocalizedDateTimeField, forms.DateField): 53 """ 25 54 A date input field which uses non-US date input formats by default. 26 55 """ 27 56 def __init__(self, input_formats=None, *args, **kwargs): 28 57 input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 29 58 super(DateField, self).__init__(input_formats=input_formats, *args, **kwargs) 30 59 31 class DateTimeField( forms.DateTimeField):60 class DateTimeField(LocalizedDateTimeField, forms.DateTimeField): 32 61 """ 33 62 A date and time input field which uses non-US date and time input formats 34 63 by default. -
django/contrib/localflavor/generic/widgets.py
1 import locale 2 3 from django.newforms.widgets import TextInput 4 from django.contrib.localflavor.generic.forms import try_locales 5 6 class DateTimeInput(TextInput): 7 locales = [] 8 def __init__(self, date_output_format, locales=None, *args, **kwargs): 9 super(DateTimeInput, self).__init__(*args, **kwargs) 10 self.date_output_format = date_output_format 11 self.locales = locales or self.locales 12 13 def render(self, name, value, attrs=None): 14 if hasattr(value, 'strftime'): 15 try: 16 cur_locale = locale.setlocale(locale.LC_TIME) 17 try_locales(self.locales) 18 str_value = value.strftime(self.date_output_format) 19 return super(DateTimeInput, self).render(name, str_value, attrs) 20 finally: 21 locale.setlocale(locale.LC_TIME, cur_locale) 22 else: 23 return super(DateTimeInput, self).render(name, value, attrs) -
tests/regressiontests/forms/localflavor.py
1529 1529 >>> s.render('provinces', 'OV') 1530 1530 u'<select name="provinces">\n<option value="DR">Drente</option>\n<option value="FL">Flevoland</option>\n<option value="FR">Friesland</option>\n<option value="GL">Gelderland</option>\n<option value="GR">Groningen</option>\n<option value="LB">Limburg</option>\n<option value="NB">Noord-Brabant</option>\n<option value="NH">Noord-Holland</option>\n<option value="OV" selected="selected">Overijssel</option>\n<option value="UT">Utrecht</option>\n<option value="ZE">Zeeland</option>\n<option value="ZH">Zuid-Holland</option>\n</select>' 1531 1531 1532 # Dutch DateField ########################################################### 1533 1534 >>> import datetime 1535 >>> from django.contrib.localflavor.nl.forms import DateField 1536 >>> f = DateField() 1537 >>> f.clean(datetime.date(2006, 10, 25)) 1538 datetime.date(2006, 10, 25) 1539 >>> f.clean('25-10-2006') 1540 datetime.date(2006, 10, 25) 1541 >>> f.clean('25/10/2006') 1542 datetime.date(2006, 10, 25) 1543 >>> f.clean('25/10/06') 1544 datetime.date(2006, 10, 25) 1545 >>> f.clean('25 okt 2006') 1546 datetime.date(2006, 10, 25) 1547 >>> f.clean('25 oktober 2006') 1548 datetime.date(2006, 10, 25) 1549 >>> f.clean('2006-10-25') 1550 Traceback (most recent call last): 1551 ... 1552 ValidationError: [u'Enter a valid date.'] 1553 >>> f.clean('October 25 2006') 1554 Traceback (most recent call last): 1555 ... 1556 ValidationError: [u'Enter a valid date.'] 1557 1558 DateField accepts an optional input_formats parameter: 1559 >>> f = DateField(input_formats=['%Y %B %d']) 1560 >>> f.clean(datetime.date(2006, 10, 25)) 1561 datetime.date(2006, 10, 25) 1562 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) 1563 datetime.date(2006, 10, 25) 1564 >>> f.clean('2006 oktober 1') 1565 datetime.date(2006, 10, 1) 1566 1567 The input_formats parameter overrides all default input formats, 1568 so the default formats won't work unless you specify them: 1569 >>> f.clean('25-10-2006') 1570 Traceback (most recent call last): 1571 ... 1572 ValidationError: [u'Enter a valid date.'] 1573 >>> f.clean('25/10/2006') 1574 Traceback (most recent call last): 1575 ... 1576 ValidationError: [u'Enter a valid date.'] 1577 >>> f.clean('25/10/06') 1578 Traceback (most recent call last): 1579 ... 1580 ValidationError: [u'Enter a valid date.'] 1581 1582 # Dutch DateTimeField ####################################################### 1583 1584 >>> import datetime 1585 >>> from django.contrib.localflavor.nl.forms import DateTimeField 1586 >>> f = DateTimeField() 1587 >>> f.clean(datetime.date(2006, 10, 25)) 1588 datetime.datetime(2006, 10, 25, 0, 0) 1589 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) 1590 datetime.datetime(2006, 10, 25, 14, 30) 1591 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) 1592 datetime.datetime(2006, 10, 25, 14, 30, 59) 1593 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) 1594 datetime.datetime(2006, 10, 25, 14, 30, 59, 200) 1595 >>> f.clean('25-10-2006 14:30:45') 1596 datetime.datetime(2006, 10, 25, 14, 30, 45) 1597 >>> f.clean('25-10-2006 14:30:00') 1598 datetime.datetime(2006, 10, 25, 14, 30) 1599 >>> f.clean('25-10-2006 14:30') 1600 datetime.datetime(2006, 10, 25, 14, 30) 1601 >>> f.clean('25-10-2006') 1602 datetime.datetime(2006, 10, 25, 0, 0) 1603 >>> f.clean('25/10/2006 14:30:45') 1604 datetime.datetime(2006, 10, 25, 14, 30, 45) 1605 >>> f.clean('25/10/2006 14:30:00') 1606 datetime.datetime(2006, 10, 25, 14, 30) 1607 >>> f.clean('25/10/2006 14:30') 1608 datetime.datetime(2006, 10, 25, 14, 30) 1609 >>> f.clean('25/10/2006') 1610 datetime.datetime(2006, 10, 25, 0, 0) 1611 >>> f.clean('25/10/06 14:30:45') 1612 datetime.datetime(2006, 10, 25, 14, 30, 45) 1613 >>> f.clean('25/10/06 14:30:00') 1614 datetime.datetime(2006, 10, 25, 14, 30) 1615 >>> f.clean('25/10/06 14:30') 1616 datetime.datetime(2006, 10, 25, 14, 30) 1617 >>> f.clean('25/10/06') 1618 datetime.datetime(2006, 10, 25, 0, 0) 1619 >>> f.clean('hello') 1620 Traceback (most recent call last): 1621 ... 1622 ValidationError: [u'Enter a valid date/time.'] 1623 >>> f = DateTimeField(input_formats=['%Y %m %d %H:%M']) 1624 >>> f.clean(datetime.date(2006, 10, 25)) 1625 datetime.datetime(2006, 10, 25, 0, 0) 1626 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30)) 1627 datetime.datetime(2006, 10, 25, 14, 30) 1628 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59)) 1629 datetime.datetime(2006, 10, 25, 14, 30, 59) 1630 >>> f.clean(datetime.datetime(2006, 10, 25, 14, 30, 59, 200)) 1631 datetime.datetime(2006, 10, 25, 14, 30, 59, 200) 1632 >>> f.clean('2006 10 25 14:30') 1633 datetime.datetime(2006, 10, 25, 14, 30) 1634 1635 The input_formats parameter overrides all default input formats, 1636 so the default formats won't work unless you specify them: 1637 >>> f.clean('25-10-2006 14:30:45') 1638 Traceback (most recent call last): 1639 ... 1640 ValidationError: [u'Enter a valid date/time.'] 1641 1642 >>> f = DateTimeField(required=False) 1643 >>> f.clean(None) 1644 >>> repr(f.clean(None)) 1645 'None' 1646 >>> f.clean('') 1647 >>> repr(f.clean('')) 1648 'None' 1649 1650 # A form using DateTimeInput with a Dutch locale ############################ 1651 1652 >>> import django.newforms as forms 1653 >>> from django.contrib.localflavor.nl.widgets import DateTimeInput 1654 >>> class NLDateTimeForm(forms.Form): 1655 ... date = DateField() 1656 ... date_custom_format = DateField(widget=DateTimeInput('%d %B %Y')) 1657 ... datetime = DateTimeField() 1658 ... foo = DateField() 1659 >>> f = NLDateTimeForm(initial={ 1660 ... 'date': datetime.date(2006, 10, 25), 1661 ... 'date_custom_format': datetime.date(2006, 10, 25), 1662 ... 'datetime': datetime.datetime(2006, 10, 25, 10, 11, 12), 1663 ... 'foo': 'bar', 1664 ... }) 1665 >>> print f['date'] 1666 <input type="text" name="date" value="25-10-2006" id="id_date" /> 1667 >>> print f['date_custom_format'] 1668 <input type="text" name="date_custom_format" value="25 oktober 2006" id="id_date_custom_format" /> 1669 >>> print f['datetime'] 1670 <input type="text" name="datetime" value="25-10-2006 10:11:12" id="id_datetime" /> 1671 >>> print f['foo'] 1672 <input type="text" name="foo" value="bar" id="id_foo" /> 1673 1532 1674 # ARProvinceField ############################################################# 1533 1675 1534 1676 >>> from django.contrib.localflavor.ar.forms import ARProvinceSelect … … 2194 2336 >>> repr(f.clean('')) 2195 2337 'None' 2196 2338 2339 # LocalizedDateTimeField ##################################################### 2340 >>> # Make sure the locale is reset after use. 2341 >>> import locale 2342 >>> from django.contrib.localflavor.nl.forms import DateField 2343 >>> locale_before = locale.setlocale(locale.LC_TIME) 2344 >>> f = DateField() 2345 >>> try: 2346 ... f.clean('foo') 2347 ... except: 2348 ... pass 2349 >>> locale_after = locale.setlocale(locale.LC_TIME) 2350 >>> locale_before == locale_after 2351 True 2352 2197 2353 """