From 15c6146eaa80cde74c76abcf9dd6d66cac904e1e Mon Sep 17 00:00:00 2001
From: Erin Mullaney <erin.mullaney@torchbox.com>
Date: Wed, 6 Feb 2019 10:45:56 -0500
Subject: [PATCH] #957 icons on reviewer form

---
 .../applicationsubmission_admin_detail.html   |  2 +-
 .../funds/includes/delegated_form_base.html   | 20 ++++++++++++++++++-
 .../funds/includes/update_reviewer_form.html  |  2 +-
 opentech/apply/funds/views.py                 |  9 +++++++--
 .../public/utils/templatetags/util_tags.py    |  8 ++++++++
 5 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html b/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html
index d39cf28ad..9bd53c818 100644
--- a/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html
+++ b/opentech/apply/funds/templates/funds/applicationsubmission_admin_detail.html
@@ -15,7 +15,7 @@
         {% include "funds/includes/screening_form.html" %}
         {% include "funds/includes/progress_form.html" %}
         {% include "funds/includes/update_lead_form.html" %}
-        {% include "funds/includes/update_reviewer_form.html" %}
+        {% include "funds/includes/update_reviewer_form.html" with objects_with_icons=objects_with_icons %}
 {% endblock %}
 
 {% block reviews %}
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 794d7fc93..9882b0e55 100644
--- a/opentech/apply/funds/templates/funds/includes/delegated_form_base.html
+++ b/opentech/apply/funds/templates/funds/includes/delegated_form_base.html
@@ -1,7 +1,25 @@
+{% load wagtailimages_tags util_tags %}
 <form class="form {{extra_classes}}" method="post" id="{{ form.name }}">
     {% csrf_token %}
     <div class="form__item">
-        {{ form }}
+        {{ form.non_field_errors }}
+
+        {% for field in form %}
+            {{ field.errors }}
+
+            {# TODO: Add styles around this later #}
+            {% if objects_with_icons %}
+                {% image objects_with_icons|get_icon_by_index:forloop.counter0 max-20x20 %}
+            {% endif %}
+
+            {{ field.label_tag }} {{ field }}
+
+            {% if field.help_text %}
+                {{ field.help_text|safe }}
+            {% endif %}
+        {% endfor %}
     </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 }}">
+
 </form>
diff --git a/opentech/apply/funds/templates/funds/includes/update_reviewer_form.html b/opentech/apply/funds/templates/funds/includes/update_reviewer_form.html
index 1ccf42e24..965ee68d6 100644
--- a/opentech/apply/funds/templates/funds/includes/update_reviewer_form.html
+++ b/opentech/apply/funds/templates/funds/includes/update_reviewer_form.html
@@ -1,4 +1,4 @@
 <div class="modal" id="update-reviewers">
     <h4>Update Reviewers</h4>
-    {% include 'funds/includes/delegated_form_base.html' with form=reviewer_form value='Update' %}
+    {% include 'funds/includes/delegated_form_base.html' with form=reviewer_form value='Update' objects_with_icons=objects_with_icons %}
 </div>
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index acb6b4168..055b8369d 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -277,8 +277,11 @@ class UpdateReviewersView(DelegatedViewMixin, UpdateView):
     context_name = 'reviewer_form'
 
     def form_valid(self, form):
+        response = super().form_valid(form)
         if len(form.cleaned_data.values()) != len(set(form.cleaned_data.values())):  # If any of the users match
-            messages.error(self.request, mark_safe(_('Users cannot be assigned to multiple roles.') + form.errors.as_ul()))
+            error_message = _('Users cannot be assigned to multiple roles.')
+            messages.error(self.request, mark_safe(error_message + form.errors.as_ul()))
+            form.add_error(None, error_message)
             return self.form_invalid(form)
 
         # Loop through cleaned_data and save reviewers by role type to submission
@@ -293,7 +296,7 @@ class UpdateReviewersView(DelegatedViewMixin, UpdateView):
                 Q(submission=form.instance),
                 ~Q(reviewer=value),
                 Q(reviewer_role=role)).delete()
-        response = super().form_valid(form)
+
         """
         old_reviewers = set(self.get_object().reviewers.all())
         new_reviewers = set(form.instance.reviewers.all())
@@ -332,6 +335,7 @@ class AdminSubmissionDetailView(ReviewContextMixin, ActivityContextMixin, Delega
 
     def get_context_data(self, **kwargs):
         other_submissions = self.model.objects.filter(user=self.object.user).current().exclude(id=self.object.id)
+        objects_with_icons = ReviewerRole.objects.all().order_by('order')
         if self.object.next:
             other_submissions = other_submissions.exclude(id=self.object.next.id)
 
@@ -340,6 +344,7 @@ class AdminSubmissionDetailView(ReviewContextMixin, ActivityContextMixin, Delega
         return super().get_context_data(
             other_submissions=other_submissions,
             public_page=public_page,
+            objects_with_icons=objects_with_icons,
             **kwargs,
         )
 
diff --git a/opentech/public/utils/templatetags/util_tags.py b/opentech/public/utils/templatetags/util_tags.py
index 8b419b5f4..110822ca5 100644
--- a/opentech/public/utils/templatetags/util_tags.py
+++ b/opentech/public/utils/templatetags/util_tags.py
@@ -33,3 +33,11 @@ def field_type(bound_field):
 @register.simple_tag
 def verbose_name(instance):
     return instance.specific._meta.verbose_name.title()
+
+
+@register.filter
+def get_icon_by_index(qs, i):
+    try:
+        return qs[int(i)].icon
+    except AttributeError:
+        return None
-- 
GitLab