Commits

remosu  committed baf55e2

Cleaning up \n Change id to _id in backmongo.py and flask_backmongo.py \n Moved todo example to examples/todo

  • Participants
  • Parent commits 8045ed4

Comments (0)

Files changed (6)

File backmongo.py

 
 from backmongo_conf import DATABASE, OID_PREFIX
 
-
 CONNECTION = pymongo.Connection()
 DB = CONNECTION[DATABASE]
 
-def clean_id(id):
-    if id.startswith(OID_PREFIX):
-        return ObjectId(id[len(OID_PREFIX):])
+def clean_id(_id):
+    if _id.startswith(OID_PREFIX):
+        return ObjectId(_id[len(OID_PREFIX):])
     else:
-        return id
+        return _id
 
 def dict_to_json_converter(obj, **kwargs):
     if isinstance(obj, ObjectId):
     else:
         return json_util.default(obj, **kwargs)
 
-def read(collection_name, id=None):
-    if id is not None:
-        result = DB[collection_name].find_one(clean_id(id))
+def read(collection_name, _id=None):
+    if _id is not None:
+        result = DB[collection_name].find_one(clean_id(_id))
     else:
         result = list(DB[collection_name].find())
     
     return json.dumps(result, default=dict_to_json_converter)
 
-def delete(collection_name, id):
-    DB[collection_name].remove(clean_id(id))
+def delete(collection_name, _id):
+    DB[collection_name].remove(clean_id(_id))
 
-def save(collection_name, model, id=None):
-    if id is not None:
+def save(collection_name, model, _id=None):
+    if _id is not None:
         model['_id'] = clean_id(model['_id'])
     return DB[collection_name].save(model, manipulate=False)

File backmongo_app.py

-from flask import Flask
-
-from flask.ext import backmongo
-
-app = Flask(__name__)
-
-backmongo.init_app(app)
-
-
-if __name__ == "__main__":
-    app.run(debug=True)

File backmongo_conf.py

-
-DATABASE = "backmongo"
-OID_PREFIX = 'oid_'

File examples/todos/backmongo_app.py

+from flask import Flask
+
+from flask.ext import backmongo
+
+app = Flask(__name__)
+
+backmongo.init_app(app)
+
+
+if __name__ == "__main__":
+    app.run(debug=True)

File examples/todos/backmongo_conf.py

+
+DATABASE = "backmongo"
+OID_PREFIX = 'oid_'

File flask_backmongo.py

-from flask import Blueprint, request, Response
+from flask import Blueprint, request, Response, Flask
 
 import backmongo
 
 bp = Blueprint('api', __name__)
 
 @bp.route("/<collection>/", methods=['GET'])
-@bp.route("/<collection>/<id>", methods=['GET'])
-def read(collection, id=None):
-    result = backmongo.read(collection, id)
+@bp.route("/<collection>/<_id>", methods=['GET'])
+def read(collection, _id=None):
+    result = backmongo.read(collection, _id)
     return Response(result, mimetype='application/json')
 
-@bp.route("/<collection>/<id>", methods=['DELETE'])
-def delete(collection, id):
-    backmongo.delete(collection, id)
+@bp.route("/<collection>/<_id>", methods=['DELETE'])
+def delete(collection, _id):
+    backmongo.delete(collection, _id)
     return Response("{status: OK}", mimetype='application/json')
 
 @bp.route("/<collection>/", methods=['POST'])
-@bp.route("/<collection>/<id>", methods=['PUT'])
-def save(collection, id=None):
-    result = backmongo.save(collection, request.json, id)
+@bp.route("/<collection>/<_id>", methods=['PUT'])
+def save(collection, _id=None):
+    result = backmongo.save(collection, request.json, _id)
     return Response(result, mimetype='application/json')
 
 def init_app(app, url_prefix=''):
-    app.register_blueprint(bp, url_prefix=url_prefix)
+    app.register_blueprint(bp, url_prefix=url_prefix)
+
+
+if __name__ == '__main__':
+    app = Flask(__name__)
+    init_app(app)
+    app.run(debug=True)