24
24
'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
25
25
'HiddenInput', 'MultipleHiddenInput',
26
'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
26
'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
27
27
'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
28
28
'CheckboxSelectMultiple', 'MultiWidget',
29
29
'SplitDateTimeWidget',
75
75
def __getitem__(self, name):
76
76
"Returns a Media object that only contains media of the given type"
77
77
if name in MEDIA_TYPES:
78
return Media(**{name: getattr(self, '_' + name)})
78
return Media(**{str(name): getattr(self, '_' + name)})
79
79
raise KeyError('Unknown media type "%s"' % name)
81
81
def add_js(self, data):
282
282
def render(self, name, value, attrs=None):
283
283
if value is None: value = ''
284
value = force_unicode(value)
285
284
final_attrs = self.build_attrs(attrs, name=name)
286
285
return mark_safe(u'<textarea%s>%s</textarea>' % (flatatt(final_attrs),
287
286
conditional_escape(force_unicode(value))))
288
class DateInput(Input):
290
format = '%Y-%m-%d' # '2006-10-25'
292
def __init__(self, attrs=None, format=None):
293
super(DateInput, self).__init__(attrs)
297
def _format_value(self, value):
300
elif hasattr(value, 'strftime'):
301
value = datetime_safe.new_date(value)
302
return value.strftime(self.format)
305
def render(self, name, value, attrs=None):
306
value = self._format_value(value)
307
return super(DateInput, self).render(name, value, attrs)
309
def _has_changed(self, initial, data):
310
return super(DateInput, self)._has_changed(self._format_value(initial), data)
289
312
class DateTimeInput(Input):
290
313
input_type = 'text'
291
314
format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59'
296
319
self.format = format
298
def render(self, name, value, attrs=None):
321
def _format_value(self, value):
299
322
if value is None:
301
324
elif hasattr(value, 'strftime'):
302
325
value = datetime_safe.new_datetime(value)
303
value = value.strftime(self.format)
326
return value.strftime(self.format)
329
def render(self, name, value, attrs=None):
330
value = self._format_value(value)
304
331
return super(DateTimeInput, self).render(name, value, attrs)
333
def _has_changed(self, initial, data):
334
return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
306
336
class TimeInput(Input):
307
337
input_type = 'text'
338
format = '%H:%M:%S' # '14:30:59'
340
def __init__(self, attrs=None, format=None):
341
super(TimeInput, self).__init__(attrs)
345
def _format_value(self, value):
348
elif hasattr(value, 'strftime'):
349
return value.strftime(self.format)
309
352
def render(self, name, value, attrs=None):
312
elif isinstance(value, time):
313
value = value.replace(microsecond=0)
353
value = self._format_value(value)
314
354
return super(TimeInput, self).render(name, value, attrs)
356
def _has_changed(self, initial, data):
357
return super(TimeInput, self)._has_changed(self._format_value(initial), data)
316
359
class CheckboxInput(Widget):
317
360
def __init__(self, attrs=None, check_test=bool):
318
361
super(CheckboxInput, self).__init__(attrs)
401
444
def value_from_datadict(self, data, files, name):
402
445
value = data.get(name, None)
403
return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
451
False: False}.get(value, None)
405
453
def _has_changed(self, initial, data):
406
454
# Sometimes data or initial could be None or u'' which should be the
656
704
A Widget that splits datetime input into two <input type="text"> boxes.
658
def __init__(self, attrs=None):
659
widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
706
date_format = DateInput.format
707
time_format = TimeInput.format
709
def __init__(self, attrs=None, date_format=None, time_format=None):
711
self.date_format = date_format
713
self.time_format = time_format
714
widgets = (DateInput(attrs=attrs, format=self.date_format),
715
TimeInput(attrs=attrs, format=self.time_format))
660
716
super(SplitDateTimeWidget, self).__init__(widgets, attrs)
662
718
def decompress(self, value):