From d911271a422d682033cfcfe042b95cffbec0b435 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Sun, 10 Feb 2019 16:35:18 +0000
Subject: [PATCH] GH-957: tidy up how we render the icons into the forms

---
 opentech/apply/funds/forms.py                  |  3 ++-
 .../funds/includes/delegated_form_base.html    | 18 +-----------------
 .../templates/funds/widgets/icon_select2.html  |  3 +++
 opentech/apply/funds/widgets.py                |  4 +++-
 4 files changed, 9 insertions(+), 19 deletions(-)
 create mode 100644 opentech/apply/funds/templates/funds/widgets/icon_select2.html

diff --git a/opentech/apply/funds/forms.py b/opentech/apply/funds/forms.py
index 51236a592..8982f6afb 100644
--- a/opentech/apply/funds/forms.py
+++ b/opentech/apply/funds/forms.py
@@ -83,7 +83,8 @@ class UpdateReviewersForm(forms.ModelForm):
             self.fields[field_name] = forms.ModelChoiceField(
                 queryset=staff_reviewers,
                 widget=Select2IconWidget(attrs={
-                    'data-placeholder': 'Select a reviewer', 'role': role}),
+                    'data-placeholder': 'Select a reviewer', 'icon': role.icon
+                }),
                 required=False,
                 label=f'{role.name} Reviewer',
             )
diff --git a/opentech/apply/funds/templates/funds/includes/delegated_form_base.html b/opentech/apply/funds/templates/funds/includes/delegated_form_base.html
index a5e618774..ec68b976b 100644
--- a/opentech/apply/funds/templates/funds/includes/delegated_form_base.html
+++ b/opentech/apply/funds/templates/funds/includes/delegated_form_base.html
@@ -2,23 +2,7 @@
 <form class="form {{extra_classes}}" method="post" id="{{ form.name }}">
     {% csrf_token %}
     <div class="form__item">
-        {{ form.non_field_errors }}
-
-        {% for field in form %}
-            {{ field.errors }}
-
-            {# TODO: Add styles around icon later #}
-            {% if field.field.widget.attrs.role and field.field.widget.attrs.role.icon %}
-                {% image field.field.widget.attrs.role.icon max-20x20 %}
-            {% endif %}
-
-
-            {{ field.label_tag }} {{ field }}
-
-            {% if field.help_text %}
-                {{ field.help_text|safe }}
-            {% endif %}
-        {% endfor %}
+        {{ form }}
     </div>
 
     <input class="button button--primary button--top-space" id="{{ form.name }}-submit" name="{{ form_prefix }}{{ form.name }}" type="submit" form="{{ form.name }}" value="{{ value }}">
diff --git a/opentech/apply/funds/templates/funds/widgets/icon_select2.html b/opentech/apply/funds/templates/funds/widgets/icon_select2.html
new file mode 100644
index 000000000..0449cc92a
--- /dev/null
+++ b/opentech/apply/funds/templates/funds/widgets/icon_select2.html
@@ -0,0 +1,3 @@
+{% load wagtailimages_tags %}
+{% if widget.attrs.icon %}<div>{% image widget.attrs.icon max-20x20 %}</div>{% endif %}
+{% include 'django/forms/widgets/select.html' %}
diff --git a/opentech/apply/funds/widgets.py b/opentech/apply/funds/widgets.py
index ecb2e9ede..dbaf8ba22 100644
--- a/opentech/apply/funds/widgets.py
+++ b/opentech/apply/funds/widgets.py
@@ -23,8 +23,10 @@ class Select2MultiCheckboxesWidget(Select2MultipleWidget):
 
 
 class Select2IconWidget(Select2Widget):
+    template_name = 'funds/widgets/icon_select2.html'
+
     def __init__(self, *args, **kwargs):
         attrs = kwargs.get('attrs', {})
-        attrs.setdefault('role', '')
+        attrs.setdefault('icon', '')
         kwargs['attrs'] = attrs
         super().__init__(*args, **kwargs)
-- 
GitLab