summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi/opnfv_testapi/resources/models.py
blob: c85c1d5b14ee9c39d7efee83f346cb596d1fc30f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
##############################################################################
# Copyright (c) 2015 Orange
# guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
# feng.xiaowei@zte.com.cn  mv Pod to pod_models.py                 5-18-2016
# feng.xiaowei@zte.com.cn  add MetaCreateResponse/MetaGetResponse  5-18-2016
# feng.xiaowei@zte.com.cn  mv TestProject to project_models.py     5-19-2016
# feng.xiaowei@zte.com.cn  delete meta class                       5-19-2016
# feng.xiaowei@zte.com.cn  add CreateResponse                      5-19-2016
# feng.xiaowei@zte.com.cn  mv TestCase to testcase_models.py       5-20-2016
# feng.xiaowei@zte.com.cn  mv TestResut to result_models.py        5-23-2016
# feng.xiaowei@zte.com.cn  add ModelBase                           12-20-2016
##############################################################################
import copy

from opnfv_testapi.tornado_swagger import swagger


class ModelBase(object):

    def _format(self, excludes):
        new_obj = copy.deepcopy(self)
        dicts = new_obj.__dict__
        print self, self.__class__
        for k in dicts.keys():
            if k in excludes:
                del dicts[k]
            elif dicts[k]:
                if hasattr(dicts[k], 'format'):
                    dicts[k] = dicts[k].format()
                elif isinstance(dicts[k], list):
                    hs = []
                    for h in dicts[k]:
                        hs.append(h.format())
                    dicts[k] = hs
                elif not isinstance(dicts[k], (str, int, float, dict)):
                    dicts[k] = str(dicts[k])
        return dicts

    def format(self):
        return self._format(['_id'])

    def format_http(self):
        return self._format([])

    @staticmethod
    def attr_parser():
        return {}

    @classmethod
    def from_dict(cls, a_dict):
        if a_dict is None:
            return None

        attr_parser = cls.attr_parser()
        t = cls()
        for k, v in a_dict.iteritems():
            value = v
            if isinstance(v, dict) and k in attr_parser:
                value = attr_parser[k].from_dict(v)
            elif isinstance(v, list) and k in attr_parser:
                value = []
                for item in v:
                    value.append(attr_parser[k].from_dict(item))

            t.__setattr__(k, value)

        return t


class CreateResponse(ModelBase):
    def __init__(self, href=''):
        self.href = href


@swagger.model()
class Versions(ModelBase):
    """
        @property versions:
        @ptype versions: C{list} of L{Version}
    """

    def __init__(self):
        self.versions = list()

    @staticmethod
    def attr_parser():
        return {'versions': Version}


@swagger.model()
class Version(ModelBase):
    def __init__(self, version=None, description=None):
        self.version = version
        self.description = description