]> git.0d.be Git - django-panik-nonstop.git/blob - nonstop/widgets.py
6686589061937bfa0a61c013c30546aba2f9e4ad
[django-panik-nonstop.git] / nonstop / widgets.py
1 from django.forms.widgets import TimeInput, SelectMultiple
2 from django.utils.safestring import mark_safe
3
4 from .models import Jingle
5
6
7 class TimeWidget(TimeInput):
8     input_type = 'time'
9
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}'
14
15
16 class JinglesWidget(SelectMultiple):
17     def render(self, name, value, attrs=None, choices=(), renderer=None):
18         s = []
19         value = value or []
20         for jingle in Jingle.objects.all():
21             choice_id = jingle.id
22             choice_label = jingle.label
23             s.append(
24                 '<li><label><input type="checkbox" '
25                 '  name="%(name)s-%(choice_id)s" %(checked)s>'
26                 '<span>%(choice_label)s</span></label></li>'
27                 % {
28                     'name': name,
29                     'checked': 'checked' if choice_id in value else '',
30                     'choice_id': choice_id,
31                     'choice_label': choice_label,
32                 }
33             )
34         return mark_safe('<ul id="%(id)s">' % attrs + '\n'.join(s) + '</ul>')
35
36     def value_from_datadict(self, data, files, name):
37         choices = []
38         for choice_id, choice_label in self.choices:
39             if data.get('%s-%s' % (name, choice_id)):
40                 choices.append(choice_id)
41         return choices