From 66655a318ab44355007cb0cfdf9c77a36974f4d5 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Mon, 28 Aug 2017 16:10:35 +0200
Subject: [PATCH] Update widgets

---
 scipost/templates/widgets/checkbox_as_btn.html       |  7 +++++++
 .../templates/widgets/checkbox_option_as_btn.html    |  4 ++++
 scipost/templatetags/extra_form_widgets.py           | 12 ++++++++++++
 scipost/widgets.py                                   |  5 +++++
 templates/search/search.html                         | 10 ++--------
 5 files changed, 30 insertions(+), 8 deletions(-)
 create mode 100644 scipost/templates/widgets/checkbox_as_btn.html
 create mode 100644 scipost/templates/widgets/checkbox_option_as_btn.html
 create mode 100644 scipost/templatetags/extra_form_widgets.py
 create mode 100644 scipost/widgets.py

diff --git a/scipost/templates/widgets/checkbox_as_btn.html b/scipost/templates/widgets/checkbox_as_btn.html
new file mode 100644
index 000000000..b4ea0363a
--- /dev/null
+++ b/scipost/templates/widgets/checkbox_as_btn.html
@@ -0,0 +1,7 @@
+{% with id=widget.attrs.id %}
+    {% for group, options, index in widget.optgroups %}
+        {% for option in options %}
+            {% include 'widgets/checkbox_option_as_btn.html' with widget=option %}
+        {% endfor %}
+    {% endfor %}
+{% endwith %}
diff --git a/scipost/templates/widgets/checkbox_option_as_btn.html b/scipost/templates/widgets/checkbox_option_as_btn.html
new file mode 100644
index 000000000..191c44ea1
--- /dev/null
+++ b/scipost/templates/widgets/checkbox_option_as_btn.html
@@ -0,0 +1,4 @@
+
+{% include "django/forms/widgets/input.html" %}
+<label{% if widget.attrs.id %} class="btn btn-info" for="{{ widget.attrs.id }}"{% endif %}>
+{{ widget.label }}</label>
diff --git a/scipost/templatetags/extra_form_widgets.py b/scipost/templatetags/extra_form_widgets.py
new file mode 100644
index 000000000..3d05ac0b9
--- /dev/null
+++ b/scipost/templatetags/extra_form_widgets.py
@@ -0,0 +1,12 @@
+from django import template
+
+from ..widgets import SelectButtonWidget
+
+
+register = template.Library()
+
+
+@register.filter
+def checkboxes_as_btn(field):
+    w = field.field.widget
+    return field.as_widget(SelectButtonWidget(attrs=w.attrs, choices=w.choices))
diff --git a/scipost/widgets.py b/scipost/widgets.py
new file mode 100644
index 000000000..93602aef9
--- /dev/null
+++ b/scipost/widgets.py
@@ -0,0 +1,5 @@
+from django.forms.widgets import CheckboxSelectMultiple
+
+
+class SelectButtonWidget(CheckboxSelectMultiple):
+    template_name = 'widgets/checkbox_as_btn.html'
diff --git a/templates/search/search.html b/templates/search/search.html
index c3d2cce40..053dd3a4d 100644
--- a/templates/search/search.html
+++ b/templates/search/search.html
@@ -1,7 +1,7 @@
 {% extends 'scipost/base.html' %}
 
 {% load bootstrap %}
-{# {% load highlight %}#}
+{% load extra_form_widgets %}
 
 {% block pagetitle %}: list{% endblock pagetitle %}
 
@@ -22,13 +22,7 @@
                 <label>Type</label>
             </div>
             <div class="form-group checkboxes auto-submit">
-                {% for field in form.models %}
-                    <input type="checkbox" name="{{field.name}}" id="{{field.id_for_label}}" value="{{field.choice_value|stringformat:'s'}}" {% if field.choice_value in form.models.value %}checked="checked"{% endif %}>
-                    <label class="btn btn-info" for="{{field.id_for_label}}">
-                        {{field.choice_label}}
-                    </label>
-
-                {% endfor %}
+                {{form.models|checkboxes_as_btn}}
             </div>
 
             <label>Date from</label>
-- 
GitLab