Merge "Add qtip job to pod zte-virtual6"
[releng.git] / utils / test / testapi / opnfv_testapi / models / base_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     @classmethod
52     def from_dict_with_raise(cls, a_dict):
53         if a_dict is None:
54             return None
55
56         attr_parser = cls.attr_parser()
57         t = cls()
58         for k, v in a_dict.iteritems():
59             if k not in t.__dict__:
60                 raise AttributeError(
61                     '{} has no attribute {}'.format(cls.__name__, k))
62             value = v
63             if isinstance(v, dict) and k in attr_parser:
64                 value = attr_parser[k].from_dict_with_raise(v)
65             elif isinstance(v, list) and k in attr_parser:
66                 value = []
67                 for item in v:
68                     value.append(attr_parser[k].from_dict_with_raise(item))
69
70             t.__setattr__(k, value)
71
72         return t
73
74     @staticmethod
75     def attr_parser():
76         return {}
77
78     def _format(self, excludes):
79         new_obj = copy.deepcopy(self)
80         dicts = new_obj.__dict__
81         for k in dicts.keys():
82             if k in excludes:
83                 del dicts[k]
84             elif dicts[k]:
85                 dicts[k] = self._obj_format(dicts[k])
86         return dicts
87
88     def _obj_format(self, obj):
89         if self._has_format(obj):
90             obj = obj.format()
91         elif isinstance(obj, unicode):
92             try:
93                 obj = self._obj_format(ast.literal_eval(obj))
94             except Exception:
95                 try:
96                     obj = str(obj)
97                 except Exception:
98                     obj = obj
99         elif isinstance(obj, list):
100             hs = list()
101             for h in obj:
102                 hs.append(self._obj_format(h))
103             obj = hs
104         elif not isinstance(obj, (str, int, float, dict)):
105             obj = str(obj)
106         return obj
107
108     @staticmethod
109     def _has_format(obj):
110         return not isinstance(obj, (str, unicode)) and hasattr(obj, 'format')
111
112
113 @swagger.model()
114 class CreateResponse(ModelBase):
115     def __init__(self, href=''):
116         self.href = href
117
118
119 @swagger.model()
120 class Versions(ModelBase):
121     """
122         @property versions:
123         @ptype versions: C{list} of L{Version}
124     """
125
126     def __init__(self):
127         self.versions = list()
128
129     @staticmethod
130     def attr_parser():
131         return {'versions': Version}
132
133
134 @swagger.model()
135 class Version(ModelBase):
136     def __init__(self, version=None, description=None):
137         self.version = version
138         self.description = description