def drop_model(appname, modelname): """ offline api to offline data Request URL: /predata/offline Http Method: POST Parameters:{ "items":["123", "124"], "server":"local"} Return :{ "status":0 "data":{}} """ try: req_dict = ArmoryJson.decode(request.data) except: return json_response_error(PARAM_ERROR, msg="json format error") id_list = [] item_ids = req_dict.get("items") server = req_dict.get("server") try: id_list.extend(map(int, item_ids)) except: return json_response_error(PARAM_ERROR, msg="item ids format error") # view logic return offline(appname, modelname, id_list, server)
def init_conf(cls, conf): if isinstance(conf, str): conf = ArmoryJson.decode(conf) if not isinstance(conf, dict): raise Exception('mongodb config should be dict') for route in conf: if route not in cls.connection_dict: (cx, db) = cls._init_conf(conf[route], route)
def init_app(cls, app): if 'pymongo' not in app.extensions: app.extensions['pymongo'] = {} config_uri = ArmoryJson.decode(app.config['mongodb_conf']) if not isinstance(config_uri, dict): raise Exception('mongodb config should be dict') for route in config_uri: if route not in app.extensions['pymongo']: (cx, db) = cls._init_conf(config_uri[route], route) app.extensions['pymongo'][route] = (cx, db)
def add_user(appname): if not session.get('uid'): return json_response_error(AUTH_ERROR) required_list = ("user_name", "group_id") data = check_required_params(request, required_list) if isinstance(data, Response): return data add_data = ArmoryJson.decode(request.data) data["mark"] = add_data.get("mark", "") # view logic return user_create(appname, data)
def check_required_params(request, params): if request.method == "GET": data = request.args elif request.method == "POST": try: data = ArmoryJson.decode(request.data) except ValueError as expt: _LOGGER.error("para except:%s", expt) return json_response_error( PARAM_ERROR, msg="json loads error,check parameters format") for key in params: if key not in data: return json_response_error(PARAM_REQUIRED, msg="no param:%s" % key) new_data = get_params(params, data) return new_data
def get_valid_params(request, keys): ''' get valid params by params rule ''' if request.method == "GET": query_dict = request.args elif request.method == "POST": try: query_dict = ArmoryJson.decode(request.data) except ValueError as expt: _LOGGER.error("post para except:%s", expt) return json_response_error( PARAM_ERROR, msg="json loads error,check parameters format") result = {} for key in keys: paras = key.split('&') paras = paras[:4] (param_key, param_option, param_type, default_value) = tuple(paras) \ + (None,) * (4 - len(paras)) if not param_key or param_option not in PARAM_OPTION_LIST: # invalid config for parameter %key% continue param_value = query_dict.get(param_key) if param_value is None: if param_option == 'need': return json_response_error( PARAM_REQUIRED, msg="no param:%s" % param_key) if param_option == 'noneed': continue if default_value is not None: param_value = _conv(eval(param_type))(default_value) else: param_value = default_value else: if param_type is not None: try: param_value = _conv(eval(param_type))(param_value) except Exception as e: return json_response_error( PARAM_ERROR, msg="param:%s type error" % param_key) result[param_key] = param_value return result
def _json_response(status, data, msg=None): d = {'status': status, 'data': data, 'msg': msg} response = make_response(ArmoryJson.encode(d)) response.headers['Content-Type'] = 'application/json; charset=utf-8' return response
def json_request(request): return ArmoryJson.decode(request.raw_post_data)