import pymongo from tools.config import get_prop client = pymongo.MongoClient(get_prop(['database','HOST']), get_prop(['database','PORT'],'int')) db = client[get_prop(['database','SCHEME'])] def __check_id_empty(collection_name,id): collection = db[collection_name] if collection.find_one({"id": id}) == None: return True return False def get_all_collection(collection_name): collection = db[collection_name] return collection.find({}, {"_id": 0}) def get_by_id(collection_name,id): collection = db[collection_name] return list(collection.find({"id": id}, {"_id": 0})) def insert_to_collection(collection_name, content): collection = db[collection_name] try: print content if __check_id_empty(collection_name, content['id']): collection.insert_one(content) return 201 else: return 406 except: return 400
from tools.config import get_prop app = flask.Flask(__name__) @app.route('/') def root(): return 'Welcome' @app.route('/<parent_name>', methods=['GET','POST']) def parent(parent_name): if flask.request.method == 'GET': return app_functions.get_parent_by_name(parent_name) elif flask.request.method == 'POST': return app_functions.insert_new_doc_to_collection(parent_name, flask.request.data) @app.route('/members/<int:member_id>', methods=['GET','PUT','DELETE']) def member(member_id): if flask.request.method == 'GET': return app_functions.get_child_by_id_by_id('members',member_id) elif flask.request.method == 'DELETE': return app_functions.remove_id_from_collection('members',member_id) @app.route('/to_do/<int:to_do_id>', methods=['GET','PUT','DELETE']) def to_do(to_do_id): if flask.request.method == 'GET': return app_functions.get_child_by_id_by_id('to_do',to_do_id) elif flask.request.method == 'DELETE': return app_functions.remove_id_from_collection('to_do',to_do_id) app.run(host='0.0.0.0', port=get_prop(['networking', 'PORT'], 'int'), debug=get_prop(['general', 'DEBUG'], 'bool'))