Anton Shestakov <engored@ya.ru>, Thu, 27 Dec 2012 00:12:30 +0900
Moving toward a multi-workspace work model. There's no auth, knowing workspace id gives full access.
app.py
Permissions: -rwxr-xr-x
from flask import Flask, g, request, render_template from flask.ext.restful import Resource, Api from CodernityDB.database import RecordNotFound from CodernityDB.database_thread_safe import ThreadSafeDatabase from fruitbar.indexes import ProjectIndex, TaskIndex db_path = os.path.join(os.path.dirname(__file__), 'db') cdb = ThreadSafeDatabase(db_path) cdb.add_index(ProjectIndex(cdb.path, 'project')) cdb.add_index(TaskIndex(cdb.path, 'task')) class ResourceList(Resource): def get(self, workspace): return [project['doc'] for project in g.db.get_many(self.db_index, workspace, with_doc=True)] def post(self, workspace): doc = request.json.copy() doc.update(self.doc_stub) doc.update({'workspace': workspace}) response = g.db.insert(doc) return g.db.get('id', response['_id'], with_doc=True) class CRUDResource(Resource): def get(self, workspace, resource_id): doc = g.db.get('id', resource_id, with_doc=True) if doc['workspace'] != workspace: def put(self, workspace, resource_id): doc = g.db.get('id', resource_id, with_doc=True) if doc['workspace'] != workspace: userdata = dict((k, v) for (k, v) in request.json.items() if k in self.safe_fields) response = g.db.update(doc) return self.get(workspace, response['_id']) def delete(self, workspace, resource_id): doc = g.db.get('id', resource_id, with_doc=True) if doc['workspace'] != workspace: class ProjectList(ResourceList): doc_stub = {'_t': 'project'} class Project(CRUDResource): safe_fields = ('name', 'color') def delete(self, workspace, resource_id): g.db.run('task', 'delete_for_project', workspace, resource_id) return super(Project, self).delete(workspace, resource_id) class TaskList(ResourceList): doc_stub = {'_t': 'task'} class Task(CRUDResource): safe_fields = ('name', 'note', 'done') api.add_resource(ProjectList, '/<workspace>/projects/') api.add_resource(Project, '/<workspace>/projects/<resource_id>/') api.add_resource(TaskList, '/<workspace>/tasks/') api.add_resource(Task, '/<workspace>/tasks/<resource_id>/') return render_template('index.html') if __name__ == '__main__':