--- a/.hgignore Fri Jan 11 13:48:48 2013 +0900
+++ b/.hgignore Fri Jan 11 14:23:54 2013 +0900
--- a/app.py Fri Jan 11 13:48:48 2013 +0900
+++ b/app.py Fri Jan 11 14:23:54 2013 +0900
-from flask import Flask, g, request, render_template
-from flask.ext.restful import Resource, Api
-from CodernityDB.database import RecordNotFound, RecordDeleted
+from flask import Flask, g, render_template
+from flask.ext.restful import Api
from CodernityDB.database_thread_safe import ThreadSafeDatabase
from fruitbar.indexes import ProjectIndex, TaskIndex
+from fruitbar.crud import ResourceList, CRUDResource
+app.config.from_object(__name__)
-db_path = os.path.join(os.path.dirname(__file__), 'db')
-cdb = ThreadSafeDatabase(db_path)
+ db_path = os.path.join(os.path.dirname(__file__), app.config['DATABASE'])
+ cdb = ThreadSafeDatabase(db_path)
- cdb.add_index(ProjectIndex(cdb.path, 'project'))
- cdb.add_index(TaskIndex(cdb.path, 'task'))
+ print list(cdb.all('id'))
+ 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):
- """ CRUD? More like RUD!
- def get(self, workspace, resource_id):
- doc = g.db.get('id', resource_id, with_doc=True)
- if doc['workspace'] != workspace:
- except (RecordNotFound, RecordDeleted):
- def put(self, workspace, resource_id):
- doc = g.db.get('id', resource_id, with_doc=True)
- if doc['workspace'] != workspace:
- except (RecordNotFound, RecordDeleted):
- 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:
- except (RecordNotFound, RecordDeleted):
+ return render_template('index.html')
class ProjectList(ResourceList):
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__':
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/fruitbar/crud.py Fri Jan 11 14:23:54 2013 +0900
+from flask import g, request
+from flask.ext.restful import Resource
+from CodernityDB.database import RecordNotFound, RecordDeleted
+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):
+ """ CRUD? More like RUD!
+ def get(self, workspace, resource_id):
+ doc = g.db.get('id', resource_id, with_doc=True)
+ if doc['workspace'] != workspace:
+ except (RecordNotFound, RecordDeleted):
+ def put(self, workspace, resource_id):
+ doc = g.db.get('id', resource_id, with_doc=True)
+ if doc['workspace'] != workspace:
+ except (RecordNotFound, RecordDeleted):
+ 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:
+ except (RecordNotFound, RecordDeleted):
--- a/test.py Fri Jan 11 13:48:48 2013 +0900
+++ b/test.py Fri Jan 11 14:23:54 2013 +0900
from unittest import TestCase, main
+from app import app, init_db
class BaseTestCase(TestCase):
if __name__ == '__main__':
app.config['TESTING'] = True
+ app.config['DATABASE'] = 'test_db'