From 98147eb8c6488bac6e4e8adafa3aca2c0585f3bc Mon Sep 17 00:00:00 2001
From: Remi Planel <rplanel@pasteur.fr>
Date: Fri, 24 Sep 2021 18:17:12 +0200
Subject: [PATCH] Create an url to list cc-qtl workflow

---
 server/api/views.py | 50 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/server/api/views.py b/server/api/views.py
index 1c9398d6..90f5fd6d 100644
--- a/server/api/views.py
+++ b/server/api/views.py
@@ -17,7 +17,7 @@ from guardian.shortcuts import assign_perm, get_perms
 import pandas as pd
 
 # DRF
-from rest_framework import viewsets, status, serializers, mixins
+from rest_framework import views, viewsets, status, serializers, mixins, authentication
 from rest_framework.permissions import IsAuthenticated
 
 
@@ -25,11 +25,16 @@ from rest_framework.permissions import IsAuthenticated
 from rest_framework.response import Response
 from rest_framework.decorators import action
 
+
+from rest_framework_simplejwt.authentication import JWTAuthentication
+
 # DRF guardian
 from rest_framework_guardian import filters
 
 from .exceptions import RessourceAlreadyExists
 
+from bioblend import ConnectionError
+
 # Serializers
 from .serializers import (
     FounderSerializer,
@@ -812,19 +817,29 @@ def tools_parameters(request):
     return JsonResponse(job, safe=False)
 
 
-def workflow_tools(request):
-    runner = GalaxyRunner()
-    gi = runner.galaxy_instance
+class WorkflowTools(views.APIView):
+    authentication_classes = [JWTAuthentication, authentication.SessionAuthentication]
+    permission_classes = [IsAuthenticated]
 
-    # TODO worflow_id should be a parameter
+    def get(self, request, *args, **kwargs):
+        wf_id = kwargs["wf_id"]
+        runner = GalaxyRunner()
+        gi = runner.galaxy_instance
 
-    # workflow_id = request["workflow_id"]
-    wf = gi.workflows.get_workflows(name="cc-qtl-wf")[0]
-    wf_with_details = gi.workflows.show_workflow(wf["id"])
-    wf_step_tool_ids = [
-        (step_id, step["tool_id"]) for step_id, step in wf_with_details["steps"].items()
-    ]
-    return JsonResponse(dict(get_job_params(wf_step_tool_ids, gi)))
+        # TODO worflow_id should be a parameter
+
+        # workflow_id = request["workflow_id"]
+        try:
+            wf_with_details = gi.workflows.show_workflow(wf_id)
+            wf_step_tool_ids = [
+                (step_id, step["tool_id"])
+                for step_id, step in wf_with_details["steps"].items()
+            ]
+            return Response(dict(get_job_params(wf_step_tool_ids, gi)))
+        except ConnectionError as e:
+            return Response(
+                status=status.HTTP_400_BAD_REQUEST, data=json.loads(e.body)["err_msg"]
+            )
 
 
 def workflow_default_parameters(request):
@@ -862,3 +877,14 @@ def get_job_params(job_ids, gi):
                 gi.tools.show_tool(job_id, io_details=True),
             )
 
+
+class CcQtlWorkflows(views.APIView):
+
+    authentication_classes = [JWTAuthentication, authentication.SessionAuthentication]
+    permission_classes = [IsAuthenticated]
+
+    def get(self, request, *args, **kwargs):
+        runner = GalaxyRunner()
+        gi = runner.galaxy_instance
+        wf = gi.workflows.get_workflows()
+        return Response(wf)
-- 
GitLab