aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_manager/moon_manager/api/models.py
blob: 56695c1757e1c26751aa9bfe28f20a917e091cd6 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.

from oslo_log import log as logging
from oslo_config import cfg
from moon_db.core import ModelManager

LOG = logging.getLogger(__name__)
CONF = cfg.CONF


class Models(object):

    def __init__(self):
        self.manager = ModelManager

    def get_models(self, ctx, args):
        try:
            data = self.manager.get_models(user_id=ctx["user_id"], model_id=ctx.get("id"))
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"models": data}

    def add_model(self, ctx, args):
        try:
            data = self.manager.add_model(user_id=ctx["user_id"], model_id=ctx.get("id"), value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"models": data}

    def delete_model(self, ctx, args):
        try:
            data = self.manager.delete_model(user_id=ctx["user_id"], model_id=ctx["id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"result": True}

    def update_model(self, ctx, args):
        try:
            data = self.manager.update_model(user_id=ctx["user_id"], model_id=ctx["id"], value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"models": data}


class MetaRules(object):

    def __init__(self):
        self.manager = ModelManager

    def add_meta_rules(self, ctx, args):
        try:
            data = self.manager.add_meta_rule(user_id=ctx["user_id"], meta_rule_id=None, value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"meta_rules": data}

    def delete_meta_rules(self, ctx, args):
        try:
            data = self.manager.delete_meta_rule(user_id=ctx["user_id"], meta_rule_id=ctx["meta_rule_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"result": True}

    def get_meta_rules(self, ctx, args):
        try:
            data = self.manager.get_meta_rules(user_id=ctx["user_id"], meta_rule_id=ctx["meta_rule_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"meta_rules": data}

    def set_meta_rules(self, ctx, args):
        try:
            data = self.manager.set_meta_rule(user_id=ctx["user_id"], meta_rule_id=ctx["meta_rule_id"], value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"meta_rules": data}


class MetaData(object):

    def __init__(self):
        self.manager = ModelManager

    def get_subject_categories(self, ctx, args):
        try:
            data = self.manager.get_subject_categories(user_id=ctx["user_id"], category_id=args["category_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"subject_categories": data}

    def set_subject_category(self, ctx, args):
        try:
            data = self.manager.add_subject_category(user_id=ctx["user_id"], value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"subject_categories": data}

    def delete_subject_category(self, ctx, args):
        try:
            data = self.manager.delete_subject_category(user_id=ctx["user_id"], category_id=args["category_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"result": True}

    def get_object_categories(self, ctx, args):
        try:
            data = self.manager.get_object_categories(user_id=ctx["user_id"], category_id=args["category_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"object_categories": data}

    def set_object_category(self, ctx, args):
        try:
            data = self.manager.add_object_category(user_id=ctx["user_id"], value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"object_categories": data}

    def delete_object_category(self, ctx, args):
        try:
            data = self.manager.delete_object_category(user_id=ctx["user_id"], category_id=args["category_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"result": True}

    def get_action_categories(self, ctx, args):
        try:
            data = self.manager.get_action_categories(user_id=ctx["user_id"], category_id=args["category_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"action_categories": data}

    def set_action_category(self, ctx, args):
        try:
            data = self.manager.add_action_category(user_id=ctx["user_id"], value=args)
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"action_categories": data}

    def delete_action_category(self, ctx, args):
        try:
            data = self.manager.delete_action_category(user_id=ctx["user_id"], category_id=args["category_id"])
        except Exception as e:
            LOG.error(e, exc_info=True)
            return {"result": False,
                    "error": str(e),
                    "ctx": ctx, "args": args}
        return {"result": True}