From 6cef6411bc0b59ee6f06f423ea656d0dd0f1c98e Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 3 Oct 2023 14:51:09 +0200
Subject: [PATCH] add inline CI form to submission fellowship

---
 scipost_django/ethics/forms.py                |  31 ++++++
 ..._submission_competing_interest_create.html |   9 ++
 scipost_django/ethics/urls.py                 |   5 +
 scipost_django/ethics/views.py                |  49 ++++++++-
 .../pool/_hx_submission_fellow_row.html       |  88 +++++++++++++++
 .../submissions/pool/_submission_fellows.html | 100 ++++--------------
 6 files changed, 201 insertions(+), 81 deletions(-)
 create mode 100644 scipost_django/ethics/templates/ethics/_hx_submission_competing_interest_create.html
 create mode 100644 scipost_django/submissions/templates/submissions/pool/_hx_submission_fellow_row.html

diff --git a/scipost_django/ethics/forms.py b/scipost_django/ethics/forms.py
index d7eede36a..7a47977be 100644
--- a/scipost_django/ethics/forms.py
+++ b/scipost_django/ethics/forms.py
@@ -74,3 +74,34 @@ class SubmissionCompetingInterestForm(forms.ModelForm):
                 css_class="row",
             ),
         )
+
+
+class SubmissionCompetingInterestTableRowForm(SubmissionCompetingInterestForm):
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+
+        self.fields["profile"].widget = forms.Select(
+            choices=[(self.initial["profile"].id, str(self.initial["profile"]))],
+        )
+        self.fields["declared_by"].widget = forms.Select(
+            choices=(
+                list(self.fields["related_profile"].choices)
+                + [(self.initial["profile"].id, str(self.initial["profile"]))]
+                + [(self.initial["declared_by"].id, str(self.initial["declared_by"]))]
+            ),
+        )
+        self.helper.layout = Layout(
+            Div(
+                FloatingField("profile", wrapper_class="mb-0"),
+                FloatingField("related_profile", wrapper_class="mb-0"),
+                FloatingField("nature", wrapper_class="mb-0"),
+                FloatingField("date_from", wrapper_class="mb-0"),
+                FloatingField("date_until", wrapper_class="mb-0"),
+                FloatingField("declared_by", wrapper_class="mb-0"),
+                ButtonHolder(Submit("submit", "Declare")),
+                css_class="d-flex justify-content-between align-items-center",
+            )
+        )
+
+        self.fields["related_profile"].label = "Submission author"
+        self.fields["nature"].label = "Nature"
diff --git a/scipost_django/ethics/templates/ethics/_hx_submission_competing_interest_create.html b/scipost_django/ethics/templates/ethics/_hx_submission_competing_interest_create.html
new file mode 100644
index 000000000..fdd345883
--- /dev/null
+++ b/scipost_django/ethics/templates/ethics/_hx_submission_competing_interest_create.html
@@ -0,0 +1,9 @@
+{% load crispy_forms_tags %}
+
+<td colspan="7">
+  <form hx-post="{% url "ethics:_hx_submission_competing_interest_create" identifier_w_vn_nr fellowship_id %}"
+        hx-target="closest tr"
+        hx-swap="outerHTML">
+    {% crispy form %}
+  </form>
+</td>
diff --git a/scipost_django/ethics/urls.py b/scipost_django/ethics/urls.py
index 78c65427b..e8558a5c5 100644
--- a/scipost_django/ethics/urls.py
+++ b/scipost_django/ethics/urls.py
@@ -41,6 +41,11 @@ urlpatterns = [
                     views._hx_submission_competing_interest_form,
                     name="_hx_submission_competing_interest_form",
                 ),
+                path(
+                    "_hx_submission_competing_interest_create/<int:fellowship_id>",
+                    views._hx_submission_competing_interest_create,
+                    name="_hx_submission_competing_interest_create",
+                ),
                 path(
                     "_hx_submission_competing_interest_delete/<int:pk>",
                     views._hx_submission_competing_interest_delete,
diff --git a/scipost_django/ethics/views.py b/scipost_django/ethics/views.py
index 21f70def2..895e03cd4 100644
--- a/scipost_django/ethics/views.py
+++ b/scipost_django/ethics/views.py
@@ -8,9 +8,13 @@ from django.shortcuts import get_object_or_404, render, redirect
 from django.urls import reverse
 
 from ethics.models import SubmissionClearance, CompetingInterest
-from ethics.forms import SubmissionCompetingInterestForm
+from ethics.forms import (
+    SubmissionCompetingInterestForm,
+    SubmissionCompetingInterestTableRowForm,
+)
 
 from colleges.permissions import is_edadmin
+from colleges.models.fellowship import Fellowship
 from submissions.models import Submission
 
 
@@ -134,3 +138,46 @@ def _hx_submission_competing_interest_delete(request, identifier_w_vn_nr, pk):
         "submissions/pool/_submission_fellows.html",
         context,
     )
+
+
+@login_required
+@user_passes_test(is_edadmin)
+def _hx_submission_competing_interest_create(
+    request, identifier_w_vn_nr, fellowship_id
+):
+    submission = get_object_or_404(
+        Submission.objects.in_pool(request.user),
+        preprint__identifier_w_vn_nr=identifier_w_vn_nr,
+    )
+    fellowship = get_object_or_404(Fellowship, id=fellowship_id)
+    initial = {
+        "profile": fellowship.contributor.profile,
+        "declared_by": request.user.contributor,
+    }
+    if submission.author_profiles.count() == 1:
+        initial["related_profile"] = submission.author_profiles.first().profile
+    form = SubmissionCompetingInterestTableRowForm(
+        request.POST or None,
+        submission=submission,
+        initial=initial,
+    )
+    if form.is_valid():
+        instance = form.save()
+        instance.affected_submissions.add(submission)
+        return render(
+            request,
+            "submissions/pool/_hx_submission_fellow_row.html",
+            context={"submission": submission, "fellowship": fellowship},
+        )
+
+    context = {
+        "submission": submission,
+        "form": form,
+        "identifier_w_vn_nr": identifier_w_vn_nr,
+        "fellowship_id": fellowship_id,
+    }
+    return render(
+        request,
+        "ethics/_hx_submission_competing_interest_create.html",
+        context,
+    )
diff --git a/scipost_django/submissions/templates/submissions/pool/_hx_submission_fellow_row.html b/scipost_django/submissions/templates/submissions/pool/_hx_submission_fellow_row.html
new file mode 100644
index 000000000..f628453b3
--- /dev/null
+++ b/scipost_django/submissions/templates/submissions/pool/_hx_submission_fellow_row.html
@@ -0,0 +1,88 @@
+{% load submissions_pool %}
+{% load ethics_extras %}
+
+<tr>
+  <td>{{ fellowship.contributor }}</td>
+  <td>{{ fellowship.get_status_display }}</td>
+  <td>
+
+    {% if fellowship.contributor.is_currently_available %}
+      <span class="text-success">{% include "bi/check-square-fill.html" %}</span>
+    {% else %}
+      <span class="text-danger">{% include "bi/x-square-fill.html" %}</span>
+    {% endif %}
+
+  </td>
+  <td>{% get_fellow_qualification_expertise_level_display submission fellowship %}</td>
+
+  {% if "edadmin" in user_roles or "active_senior_fellow" in user_roles %}
+    <td>
+      {% get_profile_clearance submission.clearances fellowship.contributor.profile as clearance %}
+
+      {% if clearance %}
+        <span class="text-success">all clear</span>
+        &nbsp;<em>(asserted by {{ clearance.asserted_by }})</em>
+      {% else %}
+        {% get_profile_competing_interests submission.competing_interests fellowship.contributor.profile as ci_qs %}
+
+        {% if ci_qs %}
+          <table class="table table-bordered bg-danger bg-opacity-10 mb-0">
+            <thead>
+              <tr>
+                <th>
+                  Related Profile / <em>(nature)</em>
+                </th>
+
+                {% if "edadmin" in user_roles %}<th>Actions</th>{% endif %}
+
+              </tr>
+            </thead>
+            <tbody>
+
+              {% for ci in ci_qs %}
+                <tr>
+                  <td>
+                    {{ ci.related_profile }}
+                    <br />
+                    <em>({{ ci.get_nature_display }})</em>
+                  </td>
+                  <td>
+
+                    {% if "edadmin" in user_roles %}
+                      <a class="btn btn-sm btn-danger px-1 py-0"
+                         hx-get="{% url 'ethics:_hx_submission_competing_interest_delete' identifier_w_vn_nr=submission.preprint.identifier_w_vn_nr pk=ci.pk %}"
+                         hx-confirm="Delete this competing interest?"
+                         hx-target="#submission-{{ submission.pk }}-fellows-details">
+                        {% include "bi/trash-fill.html" %}
+                      </a>
+                    {% endif %}
+
+                  </td>
+                </tr>
+              {% endfor %}
+
+            </tbody>
+          </table>
+        {% else %}
+          <em class="text-danger">unknown</em>
+        {% endif %}
+      {% endif %}
+
+    </td>
+
+    {% if "edadmin" in user_roles %}
+      <td>
+        <a class="btn btn-sm btn-danger px-1 py-0"
+           title="Declare competing interest"
+           hx-get="{% url 'ethics:_hx_submission_competing_interest_create' submission.preprint.identifier_w_vn_nr fellowship.id %}"
+           hx-target="closest tr">{% include "bi/plus-square-fill.html" %}</a>
+        <a class="btn btn-sm btn-danger px-1 py-0"
+           hx-get="{% url 'colleges:_hx_submission_remove_fellowship' submission.preprint.identifier_w_vn_nr fellowship.id %}"
+           hx-confirm="Remove this Fellow from this Submission's Fellowship?"
+           hx-target="#submission-{{ submission.pk }}-fellows-details">{% include "bi/trash-fill.html" %}</a>
+      </td>
+    {% endif %}
+  {% endif %}
+
+  <td>{% get_fellow_readiness_status_display submission fellowship %}</td>
+</tr>
diff --git a/scipost_django/submissions/templates/submissions/pool/_submission_fellows.html b/scipost_django/submissions/templates/submissions/pool/_submission_fellows.html
index 80e9cb0df..31d164c9e 100644
--- a/scipost_django/submissions/templates/submissions/pool/_submission_fellows.html
+++ b/scipost_django/submissions/templates/submissions/pool/_submission_fellows.html
@@ -1,19 +1,10 @@
-{% load submissions_pool %}
-{% load ethics_extras %}
-
-<h2>
-  This Submission's Fellowship
-</h2>
+<h2>This Submission's Fellowship</h2>
 
 {% if "edadmin" in user_roles or "active_senior_fellow" in user_roles %}
-  <div id="submission-{{ submission.pk }}-add-fellow"
-  >
+  <div id="submission-{{ submission.pk }}-add-fellow">
     <a class="btn btn-sm btn-primary m-2"
        hx-get="{% url 'colleges:_hx_submission_add_fellowship' submission.preprint.identifier_w_vn_nr %}"
-       hx-target="#submission-{{ submission.pk }}-add-fellow"
-    >
-      Add a Fellow to this Submission's fellowship
-    </a>
+       hx-target="#submission-{{ submission.pk }}-add-fellow">Add a Fellow to this Submission's fellowship</a>
   </div>
 {% endif %}
 
@@ -22,82 +13,31 @@
     <tr>
       <th>Fellow</th>
       <th>Status</th>
-      <th>Currently<br>available</th>
+      <th>
+        Currently
+        <br />
+        available
+      </th>
       <th>Qualification</th>
+
       {% if "edadmin" in user_roles or "active_senior_fellow" in user_roles %}
-	<th>Competing<br>interests</th>
-	{% if "edadmin" in user_roles %}
-          <th>Actions</th>
-	{% endif %}
+        <th>
+          Competing
+          <br />
+          interests
+        </th>
+
+        {% if "edadmin" in user_roles %}<th>Actions</th>{% endif %}
       {% endif %}
+
       <th>Readiness to take charge</th>
     </tr>
   </thead>
   <tbody>
+
     {% for fellowship in submission.fellows.select_related_contributor__user_and_profile %}
-      <tr>
-        <td>{{ fellowship.contributor }}</td>
-        <td>{{ fellowship.get_status_display }}</td>
-	<td>{% if fellowship.contributor.is_currently_available %}<span class="text-success">{% include "bi/check-square-fill.html" %}</span>{% else %}<span class="text-danger">{% include "bi/x-square-fill.html" %}</span>{% endif %}</td>
-	<td>{% get_fellow_qualification_expertise_level_display submission fellowship %}</td>
-	{% if "edadmin" in user_roles or "active_senior_fellow" in user_roles %}
-	  <td>
-	    {% get_profile_clearance submission.clearances fellowship.contributor.profile as clearance %}
-	    {% if clearance %}
-	      <span class="text-success">all clear</span>
-	      &nbsp;<em>(asserted by {{ clearance.asserted_by }})</em>
-	    {% else %}
-		{% get_profile_competing_interests submission.competing_interests fellowship.contributor.profile as ci_qs %}
-		{% if ci_qs %}
-		  <table class="table table-bordered bg-danger bg-opacity-10 mb-0">
-		    <thead>
-		      <tr>
-			<th>Related Profile / <em>(nature)</em></th>
-			{% if "edadmin" in user_roles %}
-			  <th>Actions</th>
-			{% endif %}
-		      </tr>
-		    </thead>
-		    <tbody>
-		      {% for ci in ci_qs %}
-			<tr>
-			  <td>
-			    {{ ci.related_profile }}
-			    <br>
-			    <em>({{ ci.get_nature_display }})</em>
-			  </td>
-			  <td>
-			    {% if "edadmin" in user_roles %}
-			      <a class="btn btn-sm btn-danger px-1 py-0"
-				 hx-get="{% url 'ethics:_hx_submission_competing_interest_delete' identifier_w_vn_nr=submission.preprint.identifier_w_vn_nr pk=ci.pk %}"
-				 hx-confirm="Delete this competing interest?"
-				 hx-target="#submission-{{ submission.pk }}-fellows-details"
-			      >
-				{% include "bi/trash-fill.html" %}
-			      </a>
-			    {% endif %}
-			  </td>
-			</tr>
-		      {% endfor %}
-		    </tbody>
-		  </table>
-		{% else %}
-		  <em class="text-danger">unknown</em>
-		{% endif %}
-	    {% endif %}
-	  </td>
-	  {% if "edadmin" in user_roles %}
-            <td>
-              <a class="btn btn-sm btn-danger px-1 py-0"
-		 hx-get="{% url 'colleges:_hx_submission_remove_fellowship' submission.preprint.identifier_w_vn_nr fellowship.id %}"
-		 hx-contirm="Remove this Fellow from this Submission's Fellowship?"
-		 hx-target="#submission-{{ submission.pk }}-fellows-details"
-	      >{% include "bi/trash-fill.html" %}</a>
-            </td>
-	  {% endif %}
-	{% endif %}
-	<td>{% get_fellow_readiness_status_display submission fellowship %}</td>
-      </tr>
+      {% include "submissions/pool/_hx_submission_fellow_row.html" with fellowship=fellowship submission=submission %}
     {% endfor %}
+
   </tbody>
 </table>
-- 
GitLab