aboutsummaryrefslogtreecommitdiff
path: root/src/web/views/api/v3/common.py
blob: 51e4e6bebcaa950a5ed9e423d58246e25d7e08fd (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
from flask import request
from flask.ext.login import current_user
from flask.ext.restless import ProcessingException
from werkzeug.exceptions import NotFound
from web.controllers import ArticleController, UserController
from web.views.common import login_user_bundle

url_prefix = '/api/v3'

def auth_func(*args, **kw):
    if request.authorization:
        ucontr = UserController()
        try:
            user = ucontr.get(nickname=request.authorization.username)
        except NotFound:
            raise ProcessingException("Couldn't authenticate your user",
                                        code=401)
        if not ucontr.check_password(user, request.authorization.password):
            raise ProcessingException("Couldn't authenticate your user",
                                        code=401)
        if not user.is_active:
            raise ProcessingException("User is desactivated", code=401)
        login_user_bundle(user)
    if not current_user.is_authenticated:
        raise ProcessingException(description='Not authenticated!', code=401)

class AbstractProcessor():
    """Abstract processors for the Web services.
    """

    def is_authorized(self, user, obj):
        if user.id != obj.user_id:
            raise ProcessingException(description='Not Authorized', code=401)

    def get_single_preprocessor(self, instance_id=None, **kw):
        # Check if the user is authorized to modify the specified
        # instance of the model.
        pass

    def get_many_preprocessor(self, search_params=None, **kw):
        """Accepts a single argument, `search_params`, which is a dictionary
        containing the search parameters for the request.
        """
        filt = dict(name="user_id",
                    op="eq",
                    val=current_user.id)

        # Check if there are any filters there already.
        if "filters" not in search_params:
          search_params["filters"] = []

        search_params["filters"].append(filt)

    def post_preprocessor(self, data=None, **kw):
        pass

    def put_single_preprocessor(instance_id=None, data=None, **kw):
        """Accepts two arguments, `instance_id`, the primary key of the
        instance of the model to patch, and `data`, the dictionary of fields
        to change on the instance.
        """
        pass

    def delete_preprocessor(self, instance_id=None, **kw):
        pass
bgstack15