2012-07-11 17:39:20 +02:00
|
|
|
from django.forms.widgets import Widget
|
2016-05-24 00:35:55 +02:00
|
|
|
from django.forms.utils import flatatt
|
2012-07-11 17:39:20 +02:00
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
class TriStateCheckbox(Widget):
|
2012-07-11 17:39:20 +02:00
|
|
|
def __init__(self, attrs=None, choices=()):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().__init__(attrs)
|
2012-07-11 17:39:20 +02:00
|
|
|
# choices can be any iterable, but we may need to render this widget
|
|
|
|
# multiple times. Thus, collapse it into a list so it can be consumed
|
|
|
|
# more than once.
|
|
|
|
self.choices = list(choices)
|
|
|
|
|
|
|
|
def render(self, name, value, attrs=None, choices=()):
|
2016-07-09 22:31:56 +02:00
|
|
|
if value is None:
|
|
|
|
value = 'none'
|
2018-01-19 18:15:57 +01:00
|
|
|
attrs['value'] = value
|
|
|
|
final_attrs = self.build_attrs(self.attrs, attrs)
|
2016-05-26 22:44:10 +02:00
|
|
|
output = ["<span class=\"tristate\"%s></span>" % flatatt(final_attrs)]
|
2012-07-11 17:39:20 +02:00
|
|
|
return mark_safe('\n'.join(output))
|