Login

SelectDateWidget

Author:
silent1mezzo
Posted:
December 15, 2010
Language:
Python
Version:
1.2
Tags:
datetime select widget daterange
Score:
0 (after 2 ratings)

This widget will produce a select box with the range of dates that you input.

Usage:

widget=SelectDateWidget('2010-12-15', '2010-12-20')

Output:

`<select>

<option value="2010-12-15">Wed January 01, 2010</option>

<option value="2010-12-16">Thu January 02, 2010</option>

<option value="2010-12-17">Fri January 03, 2010</option>

<option value="2010-12-18">Sat January 04, 2010</option>

<option value="2010-12-19">Sun January 05, 2010</option>

<option value="2010-12-20">Mon January 06, 2010</option>

</select>`

 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
33
34
import datetime
import time
import re

from django.forms.widgets import Widget
from django.forms.util import flatatt 
from django.utils.html import escape
from django.utils.encoding import smart_unicode

RE_DATE = re.compile(r'\d{4}-\d\d?-\d\d?$')

def daterange(start_date, end_date):
    start_date = datetime.datetime.strptime(start_date, '%Y-%M-%d')
    end_date = datetime.datetime.strptime(end_date, '%Y-%M-%d')
    for n in range((end_date - start_date).days+1):
        yield start_date + datetime.timedelta(n)

class SelectDateWidget(Widget):
    field = '%s_date'
    def __init__(self, start_date, end_date, attrs=None, required=True):
        self.attrs = attrs or {}
        self.required = required
        self.start_date = start_date
        self.end_date = end_date

    def render(self, name, value, attrs=None, choices=()):                 
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<select%s>' % flatatt(final_attrs)]
        str_value = smart_unicode(value)
        for single_date in daterange(self.start_date, self.end_date):
            output.append(u'<option value="%s">%s</option>' % (time.strftime("%Y-%m-%d", single_date.timetuple()), time.strftime("%a %B %d, %Y", single_date.timetuple())))
        output.append(u'</select>') 
        return u'\n'.join(output)

More like this

  1. Dynamically change a form select widget to a hidden widget by epicserve 6 years ago
  2. Allow disabling options in a select widget by scjody 4 years, 1 month ago
  3. SplitSelectDateTimeWidget by bradmontgomery 6 years, 8 months ago
  4. Split a string to a list and add to select options by xuqingkuang 6 years, 3 months ago
  5. Page numbers with ... like in Digg by Ciantic 6 years, 3 months ago

Comments

cricogik (on December 16, 2010):

Creating new widget is useless. You can use base Select widget.

from datetime import timedelta

DATES_CHOICES = [(<my_date_object> + timedelta(i), <my_date_object> + timedelta(i)) for i in range(DAYS)]

class MyForm(forms.Form):
    dates = forms.CharField(max_length=10,
                  widget=forms.Select(choices=DATES_CHOICES))

#

pedromagnus (on January 15, 2011):

Without the intention of taking out credit from the author silent1mezzo, it's incredible the alternative of cricogik provided in just a few lines of code. Conclussion: python is awesome. Regards!

#

Please login first before commenting.