Ticket #8962: 8962-datetime-format-r9843.diff

File 8962-datetime-format-r9843.diff, 14.4 KB (added by Tai Lee, 16 years ago)

Updated to r9843.

  • django/forms/fields.py

     
    2828from django.utils.encoding import smart_unicode, smart_str
    2929
    3030from util import ErrorList, ValidationError
    31 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget
     31from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget
    3232from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
    3333
    3434__all__ = (
     
    283283)
    284284
    285285class DateField(Field):
     286    widget = DateInput
    286287    default_error_messages = {
    287288        'invalid': _(u'Enter a valid date.'),
    288289    }
     
    850851        'invalid_time': _(u'Enter a valid time.'),
    851852    }
    852853
    853     def __init__(self, *args, **kwargs):
     854    def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs):
    854855        errors = self.default_error_messages.copy()
    855856        if 'error_messages' in kwargs:
    856857            errors.update(kwargs['error_messages'])
    857858        fields = (
    858             DateField(error_messages={'invalid': errors['invalid_date']}),
    859             TimeField(error_messages={'invalid': errors['invalid_time']}),
     859            DateField(input_formats=input_date_formats, error_messages={'invalid': errors['invalid_date']}),
     860            TimeField(input_formats=input_time_formats, error_messages={'invalid': errors['invalid_time']}),
    860861        )
    861862        super(SplitDateTimeField, self).__init__(fields, *args, **kwargs)
    862863
  • django/forms/widgets.py

     
    2323__all__ = (
    2424    'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
    2525    'HiddenInput', 'MultipleHiddenInput',
    26     'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
     26    'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
    2727    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
    2828    'CheckboxSelectMultiple', 'MultiWidget',
    2929    'SplitDateTimeWidget',
     
    285285        return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs),
    286286                conditional_escape(force_unicode(value))))
    287287
     288class DateInput(Input):
     289    input_type = 'text'
     290    format = '%Y-%m-%d'     # '2006-10-25'
     291
     292    def __init__(self, attrs=None, format=None):
     293        super(DateInput, self).__init__(attrs)
     294        if format:
     295            self.format = format
     296
     297    def render(self, name, value, attrs=None):
     298        if value is None:
     299            value = ''
     300        elif hasattr(value, 'strftime'):
     301            value = datetime_safe.new_date(value)
     302            value = value.strftime(self.format)
     303        return super(DateInput, self).render(name, value, attrs)
     304
    288305class DateTimeInput(Input):
    289306    input_type = 'text'
    290307    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
     
    304321
    305322class TimeInput(Input):
    306323    input_type = 'text'
     324    format = '%H:%M:%S'     # '14:30:59'
    307325
     326    def __init__(self, attrs=None, format=None):
     327        super(TimeInput, self).__init__(attrs)
     328        if format:
     329            self.format = format
     330
    308331    def render(self, name, value, attrs=None):
    309332        if value is None:
    310333            value = ''
    311         elif isinstance(value, time):
    312             value = value.replace(microsecond=0)
     334        elif hasattr(value, 'strftime'):
     335            value = value.strftime(self.format)
    313336        return super(TimeInput, self).render(name, value, attrs)
    314337
    315338class CheckboxInput(Widget):
     
    654677    """
    655678    A Widget that splits datetime input into two <input type="text"> boxes.
    656679    """
    657     def __init__(self, attrs=None):
    658         widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
     680    date_format = DateInput.format
     681    time_format = TimeInput.format
     682
     683    def __init__(self, attrs=None, date_format=None, time_format=None):
     684        if date_format:
     685            self.date_format = date_format
     686        if time_format:
     687            self.time_format = time_format
     688        widgets = (DateInput(attrs=attrs, format=self.date_format),
     689                   TimeInput(attrs=attrs, format=self.time_format))
    659690        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
    660691
    661692    def decompress(self, value):
     
    670701    def __init__(self, attrs=None):
    671702        widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs))
    672703        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
    673 
  • django/contrib/localflavor/generic/forms.py

     
    3636    def __init__(self, input_formats=None, *args, **kwargs):
    3737        input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
    3838        super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs)
     39
     40class SplitDateTimeField(forms.SplitDateTimeField):
     41    """
     42    Split date and time input fields which use non-US date and time input
     43    formats by default.
     44    """
     45    def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs):
     46        input_date_formats = input_date_formats or DEFAULT_DATE_INPUT_FORMATS
     47        super(SplitDateTimeField, self).__init__(input_date_formats=input_date_formats,
     48                                                 input_time_formats=input_time_formats, *args, **kwargs)
  • tests/regressiontests/forms/widgets.py

     
    10711071>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
    10721072u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
    10731073
     1074Use 'date_format' and 'time_format' to change the way a value is displayed.
     1075>>> w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M')
     1076>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
     1077u'<input type="text" name="date_0" value="10/01/2006" /><input type="text" name="date_1" value="07:30" />'
     1078
    10741079>>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00'])
    10751080False
    10761081>>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00'])
     
    10931098>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    10941099u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
    10951100
     1101Use 'format' to change the way a value is displayed.
     1102>>> w = DateTimeInput(format='%d/%m/%Y %H:%M')
     1103>>> w.render('date', d)
     1104u'<input type="text" name="date" value="17/09/2007 12:51" />'
     1105
     1106# DateInput ###################################################################
     1107
     1108>>> w = DateInput()
     1109>>> w.render('date', None)
     1110u'<input type="text" name="date" />'
     1111>>> d = datetime.date(2007, 9, 17)
     1112>>> print d
     11132007-09-17
     1114
     1115>>> w.render('date', d)
     1116u'<input type="text" name="date" value="2007-09-17" />'
     1117>>> w.render('date', datetime.date(2007, 9, 17))
     1118u'<input type="text" name="date" value="2007-09-17" />'
     1119
     1120We should be able to initialize from a unicode value.
     1121>>> w.render('date', u'2007-09-17')
     1122u'<input type="text" name="date" value="2007-09-17" />'
     1123
     1124Use 'format' to change the way a value is displayed.
     1125>>> w = DateInput(format='%d/%m/%Y')
     1126>>> w.render('date', d)
     1127u'<input type="text" name="date" value="17/09/2007" />'
     1128
    10961129# TimeInput ###################################################################
    10971130
    10981131>>> w = TimeInput()
     
    11141147>>> w.render('time', u'13:12:11')
    11151148u'<input type="text" name="time" value="13:12:11" />'
    11161149
     1150Use 'format' to change the way a value is displayed.
     1151>>> w = TimeInput(format='%H:%M')
     1152>>> w.render('time', t)
     1153u'<input type="text" name="time" value="12:51" />'
     1154
    11171155# SplitHiddenDateTimeWidget ###################################################
    11181156
    11191157>>> from django.forms.widgets import SplitHiddenDateTimeWidget
     
    11211159>>> w = SplitHiddenDateTimeWidget()
    11221160>>> w.render('date', '')
    11231161u'<input type="hidden" name="date_0" /><input type="hidden" name="date_1" />'
     1162>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
     1163>>> print d
     11642007-09-17 12:51:34.482548
    11241165>>> w.render('date', d)
    11251166u'<input type="hidden" name="date_0" value="2007-09-17" /><input type="hidden" name="date_1" value="12:51:34" />'
    11261167>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
  • AUTHORS

     
    250250    Eugene Lazutkin <http://lazutkin.com/blog/>
    251251    lcordier@point45.com
    252252    Jeong-Min Lee <falsetru@gmail.com>
     253    Tai Lee <real.human@mrmachine.net>
    253254    Jannis Leidel <jl@websushi.org>
    254255    Christopher Lenz <http://www.cmlenz.net/>
    255256    lerouxb@gmail.com
     
    289290    Aljosa Mohorovic <aljosa.mohorovic@gmail.com>
    290291    Ramiro Morales <rm0@gmx.net>
    291292    Eric Moritz <http://eric.themoritzfamily.com/>
    292     mrmachine <real.human@mrmachine.net>
    293293    Robin Munn <http://www.geekforgod.com/>
    294294    James Murty
    295295    msundstr
  • docs/ref/contrib/localflavor.txt

     
    6565    * `United States of America`_
    6666
    6767The ``django.contrib.localflavor`` package also includes a ``generic`` subpackage,
    68 containing useful code that is not specific to one particular country or
    69 culture. Currently, it defines date and datetime input fields based on those
    70 from :ref:`forms <topics-forms-index>`, but with non-US default formats.
     68containing useful code that is not specific to one particular country or culture.
     69Currently, it defines date, datetime and split datetime input fields based on
     70those from :ref:`forms <topics-forms-index>`, but with non-US default formats.
    7171Here's an example of how to use them::
    7272
    7373    from django import forms
  • docs/ref/forms/fields.txt

     
    397397
    398398.. class:: DateField(**kwargs)
    399399
    400     * Default widget: ``TextInput``
     400    * Default widget: ``DateInput``
    401401    * Empty value: ``None``
    402402    * Normalizes to: A Python ``datetime.date`` object.
    403403    * Validates that the given value is either a ``datetime.date``,
     
    419419    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
    420420    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'
    421421
     422.. versionchanged:: development version
     423   The ``DateField`` previously used a ``TextInput`` widget by default. It now
     424   uses a ``DateInput`` widget.
     425
    422426``DateTimeField``
    423427~~~~~~~~~~~~~~~~~
    424428
     
    738742
    739743.. class:: SplitDateTimeField(**kwargs)
    740744
     745    * Default widget: ``SplitDateTimeWidget``
     746    * Empty value: ``None``
     747    * Normalizes to: A Python ``datetime.datetime`` object.
     748    * Validates that the given value is a ``datetime.datetime`` or string
     749      formatted in a particular datetime format.
     750    * Error message keys: ``required``, ``invalid``
     751
     752Takes two optional arguments:
     753
     754.. attribute:: SplitDateTimeField.input_date_formats
     755
     756    A list of formats used to attempt to convert a string to a valid
     757    ``datetime.date`` object.
     758
     759If no ``input_date_formats`` argument is provided, the default input formats
     760for ``DateField`` are used.
     761
     762.. attribute:: SplitDateTimeField.input_time_formats
     763
     764    A list of formats used to attempt to convert a string to a valid
     765    ``datetime.time`` object.
     766
     767If no ``input_time_formats`` argument is provided, the default input formats
     768for ``TimeField`` are used.
     769
     770.. versionchanged:: development version
     771   The ``SplitDateTimeField`` previously used two ``TextInput`` widgets by
     772   default. The ``input_date_formats`` and ``input_time_formats`` arguments
     773   are also new in the development version.
     774
    741775Fields which handle relationships
    742776---------------------------------
    743777
  • docs/ref/forms/widgets.txt

     
    3636
    3737    File upload input: ``<input type='file' ...>``
    3838
     39.. class:: DateInput
     40
     41    .. versionadded:: development version
     42
     43    Date input as a simple text box: ``<input type='text' ...>``
     44
     45    Takes one optional argument:
     46   
     47    .. attribute:: DateInput.format
     48
     49        The format in which this field's initial value will be displayed.
     50
     51    If no ``format`` argument is provided, the default format is ``'%Y-%m-%d'``.
     52
    3953.. class:: DateTimeInput
    4054
    4155    .. versionadded:: 1.0
    4256
    4357    Date/time input as a simple text box: ``<input type='text' ...>``
    4458
     59    Takes one optional argument:
     60   
     61    .. attribute:: DateTimeInput.format
     62   
     63        The format in which this field's initial value will be displayed.
     64   
     65    If no ``format`` argument is provided, the default format is ``'%Y-%m-%d %H:%M:%S'``.
     66
     67.. class:: TimeInput
     68
     69    Time input as a simple text box: ``<input type='text' ...>``
     70
     71    Takes one optional argument:
     72   
     73    .. attribute:: TimeInput.format
     74   
     75        The format in which this field's initial value will be displayed.
     76   
     77    If no ``format`` argument is provided, the default format is ``'%H:%M:%S'``.
     78
     79    .. versionchanged:: development version
     80       The ``format`` argument is new in the development version.
     81
    4582.. class:: Textarea
    4683
    4784    Text area: ``<textarea>...</textarea>``
     
    91128
    92129.. class:: SplitDateTimeWidget
    93130
    94     Wrapper around two ``TextInput`` widgets: one for the date, and one for the
    95     time.
     131    Wrapper around two widgets: ``DateInput`` for the date, and ``TimeInput``
     132    for the time.
    96133
     134    Takes two optional arguments, ``date_format`` and ``time_format``, which
     135    work just like the ``format`` argument for ``DateInput`` and ``TimeInput``.
     136
    97137Specifying widgets
    98138------------------
    99139
Back to Top