Created
September 25, 2011 21:43
-
-
Save zen4ever/1241198 to your computer and use it in GitHub Desktop.
Hour widget django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
TIME_CHOICES = [('', '--------')] | |
for i in range(0, 24): | |
for m in ["00", "30"]: | |
hour = i % 12 | |
if i == 12: | |
hour = 12 | |
TIME_CHOICES.append( | |
("%02d:%s" % (i, m), | |
"%02d:%s " % (hour, m) + ((i < 12) and "am" or "pm"))) | |
class SelectTimeInput(forms.Select): | |
format = '%H:%M' | |
def __init__(self, attrs=None, choices=()): | |
if not choices: | |
choices = TIME_CHOICES | |
else: | |
choices = list(choices) | |
super(SelectTimeInput, self).__init__(attrs, choices) | |
def render_options(self, choices, selected_choices): | |
def convert_choices(v): | |
if hasattr(v, 'strftime'): | |
v = v.strftime(self.format) | |
return v | |
selected_choices = map(convert_choices, selected_choices) | |
return super(SelectTimeInput, self).render_options( | |
choices, | |
selected_choices) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment