1 | from django.conf import settings
|
---|
2 | from django.utils import dateformat
|
---|
3 |
|
---|
4 | class 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 |
|
---|
12 | class 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 |
|
---|
21 | class 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 |
|
---|