You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by ma...@apache.org on 2021/04/26 20:55:05 UTC

[airavata-django-portal-sdk] 07/07: AIRAVATA-3420 /sdk/download redirects to the download url

This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch mft-integration
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal-sdk.git

commit 197731ff0587e1f9e58f7d2a87cc0fb583076771
Author: Marcus Christie <ma...@apache.org>
AuthorDate: Mon Apr 26 15:54:31 2021 -0400

    AIRAVATA-3420 /sdk/download redirects to the download url
---
 airavata_django_portal_sdk/urls.py                 |  3 +-
 airavata_django_portal_sdk/user_storage/api.py     | 38 +++++++++++++---------
 .../backends/django_filesystem_provider.py         |  8 -----
 airavata_django_portal_sdk/views.py                | 16 +++------
 4 files changed, 30 insertions(+), 35 deletions(-)

diff --git a/airavata_django_portal_sdk/urls.py b/airavata_django_portal_sdk/urls.py
index 652157e..2337ef6 100644
--- a/airavata_django_portal_sdk/urls.py
+++ b/airavata_django_portal_sdk/urls.py
@@ -15,5 +15,6 @@ def get_download_url(data_product_uri):
 
 app_name = 'airavata_django_portal_sdk'
 urlpatterns = [
-    path('download', views.download_file, name='download_file'),
+    path('download-file', views.download_file, name='download_file'),
+    path('download', views.download, name='download'),
 ]
diff --git a/airavata_django_portal_sdk/user_storage/api.py b/airavata_django_portal_sdk/user_storage/api.py
index 600a3b3..afd8eba 100644
--- a/airavata_django_portal_sdk/user_storage/api.py
+++ b/airavata_django_portal_sdk/user_storage/api.py
@@ -19,10 +19,13 @@ from airavata.model.data.replica.ttypes import (
 )
 from django.conf import settings
 from django.core.exceptions import ObjectDoesNotExist
+from django.urls import reverse
+
+from airavata_django_portal_sdk.user_storage.backends.base import (
+    ProvidesDownloadUrl
+)
 
 from ..util import convert_iso8601_to_datetime
-from django.urls import reverse
-from airavata_django_portal_sdk.user_storage.backends.base import ProvidesDownloadUrl
 
 logger = logging.getLogger(__name__)
 
@@ -186,20 +189,25 @@ def move_input_file(request, data_product=None, path=None, data_product_uri=None
 def get_download_url(request, data_product=None, data_product_uri=None):
     if data_product is None:
         data_product = _get_data_product(request, data_product_uri)
-    if _is_remote_api():
-        raise NotImplementedError()
+    storage_resource_id, path = _get_replica_resource_id_and_filepath(data_product)
+    backend = get_user_storage_provider(request,
+                                        owner_username=data_product.ownerName,
+                                        storage_resource_id=storage_resource_id)
+    if isinstance(backend, ProvidesDownloadUrl):
+        return backend.get_download_url(path)
     else:
-        storage_resource_id, path = _get_replica_resource_id_and_filepath(data_product)
-        backend = get_user_storage_provider(request,
-                                            owner_username=data_product.ownerName,
-                                            storage_resource_id=storage_resource_id)
-        if isinstance(backend, ProvidesDownloadUrl):
-            return backend.get_download_url(path)
-        else:
-            # if backend doesn't provide a download url, then use default one
-            # that uses backend to read the file
-            return (reverse("airavata_django_portal_sdk:download_file") + "?" +
-                    urlencode({"data-product-uri": data_product.productUri}))
+        # if backend doesn't provide a download url, then use default one
+        # that uses backend to read the file
+        return (reverse("airavata_django_portal_sdk:download_file") + "?" +
+                urlencode({"data-product-uri": data_product.productUri}))
+
+
+def get_lazy_download_url(request, data_product=None, data_product_uri=None):
+    if data_product is None:
+        data_product = _get_data_product(request, data_product_uri)
+    # /download will call get_download_url and redirect to it
+    return (reverse("airavata_django_portal_sdk:download") + "?" +
+            urlencode({"data-product-uri": data_product.productUri}))
 
 
 def open_file(request, data_product=None, data_product_uri=None):
diff --git a/airavata_django_portal_sdk/user_storage/backends/django_filesystem_provider.py b/airavata_django_portal_sdk/user_storage/backends/django_filesystem_provider.py
index 97e2f9e..98a0038 100644
--- a/airavata_django_portal_sdk/user_storage/backends/django_filesystem_provider.py
+++ b/airavata_django_portal_sdk/user_storage/backends/django_filesystem_provider.py
@@ -20,17 +20,9 @@ class DjangoFileSystemProvider(UserStorageProvider):
         full_path = self.datastore.save(path, file, name=name)
         return self.storage_resource_id, full_path
 
-    def get_upload_url(self, resource_path):
-        # TODO: implement
-        return super().get_upload_url(resource_path)
-
     def open(self, resource_path):
         return self.datastore.open(resource_path)
 
-    def get_download_url(self, resource_path):
-        # TODO: implement
-        return super().get_download_url(resource_path)
-
     def exists(self, resource_path):
         return self.datastore.exists(resource_path)
 
diff --git a/airavata_django_portal_sdk/views.py b/airavata_django_portal_sdk/views.py
index 688ca84..e9e3612 100644
--- a/airavata_django_portal_sdk/views.py
+++ b/airavata_django_portal_sdk/views.py
@@ -1,32 +1,26 @@
 import logging
 import os
-from urllib.parse import urlparse
 
 from django.contrib.auth.decorators import login_required
 from django.core.exceptions import ObjectDoesNotExist
 from django.http import FileResponse, Http404
 from django.shortcuts import redirect
-from django.urls import reverse
 
 from airavata_django_portal_sdk import user_storage
 
 logger = logging.getLogger(__name__)
 
 
-# TODO: moving this view out of REST API means losing access token based authentication
 @login_required
-def download_file(request):
-
+def download(request):
     data_product_uri = request.GET.get('data-product-uri', '')
     download_url = user_storage.get_download_url(request, data_product_uri=data_product_uri)
-    # If the download_url resolves to this view, then handle it directly
-    if urlparse(download_url).path == reverse('airavata_django_portal_sdk:download_file'):
-        return _internal_download_file(request)
-    else:
-        return redirect(download_url)
+    return redirect(download_url)
 
 
-def _internal_download_file(request):
+# TODO: moving this view out of REST API means losing access token based authentication
+@login_required
+def download_file(request):
     data_product_uri = request.GET.get('data-product-uri', '')
     force_download = 'download' in request.GET
     data_product = None