~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/forms/widgets.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-24 22:44:32 UTC
  • mfrom: (1.1.11 upstream) (4.4.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100524224432-lz0tq65rhy8cov3y
Tags: 1.2.1-1
New upstream bugfix release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
from django.utils.translation import ugettext
11
11
from django.utils.encoding import StrAndUnicode, force_unicode
12
12
from django.utils.safestring import mark_safe
13
 
from django.utils import formats
 
13
from django.utils import datetime_safe, formats
14
14
import time
15
15
import datetime
16
16
from util import flatatt
133
133
    __metaclass__ = MediaDefiningClass
134
134
    is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
135
135
    needs_multipart_form = False # Determines does this widget need multipart-encrypted form
 
136
    is_localized = False
136
137
 
137
138
    def __init__(self, attrs=None):
138
139
        if attrs is not None:
208
209
    """
209
210
    input_type = None # Subclasses must define this.
210
211
 
 
212
    def _format_value(self, value):
 
213
        if self.is_localized:
 
214
            return formats.localize_input(value)
 
215
        return value
 
216
 
211
217
    def render(self, name, value, attrs=None):
212
 
        if value is None: value = ''
 
218
        if value is None:
 
219
            value = ''
213
220
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
214
221
        if value != '':
215
222
            # Only add the 'value' attribute if a value is non-empty.
216
 
            final_attrs['value'] = force_unicode(value)
 
223
            final_attrs['value'] = force_unicode(self._format_value(value))
217
224
        return mark_safe(u'<input%s />' % flatatt(final_attrs))
218
225
 
219
226
class TextInput(Input):
295
302
 
296
303
class DateInput(Input):
297
304
    input_type = 'text'
298
 
    format = None
 
305
    format = '%Y-%m-%d'     # '2006-10-25'
299
306
 
300
307
    def __init__(self, attrs=None, format=None):
301
308
        super(DateInput, self).__init__(attrs)
303
310
            self.format = format
304
311
 
305
312
    def _format_value(self, value):
306
 
        if value is None:
307
 
            return ''
 
313
        if self.is_localized:
 
314
            return formats.localize_input(value)
308
315
        elif hasattr(value, 'strftime'):
309
 
            return formats.localize_input(value, self.format)
 
316
            value = datetime_safe.new_date(value)
 
317
            return value.strftime(self.format)
310
318
        return value
311
319
 
312
 
    def render(self, name, value, attrs=None):
313
 
        value = self._format_value(value)
314
 
        return super(DateInput, self).render(name, value, attrs)
315
 
 
316
320
    def _has_changed(self, initial, data):
317
321
        # If our field has show_hidden_initial=True, initial will be a string
318
322
        # formatted by HiddenInput using formats.localize_input, which is not
326
330
 
327
331
class DateTimeInput(Input):
328
332
    input_type = 'text'
329
 
    format = None
 
333
    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
330
334
 
331
335
    def __init__(self, attrs=None, format=None):
332
336
        super(DateTimeInput, self).__init__(attrs)
334
338
            self.format = format
335
339
 
336
340
    def _format_value(self, value):
337
 
        if value is None:
338
 
            return ''
 
341
        if self.is_localized:
 
342
            return formats.localize_input(value)
339
343
        elif hasattr(value, 'strftime'):
340
 
            return formats.localize_input(value, self.format)
 
344
            value = datetime_safe.new_datetime(value)
 
345
            return value.strftime(self.format)
341
346
        return value
342
347
 
343
 
    def render(self, name, value, attrs=None):
344
 
        value = self._format_value(value)
345
 
        return super(DateTimeInput, self).render(name, value, attrs)
346
 
 
347
348
    def _has_changed(self, initial, data):
348
349
        # If our field has show_hidden_initial=True, initial will be a string
349
350
        # formatted by HiddenInput using formats.localize_input, which is not
357
358
 
358
359
class TimeInput(Input):
359
360
    input_type = 'text'
360
 
    format = None
 
361
    format = '%H:%M:%S'     # '14:30:59'
361
362
 
362
363
    def __init__(self, attrs=None, format=None):
363
364
        super(TimeInput, self).__init__(attrs)
365
366
            self.format = format
366
367
 
367
368
    def _format_value(self, value):
368
 
        if value is None:
369
 
            return ''
 
369
        if self.is_localized:
 
370
            return formats.localize_input(value)
370
371
        elif hasattr(value, 'strftime'):
371
 
            return formats.localize_input(value, self.format)
 
372
            return value.strftime(self.format)
372
373
        return value
373
374
 
374
 
    def render(self, name, value, attrs=None):
375
 
        value = self._format_value(value)
376
 
        return super(TimeInput, self).render(name, value, attrs)
377
 
 
378
375
    def _has_changed(self, initial, data):
379
376
        # If our field has show_hidden_initial=True, initial will be a string
380
377
        # formatted by HiddenInput using formats.localize_input, which is not
674
671
        super(MultiWidget, self).__init__(attrs)
675
672
 
676
673
    def render(self, name, value, attrs=None):
 
674
        if self.is_localized:
 
675
            for widget in self.widgets:
 
676
                widget.is_localized = self.is_localized
677
677
        # value is a list of values, each corresponding to a widget
678
678
        # in self.widgets.
679
679
        if not isinstance(value, list):