aboutsummaryrefslogtreecommitdiff
path: root/budget/api.py
blob: 70864a6527ac900ac97066810b50f2da7552ebb1 (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
# -*- coding: utf-8 -*-
from flask import *
import werkzeug

from models import db, Project, Person, Bill
from utils import for_all_methods

from rest import RESTResource, DefaultHandler, need_auth # FIXME make it an ext


api = Blueprint("api", __name__, url_prefix="/api")

def check_project(*args, **kwargs):
    """Check the request for basic authentication for a given project.

    Return the project if the authorization is good, False otherwise
    """
    auth = request.authorization

    # project_id should be contained in kwargs and equal to the username
    if auth and "project_id" in kwargs and \
            auth.username == kwargs["project_id"]:
        project = Project.query.get(auth.username)
        if project.password == auth.password:
            return project
    return False


class ProjectHandler(DefaultHandler):

    def get(self, *args, **kwargs):
        return "get"

    def delete(self, *args, **kwargs):
        return "delete"

project_resource = RESTResource(
    name="project",
    route="/project", 
    app=api, 
    actions=["add", "update", "delete", "get"],
    authentifier=check_project,
    handler=ProjectHandler())

# projects: add, delete, edit, get
# GET /project/<id> → get
# PUT /project/<id> → add & edit
# DELETE /project/<id> → delete

# project members: list, add, delete
# GET /project/<id>/members → list
# POST /project/<id>/members/ → add
# PUT /project/<id>/members/<user_id> → edit
# DELETE /project/<id>/members/<user_id> → delete

# project bills: list, add, delete, edit, get
# GET /project/<id>/bills → list
# GET /project/<id>/bills/<bill_id> → get
# DELETE /project/<id>/bills/<bill_id> → delete
# POST /project/<id>/bills/ → add


# GET, PUT, DELETE: /<id> : Get, update and delete
# GET, POST: / Add & List