correct the import impl of TestAPI
[releng.git] / utils / test / testapi / opnfv_testapi / resources / models.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 # feng.xiaowei@zte.com.cn  mv Pod to pod_models.py                 5-18-2016
9 # feng.xiaowei@zte.com.cn  add MetaCreateResponse/MetaGetResponse  5-18-2016
10 # feng.xiaowei@zte.com.cn  mv TestProject to project_models.py     5-19-2016
11 # feng.xiaowei@zte.com.cn  delete meta class                       5-19-2016
12 # feng.xiaowei@zte.com.cn  add CreateResponse                      5-19-2016
13 # feng.xiaowei@zte.com.cn  mv TestCase to testcase_models.py       5-20-2016
14 # feng.xiaowei@zte.com.cn  mv TestResut to result_models.py        5-23-2016
15 # feng.xiaowei@zte.com.cn  add ModelBase                           12-20-2016
16 ##############################################################################
17 import ast
18 import copy
19
20 from opnfv_testapi.tornado_swagger import swagger
21
22
23 class ModelBase(object):
24
25     def format(self):
26         return self._format(['_id'])
27
28     def format_http(self):
29         return self._format([])
30
31     @classmethod
32     def from_dict(cls, a_dict):
33         if a_dict is None:
34             return None
35
36         attr_parser = cls.attr_parser()
37         t = cls()
38         for k, v in a_dict.iteritems():
39             value = v
40             if isinstance(v, dict) and k in attr_parser:
41                 value = attr_parser[k].from_dict(v)
42             elif isinstance(v, list) and k in attr_parser:
43                 value = []
44                 for item in v:
45                     value.append(attr_parser[k].from_dict(item))
46
47             t.__setattr__(k, value)
48
49         return t
50
51     @staticmethod
52     def attr_parser():
53         return {}
54
55     def _format(self, excludes):
56         new_obj = copy.deepcopy(self)
57         dicts = new_obj.__dict__
58         for k in dicts.keys():
59             if k in excludes:
60                 del dicts[k]
61             elif dicts[k]:
62                 dicts[k] = self._obj_format(dicts[k])
63         return dicts
64
65     def _obj_format(self, obj):
66         if self._has_format(obj):
67             obj = obj.format()
68         elif isinstance(obj, unicode):
69             try:
70                 obj = self._obj_format(ast.literal_eval(obj))
71             except:
72                 try:
73                     obj = str(obj)
74                 except:
75                     obj = obj
76         elif isinstance(obj, list):
77             hs = list()
78             for h in obj:
79                 hs.append(self._obj_format(h))
80             obj = hs
81         elif not isinstance(obj, (str, int, float, dict)):
82             obj = str(obj)
83         return obj
84
85     @staticmethod
86     def _has_format(obj):
87         return not isinstance(obj, (str, unicode)) and hasattr(obj, 'format')
88
89
90 @swagger.model()
91 class CreateResponse(ModelBase):
92     def __init__(self, href=''):
93         self.href = href
94
95
96 @swagger.model()
97 class Versions(ModelBase):
98     """
99         @property versions:
100         @ptype versions: C{list} of L{Version}
101     """
102
103     def __init__(self):
104         self.versions = list()
105
106     @staticmethod
107     def attr_parser():
108         return {'versions': Version}
109
110
111 @swagger.model()
112 class Version(ModelBase):
113     def __init__(self, version=None, description=None):
114         self.version = version
115         self.description = description