diff options
author | Fu Jinhua <fu.jinhua@zte.com.cn> | 2018-08-27 00:54:38 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-08-27 00:54:38 +0000 |
commit | baee0608458dd63bc5abb7cda057eb1f7ae920e7 (patch) | |
tree | 4d5c56bf56ac1e6855e44c4af13a29242a1deb96 | |
parent | 3fc3bae4484a8001c9a10ea69cda84b8e72f3d79 (diff) | |
parent | 90b119e3dc533ce8b2708d8ec3f0bc9f5579adbc (diff) |
Merge "Ns descriptor related stuffs."
-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) |