Ticket #4428: new_datetimefield_millisecond.diff

File new_datetimefield_millisecond.diff, 3.9 KB (added by Wiliam Alves de Souza(waa), 17 years ago)

Code improvements and tests

  • django/newforms/fields.py

     
    1111from django.utils.encoding import StrAndUnicode, smart_unicode
    1212
    1313from util import ErrorList, ValidationError
    14 from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
     14from widgets import (TextInput, PasswordInput, HiddenInput,
     15                     MultipleHiddenInput, FileInput, CheckboxInput, Select,
     16                     NullBooleanSelect, SelectMultiple, DateTimeInput)
    1517
    1618try:
    1719    from decimal import Decimal, DecimalException
     
    284286)
    285287
    286288class DateTimeField(Field):
     289    widget = DateTimeInput
    287290    def __init__(self, input_formats=None, *args, **kwargs):
    288291        super(DateTimeField, self).__init__(*args, **kwargs)
    289292        self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
  • django/newforms/widgets.py

     
    77except NameError:
    88    from sets import Set as set   # Python 2.3 fallback
    99
     10try:
     11    import settings
     12except ImportError:
     13    pass
     14
    1015from itertools import chain
    1116from django.utils.datastructures import MultiValueDict
    1217from django.utils.html import escape
     
    1520from util import flatatt
    1621
    1722__all__ = (
    18     'Widget', 'TextInput', 'PasswordInput',
     23    'Widget', 'TextInput', 'DateTimeInput', 'PasswordInput',
    1924    'HiddenInput', 'MultipleHiddenInput',
    2025    'FileInput', 'Textarea', 'CheckboxInput',
    2126    'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
     
    8489class TextInput(Input):
    8590    input_type = 'text'
    8691
     92class DateTimeInput(Input):
     93    input_type = 'text'
     94   
     95    def __init__(self, attrs=None, format=None):
     96        self.attrs = attrs or {}
     97        self.format = format
     98        if not self.format:
     99            try:
     100                self.format = settings.DATETIME_FORMAT
     101            except: # Silently catch exceptions
     102                self.format = 'Y-m-d h:i:s'
     103
     104    def render(self, name, value, attrs=None):
     105        from django.utils.dateformat import format
     106        return super(DateTimeInput, self).render(name,
     107                                                 format(value, self.format),
     108                                                 attrs)   
     109
    87110class PasswordInput(Input):
    88111    input_type = 'password'
    89112
  • tests/regressiontests/forms/tests.py

     
    848848>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
    849849u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
    850850
     851# DateTimeInput ###############################################################
     852
     853>>> w = DateTimeInput()
     854>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34, 482548))
     855u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
     856>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
     857u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
     858>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
     859u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
     860>>> w = DateTimeInput(format='h:i:s')
     861>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34, 482548))
     862u'<input type="text" name="date" value="12:51:34" />'
     863>>> w = DateTimeInput(format='x:c:p')
     864>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34, 482548))
     865u'<input type="text" name="date" value="x:c:p" />'
     866
    851867##########
    852868# Fields #
    853869##########
Back to Top