re-enable testapi auto update
[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 copy
18 import ast
19
20
21 from opnfv_testapi.tornado_swagger import swagger
22
23
24 class ModelBase(object):
25
26     def format(self):
27         return self._format(['_id'])
28
29     def format_http(self):
30         return self._format([])
31
32     @classmethod
33     def from_dict(cls, a_dict):
34         if a_dict is None:
35             return None
36
37         attr_parser = cls.attr_parser()
38         t = cls()
39         for k, v in a_dict.iteritems():
40             value = v
41             if isinstance(v, dict) and k in attr_parser:
42                 value = attr_parser[k].from_dict(v)
43             elif isinstance(v, list) and k in attr_parser:
44                 value = []
45                 for item in v:
46                     value.append(attr_parser[k].from_dict(item))
47
48             t.__setattr__(k, value)
49
50         return t
51
52     @staticmethod
53     def attr_parser():
54         return {}
55
56     def _format(self, excludes):
57         new_obj = copy.deepcopy(self)
58         dicts = new_obj.__dict__
59         for k in dicts.keys():
60             if k in excludes:
61                 del dicts[k]
62             elif dicts[k]:
63                 dicts[k] = self._obj_format(dicts[k])
64         return dicts
65
66     def _obj_format(self, obj):
67         if self._has_format(obj):
68             obj = obj.format()
69         elif isinstance(obj, unicode):
70             try:
71                 obj = self._obj_format(ast.literal_eval(obj))
72             except:
73                 try:
74                     obj = str(obj)
75                 except:
76                     obj = obj
77         elif isinstance(obj, list):
78             hs = list()
79             for h in obj:
80                 hs.append(self._obj_format(h))
81             obj = hs
82         elif not isinstance(obj, (str, int, float, dict)):
83             obj = str(obj)
84         return obj
85
86     @staticmethod
87     def _has_format(obj):
88         return not isinstance(obj, (str, unicode)) and hasattr(obj, 'format')
89
90
91 @swagger.model()
92 class CreateResponse(ModelBase):
93     def __init__(self, href=''):
94         self.href = href
95
96
97 @swagger.model()
98 class Versions(ModelBase):
99     """
100         @property versions:
101         @ptype versions: C{list} of L{Version}
102     """
103
104     def __init__(self):
105         self.versions = list()
106
107     @staticmethod
108     def attr_parser():
109         return {'versions': Version}
110
111
112 @swagger.model()
113 class Version(ModelBase):
114     def __init__(self, version=None, description=None):
115         self.version = version
116         self.description = description