1 from django.forms.widgets import TimeInput, SelectMultiple
2 from django.utils.safestring import mark_safe
4 from .models import Jingle
7 class TimeWidget(TimeInput):
10 def __init__(self, **kwargs):
11 super(TimeWidget, self).__init__(**kwargs)
12 self.attrs['step'] = '300' # 5 minutes
13 self.attrs['pattern'] = '[0-9]{2}:[0-9]{2}'
16 class JinglesWidget(SelectMultiple):
17 def render(self, name, value, attrs=None, choices=(), renderer=None):
20 for jingle in Jingle.objects.all():
22 choice_label = jingle.label
24 '<li><label><input type="checkbox" '
25 ' name="%(name)s-%(choice_id)s" %(checked)s>'
26 '<span>%(choice_label)s</span></label></li>'
29 'checked': 'checked' if choice_id in value else '',
30 'choice_id': choice_id,
31 'choice_label': choice_label,
34 return mark_safe('<ul id="%(id)s">' % attrs + '\n'.join(s) + '</ul>')
36 def value_from_datadict(self, data, files, name):
38 for choice_id, choice_label in self.choices:
39 if data.get('%s-%s' % (name, choice_id)):
40 choices.append(choice_id)