airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [2/2] airavata-django-portal git commit: Implemented project creation via REST API
Date Tue, 12 Sep 2017 16:04:45 GMT
Implemented project creation via REST API


Project: http://git-wip-us.apache.org/repos/asf/airavata-django-portal/repo
Commit: http://git-wip-us.apache.org/repos/asf/airavata-django-portal/commit/88e8db3d
Tree: http://git-wip-us.apache.org/repos/asf/airavata-django-portal/tree/88e8db3d
Diff: http://git-wip-us.apache.org/repos/asf/airavata-django-portal/diff/88e8db3d

Branch: refs/heads/master
Commit: 88e8db3d4209f5aeb6ff52e57fa85274d8e9b705
Parents: d550143
Author: Marcus Christie <machrist@iu.edu>
Authored: Tue Sep 12 12:04:37 2017 -0400
Committer: Marcus Christie <machrist@iu.edu>
Committed: Tue Sep 12 12:04:37 2017 -0400

----------------------------------------------------------------------
 django_airavata/apps/api/serializers.py | 39 +++++++++++++++++++++++-----
 django_airavata/apps/api/views.py       | 15 ++++++++++-
 2 files changed, 46 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/airavata-django-portal/blob/88e8db3d/django_airavata/apps/api/serializers.py
----------------------------------------------------------------------
diff --git a/django_airavata/apps/api/serializers.py b/django_airavata/apps/api/serializers.py
index c082f6b..4a56838 100644
--- a/django_airavata/apps/api/serializers.py
+++ b/django_airavata/apps/api/serializers.py
@@ -1,6 +1,8 @@
 from apache.airavata.model.experiment.ttypes import ExperimentModel
 from apache.airavata.model.workspace.ttypes import Project
 
+from django.conf import settings
+
 from rest_framework import serializers
 
 from urllib.parse import quote
@@ -17,30 +19,53 @@ class FullyEncodedHyperlinkedIdentityField(serializers.HyperlinkedIdentityField)
         url = self.reverse(view_name, kwargs=kwargs, request=request, format=format)
         return url.replace("__PLACEHOLDER__", encoded_lookup_value)
 
+class GetGatewayUsername(object):
+
+    def __call__(self):
+        return self.field.context['request'].user.username
+
+    def set_context(self, field):
+        self.field = field
+
+class GatewayUsernameDefaultField(serializers.CharField):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.read_only = True
+        self.default = GetGatewayUsername()
+
+class GatewayIdDefaultField(serializers.CharField):
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        self.read_only = True
+        self.default = settings.GATEWAY_ID
 
 class ProjectSerializer(serializers.Serializer):
-    projectID = serializers.CharField(required=True)
+    projectID = serializers.CharField(read_only=True)
     name = serializers.CharField(required=True)
-    owner = serializers.CharField(required=True)
-    gatewayId = serializers.CharField(required=True)
+    description = serializers.CharField(required=False)
+    owner = GatewayUsernameDefaultField()
+    gatewayId = GatewayIdDefaultField()
     experiments = FullyEncodedHyperlinkedIdentityField(view_name='api_project_experiments_list',
lookup_field='projectID', lookup_url_kwarg='project_id')
 
-    # TODO: maybe just have a get() method to get the deserialized object?
     def create(self, validated_data):
         return Project(**validated_data)
 
     def update(self, instance, validated_data):
         raise Exception("Not implemented")
 
+    def get_username(self):
+        return self.context.request.user.username
 
 class ExperimentSerializer(serializers.Serializer):
 
-    experimentId = serializers.CharField(required=True)
+    experimentId = serializers.CharField(read_only=True)
     projectId = serializers.CharField(required=True)
     project = FullyEncodedHyperlinkedIdentityField(view_name='api_project_detail', lookup_field='projectId',
lookup_url_kwarg='project_id')
-    gatewayId = serializers.CharField(required=True)
+    gatewayId = GatewayIdDefaultField()
     experimentType = serializers.CharField(required=True)
-    userName = serializers.CharField(required=True)
+    userName = GatewayUsernameDefaultField()
     experimentName = serializers.CharField(required=True)
 
     def create(self, validated_data):

http://git-wip-us.apache.org/repos/asf/airavata-django-portal/blob/88e8db3d/django_airavata/apps/api/views.py
----------------------------------------------------------------------
diff --git a/django_airavata/apps/api/views.py b/django_airavata/apps/api/views.py
index fe9fc78..0d0a052 100644
--- a/django_airavata/apps/api/views.py
+++ b/django_airavata/apps/api/views.py
@@ -28,13 +28,26 @@ class ProjectList(APIView):
         projects = request.airavata_client.getUserProjects(request.authz_token, gateway_id,
username, -1, 0)
         serializer = serializers.ProjectSerializer(projects, many=True, context={'request':
request})
         return Response(serializer.data)
-    # TODO: add project creation
+
+    def post(self, request, format=None):
+        gateway_id = settings.GATEWAY_ID
+        username = request.user.username
+        serializer = serializers.ProjectSerializer(data=request.data, context={'request':
request})
+        if serializer.is_valid():
+            project = serializer.create(serializer.validated_data)
+            project_id = request.airavata_client.createProject(request.authz_token, gateway_id,
project)
+            project.projectID = project_id
+            serializer = serializers.ProjectSerializer(project, context={'request': request})
+            return Response(serializer.data, status=status.HTTP_201_CREATED)
+        else:
+            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 
 class ProjectDetail(APIView):
     def get(self, request, project_id, format=None):
         gateway_id = settings.GATEWAY_ID
         username = request.user.username
 
+        print(project_id)
         project = request.airavata_client.getProject(request.authz_token, project_id)
         serializer = serializers.ProjectSerializer(project, context={'request': request})
         return Response(serializer.data)


Mime
View raw message