1 # Copyright 2016 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from rest_framework import status
19 from rest_framework.response import Response
20 from rest_framework.views import APIView
21 from drf_yasg.utils import swagger_auto_schema
23 from lcm.pub.database import models
25 from lcm.samples.serializers import RecordCountSerializer
27 logger = logging.getLogger(__name__)
30 class SampleList(APIView):
37 status.HTTP_200_OK: "Status is active"
40 def get(self, request, format=None):
41 count = len(models.NSDModel.objects.filter())
42 logger.debug("get, count of NSDModel is %s", count)
43 return Response({"status": "active"})
46 class TablesList(APIView):
50 status.HTTP_204_NO_CONTENT: 'successful',
51 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
54 def delete(self, request, modelName):
55 logger.debug("Start delete model %s", modelName)
57 modelNames = modelName.split("-")
58 for name in modelNames:
59 model_obj = eval("models.%s.objects" % name)
60 model_obj.filter().delete()
61 logger.debug("End delete model %s", name)
63 logger.error(traceback.format_exc())
64 return Response(data={"error": "failed"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
65 return Response(data={}, status=status.HTTP_204_NO_CONTENT)
70 status.HTTP_200_OK: RecordCountSerializer(),
71 status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
74 def get(self, request, modelName):
75 logger.debug("Get model %s", modelName)
78 model_obj = eval("models.%s.objects" % modelName)
79 count = len(model_obj.filter())
80 resp_serializer = RecordCountSerializer(data={"count": count})
81 if not resp_serializer.is_valid():
82 raise Exception(resp_serializer.errors)
83 return Response(data=resp_serializer.data, status=status.HTTP_200_OK)
84 except Exception as e:
85 logger.error(e.message)
86 logger.error(traceback.format_exc())
87 return Response(data={"error": e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)