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 ##############################################################################
20 from opnfv_testapi.tornado_swagger import swagger
23 class ModelBase(object):
26 return self._format(['_id'])
28 def format_http(self):
29 return self._format([])
32 def from_dict(cls, a_dict):
36 attr_parser = cls.attr_parser()
38 for k, v in a_dict.iteritems():
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:
45 value.append(attr_parser[k].from_dict(item))
47 t.__setattr__(k, value)
55 def _format(self, excludes):
56 new_obj = copy.deepcopy(self)
57 dicts = new_obj.__dict__
58 for k in dicts.keys():
62 dicts[k] = self._obj_format(dicts[k])
65 def _obj_format(self, obj):
66 if self._has_format(obj):
68 elif isinstance(obj, unicode):
70 obj = self._obj_format(ast.literal_eval(obj))
76 elif isinstance(obj, list):
79 hs.append(self._obj_format(h))
81 elif not isinstance(obj, (str, int, float, dict)):
87 return not isinstance(obj, (str, unicode)) and hasattr(obj, 'format')
91 class CreateResponse(ModelBase):
92 def __init__(self, href=''):
97 class Versions(ModelBase):
100 @ptype versions: C{list} of L{Version}
104 self.versions = list()
108 return {'versions': Version}
112 class Version(ModelBase):
113 def __init__(self, version=None, description=None):
114 self.version = version
115 self.description = description