2011-09-11 22:11:36 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from flask import *
|
|
|
|
import werkzeug
|
|
|
|
|
|
|
|
from models import db, Project, Person, Bill
|
|
|
|
from utils import for_all_methods
|
|
|
|
|
2011-09-11 23:00:32 +02:00
|
|
|
from rest import RESTResource, need_auth # FIXME make it an ext
|
2011-09-11 22:11:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2011-09-11 23:00:32 +02:00
|
|
|
class ProjectHandler(object):
|
2011-09-11 22:11:36 +02:00
|
|
|
|
2011-09-11 23:00:32 +02:00
|
|
|
def add(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@need_auth(check_project, "project")
|
|
|
|
def get(self, project):
|
2011-09-11 22:11:36 +02:00
|
|
|
return "get"
|
|
|
|
|
2011-09-11 23:00:32 +02:00
|
|
|
@need_auth(check_project, "project")
|
|
|
|
def delete(self, project):
|
2011-09-11 22:11:36 +02:00
|
|
|
return "delete"
|
|
|
|
|
2011-09-11 23:00:32 +02:00
|
|
|
@need_auth(check_project, "project")
|
|
|
|
def update(self, project):
|
|
|
|
return "update"
|
|
|
|
|
|
|
|
|
|
|
|
class MemberHandler(object):
|
|
|
|
|
|
|
|
def get(self, project, member_id):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def list(self, project):
|
2011-09-13 11:27:36 +02:00
|
|
|
return project.members
|
2011-09-11 23:00:32 +02:00
|
|
|
|
|
|
|
def add(self, project):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, project, member_id):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delete(self, project, member_id):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BillHandler(object):
|
|
|
|
|
|
|
|
def get(self, project, member_id):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def list(self, project):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def add(self, project):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, project, member_id):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def delete(self, project, member_id):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-09-11 22:11:36 +02:00
|
|
|
project_resource = RESTResource(
|
|
|
|
name="project",
|
|
|
|
route="/project",
|
|
|
|
app=api,
|
|
|
|
actions=["add", "update", "delete", "get"],
|
|
|
|
handler=ProjectHandler())
|
|
|
|
|
2011-09-11 23:00:32 +02:00
|
|
|
member_resource = RESTResource(
|
|
|
|
name="member",
|
|
|
|
inject_name="project",
|
|
|
|
route="/project/<project_id>/members",
|
|
|
|
app=api,
|
|
|
|
handler=MemberHandler(),
|
|
|
|
authentifier=check_project)
|
|
|
|
|
|
|
|
bill_resource = RESTResource(
|
|
|
|
name="bill",
|
|
|
|
inject_name="project",
|
|
|
|
route="/project/<project_id>/bills",
|
|
|
|
app=api,
|
|
|
|
handler=BillHandler(),
|
|
|
|
authentifier=check_project)
|
|
|
|
|
2011-09-11 22:11:36 +02:00
|
|
|
# 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
|