aboutsummaryrefslogtreecommitdiffstats
path: root/api/resources/v2/projects.py
blob: 7d51cf0409a6a6d98bca5706f5fb39c4d8ac1e4b (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
import uuid

from datetime import datetime

from api import ApiResource
from api.database.v2.handlers import V2ProjectHandler
from yardstick.common.utils import result_handler
from yardstick.common.utils import change_obj_to_dict
from yardstick.common import constants as consts


class V2Projects(ApiResource):

    def get(self):
        project_handler = V2ProjectHandler()
        projects = [change_obj_to_dict(p) for p in project_handler.list_all()]

        for p in projects:
            tasks = p['tasks']
            p['tasks'] = tasks.split(',') if tasks else []

        return result_handler(consts.API_SUCCESS, {'projects': projects})

    def post(self):
        return self._dispatch_post()

    def create_project(self, args):
        try:
            name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        project_id = str(uuid.uuid4())
        create_time = datetime.now()
        project_handler = V2ProjectHandler()

        project_init_data = {
            'uuid': project_id,
            'name': name,
            'time': create_time
        }
        project_handler.insert(project_init_data)

        return result_handler(consts.API_SUCCESS, {'uuid': project_id})


class V2Project(ApiResource):

    def get(self, project_id):
        try:
            uuid.UUID(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid project id')

        project_handler = V2ProjectHandler()
        try:
            project = project_handler.get_by_uuid(project_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such project id')

        project_info = change_obj_to_dict(project)
        tasks = project_info['tasks']
        project_info['tasks'] = tasks.split(',') if tasks else []

        return result_handler(consts.API_SUCCESS, {'project': project_info})