From 8e144552f3f561033b93bfc1c02670e80387557d Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Thu, 4 Apr 2024 10:30:35 +0200
Subject: [PATCH] Fix user and staff flagg views. Add staff flag view to main
 menu. (#3843)

Fixes these issues:

* The flagg views are not updated with the new adminbar component.
* Staff flagged view are not included in the main menu.
* Unflagging submissions did not work in the user and staff flagged
views, only on dashboard.
* Staff flagged filter was missing from new all-beta view.
---
 .../applications/submission-search.md         |  1 +
 .../templates/dashboard/includes/flagged.html |  2 +-
 hypha/apply/dashboard/views.py                |  9 ++----
 hypha/apply/funds/tables.py                   | 16 ++++++++++
 .../funds/base_submissions_table.html         |  1 -
 .../templates/funds/reviewer_leaderboard.html |  2 +-
 .../funds/reviewer_leaderboard_detail.html    |  2 +-
 .../templates/funds/staff_assignments.html    |  8 ++++-
 .../funds/templates/funds/submissions.html    | 16 +++++-----
 .../templates/funds/submissions_overview.html |  2 +-
 .../templates/funds/submissions_result.html   |  2 +-
 .../templates/funds/submissions_search.html   | 11 -------
 .../funds/submissions_staff_flagged.html      | 31 ++++++++++++++++---
 .../funds/submissions_user_flagged.html       | 24 +++++++++++---
 .../funds/templates/submissions/all.html      | 12 +++++++
 hypha/apply/funds/views.py                    |  4 +++
 hypha/apply/funds/views_beta.py               |  2 ++
 hypha/core/navigation.py                      |  5 +++
 18 files changed, 107 insertions(+), 43 deletions(-)
 delete mode 100644 hypha/apply/funds/templates/funds/submissions_search.html

diff --git a/docs/user-guides/applications/submission-search.md b/docs/user-guides/applications/submission-search.md
index a659af8e8..91d3d943b 100644
--- a/docs/user-guides/applications/submission-search.md
+++ b/docs/user-guides/applications/submission-search.md
@@ -8,6 +8,7 @@ You can use advanced filters to search for application submissions that meet spe
 - Filter when applications were submitted: `submitted:2023-09-01`, `submitted:>2023-01`, `submitted:<=2023`
 - Filter when applications was last updated: `updated:2023-09-01`, `updated:>2023-01`, `updated:<=2023`
 - Filter your flagged submissions: `flagged:@me`
+- Filter staff flagged submissions: `flagged:@staff`
 
 
 ## Sharing Filters
diff --git a/hypha/apply/dashboard/templates/dashboard/includes/flagged.html b/hypha/apply/dashboard/templates/dashboard/includes/flagged.html
index 0badca3a3..ef096a47f 100644
--- a/hypha/apply/dashboard/templates/dashboard/includes/flagged.html
+++ b/hypha/apply/dashboard/templates/dashboard/includes/flagged.html
@@ -2,7 +2,7 @@
 {% load i18n %}
 
 <h2 class="text-xl mb-2">
-    {% trans "Your Flagged Submissions" %}
+    {% trans "Your flagged submissions" %}
 </h2>
 
 {% if my_flagged.data %}
diff --git a/hypha/apply/dashboard/views.py b/hypha/apply/dashboard/views.py
index 766fadbc5..0665f4ae0 100644
--- a/hypha/apply/dashboard/views.py
+++ b/hypha/apply/dashboard/views.py
@@ -16,8 +16,8 @@ from hypha.apply.funds.tables import (
     SubmissionFilterAndSearch,
     SubmissionReviewerFilterAndSearch,
     SubmissionsTable,
-    SummarySubmissionsTable,
     SummarySubmissionsTableWithRole,
+    UserFlaggedSubmissionsTable,
     review_filter_for_user,
 )
 from hypha.apply.projects.filters import ProjectListFilter
@@ -62,17 +62,12 @@ class MySubmissionContextMixin:
 class MyFlaggedMixin:
     def my_flagged(self, submissions):
         submissions = submissions.flagged_by(self.request.user).order_by("-submit_time")
-        row_attrs = dict(
-            {"data-flag-type": "user"}, **SummarySubmissionsTable._meta.row_attrs
-        )
 
         limit = 5
         return {
-            "table": SummarySubmissionsTable(
+            "table": UserFlaggedSubmissionsTable(
                 submissions[:limit],
                 prefix="my-flagged-",
-                attrs={"class": "all-submissions-table flagged-table"},
-                row_attrs=row_attrs,
             ),
             "display_more": submissions.count() > limit,
         }
diff --git a/hypha/apply/funds/tables.py b/hypha/apply/funds/tables.py
index d381df11f..082b46832 100644
--- a/hypha/apply/funds/tables.py
+++ b/hypha/apply/funds/tables.py
@@ -243,6 +243,22 @@ class SummarySubmissionsTableWithRole(BaseAdminSubmissionsTable):
         return ""
 
 
+class StaffFlaggedSubmissionsTable(SummarySubmissionsTable):
+    class Meta(SummarySubmissionsTable.Meta):
+        row_attrs = dict(
+            {"data-flag-type": "staff"}, **SummarySubmissionsTable._meta.row_attrs
+        )
+        attrs = {"class": "all-submissions-table flagged-table"}
+
+
+class UserFlaggedSubmissionsTable(SummarySubmissionsTable):
+    class Meta(SummarySubmissionsTable.Meta):
+        row_attrs = dict(
+            {"data-flag-type": "user"}, **SummarySubmissionsTable._meta.row_attrs
+        )
+        attrs = {"class": "all-submissions-table flagged-table"}
+
+
 def get_used_rounds(request):
     return Round.objects.filter(submissions__isnull=False).distinct()
 
diff --git a/hypha/apply/funds/templates/funds/base_submissions_table.html b/hypha/apply/funds/templates/funds/base_submissions_table.html
index bd1e3720c..33b9669cf 100644
--- a/hypha/apply/funds/templates/funds/base_submissions_table.html
+++ b/hypha/apply/funds/templates/funds/base_submissions_table.html
@@ -25,5 +25,4 @@
     <script src="{% static 'js/submission-filters.js' %}"></script>
     <script src="{% static 'js/tabs.js' %}"></script>
     <script src="{% static 'js/batch-actions.js' %}"></script>
-    <script src="{% static 'js/flag.js' %}"></script>
 {% endblock %}
diff --git a/hypha/apply/funds/templates/funds/reviewer_leaderboard.html b/hypha/apply/funds/templates/funds/reviewer_leaderboard.html
index 26b2491d8..7779ab602 100644
--- a/hypha/apply/funds/templates/funds/reviewer_leaderboard.html
+++ b/hypha/apply/funds/templates/funds/reviewer_leaderboard.html
@@ -1,4 +1,4 @@
-{% extends "funds/submissions_overview.html" %}
+{% extends "funds/base_submissions_table.html" %}
 {% load i18n static %}
 {% load render_table from django_tables2 %}
 
diff --git a/hypha/apply/funds/templates/funds/reviewer_leaderboard_detail.html b/hypha/apply/funds/templates/funds/reviewer_leaderboard_detail.html
index ab2dba329..03d0d18bc 100644
--- a/hypha/apply/funds/templates/funds/reviewer_leaderboard_detail.html
+++ b/hypha/apply/funds/templates/funds/reviewer_leaderboard_detail.html
@@ -1,4 +1,4 @@
-{% extends "funds/submissions_overview.html" %}
+{% extends "funds/base_submissions_table.html" %}
 {% load i18n static %}
 {% load render_table from django_tables2 %}
 
diff --git a/hypha/apply/funds/templates/funds/staff_assignments.html b/hypha/apply/funds/templates/funds/staff_assignments.html
index bf5bae87f..0be6503cc 100644
--- a/hypha/apply/funds/templates/funds/staff_assignments.html
+++ b/hypha/apply/funds/templates/funds/staff_assignments.html
@@ -1,4 +1,4 @@
-{% extends "funds/submissions_overview.html" %}
+{% extends "funds/base_submissions_table.html" %}
 {% load i18n static %}
 {% load render_table from django_tables2 %}
 
@@ -17,3 +17,9 @@
         {% endblock %}
     </div>
 {% endblock %}
+
+{% block extra_js %}
+    {{ filter.form.media.js }}
+    <script src="{% static 'js/symbol-es6.min.js' %}"></script>
+    <script src="{% static 'js/flag.js' %}"></script>
+{% endblock %}
diff --git a/hypha/apply/funds/templates/funds/submissions.html b/hypha/apply/funds/templates/funds/submissions.html
index 205b3bb45..347896fbe 100644
--- a/hypha/apply/funds/templates/funds/submissions.html
+++ b/hypha/apply/funds/templates/funds/submissions.html
@@ -8,14 +8,14 @@
 
     {% adminbar %}
         {% slot header %}
-            {% trans "All Submissions" %}<span class="submissions-count"> ({{ table.rows|length }})
-                <a class="text-sm text-lime-300 border-b border-transparent hover:opacity-70 transition-all"
-                   hx-boost="true"
-                   href="{% url 'apply:submissions:list-beta' %}"
-                >
-                    {% heroicon_outline "rocket-launch" aria_hidden="true" size=15 stroke_width="2" class="inline align-text-bottom" %}
-                    {% trans "Try newer version" %}
-                </a>
+            {% trans "All Submissions" %}<span class="submissions-count"> ({{ table.rows|length }})</span>
+            <a class="text-sm text-lime-300 border-b border-transparent hover:opacity-70 transition-all"
+               hx-boost="true"
+               href="{% url 'apply:submissions:list-beta' %}"
+            >
+                {% heroicon_outline "rocket-launch" aria_hidden="true" size=15 stroke_width="2" class="inline align-text-bottom" %}
+                {% trans "Try newer version" %}
+            </a>
         {% endslot %}
         {% slot sub_heading %}{% trans "Search and filter all submissions" %}{% endslot %}
     {% endadminbar %}
diff --git a/hypha/apply/funds/templates/funds/submissions_overview.html b/hypha/apply/funds/templates/funds/submissions_overview.html
index 77f2725d0..410be73ac 100644
--- a/hypha/apply/funds/templates/funds/submissions_overview.html
+++ b/hypha/apply/funds/templates/funds/submissions_overview.html
@@ -32,7 +32,7 @@
 
             {% if staff_flagged.table.data %}
                 <div class="wrapper wrapper--bottom-space">
-                    <h4 class="heading heading--normal">{% trans "Staff Flagged Submissions" %}</h4>
+                    <h4 class="heading heading--normal">{% trans "Staff flagged submissions" %}</h4>
                     {% render_table staff_flagged.table %}
                     {% if staff_flagged.display_more %}
                         <div class="all-submissions-table__more">
diff --git a/hypha/apply/funds/templates/funds/submissions_result.html b/hypha/apply/funds/templates/funds/submissions_result.html
index 9d6c917f3..e696b8240 100644
--- a/hypha/apply/funds/templates/funds/submissions_result.html
+++ b/hypha/apply/funds/templates/funds/submissions_result.html
@@ -1,4 +1,4 @@
-{% extends "funds/submissions_overview.html" %}
+{% extends "funds/base_submissions_table.html" %}
 {% load i18n static apply_tags %}
 {% block title %}{% trans "Submissions results" %}{% endblock %}
 
diff --git a/hypha/apply/funds/templates/funds/submissions_search.html b/hypha/apply/funds/templates/funds/submissions_search.html
deleted file mode 100644
index 8457dcba3..000000000
--- a/hypha/apply/funds/templates/funds/submissions_search.html
+++ /dev/null
@@ -1,11 +0,0 @@
-{% extends "funds/submissions.html" %}
-{% load i18n %}
-
-{% block page_header %}
-    <div class="wrapper wrapper--medium">
-        <h3>{% trans "Search results" %}</h3>
-        {% if search_term %}
-            <h5>{% blocktrans with count=object_list.count %}There are {{ count }} results for: {{ search_term }}{% endblocktrans %}</h5>
-        {% endif %}
-    </div>
-{% endblock %}
diff --git a/hypha/apply/funds/templates/funds/submissions_staff_flagged.html b/hypha/apply/funds/templates/funds/submissions_staff_flagged.html
index 187911ccf..00436c3c9 100644
--- a/hypha/apply/funds/templates/funds/submissions_staff_flagged.html
+++ b/hypha/apply/funds/templates/funds/submissions_staff_flagged.html
@@ -1,8 +1,29 @@
-{% extends "funds/submissions.html" %}
-{% load i18n %}
+{% extends "funds/base_submissions_table.html" %}
+{% load i18n static %}
+{% load render_table from django_tables2 %}
 
-{% block page_header %}
-    <div>
-        <h1 class="gamma heading heading--no-margin heading--bold">{% trans "Staff Flagged Submissions" %}</h1>
+{% block title %}{% trans "Staff flagged submissions" %}{% endblock %}
+
+{% block content %}
+
+    {% adminbar %}
+        {% slot header %}
+            {% trans "Staff flagged submissions" %}<span class="submissions-count"> ({{ table.rows|length }})</span>
+        {% endslot %}
+        {% slot sub_heading %}{% trans "Submissions flagged by staff" %}{% endslot %}
+    {% endadminbar %}
+
+
+    <div class="wrapper wrapper--large wrapper--inner-space-medium">
+        {% block table %}
+            {% render_table table %}
+        {% endblock %}
     </div>
 {% endblock %}
+
+{% block extra_js %}
+    {{ filter.form.media.js }}
+    <script src="{% static 'js/all-submissions-table.js' %}"></script>
+    <script src="{% static 'js/symbol-es6.min.js' %}"></script>
+    <script src="{% static 'js/flag.js' %}"></script>
+{% endblock %}
diff --git a/hypha/apply/funds/templates/funds/submissions_user_flagged.html b/hypha/apply/funds/templates/funds/submissions_user_flagged.html
index b570eb1c7..05653fdf8 100644
--- a/hypha/apply/funds/templates/funds/submissions_user_flagged.html
+++ b/hypha/apply/funds/templates/funds/submissions_user_flagged.html
@@ -1,8 +1,22 @@
-{% extends "funds/submissions.html" %}
-{% load i18n %}
+{% extends "funds/base_submissions_table.html" %}
+{% load i18n static %}
+{% load render_table from django_tables2 %}
 
-{% block page_header %}
-    <div>
-        <h1 class="gamma heading heading--no-margin heading--bold">{% trans "Your Flagged Submissions" %}</h1>
+{% block title %}{% trans "Your flagged submissions" %}{% endblock %}
+
+{% block content %}
+
+    {% adminbar %}
+        {% slot header %}
+            {% trans "Your flagged submissions" %} <span class="submissions-count">({{ table.rows|length }})</span>
+        {% endslot %}
+        {% slot sub_heading %}{% trans "Submissions flagged by you" %}{% endslot %}
+    {% endadminbar %}
+
+
+    <div class="wrapper wrapper--large wrapper--inner-space-medium">
+        {% block table %}
+            {% render_table table %}
+        {% endblock %}
     </div>
 {% endblock %}
diff --git a/hypha/apply/funds/templates/submissions/all.html b/hypha/apply/funds/templates/submissions/all.html
index 6d36bd2c0..e3241d6f8 100644
--- a/hypha/apply/funds/templates/submissions/all.html
+++ b/hypha/apply/funds/templates/submissions/all.html
@@ -51,6 +51,18 @@
                 {% endif %}
                 {% trans "Your flagged submissions" %}
             </a>
+            <a
+                href="{% url "apply:submissions:list-beta" %}?query=flagged:@staff"
+                hx-get="{% url "apply:submissions:list-beta" %}?query=flagged:@staff"
+                hx-push-url="true"
+                hx-target="#main"
+                hx-swap="outerHTML"
+                class="flex {% if request.GET.query == "flagged:@staff" %}ps-2 font-medium bg-gray-100{% else %}ps-8 font-normal{% endif %} pe-3 py-2 text-gray-800 border-b items-center hover:bg-gray-100 focus:bg-gray-100">
+                {% if request.GET.query == "flagged:@staff" %}
+                    {% heroicon_mini "check" aria_hidden="true" size=16 class="stroke-2 me-1" %}
+                {% endif %}
+                {% trans "Staff flagged submissions" %}
+            </a>
             <a
                 href="{% url "apply:submissions:list-beta" %}?query=reviewer:@me"
                 href="{% url "apply:submissions:list-beta" %}?query=reviewer:@me"
diff --git a/hypha/apply/funds/views.py b/hypha/apply/funds/views.py
index e5dc76c60..81a2cef5c 100644
--- a/hypha/apply/funds/views.py
+++ b/hypha/apply/funds/views.py
@@ -114,9 +114,11 @@ from .tables import (
     RoundsFilter,
     RoundsTable,
     StaffAssignmentsTable,
+    StaffFlaggedSubmissionsTable,
     SubmissionFilterAndSearch,
     SubmissionReviewerFilterAndSearch,
     SummarySubmissionsTable,
+    UserFlaggedSubmissionsTable,
 )
 from .utils import get_default_screening_statues
 from .workflow import (
@@ -582,6 +584,7 @@ class SubmissionListView(ViewDispatcher):
 
 @method_decorator(staff_required, name="dispatch")
 class SubmissionStaffFlaggedView(BaseAdminSubmissionsTable):
+    table_class = StaffFlaggedSubmissionsTable
     template_name = "funds/submissions_staff_flagged.html"
 
     def get_queryset(self):
@@ -595,6 +598,7 @@ class SubmissionStaffFlaggedView(BaseAdminSubmissionsTable):
 
 @method_decorator(login_required, name="dispatch")
 class SubmissionUserFlaggedView(UserPassesTestMixin, BaseAdminSubmissionsTable):
+    table_class = UserFlaggedSubmissionsTable
     template_name = "funds/submissions_user_flagged.html"
 
     def get_queryset(self):
diff --git a/hypha/apply/funds/views_beta.py b/hypha/apply/funds/views_beta.py
index 4c8eedb30..65044abfd 100644
--- a/hypha/apply/funds/views_beta.py
+++ b/hypha/apply/funds/views_beta.py
@@ -124,6 +124,8 @@ def submission_all_beta(
     if "flagged" in search_filters:
         if "@me" in search_filters["flagged"]:
             qs = qs.flagged_by(request.user)
+        if "@staff" in search_filters["flagged"]:
+            qs = qs.flagged_staff()
 
     if "lead" in search_filters:
         if "@me" in search_filters["lead"]:
diff --git a/hypha/core/navigation.py b/hypha/core/navigation.py
index 0b036c577..2e77ea268 100644
--- a/hypha/core/navigation.py
+++ b/hypha/core/navigation.py
@@ -34,6 +34,11 @@ nav_items = [
                 "url": reverse_lazy("apply:submissions:result"),
                 "permission_method": "is_apply_staff",
             },
+            {
+                "title": _("Staff flagged"),
+                "url": reverse_lazy("apply:submissions:staff_flagged"),
+                "permission_method": "is_apply_staff",
+            },
         ],
     },
     {
-- 
GitLab