Merge "unify pod keywork so api can easily used"
[yardstick.git] / api / database / v1 / handlers.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9 from api.database import db_session
10 from api.database.v1.models import Tasks
11 from api.database.v1.models import AsyncTasks
12
13
14 class TasksHandler(object):
15
16     def insert(self, kwargs):
17         task = Tasks(**kwargs)
18         db_session.add(task)
19         db_session.commit()
20         return task
21
22     def get_task_by_taskid(self, task_id):
23         task = Tasks.query.filter_by(task_id=task_id).first()
24         if not task:
25             raise ValueError
26
27         return task
28
29     def update_attr(self, task_id, attr):
30         task = self.get_task_by_taskid(task_id)
31
32         for k, v in attr.items():
33             setattr(task, k, v)
34         db_session.commit()
35
36
37 class AsyncTaskHandler(object):
38     def insert(self, kwargs):
39         task = AsyncTasks(**kwargs)
40         db_session.add(task)
41         db_session.commit()
42         return task
43
44     def get_task_by_taskid(self, task_id):
45         task = AsyncTasks.query.filter_by(task_id=task_id).first()
46         if not task:
47             raise ValueError
48
49         return task
50
51     def update_attr(self, task_id, attr):
52         task = self.get_task_by_taskid(task_id)
53
54         for k, v in attr.items():
55             setattr(task, k, v)
56         db_session.commit()