SplitSelectDateTimeWidget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class SplitSelectDateTimeWidget(MultiWidget):
    '''
    This class combines SelectTimeWidget (from: http://www.djangosnippets.org/snippets/1202/) 
    and SelectDateWidget (from django.forms.extras) so we have something 
    like SpliteDateTimeWidget (in django.forms.widgets), but with Select elements.
    '''
   def __init__(self, attrs=None, hour_step=None, minute_step=None, \
                        second_step=None, twelve_hr=None, years=None):
        ''' pass all these parameters to their 
            respective widget constructors...
        '''

        widgets = (SelectDateWidget(attrs=attrs, years=years), \
            SelectTimeWidget(attrs=attrs, hour_step=hour_step, \
            minute_step=minute_step, second_step=second_step, \
            twelve_hr=twelve_hr))
        super(SplitSelectDateTimeWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            return [value.date(), value.time().replace(microsecond=0)]
        return [None, None]

    def format_output(self, rendered_widgets):
        """
        Given a list of rendered widgets (as strings), it inserts an HTML
        linebreak between them.
        
        Returns a Unicode string representing the HTML for the whole lot.
        """
        rendered_widgets.insert(-1, '<br/>')
        return u''.join(rendered_widgets)

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.