From 8ff87029d741b50cc72eaa1e07804caa52396bcc Mon Sep 17 00:00:00 2001
From: Ernesto Rico Schmidt <ernesto@helsinki.at>
Date: Tue, 30 Apr 2024 18:39:08 -0400
Subject: [PATCH] feat: add initialize & loadfixtures management commands

---
 program/management/commands/initialize.py   | 62 +++++++++++++++++++++
 program/management/commands/loadfixtures.py | 17 ++++++
 2 files changed, 79 insertions(+)
 create mode 100644 program/management/commands/initialize.py
 create mode 100644 program/management/commands/loadfixtures.py

diff --git a/program/management/commands/initialize.py b/program/management/commands/initialize.py
new file mode 100644
index 00000000..9067fe4c
--- /dev/null
+++ b/program/management/commands/initialize.py
@@ -0,0 +1,62 @@
+import os
+
+from django.core.management import call_command
+from django.core.management.base import BaseCommand, CommandError
+
+
+class Command(BaseCommand):
+    help = "Initializes the application."
+
+    def handle(self, *args, **options):
+        AURA_PROTO = os.getenv("AURA_PROTO")
+        AURA_HOST = os.getenv("AURA_HOST")
+
+        TANK_CALLBACK_BASE_URL = os.getenv(
+            "TANK_CALLBACK_BASE_URL",
+            default=f"{AURA_PROTO}://{AURA_HOST}/tank",
+        )
+        DASHBOARD_CALLBACK_BASE_URL = os.getenv(
+            "DASHBOARD_CALLBACK_BASE_URL",
+            default=f"{AURA_PROTO}://${AURA_HOST}",
+        )
+
+        call_command("migrate", "--no-input")
+
+        try:
+            call_command("createsuperuser", "--no-input")
+        except CommandError:
+            raise CommandError(f"The application is already initialized.")
+
+        call_command("collectstatic", "--no-input")
+        call_command(
+            "create_oidc_client",
+            "dashboard",
+            "public",
+            "--client-id",
+            os.getenv("DASHBOARD_OIDC_CLIENT_ID"),
+            "--client-secret",
+            os.getenv("DASHBOARD_OIDC_CLIENT_SECRET"),
+            "-r",
+            "id_token token",
+            "-u",
+            f"{DASHBOARD_CALLBACK_BASE_URL}/oidc_callback.html",
+            "-u",
+            f"{DASHBOARD_CALLBACK_BASE_URL}/oidc_callback_silentRenew.html",
+            "-p",
+            DASHBOARD_CALLBACK_BASE_URL,
+        )
+        call_command(
+            "create_oidc_client",
+            "tank",
+            "confidential",
+            "--client-id",
+            os.getenv("TANK_OIDC_CLIENT_ID"),
+            "--client-secret",
+            os.getenv("TANK_OIDC_CLIENT_SECRET"),
+            "-r",
+            "code",
+            "-u",
+            f"{TANK_CALLBACK_BASE_URL}/tank/auth/oidc/callback",
+        )
+        call_command("addpermissions")
+        call_command("creatersakey")
diff --git a/program/management/commands/loadfixtures.py b/program/management/commands/loadfixtures.py
new file mode 100644
index 00000000..354be283
--- /dev/null
+++ b/program/management/commands/loadfixtures.py
@@ -0,0 +1,17 @@
+import glob
+
+from django.core.management import BaseCommand, call_command
+
+
+class Command(BaseCommand):
+    help = "Loads the fixtures from the directory."
+
+    def add_arguments(self, parser):
+        parser.add_argument("directory", help="Directory with fixtures.", nargs="+", type=str)
+
+    def handle(self, *args, **options):
+        directories = options["directory"]
+
+        for directory in directories:
+            if files := glob.glob(f"fixtures/{directory}/*.json"):
+                call_command("loaddata", files)
-- 
GitLab