Ticket #3672: date-time-formatting-widgets.py

File date-time-formatting-widgets.py, 1.1 KB (added by scott@…, 18 years ago)

Widgets that format date or time before rendering

Line 
1from django.conf import settings
2from django.utils import dateformat
3
4class FormattedTextInput(forms.widgets.TextInput):
5 "Overrides TextInput to render formatted value."
6 def render(self, name, value, attrs=None):
7 formatted_value = None
8 if value:
9 formatted_value = self.format_value(value)
10 return super(FormattedTextInput, self).render(name, formatted_value, attrs)
11
12class DateFormattedTextInput(FormattedTextInput):
13 "Renders formatted date."
14 def __init__(self, format=None, attrs=None):
15 super(DateFormattedTextInput, self).__init__(attrs)
16 self.format = format or settings.DATE_FORMAT
17
18 def format_value(self, value):
19 return dateformat.format(value, self.format)
20
21class TimeFormattedTextInput(FormattedTextInput):
22 "Renders formatted time."
23 def __init__(self, format=None, attrs=None):
24 super(TimeFormattedTextInput, self).__init__(attrs)
25 self.format = format or settings.TIME_FORMAT
26
27 def format_value(self, value):
28 return dateformat.time_format(value, self.format)
29
30
Back to Top