From 976c1003d20a61b18bafb59a814667afe04aedb8 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Mon, 27 Nov 2023 17:33:13 +0100
Subject: [PATCH] add ror fetching management command

---
 .../commands/organization_fetch_ror_data.py   | 35 +++++++++++++++++++
 .../_organization_detail_contents.html        | 14 ++++----
 2 files changed, 43 insertions(+), 6 deletions(-)
 create mode 100644 scipost_django/organizations/management/commands/organization_fetch_ror_data.py

diff --git a/scipost_django/organizations/management/commands/organization_fetch_ror_data.py b/scipost_django/organizations/management/commands/organization_fetch_ror_data.py
new file mode 100644
index 000000000..0121e13fa
--- /dev/null
+++ b/scipost_django/organizations/management/commands/organization_fetch_ror_data.py
@@ -0,0 +1,35 @@
+__copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
+__license__ = "AGPL v3"
+
+
+from django.core.management.base import BaseCommand
+
+from organizations.models import Organization
+from organizations.utils import RORAPIHandler
+
+
+class Command(BaseCommand):
+    help = (
+        "For all Organization model instances, "
+        "this command updates the `ror_json` field by fetching the latest data "
+        "using the `id` property of the `ror_json` field."
+    )
+
+    def handle(self, *args, **kwargs):
+        ror_api_handler = RORAPIHandler()
+
+        updated = 0
+        missing = 0
+        for org in Organization.objects.all():
+            if ror_id := org.ror_json.get("id", None):
+                org.ror_json = ror_api_handler.from_id(ror_id)
+                org.save()
+                updated += 1
+            else:
+                missing += 1
+
+        self.stdout.write(
+            self.style.SUCCESS(
+                f"Successfully updated {updated} organizations, {missing} organizations missing `id`"
+            )
+        )
diff --git a/scipost_django/organizations/templates/organizations/_organization_detail_contents.html b/scipost_django/organizations/templates/organizations/_organization_detail_contents.html
index 682555dbb..e37f913f6 100644
--- a/scipost_django/organizations/templates/organizations/_organization_detail_contents.html
+++ b/scipost_django/organizations/templates/organizations/_organization_detail_contents.html
@@ -47,12 +47,14 @@
         No ROR id found.
       {% endif %}
 
-      <button class="btn btn-sm btn-primary ms-auto"
-              hx-get="{% url "organizations:ror_search_form" pk=org.id %}"
-              hx-target="#ror-container">Edit</button>
-      <button class="btn btn-sm btn-secondary ms-2"
-              hx-get="{% url "organizations:ror_add" pk=org.id ror_id="None" %}"
-              hx-target="#organization-info">Remove</button>
+      {% if perms.scipost.can_manage_organizations %}
+        <button class="btn btn-sm btn-primary ms-auto"
+                hx-get="{% url "organizations:ror_search_form" pk=org.id %}"
+                hx-target="#ror-container">Edit</button>
+        <button class="btn btn-sm btn-secondary ms-2"
+                hx-get="{% url "organizations:ror_add" pk=org.id ror_id="None" %}"
+                hx-target="#organization-info">Remove</button>
+      {% endif %}
 
     </td>
   </tr>
-- 
GitLab