def try_check_request_data(request_data, *lists): # item[1] 0 int; 1 str; 2 nothing;3 float # item[2] 0:非必传; 1必传 try: dic = dict() for item in lists: if item[2] == 1: if request_data.get(item[0]) == 'null' or request_data.get(item[0]) is None or request_data.get( item[0]) == '': return resq_wrapper(cs.REQUEST_VALUE_ERR, request_data + '缺少必传值!==^~~^==') value = request_data.get(item[0], '') key = item[0] type = item[1] if type == 0: if value == '': dic.update({key: None}) else: dic.update({key: int(value)}) elif type == 1: # 不信任用户输入,转义html标签 dic.update({key:cgi.escape(str(value))}) elif type == 2: dic.update({key: value}) elif type == 3: if value == '': dic.update({key: None}) else: dic.update({key: float(value)}) return dic except TypeError, e: logger.error(e) return resq_wrapper(cs.REQUEST_TYPE_ERR, request_data)
def db_commit_all(lists,logs): try: db.session.add_all(lists) db.session.commit() log_write(logs[0].remote_addr, logs[0].path,logs[0].method, logs[1],'1800',logs[2]) return resq_wrapper(cs.OK) except Exception, e: logger.error(e) return resq_wrapper(cs.DB_COMMIT_ERR)
def unhandled_exception(e): logger.error('Unhandled Exception: %s', e) logger.error(traceback.format_exc()) return rw(cs.SERVER_ERR), 500
elif type == 1: # 不信任用户输入,转义html标签 dic.update({key:cgi.escape(str(value))}) elif type == 2: dic.update({key: value}) elif type == 3: if value == '': dic.update({key: None}) else: dic.update({key: float(value)}) return dic except TypeError, e: logger.error(e) return resq_wrapper(cs.REQUEST_TYPE_ERR, request_data) except ValueError, e: logger.error(e) return resq_wrapper(cs.REQUEST_VALUE_ERR, request_data) except Exception, e: logger.warning(e) return jsonify({'code': cs.EXCEPTION, 'errmsg': 'unexpected error : s%' % e, 'data': request_data}) def string_to_json(string_data): try: return json.loads(string_data) except: return None def upload_file(file_upload, user_id, flag): if file_upload and v_file(file_upload.filename): timestamp = str(time.time()).replace(".", "")
def db_exception(e): logger.error('Db Exception: %s', e) logger.error("meet fails when the execution of a database operation") db.session.rollback() return rw(cs.DB_ERROR), 500