summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/resources/project_handlers.py
blob: 69ce3b5921de358dd2b6298ae27191dcafd4f978 (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
from tornado import gen
from tornado.web import HTTPError, asynchronous

from tornado_swagger_ui.tornado_swagger import swagger
from handlers import GenericApiHandler, prepare_put_request, format_data
from common.constants import HTTP_BAD_REQUEST, HTTP_FORBIDDEN, HTTP_NOT_FOUND
from project_models import Project


class GenericProjectHandler(GenericApiHandler):
    def __init__(self, application, request, **kwargs):
        super(GenericProjectHandler, self).__init__(application,
                                                    request,
                                                    **kwargs)
        self.table = 'projects'
        self.table_cls = Project


class ProjectCLHandler(GenericProjectHandler):
    @swagger.operation(nickname="list-all")
    def get(self):
        """
            @description: list all projects
            @return 200: return all projects, empty list is no project exist
            @rtype: L{Projects}
        """
        self._list()

    @swagger.operation(nickname="create")
    def post(self):
        """
            @description: create a project
            @param body: project to be created
            @type body: L{ProjectCreateRequest}
            @in body: body
            @rtype: L{Project}
            @return 200: project is created.
            @raise 403: project already exists
            @raise 400: post without body
        """
        self._create('{} already exists as a {}')


class ProjectGURHandler(GenericProjectHandler):
    @swagger.operation(nickname='get-one')
    def get(self, project_name):
        """
            @description: get a single project by project_name
            @rtype: L{Project}
            @return 200: project exist
            @raise 404: project not exist
        """
        self._get_one({'name': project_name})

    @asynchronous
    @gen.coroutine
    @swagger.operation(nickname="update")
    def put(self, project_name):
        """
            @description: update a single project by project_name
            @param body: project to be updated
            @type body: L{ProjectUpdateRequest}
            @in body: body
            @rtype: L{Project}
            @return 200: update success
            @raise 404: project not exist
            @raise 403: new project name already exist or nothing to update
        """
        if self.json_args is None:
            raise HTTPError(HTTP_BAD_REQUEST)

        query = {'name': project_name}
        from_project = yield self.db.projects.find_one(query)
        if from_project is None:
            raise HTTPError(HTTP_NOT_FOUND,
                            "{} could not be found".format(project_name))

        project = Project.from_dict(from_project)
        new_name = self.json_args.get("name")
        new_description = self.json_args.get("description")

        # check for payload name parameter in db
        # avoid a request if the project name has not changed in the payload
        if new_name != project.name:
            to_project = yield self.db.projects.find_one(
                {"name": new_name})
            if to_project is not None:
                raise HTTPError(HTTP_FORBIDDEN,
                                "{} already exists as a project"
                                .format(new_name))

        # new dict for changes
        request = dict()
        request = prepare_put_request(request,
                                      "name",
                                      new_name,
                                      project.name)
        request = prepare_put_request(request,
                                      "description",
                                      new_description,
                                      project.description)

        """ raise exception if there isn't a change """
        if not request:
            raise HTTPError(HTTP_FORBIDDEN, "Nothing to update")

        """ we merge the whole document """
        edit_request = project.format()
        edit_request.update(request)

        """ Updating the DB """
        yield self.db.projects.update(query, edit_request)
        new_project = yield self.db.projects.find_one({"_id": project._id})

        self.finish_request(format_data(new_project, Project))

    @swagger.operation(nickname='delete')
    def delete(self, project_name):
        """
            @description: delete a project by project_name
            @return 200: delete success
            @raise 404: project not exist
        """
        self._delete({'name': project_name})