diff options
author | laili <lai.li@zte.com.cn> | 2018-08-24 16:11:38 +0800 |
---|---|---|
committer | laili <lai.li@zte.com.cn> | 2018-08-24 16:19:22 +0800 |
commit | 90b119e3dc533ce8b2708d8ec3f0bc9f5579adbc (patch) | |
tree | 63130ce2320c66a4b1718074f09aa144e2bd7d63 | |
parent | 91ea3262f96ff2e73c19f002acb565a13226ee04 (diff) |
Ns descriptor related stuffs.
Implement the biz and view of pnfd downloading.
Change-Id: Ic8f66f95178f7dbb07e1092958ac648632d9cf29
Issue-ID: VFC-1037
Signed-off-by: laili <lai.li@zte.com.cn>
-rw-r--r-- | catalog/packages/biz/pnf_descriptor.py | 10 | ||||
-rw-r--r-- | catalog/packages/views/pnf_descriptor_views.py | 46 |
2 files changed, 46 insertions, 10 deletions
diff --git a/catalog/packages/biz/pnf_descriptor.py b/catalog/packages/biz/pnf_descriptor.py index 0ae89e0f..d52e82f2 100644 --- a/catalog/packages/biz/pnf_descriptor.py +++ b/catalog/packages/biz/pnf_descriptor.py @@ -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) diff --git a/catalog/packages/views/pnf_descriptor_views.py b/catalog/packages/views/pnf_descriptor_views.py index 1103c43c..b571f01e 100644 --- a/catalog/packages/views/pnf_descriptor_views.py +++ b/catalog/packages/views/pnf_descriptor_views.py @@ -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) |