From a43e61ed4692085bcfcece6fc52ada09b391f199 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Mon, 14 Aug 2023 13:19:45 +0300
Subject: [PATCH] allow deleting of grant objects from metadata page

---
 ...lication_metadata_add_generic_funding.html |  8 +++-
 ...ublication_metadata_add_grant_funding.html |  8 +++-
 scipost_django/journals/urls/general.py       | 10 ++++
 scipost_django/journals/views.py              | 48 +++++++++++++++++--
 4 files changed, 67 insertions(+), 7 deletions(-)

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
index 145800cc1..b2213dbce 100644
--- 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
@@ -4,7 +4,13 @@
 <ul>
 
   {% for funder in publication.funders_generic.all %}
-    <li>{{ funder }}</li>
+    <li id="funder-{{ funder.id }}">
+      {{ funder }}
+      <a class="text-danger ms-2"
+         hx-get="{% url 'journals:_hx_publication_metadata_delete_generic_funding' publication.doi_label funder.id %}"
+         hx-target="#funder-{{ funder.id }}"
+         hx-swap="delete">{% include "bi/trash-fill.html" %}</a>
+    </li>
   {% empty %}
     <li>No generic funder found</li>
   {% endfor %}
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
index d264b425d..7a2aabac6 100644
--- 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
@@ -12,7 +12,13 @@
 <ul>
 
   {% for grant in publication.grants.all %}
-    <li>{{ grant }}</li>
+    <li id="grant-{{ grant.id }}">
+      {{ grant }}
+      <a class="text-danger ms-2"
+         hx-get="{% url 'journals:_hx_publication_metadata_delete_grant_funding' publication.doi_label grant.id %}"
+         hx-target="#grant-{{ grant.id }}"
+         hx-swap="delete">{% include "bi/trash-fill.html" %}</a>
+    </li>
   {% empty %}
     <li>no associated grants found</li>
   {% endfor %}
diff --git a/scipost_django/journals/urls/general.py b/scipost_django/journals/urls/general.py
index aa7fd358b..0ec0d9fb6 100644
--- a/scipost_django/journals/urls/general.py
+++ b/scipost_django/journals/urls/general.py
@@ -149,11 +149,21 @@ urlpatterns = [
         journals_views._hx_publication_metadata_add_generic_funding,
         name="_hx_publication_metadata_add_generic_funding",
     ),
+    path(
+        "admin/<publication_doi_label:doi_label>/funders/<int:funder_id>/delete",
+        journals_views._hx_publication_metadata_delete_generic_funding,
+        name="_hx_publication_metadata_delete_generic_funding",
+    ),
     path(
         "admin/<publication_doi_label:doi_label>/grants/add",
         journals_views._hx_publication_metadata_add_grant_funding,
         name="_hx_publication_metadata_add_grant_funding",
     ),
+    path(
+        "admin/<publication_doi_label:doi_label>/grants/<int:grant_id>/delete",
+        journals_views._hx_publication_metadata_delete_grant_funding,
+        name="_hx_publication_metadata_delete_grant_funding",
+    ),
     path(
         "admin/<publication_doi_label:doi_label>/view_autotemplate/<int:autotemplate_id>/",
         journals_views.view_autogenerated_file,
diff --git a/scipost_django/journals/views.py b/scipost_django/journals/views.py
index 4a43e3f9b..899228435 100644
--- a/scipost_django/journals/views.py
+++ b/scipost_django/journals/views.py
@@ -83,7 +83,7 @@ from .utils import JournalUtils
 from comments.models import Comment
 from common.utils import get_current_domain
 from funders.forms import FunderSelectForm, GrantSelectForm
-from funders.models import Grant
+from funders.models import Grant, Funder
 from mails.views import MailEditorSubview
 from ontology.models import AcademicField, Topic
 from ontology.forms import SelectTopicForm
@@ -917,7 +917,7 @@ def _hx_publication_metadata_add_grant_funding(request, doi_label):
         publication.grants.add(grant_select_form.cleaned_data["grant"])
         publication.doideposit_needs_updating = True
         publication.save()
-        messages.success(request, "Grant added to publication %s" % str(publication))
+        messages.success(request, "Grant added to publication.")
     return render(
         request,
         "journals/_hx_publication_metadata_add_grant_funding.html",
@@ -928,6 +928,26 @@ def _hx_publication_metadata_add_grant_funding(request, doi_label):
     )
 
 
+@permission_required("scipost.can_draft_publication", return_403=True)
+@transaction.atomic
+def _hx_publication_metadata_delete_grant_funding(request, doi_label, grant_id):
+    """
+    Called by an Editorial Administrator.
+    This deletes a grant of this publication from the database.
+    """
+    publication = get_object_or_404(Publication, doi_label=doi_label)
+    grant = get_object_or_404(Grant, id=grant_id)
+    if not publication.is_draft and not request.user.has_perm(
+        "scipost.can_publish_accepted_submission"
+    ):
+        raise Http404("You do not have permission to edit this non-draft Publication")
+
+    if grant in publication.grants.all():
+        publication.grants.remove(grant)
+        messages.success(request, "Grant removed from publication.")
+    return HttpResponse("")
+
+
 @permission_required("scipost.can_draft_publication", return_403=True)
 @transaction.atomic
 def _hx_publication_metadata_add_generic_funding(request, doi_label):
@@ -944,9 +964,7 @@ def _hx_publication_metadata_add_generic_funding(request, doi_label):
     if funder_select_form.is_valid():
         publication.funders_generic.add(funder_select_form.cleaned_data["funder"])
         publication.save()
-        messages.success(
-            request, "Generic funder added to publication %s" % str(publication)
-        )
+        messages.success(request, "Generic funder added to publication")
     return render(
         request,
         "journals/_hx_publication_metadata_add_generic_funding.html",
@@ -957,6 +975,26 @@ def _hx_publication_metadata_add_generic_funding(request, doi_label):
     )
 
 
+@permission_required("scipost.can_draft_publication", return_403=True)
+@transaction.atomic
+def _hx_publication_metadata_delete_generic_funding(request, doi_label, funder_id):
+    """
+    Called by an Editorial Administrator.
+    This deletes a funder (generic, not via funder) of this publication from the database.
+    """
+    publication = get_object_or_404(Publication, doi_label=doi_label)
+    funder = get_object_or_404(Funder, id=funder_id)
+    if not publication.is_draft and not request.user.has_perm(
+        "scipost.can_publish_accepted_submission"
+    ):
+        raise Http404("You do not have permission to edit this non-draft Publication")
+
+    if funder in publication.funders_generic.all():
+        publication.funders_generic.remove(funder)
+        messages.success(request, "Generic funder removed from publication.")
+    return HttpResponse("")
+
+
 class CreateMetadataXMLView(
     PublicationMixin, ProdSupervisorPublicationPermissionMixin, UpdateView
 ):
-- 
GitLab