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)
52 def from_dict_with_raise(cls, a_dict):
56 attr_parser = cls.attr_parser()
58 for k, v in a_dict.iteritems():
59 if k not in t.__dict__:
61 '{} has no attribute {}'.format(cls.__name__, k))
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:
68 value.append(attr_parser[k].from_dict_with_raise(item))
70 t.__setattr__(k, value)
78 def _format(self, excludes):
79 new_obj = copy.deepcopy(self)
80 dicts = new_obj.__dict__
81 for k in dicts.keys():
85 dicts[k] = self._obj_format(dicts[k])
88 def _obj_format(self, obj):
89 if self._has_format(obj):
91 elif isinstance(obj, unicode):
93 obj = self._obj_format(ast.literal_eval(obj))
99 elif isinstance(obj, list):
102 hs.append(self._obj_format(h))
104 elif not isinstance(obj, (str, int, float, dict)):
109 def _has_format(obj):
110 return not isinstance(obj, (str, unicode)) and hasattr(obj, 'format')
114 class CreateResponse(ModelBase):
115 def __init__(self, href=''):
120 class Versions(ModelBase):
123 @ptype versions: C{list} of L{Version}
127 self.versions = list()
131 return {'versions': Version}
135 class Version(ModelBase):
136 def __init__(self, version=None, description=None):
137 self.version = version
138 self.description = description