Ns descriptor related stuffs. 77/62277/2
authorlaili <lai.li@zte.com.cn>
Fri, 24 Aug 2018 08:11:38 +0000 (16:11 +0800)
committerlaili <lai.li@zte.com.cn>
Fri, 24 Aug 2018 08:19:22 +0000 (16:19 +0800)
Implement the biz and view of pnfd downloading.

Change-Id: Ic8f66f95178f7dbb07e1092958ac648632d9cf29
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
catalog/packages/biz/pnf_descriptor.py
catalog/packages/views/pnf_descriptor_views.py

index 0ae89e0..d52e82f 100644 (file)
@@ -112,6 +112,16 @@ def upload(files, pnfd_info_id):
                 local_file.write(data)
 
 
+def download(pnfd_info_id):
+    pnf_pkgs = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id)
+    if not pnf_pkgs.exists():
+        raise CatalogException('The PNF Descriptor (%s) does not exist.' % pnfd_info_id)
+    if pnf_pkgs[0].onboardingState != 'ONBOARDED':
+        raise CatalogException('The PNF Descriptor (%s) is not ONBOARDED.' % pnfd_info_id)
+    local_file_path = pnf_pkgs[0].localFilePath
+    return local_file_path
+
+
 def query_single(pnfd_info_id):
     pkg_info = {}
     pnf_pkg = PnfPackageModel.objects.filter(pnfPackageId=pnfd_info_id)
index 1103c43..b571f01 100644 (file)
@@ -19,8 +19,10 @@ from drf_yasg.utils import no_body, swagger_auto_schema
 from rest_framework import status
 from rest_framework.decorators import api_view
 from rest_framework.response import Response
+from django.http import FileResponse
 
-from catalog.packages.biz.pnf_descriptor import create, query_multiple, upload, query_single, delete_pnf
+
+from catalog.packages.biz.pnf_descriptor import create, query_multiple, query_single, upload, download, delete_pnf
 from catalog.packages.serializers.create_pnfd_info_request import \
     CreatePnfdInfoRequestSerializer
 from catalog.packages.serializers.pnfd_info import PnfdInfoSerializer
@@ -140,14 +142,38 @@ def pnf_descriptors_rc(request, *args, **kwargs):
         status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
     }
 )
-@api_view(http_method_names=['PUT'])
+@swagger_auto_schema(
+    method='GET',
+    operation_description="Fetch PNFD content",
+    request_body=no_body,
+    responses={
+        status.HTTP_204_NO_CONTENT: {},
+        status.HTTP_500_INTERNAL_SERVER_ERROR: "Internal error"
+    }
+)
+@api_view(http_method_names=['PUT', 'GET'])
 def pnfd_content_ru(request, *args, **kwargs):
     pnfd_info_id = kwargs.get("pnfdInfoId")
-    files = request.FILES.getlist('file')
-    try:
-        upload(files, pnfd_info_id)
-        return Response(data={}, status=status.HTTP_204_NO_CONTENT)
-    except IOError:
-        logger.error(traceback.format_exc())
-        raise CatalogException
-        return Response(data={'error': 'Uploading pnfd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+    if request.method == 'PUT':
+        files = request.FILES.getlist('file')
+        try:
+            upload(files, pnfd_info_id)
+            return Response(data={}, status=status.HTTP_204_NO_CONTENT)
+        except IOError:
+            logger.error(traceback.format_exc())
+            raise CatalogException
+            return Response(data={'error': 'Uploading pnfd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+
+    if request.method == 'GET':
+        try:
+            file_path = download(pnfd_info_id)
+            file_name = file_path.split('/')[-1]
+            file_name = file_name.split('\\')[-1]
+            response = FileResponse(open(file_path, 'rb'), status=status.HTTP_200_OK)
+            response['Content-Disposition'] = 'attachment; filename=%s' % file_name.encode('utf-8')
+            return response
+        except IOError:
+            logger.error(traceback.format_exc())
+            raise CatalogException
+            return Response(data={'error': 'Downloading pnfd content failed.'},
+                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)