def insert(): kwargs = {} for k, v in request.args.items(): try: v = int(v) except Exception: pass kwargs[k] = v # 绑定数据库和集合到db对象中 if 'db' in kwargs and 'coll' in kwargs: db_ = kwargs.pop('db') coll_ = kwargs.pop('coll') try: db.init(db_, coll_) except CollectionLimitError as err: return str(err) except Exception as err: return str(err) try: db.insert(kwargs) return '插入成功!' except Exception as err: return str(err)
def recovery(): # 绑定数据库和集合到db对象中 try: db_ = request.args.get('db') coll_ = request.args.get('coll') db.init(db_, coll_) except CollectionLimitError as err: return str(err) except Exception as err: return str(err) query = {} for k, v in request.args.items(): try: v = int(v) except Exception: pass query[k] = v if 'multi' in query: query.pop('multi') multi = request.args.get('multi', default='') try: count = db.recovery(query, multi) return '成功恢复%d记录' % count except QueryParamsError as err: return str(err) except Exception as err: return str(err)
def update(): # 绑定数据库和集合到db对象中 try: db_ = request.args.get('db') coll_ = request.args.get('coll') db.init(db_, coll_) except CollectionLimitError as err: return str(err) except Exception as err: return str(err) query, update = {}, {} for arg in request.args: if arg[0:2] == 'q_': try: value = int(request.args.get(arg)) except ValueError: value = request.args.get(arg) query[arg[2:]] = value if arg[0:2] == 'u_': try: value = int(request.args.get(arg)) except ValueError: value = request.args.get(arg) update[arg[2:]] = value multi = request.args.get('multi', default='') try: count = db.update(query, update, multi=multi) return ''' <br> <html> <body> <head></head> <h3>成功更新{count}条记录</h3> <h3>查询刚更新的记录: <a href="{url}">{url}</a></h3> <a href="/">接口文档</a> </body> </html> '''.format(url=url_for('find', **update), count=count) except QueryParamsError as err: return str(err) except QuerySetNull as err: return str(err) except Exception as err: if not update: return '更新参数未传入' return str(err)
def find(): kwargs = {} for k, v in request.args.items(): try: v = int(v) except Exception: pass kwargs[k] = v # 绑定数据库和集合到db对象中 if 'db' in kwargs and 'coll' in kwargs: db_ = kwargs.pop('db') coll_ = kwargs.pop('coll') try: db.init(db_, coll_) except CollectionLimitError as err: return str(err) except Exception as err: return str(err) datas = db.find(kwargs) ret = json.dumps(datas) return ret