0e3c6acfe53d2e252da3ee287d0b461cc3bd5d53
[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.views import APIView
19 from rest_framework.response import Response
20 from rest_framework import status
21 from lcm.pub.database import models
22
23
24 logger = logging.getLogger(__name__)
25
26
27 class SampleList(APIView):
28     """
29     List all samples.
30     """
31     def get(self, request, format=None):
32         logger.debug("get")
33         return Response({"status": "active"})
34
35 class TablesList(APIView):
36     def delete(self, request, modelName):
37         logger.debug("Start delete model %s", modelName)
38         try:
39             modelNames = modelName.split("-")
40             for name in modelNames:
41                 model_obj = eval("models.%s.objects" % name)
42                 model_obj.filter().delete()
43                 logger.debug("End delete model %s", name)
44         except:
45             logger.error(traceback.format_exc())
46             return Response(data={"error": "failed"}, 
47                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
48         return Response(data={}, status=status.HTTP_204_NO_CONTENT)
49
50
51     def get(self, request, modelName):
52         logger.debug("Get model %s", modelName)
53         count = 0
54         try:
55             model_obj = eval("models.%s.objects" % modelName)
56             count = len(model_obj.filter())
57         except:
58             logger.error(traceback.format_exc())
59             return Response(data={"error": "failed"}, 
60                 status=status.HTTP_500_INTERNAL_SERVER_ERROR)
61         return Response(data={"count": count}, status=status.HTTP_200_OK)
62
63
64
65