aboutsummaryrefslogtreecommitdiff
path: root/src/web/views/api/v2/common.py
blob: 092b33b092c16b6ef402ede54db2bc5e4db8beb0 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"""For a given resources, classes in the module intend to create the following
routes :
    GET resource/<id>
        -> to retrieve one
    POST resource
        -> to create one
    PUT resource/<id>
        -> to update one
    DELETE resource/<id>
        -> to delete one

    GET resources
        -> to retrieve several
    POST resources
        -> to create several
    PUT resources
        -> to update several
    DELETE resources
        -> to delete several
"""
import ast
import logging
from functools import wraps
from werkzeug.exceptions import Unauthorized, BadRequest, Forbidden, NotFound
from flask import request
from flask.ext.restful import Resource, reqparse
from flask.ext.login import current_user

from web.views.common import admin_permission, api_permission, \
                             login_user_bundle, jsonify
from web.controllers import UserController

logger = logging.getLogger(__name__)


def authenticate(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if request.authorization:
            ucontr = UserController()
            try:
                user = ucontr.get(login=request.authorization.username)
            except NotFound:
                raise Forbidden("Couldn't authenticate your user")
            if not ucontr.check_password(user, request.authorization.password):
                raise Forbidden("Couldn't authenticate your user")
            if not user.is_active:
                raise Forbidden("User is desactivated")
            login_user_bundle(user)
        if current_user.is_authenticated:
            return func(*args, **kwargs)
        raise Unauthorized()
    return wrapper


class PyAggAbstractResource(Resource):
    method_decorators = [authenticate, jsonify]
    controller_cls = None
    attrs = None

    @property
    def controller(self):
        if admin_permission.can():
            return self.controller_cls()
        return self.controller_cls(current_user.id)

    def reqparse_args(self, right, req=None, strict=False, default=True,
                      allow_empty=False):
        """
        strict: bool
            if True will throw 400 error if args are defined and not in request
        default: bool
            if True, won't return defaults
        args: dict
            the args to parse, if None, self.attrs will be used
        """
        try:
            in_values = req.json if req else (request.json or {})
            if not in_values and allow_empty:
                return {}
        except BadRequest:
            if allow_empty:
                return {}
            raise
        parser = reqparse.RequestParser()
        if self.attrs is not None:
            attrs = self.attrs
        elif admin_permission.can():
            attrs = self.controller_cls._get_attrs_desc('admin')
        elif api_permission.can():
            attrs = self.controller_cls._get_attrs_desc('api', right)
        else:
            attrs = self.controller_cls._get_attrs_desc('base', right)
        assert attrs, "No defined attrs for %s" % self.__class__.__name__

        for attr_name, attr in attrs.items():
            if not default and attr_name not in in_values:
                continue
            else:
                parser.add_argument(attr_name, location='json', **attr)
        #return parser.parse_args(req=req, strict=strict)
        return attrs


class PyAggResourceNew(PyAggAbstractResource):

    @api_permission.require(http_exception=403)
    def post(self):
        """Create a single new object"""
        return self.controller.create(**self.reqparse_args(right='write')), 201


class PyAggResourceExisting(PyAggAbstractResource):

    def get(self, obj_id=None):
        """Retreive a single object"""
        return self.controller.get(id=obj_id)

    def put(self, obj_id=None):
        """update an object, new attrs should be passed in the payload"""
        args = self.reqparse_args(right='write', default=False)
        if not args:
            raise BadRequest()
        return self.controller.update({'id': obj_id}, args), 200

    def delete(self, obj_id=None):
        """delete a object"""
        self.controller.delete(obj_id)
        return None, 204


class PyAggResourceMulti(PyAggAbstractResource):

    def get(self):
        """retrieve several objects. filters can be set in the payload on the
        different fields of the object, and a limit can be set in there as well
        """
        args = {}
        try:
            limit = request.json.pop('limit', 10)
            order_by = request.json.pop('order_by', None)
        except Exception:
            attrs = self.reqparse_args(right='read', default=False)
            for k, v in request.args.items():
                if k in attrs.keys():
                    if attrs[k]['type'] in [bool, int]:
                        args[k] = ast.literal_eval(v)
                    else:
                        args[k] = v
            limit = request.args.get('limit', 10)
            order_by = request.args.get('order_by', None)
        query = self.controller.read(**args)
        if order_by:
            query = query.order_by(order_by)
        if limit:
            query = query.limit(limit)
        return [res for res in query]

    @api_permission.require(http_exception=403)
    def post(self):
        """creating several objects. payload should be:
        >>> payload
        [{attr1: val1, attr2: val2}, {attr1: val1, attr2: val2}]
        """
        status, fail_count, results = 200, 0, []

        class Proxy:
            pass
        for attrs in request.json:
            try:
                Proxy.json = attrs
                args = self.reqparse_args('write', req=Proxy, default=False)
                obj = self.controller.create(**args)
                results.append(obj)
            except Exception as error:
                fail_count += 1
                results.append(str(error))
        if fail_count == len(results):  # all failed => 500
            status = 500
        elif fail_count:  # some failed => 206
            status = 206
        return results, status

    def put(self):
        """updating several objects. payload should be:
        >>> payload
        [[obj_id1, {attr1: val1, attr2: val2}]
         [obj_id2, {attr1: val1, attr2: val2}]]
        """
        status, results = 200, []

        class Proxy:
            pass
        for obj_id, attrs in request.json:
            try:
                Proxy.json = attrs
                args = self.reqparse_args('write', req=Proxy, default=False)
                result = self.controller.update({'id': obj_id}, args)
                if result:
                    results.append('ok')
                else:
                    results.append('nok')
            except Exception as error:
                results.append(str(error))
        if results.count('ok') == 0:  # all failed => 500
            status = 500
        elif results.count('ok') != len(results):  # some failed => 206
            status = 206
        return results, status

    def delete(self):
        """will delete several objects,
        a list of their ids should be in the payload"""
        status, results = 204, []
        for obj_id in request.json:
            try:
                self.controller.delete(obj_id)
                results.append('ok')
            except Exception as error:
                status = 206
                results.append(error)
        # if no operation succeded, it's not partial anymore, returning err 500
        if status == 206 and results.count('ok') == 0:
            status = 500
        return results, status
bgstack15