From 2b4202dd7f2d71c055c35de8b38bca88030e29d9 Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Mon, 11 May 2020 16:20:36 +0200
Subject: [PATCH] Move template, add link to leaderbord, default sort on total,
 comment out slow queries.

---
 hypha/apply/funds/tables.py                                  | 1 +
 .../v1 => funds}/templates/funds/review_leaderboard.html     | 5 +----
 hypha/apply/funds/templates/funds/submissions_overview.html  | 3 +++
 hypha/apply/funds/views.py                                   | 4 ++--
 hypha/apply/review/models.py                                 | 3 +++
 5 files changed, 10 insertions(+), 6 deletions(-)
 rename hypha/apply/{api/v1 => funds}/templates/funds/review_leaderboard.html (84%)

diff --git a/hypha/apply/funds/tables.py b/hypha/apply/funds/tables.py
index b9824e848..edf1b6c21 100644
--- a/hypha/apply/funds/tables.py
+++ b/hypha/apply/funds/tables.py
@@ -421,6 +421,7 @@ class LeaderboardTable(tables.Table):
             'most_recent',
         ]
         model = User
+        order_by = ('-total',)
 
     def render_most_recent(self, record):
         review = (Review.objects.filter(author__reviewer=record)
diff --git a/hypha/apply/api/v1/templates/funds/review_leaderboard.html b/hypha/apply/funds/templates/funds/review_leaderboard.html
similarity index 84%
rename from hypha/apply/api/v1/templates/funds/review_leaderboard.html
rename to hypha/apply/funds/templates/funds/review_leaderboard.html
index 36131fa70..620bbeb76 100644
--- a/hypha/apply/api/v1/templates/funds/review_leaderboard.html
+++ b/hypha/apply/funds/templates/funds/review_leaderboard.html
@@ -19,12 +19,9 @@
 
 <div class="wrapper wrapper--large wrapper--inner-space-medium">
     {% block table %}
-        {% include "funds/includes/table_filter_and_search.html" with filter_form=filter_form search_term=search_term use_search=True filter_action=filter_action use_batch_actions=False heading="All Submissions" %}
+        {% include "funds/includes/table_filter_and_search.html" with filter_form=filter_form search_term=search_term use_search=True filter_action=filter_action use_batch_actions=False heading="All reviewers" %}
 
         {% render_table table %}
-        <div class="all-submissions-table__more">
-            <a href="{% url 'apply:submissions:list' %}">Show all</a>
-        </div>
     {% endblock %}
 </div>
 {% endblock %}
diff --git a/hypha/apply/funds/templates/funds/submissions_overview.html b/hypha/apply/funds/templates/funds/submissions_overview.html
index b83f169b8..d954386f0 100644
--- a/hypha/apply/funds/templates/funds/submissions_overview.html
+++ b/hypha/apply/funds/templates/funds/submissions_overview.html
@@ -13,6 +13,9 @@
             {% if request.user.is_apply_staff %}
             <div class="tabs">
                 <div class="tabs__container">
+                    <a class="tab__item tab__item--right" href="{% url 'apply:submissions:leaderboard' %}">
+                        Leaderboard
+                    </a>
                     <a class="tab__item tab__item--right" href="{% url 'apply:submissions:result' %}">
                         Results
                     </a>
diff --git a/hypha/apply/funds/views.py b/hypha/apply/funds/views.py
index 2061a89bf..9b951f00c 100644
--- a/hypha/apply/funds/views.py
+++ b/hypha/apply/funds/views.py
@@ -1206,11 +1206,11 @@ class ReviewLeaderboard(SingleTableMixin, FilterView):
         ninety_days_ago = timezone.now() - timedelta(days=90)
         this_year = timezone.now().year
         last_year = timezone.now().year - 1
-        latest_reviews = Review.objects.filter(author__reviewer_id=OuterRef('pk')).order_by('-created_at')
+        # latest_reviews = Review.objects.filter(author__reviewer_id=OuterRef('pk')).order_by('-created_at')
         return super().get_table_data().filter(submissions_reviewer__isnull=False).annotate(
             total=Count('assignedreviewers__review'),
             ninety_days=Count('assignedreviewers__review', filter=Q(assignedreviewers__review__created_at__date__gte=ninety_days_ago)),
             this_year=Count('assignedreviewers__review', filter=Q(assignedreviewers__review__created_at__year=this_year)),
             last_year=Count('assignedreviewers__review', filter=Q(assignedreviewers__review__created_at__year=last_year)),
-            most_recent=Subquery(latest_reviews.values('id')[:1])
+            # most_recent=Subquery(latest_reviews.values('id')[:1])
         )
diff --git a/hypha/apply/review/models.py b/hypha/apply/review/models.py
index 0f91ec9de..6d782e855 100644
--- a/hypha/apply/review/models.py
+++ b/hypha/apply/review/models.py
@@ -101,6 +101,9 @@ class ReviewQuerySet(models.QuerySet):
     def by_partners(self):
         return self.submitted()._by_group(PARTNER_GROUP_NAME)
 
+    def by_user(self, user):
+        return self.submitted().filter(author__reviewer=user).order_by('-created_at')
+
     def staff_score(self):
         return self.by_staff().score()
 
-- 
GitLab