From 5d359d0509e9e9d2982174a7be2750be7f632f0c Mon Sep 17 00:00:00 2001
From: Erin Mullaney <erin.mullaney@torchbox.com>
Date: Tue, 22 Jan 2019 09:51:25 -0500
Subject: [PATCH] 810 filter rounds for leads/staff only, adjust round block
 title

---
 opentech/apply/funds/models/applications.py   | 10 ++++++
 .../templates/funds/includes/round-block.html |  2 +-
 .../funds/templates/funds/submissions.html    |  5 +++
 opentech/apply/funds/views.py                 | 31 +++++++++++--------
 4 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/opentech/apply/funds/models/applications.py b/opentech/apply/funds/models/applications.py
index 30097f1d3..f2a791dee 100644
--- a/opentech/apply/funds/models/applications.py
+++ b/opentech/apply/funds/models/applications.py
@@ -345,6 +345,9 @@ class RoundsAndLabsQueryset(PageQuerySet):
     def closed(self):
         return self.filter(end_date__lt=date.today())
 
+    def by_lead(self, user):
+        return self.filter(lead_pk=user.pk)
+
 
 class RoundsAndLabsProgressQueryset(RoundsAndLabsQueryset):
     def active(self):
@@ -367,6 +370,10 @@ class RoundsAndLabsManager(PageManager):
             end_date=F('roundbase__end_date'),
             parent_path=Left(F('path'), Length('path') - ApplicationBase.steplen, output_field=CharField()),
             fund=Subquery(funds.values('title')[:1]),
+            lead_pk=Coalesce(
+                F('roundbase__lead__pk'),
+                F('labbase__lead__pk'),
+            ),
         )
 
     def with_progress(self):
@@ -406,6 +413,9 @@ class RoundsAndLabsManager(PageManager):
     def new(self):
         return self.get_queryset().new()
 
+    def by_lead(self, user):
+        return self.get_queryset().by_lead(user)
+
 
 class RoundsAndLabs(Page):
     """
diff --git a/opentech/apply/funds/templates/funds/includes/round-block.html b/opentech/apply/funds/templates/funds/includes/round-block.html
index 8fdca2061..56614a4a9 100644
--- a/opentech/apply/funds/templates/funds/includes/round-block.html
+++ b/opentech/apply/funds/templates/funds/includes/round-block.html
@@ -1,6 +1,6 @@
 <div class="wrapper wrapper--bottom-space">
     <section class="section section--with-options">
-        <h4 class="heading heading--normal heading--no-margin">All Rounds and Labs</h4>
+        <h4 class="heading heading--normal heading--no-margin">Your Rounds and Labs</h4>
         <div class="js-tabs">
             <a class="tab__item tab__item--alt" href="#closed-rounds" data-tab="tab-1">Closed</a>
             <a class="tab__item tab__item--alt" href="#open-rounds" data-tab="tab-2">Open</a>
diff --git a/opentech/apply/funds/templates/funds/submissions.html b/opentech/apply/funds/templates/funds/submissions.html
index 353450fa7..6c938d529 100644
--- a/opentech/apply/funds/templates/funds/submissions.html
+++ b/opentech/apply/funds/templates/funds/submissions.html
@@ -14,6 +14,11 @@
 </div>
 
 <div class="wrapper wrapper--large wrapper--inner-space-medium">
+
+    {% if closed_rounds or open_rounds %}
+        {% include "funds/includes/round-block.html" with closed_rounds=closed_rounds open_rounds=open_rounds %}
+    {% endif %}
+
     {% block table %}
         {{ block.super }}
     {% endblock %}
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index b75cf1232..b7c786c28 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -89,19 +89,24 @@ class SubmissionOverviewView(AllActivityContextMixin, BaseAdminSubmissionsTable)
         return super().get_table_data().order_by(F('last_update').desc(nulls_last=True))[:5]
 
     def get_context_data(self, **kwargs):
-        base_query = RoundsAndLabs.objects.with_progress().order_by('end_date')
-        open_rounds = base_query.open()[:6]
-        open_query = '?round_state=open'
-        closed_rounds = base_query.closed()[:6]
-        closed_query = '?round_state=closed'
-
-        return super().get_context_data(
-            open_rounds=open_rounds,
-            open_query=open_query,
-            closed_rounds=closed_rounds,
-            closed_query=closed_query,
-            **kwargs,
-        )
+        # For OTF staff, display rounds/labs where user is the lead
+        if self.request.user.is_staff:
+            base_query = RoundsAndLabs.objects.with_progress().active().order_by('-end_date')
+            base_query = base_query.by_lead(self.request.user)
+            open_rounds = base_query.open()[:6]
+            open_query = '?round_state=open'
+            closed_rounds = base_query.closed()[:6]
+            closed_query = '?round_state=closed'
+
+            return super().get_context_data(
+                open_rounds=open_rounds,
+                open_query=open_query,
+                closed_rounds=closed_rounds,
+                closed_query=closed_query,
+                **kwargs,
+            )
+        else:
+            return super().get_context_data(**kwargs)
 
 
 class SubmissionListView(AllActivityContextMixin, BaseAdminSubmissionsTable):
-- 
GitLab