From 40efe786f9c406c6edfa29f0c18eee820b18db12 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Tue, 1 Aug 2023 18:50:39 +0300
Subject: [PATCH] convert metadata funding to HTMX

---
 ...lication_metadata_add_generic_funding.html | 22 ++++++++++
 ...ublication_metadata_add_grant_funding.html | 31 ++++++++++++++
 .../manage_metadata_for_publication.html      | 41 ++-----------------
 scipost_django/journals/urls/general.py       |  8 ++--
 scipost_django/journals/views.py              | 24 ++++++++---
 5 files changed, 78 insertions(+), 48 deletions(-)
 create mode 100644 scipost_django/journals/templates/journals/_hx_publication_metadata_add_generic_funding.html
 create mode 100644 scipost_django/journals/templates/journals/_hx_publication_metadata_add_grant_funding.html

diff --git a/scipost_django/journals/templates/journals/_hx_publication_metadata_add_generic_funding.html b/scipost_django/journals/templates/journals/_hx_publication_metadata_add_generic_funding.html
new file mode 100644
index 000000000..145800cc1
--- /dev/null
+++ b/scipost_django/journals/templates/journals/_hx_publication_metadata_add_generic_funding.html
@@ -0,0 +1,22 @@
+{% load bootstrap %}
+
+<h2>Generic (not via grant) funders associated to this publication:</h2>
+<ul>
+
+  {% for funder in publication.funders_generic.all %}
+    <li>{{ funder }}</li>
+  {% empty %}
+    <li>No generic funder found</li>
+  {% endfor %}
+
+</ul>
+
+<div class="card p-3 mb-4">
+  <h3>Associate a generic funder to this publication:</h3>
+  <form hx-post="{% url 'journals:_hx_publication_metadata_add_generic_funding' publication.doi_label %}"
+        hx-target="#publication-generic-funding">
+    {% csrf_token %}
+    {{ form|bootstrap }}
+    <input class="btn btn-outline-secondary" type="submit" value="Add" />
+  </form>
+</div>
diff --git a/scipost_django/journals/templates/journals/_hx_publication_metadata_add_grant_funding.html b/scipost_django/journals/templates/journals/_hx_publication_metadata_add_grant_funding.html
new file mode 100644
index 000000000..d264b425d
--- /dev/null
+++ b/scipost_django/journals/templates/journals/_hx_publication_metadata_add_grant_funding.html
@@ -0,0 +1,31 @@
+{% load bootstrap %}
+
+<h2>Funding statement for this publication:</h2>
+
+{% if publication.metadata.funding_statement %}
+  <p>{{ publication.metadata.funding_statement }}</p>
+{% else %}
+  <p>No funding statement was found</p>
+{% endif %}
+
+<h2>Grants associated to this publication:</h2>
+<ul>
+
+  {% for grant in publication.grants.all %}
+    <li>{{ grant }}</li>
+  {% empty %}
+    <li>no associated grants found</li>
+  {% endfor %}
+
+</ul>
+
+<div class="card p-3 mb-4">
+  <h3>Associate a grant to this publication:</h3>
+  <form hx-post="{% url 'journals:_hx_publication_metadata_add_grant_funding' publication.doi_label %}"
+        hx-target="#publication-grant-funding">
+    {% csrf_token %}
+    {{ form|bootstrap }}
+ 
+    <input class="btn btn-outline-secondary" type="submit" value="Add" />
+  </form>
+</div>
diff --git a/scipost_django/journals/templates/journals/manage_metadata_for_publication.html b/scipost_django/journals/templates/journals/manage_metadata_for_publication.html
index 433616f75..4434afffc 100644
--- a/scipost_django/journals/templates/journals/manage_metadata_for_publication.html
+++ b/scipost_django/journals/templates/journals/manage_metadata_for_publication.html
@@ -34,47 +34,12 @@
     </div>
 
     <div class="col-md-6">
-      <h2>Funding statement for this publication:</h2>
-      {% if publication.metadata.funding_statement %}
-	<p>{{ publication.metadata.funding_statement }}</p>
-      {% else %}
-	<p>No funding statement was found</p>
-      {% endif %}
-      <h2>Grants associated to this publication:</h2>
-      <ul>
-	{% for grant in publication.grants.all %}
-	  <li>{{ grant }}</li>
-	{% empty %}
-	  <li>no associated grants found</li>
-	{% endfor %}
-      </ul>
-      <div class="card p-3 mb-4">
-	<h3>Associate a grant to this publication:</h3>
-	<form action="{% url 'journals:add_associated_grant' publication.doi_label %}" method="post">
-    	  {% csrf_token %}
-    	  {{ associate_grant_form|bootstrap }}
-    	  <input class="btn btn-outline-secondary" type="submit" value="Add">
-	</form>
+      <div id="publication-grant-funding" hx-get="{% url 'journals:_hx_publication_metadata_add_grant_funding' publication.doi_label %}" hx-trigger="intersect once">
       </div>
 
-      <h2>Generic (not via grant) funders associated to this publication:</h2>
-      <ul>
-	{% for funder in publication.funders_generic.all %}
-	  <li>{{ funder }}</li>
-	{% empty %}
-	  <li>No generic funder found</li>
-	{% endfor %}
-      </ul>
-
-      <div class="card p-3 mb-4">
-	<h3>Associate a generic funder to this publication:</h3>
-	<form action="{% url 'journals:add_generic_funder' publication.doi_label %}" method="post">
-    	  {% csrf_token %}
-    	  {{ associate_generic_funder_form|bootstrap }}
-    	  <input class="btn btn-outline-secondary" type="submit" value="Add">
-	</form>
+      <div id="publication-generic-funding" hx-get="{% url 'journals:_hx_publication_metadata_add_generic_funding' publication.doi_label %}" hx-trigger="intersect once">
       </div>
-
+      
       <h3>Other funding-related actions:</h3>
       <ul>
 	<li><a href="{% url 'funders:funders_dashboard' %}" target="_blank">go to the Funders page to add a Funder and/or Grant instance</a></li>
diff --git a/scipost_django/journals/urls/general.py b/scipost_django/journals/urls/general.py
index 6a20f8bfd..aa7fd358b 100644
--- a/scipost_django/journals/urls/general.py
+++ b/scipost_django/journals/urls/general.py
@@ -146,13 +146,13 @@ urlpatterns = [
     ),
     path(
         "admin/<publication_doi_label:doi_label>/funders/add_generic",
-        journals_views.add_generic_funder,
-        name="add_generic_funder",
+        journals_views._hx_publication_metadata_add_generic_funding,
+        name="_hx_publication_metadata_add_generic_funding",
     ),
     path(
         "admin/<publication_doi_label:doi_label>/grants/add",
-        journals_views.add_associated_grant,
-        name="add_associated_grant",
+        journals_views._hx_publication_metadata_add_grant_funding,
+        name="_hx_publication_metadata_add_grant_funding",
     ),
     path(
         "admin/<publication_doi_label:doi_label>/view_autotemplate/<int:autotemplate_id>/",
diff --git a/scipost_django/journals/views.py b/scipost_django/journals/views.py
index ab608801f..4a43e3f9b 100644
--- a/scipost_django/journals/views.py
+++ b/scipost_django/journals/views.py
@@ -705,11 +705,9 @@ class PublicationPublishView(PermissionsMixin, RequestViewMixin, UpdateView):
 def manage_metadata(request, doi_label=None):
     if doi_label:
         publication = get_object_or_404(Publication, doi_label=doi_label)
-        associate_grant_form = GrantSelectForm()
         associate_generic_funder_form = FunderSelectForm()
         context = {
             "publication": publication,
-            "associate_grant_form": associate_grant_form,
             "associate_generic_funder_form": associate_generic_funder_form,
         }
         return render(request, "journals/manage_metadata_for_publication.html", context)
@@ -904,7 +902,7 @@ class FundingInfoView(
 
 @permission_required("scipost.can_draft_publication", return_403=True)
 @transaction.atomic
-def add_associated_grant(request, doi_label):
+def _hx_publication_metadata_add_grant_funding(request, doi_label):
     """
     Called by an Editorial Administrator.
     This associates a grant from the database to this publication.
@@ -920,12 +918,19 @@ def add_associated_grant(request, doi_label):
         publication.doideposit_needs_updating = True
         publication.save()
         messages.success(request, "Grant added to publication %s" % str(publication))
-    return redirect(publication.get_absolute_url())
+    return render(
+        request,
+        "journals/_hx_publication_metadata_add_grant_funding.html",
+        {
+            "form": grant_select_form,
+            "publication": publication,
+        },
+    )
 
 
 @permission_required("scipost.can_draft_publication", return_403=True)
 @transaction.atomic
-def add_generic_funder(request, doi_label):
+def _hx_publication_metadata_add_generic_funding(request, doi_label):
     """
     Called by an Editorial Administrator.
     This associates a funder (generic, not via grant) from the database to this publication.
@@ -942,7 +947,14 @@ def add_generic_funder(request, doi_label):
         messages.success(
             request, "Generic funder added to publication %s" % str(publication)
         )
-    return redirect(publication.get_absolute_url())
+    return render(
+        request,
+        "journals/_hx_publication_metadata_add_generic_funding.html",
+        {
+            "form": funder_select_form,
+            "publication": publication,
+        },
+    )
 
 
 class CreateMetadataXMLView(
-- 
GitLab