From a7b1eb7154a3d8d078686b93b985524a197b0038 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Fri, 29 Sep 2023 16:36:45 +0200
Subject: [PATCH] hide nomination new round btn when not needed automatically
 open correct details in nomination

---
 .../colleges/_hx_nomination_li_contents.html  |  4 ++--
 .../_hx_nomination_voting_rounds_tab.html     | 22 +++++++++----------
 scipost_django/colleges/views.py              | 12 +++++++++-
 3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html b/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html
index 216193a4d..a0abaa3bc 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_li_contents.html
@@ -129,7 +129,7 @@
     </div>
   </div>
 
-  <details class="card mb-3">
+  <details {% if not nomination.invitation %}open{% endif %} class="card mb-3">
     <summary class="card-header list-triangle">Voting Rounds</summary>
     <div class="card-body">
       <div hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination.id round_id=nomination.latest_voting_round.id|default:0 %}"
@@ -138,7 +138,7 @@
   </details>
 
   {% if "edadmin" in user_roles and nomination.decision.outcome == 'elected' %}
-    <details open class="card">
+    <details {% if nomination.invitation %}open{% endif %} class="card">
       <summary class="card-header d-flex flex-row justify-content-between list-triangle">
         <div>Invitation</div>
         <div>{{ nomination.invitation.get_response_display }}</div>
diff --git a/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html b/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
index 4e5f8503f..eb99f0166 100644
--- a/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
+++ b/scipost_django/colleges/templates/colleges/_hx_nomination_voting_rounds_tab.html
@@ -1,14 +1,14 @@
-<div id="nomination-{{ nomination_id }}-round-tab-holder">
+<div id="nomination-{{ nomination.id }}-round-tab-holder">
 
   <nav class="nav nav-pills m-2 overflow-scroll">
 
-    {% if "edadmin" in user_roles %}
-      <div type="button" class="me-2 px-2 nav-link 
-        {% if new_round %}bg-success{% else %}border border-success{% endif %}
-         " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_create' nomination_id=nomination_id %}" hx-target="#nomination-{{ nomination_id }}-round-tab-holder" hx-swap="outerHTML">
-        <span class="fs-1 align-items-center text- 
-          {% if new_round %}white{% else %}success{% endif %}
-         ">+</span>
+    {% if should_show_new_round_tab_btn %}
+      <div type="button" 
+          class="me-2 px-2 nav-link {% if new_round %}bg-success{% else %}border border-success{% endif %}" 
+          hx-get="{% url 'colleges:_hx_nomination_voting_rounds_create' nomination_id=nomination.id %}" 
+          hx-target="#nomination-{{ nomination.id }}-round-tab-holder" 
+          hx-swap="outerHTML">
+        <span class="fs-1 align-items-center text-{% if new_round %}white{% else %}success{% endif %}">+</span>
       </div>
     {% endif %}
 
@@ -17,7 +17,7 @@
         {% if selected_round and selected_round.id == voting_round.id %}active{% endif %}
   
         {% if voting_round.id in inaccessible_round_ids %}disabled opacity-50{% endif %}
-         " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination_id round_id=voting_round.id %}" hx-target="#nomination-{{ nomination_id }}-round-tab-holder" hx-swap="outerHTML">
+         " hx-get="{% url 'colleges:_hx_nomination_voting_rounds_tab' nomination_id=nomination.id round_id=voting_round.id %}" hx-target="#nomination-{{ nomination.id }}-round-tab-holder" hx-swap="outerHTML">
         <span class="d-block text-nowrap">
 
           {% if voting_round.voting_opens and voting_round.voting_deadline %}
@@ -42,7 +42,7 @@
 
 
 
-    <div id="indicator-nomination-{{ nomination_id }}-details-contents"
+    <div id="indicator-nomination-{{ nomination.id }}-details-contents"
          class="htmx-indicator p-2 ms-auto">
       <button class="btn btn-warning" type="button" disabled>
         <strong>Loading ...</strong>
@@ -56,7 +56,7 @@
   </nav>
 
   {% if selected_round %}
-    <div id="nomination-{{ nomination_id }}-round-{{ selected_round.id }}-tab-content-holder"
+    <div id="nomination-{{ nomination.id }}-round-{{ selected_round.id }}-tab-content-holder"
          class="p-3">
       {% include "colleges/_hx_voting_round_details.html" with voting_round=selected_round round_start_form=round_start_form new_voter_form=new_voter_form %}
     </div>
diff --git a/scipost_django/colleges/views.py b/scipost_django/colleges/views.py
index 9c0277f94..bab345269 100644
--- a/scipost_django/colleges/views.py
+++ b/scipost_django/colleges/views.py
@@ -910,10 +910,20 @@ def _hx_nomination_voting_rounds_tab(request, nomination_id, round_id):
         round.id for round in voting_rounds if not round.can_view(request.user)
     ]
 
+    should_show_new_round_tab_btn = request.user.contributor.is_ed_admin and (
+        nomination.voting_rounds.count() == 0
+        or (
+            nomination.latest_voting_round.is_closed
+            and (decision := getattr(nomination.latest_voting_round, "decision", None))
+            and not decision.outcome == FellowshipNominationDecision.OUTCOME_ELECTED
+        )
+    )
+
     context = {
-        "nomination_id": nomination_id,
+        "nomination": nomination,
         "voting_rounds": voting_rounds,
         "inaccessible_round_ids": inaccessible_round_ids,
+        "should_show_new_round_tab_btn": should_show_new_round_tab_btn,
         "new_round": False,
     }
 
-- 
GitLab