update link to upper-constraints.txt
[vfc/nfvo/lcm.git] / lcm / samples / views.py
1 # Copyright 2016 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import logging
16 import traceback
17
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
22
23 from lcm.pub.database import models
24
25 from lcm.samples.serializers import RecordCountSerializer
26
27 logger = logging.getLogger(__name__)
28
29
30 class SampleList(APIView):
31     """
32     List all samples.
33     """
34     @swagger_auto_schema(
35         request_body=None,
36         responses={
37             status.HTTP_200_OK: "Status is active"
38         }
39     )
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"})
44
45
46 class TablesList(APIView):
47     @swagger_auto_schema(
48         request_body=None,
49         responses={
50             status.HTTP_204_NO_CONTENT: 'successful',
51             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
52         }
53     )
54     def delete(self, request, modelName):
55         logger.debug("Start delete model %s", modelName)
56         try:
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)
62         except:
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)
66
67     @swagger_auto_schema(
68         request_body=None,
69         responses={
70             status.HTTP_200_OK: RecordCountSerializer(),
71             status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error"
72         }
73     )
74     def get(self, request, modelName):
75         logger.debug("Get model %s", modelName)
76         count = 0
77         try:
78             model_obj = eval("models.%s.objects" % modelName)
79             count = len(model_obj.filter())
80             ret = {"count": count}
81             resp_serializer = RecordCountSerializer(data=ret)
82             if not resp_serializer.is_valid():
83                 raise Exception(resp_serializer.errors)
84             return Response(data=ret, status=status.HTTP_200_OK)
85         except Exception as e:
86             logger.error(e.args[0])
87             logger.error(traceback.format_exc())
88             return Response(data={"error": e.args[0]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)