From: laili Date: Thu, 23 Aug 2018 12:19:51 +0000 (+0800) Subject: Ns descriptor related stuffs. X-Git-Tag: 1.2.0~75^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F99%2F62099%2F2;p=vfc%2Fnfvo%2Fcatalog.git Ns descriptor related stuffs. Implement the view for fetching a nsd. Change-Id: Ibb6f49c1292b1ba78537d2576022800ece368462 Issue-ID: VFC-1037 Signed-off-by: laili --- diff --git a/catalog/packages/views/ns_descriptor_views.py b/catalog/packages/views/ns_descriptor_views.py index 68dd4ed5..9ba5538c 100644 --- a/catalog/packages/views/ns_descriptor_views.py +++ b/catalog/packages/views/ns_descriptor_views.py @@ -19,8 +19,9 @@ 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.ns_descriptor import create, query_multiple, query_single, delete_single, upload +from catalog.packages.biz.ns_descriptor import create, query_multiple, query_single, delete_single, upload, download from catalog.packages.serializers.create_nsd_info_request import \ CreateNsdInfoRequestSerializer from catalog.packages.serializers.nsd_info import NsdInfoSerializer @@ -134,14 +135,38 @@ def ns_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 NSD 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 nsd_content_ru(request, *args, **kwargs): nsd_info_id = kwargs.get("nsdInfoId") - files = request.FILES.getlist('file') - try: - upload(files[0], nsd_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 nsd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + if request.method == 'PUT': + files = request.FILES.getlist('file') + try: + upload(files[0], nsd_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 nsd content failed.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) + + if request.method == 'GET': + try: + file_path = download(nsd_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 nsd content failed.'}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR)