From 42a42c10061d6955b51b3c2b6c2105db038ea072 Mon Sep 17 00:00:00 2001
From: Erin Mullaney <erin.mullaney@torchbox.com>
Date: Wed, 20 Feb 2019 10:30:46 -0500
Subject: [PATCH] GH-962 make review opinions a reusable include, get the
 opinion author's submission role as a method on ReviewOpinion

---
 opentech/apply/review/models.py                        |  5 +++++
 .../review/includes/review_opinions_list.html          | 10 ++++++++++
 .../apply/review/templates/review/review_detail.html   |  7 +------
 opentech/apply/review/views.py                         |  7 +++----
 4 files changed, 19 insertions(+), 10 deletions(-)
 create mode 100644 opentech/apply/review/templates/review/includes/review_opinions_list.html

diff --git a/opentech/apply/review/models.py b/opentech/apply/review/models.py
index 294b622e8..59f4b19b3 100644
--- a/opentech/apply/review/models.py
+++ b/opentech/apply/review/models.py
@@ -184,3 +184,8 @@ class ReviewOpinion(models.Model):
     @property
     def opinion_display(self):
         return self.get_opinion_display()
+
+    def get_author_role(self):
+        role = self.review.submission.assigned.with_roles().filter(reviewer=self.author).first()
+        role = role.role if role else None
+        return role
diff --git a/opentech/apply/review/templates/review/includes/review_opinions_list.html b/opentech/apply/review/templates/review/includes/review_opinions_list.html
new file mode 100644
index 000000000..60078d119
--- /dev/null
+++ b/opentech/apply/review/templates/review/includes/review_opinions_list.html
@@ -0,0 +1,10 @@
+{% load wagtailimages_tags %}
+
+<ul>
+{% for opinion in opinions %}
+    <li>
+        {% if opinion.get_author_role %}{% image opinion.get_author_role.icon max-12x12 %}{% endif %}
+        {{ opinion.author }} {{ opinion.get_opinion_display }}s
+    </li>
+{% endfor %}
+</ul>
\ No newline at end of file
diff --git a/opentech/apply/review/templates/review/review_detail.html b/opentech/apply/review/templates/review/review_detail.html
index 36be86240..ef4075778 100644
--- a/opentech/apply/review/templates/review/review_detail.html
+++ b/opentech/apply/review/templates/review/review_detail.html
@@ -7,12 +7,7 @@
         <h1 class="beta heading heading--no-margin heading--bold">Review</h1>
         <h5>For <a href="{% url "funds:submissions:detail" review.submission.id %}">{{ review.submission.title }}</a> by {{ review.author }} at {{ review.updated_at|date:"Y-m-d" }}</h5>
 
-        {% for opinion in review.opinions.all %}
-            <div>
-                {# TODO: role icon #} {{ opinion.author }} {{ opinion.get_opinion_display }}s
-            </div>
-        {% endfor %}
-
+        {% include 'review/includes/review_opinions_list.html' with opinions=review.opinions.all %}
     </div>
 </div>
 
diff --git a/opentech/apply/review/views.py b/opentech/apply/review/views.py
index 2fae0ed08..477899310 100644
--- a/opentech/apply/review/views.py
+++ b/opentech/apply/review/views.py
@@ -63,14 +63,13 @@ class ReviewContextMixin:
                     'role': role,
                 }
                 opinions_list = []
-                if review and review.opinions:
+                if review:
+                    # Loop through all opinions and include the current author's role for this submission
                     for opinion in review.opinions.all():
-                        author_role = self.object.assigned.with_roles().filter(reviewer=opinion.author).first()
-                        role = author_role.role if author_role else None
                         opinions_list.append({
                             'author': opinion.author,
                             'opinion': opinion.get_opinion_display(),
-                            'role': role,
+                            'role': opinion.get_author_role(),
                         })
                     review_info_dict['opinions'] = opinions_list
                 reviews_block[key].append(review_info_dict)
-- 
GitLab