def edit_model_data_via_post_form(data, model, id_): """ 创建/修改数据模型内容 Args: data: model: id_: Returns: """ if not check_model_name(model): return {"msg": "invalid model"} mod = importlib.import_module("application.models", model) if id_ == '$$9527$$': if 's_name' in data.keys(): query_res = getattr( mod, model).query.filter_by(s_name=data['s_name']).first() if query_res: return {'msg': '已存在同名数据'} query_res = getattr(mod, model)() query_res.add(data) else: query_res = getattr(mod, model).query.get(id_) query_res.update(data) return {"msg": "success"}
def data_update(data: dict, cls_str: str) -> str: """ 根据名字改,后期改成根据id改 Args: cls_str: data: Returns: """ if not check_model_name(cls_str): return "invalid model" pk = get_cls_primary_key(cls_str) if "id" in data: query = {pk: data.pop("id"), "i_status": 1} tmp_cls = importlib.import_module("application.models", cls_str) cls = getattr(tmp_cls, cls_str) query_res = cls.query.filter_by(**query).first() if query_res: if is_locked_field_in(data, query_res): return "what the f*****g data?" query_res.update(data) return "update success" else: return "this value(name) is not in the database" else: return "f*****g query"
def data_create(data: dict, cls_str: str) -> str: """ Args: cls_str: data: Returns: """ if "s_name" not in data: return "loss necessary field" if not check_model_name(cls_str): return "invalid model" tmp_cls = importlib.import_module("application.models", cls_str) cls = getattr(tmp_cls, cls_str) query_res = cls.query.filter_by(s_name=data["s_name"]).first() if query_res: return "data with the name has existed" else: cls = cls() if is_locked_field_in(data, cls): return "what the f*****g data?" cls.add(data) return "add success"
def test_sql_checkers(): """ 测试数据库的各个约束函数 Returns: """ assert get_cls_primary_key("Herb") == "bin_hid" assert get_cls_primary_key("Prescription") == "bin_pid" assert ~is_locked_field_in({'i_status': 1}, Herb) assert check_model_name('Herb')
def data_query(post_data: dict): """ Args: post_data: Returns: """ type_ = post_data["type_"] draw = post_data.get("draw", 0) not_need_status_list = ["Role", "User"] if not check_model_name(type_): return { "draw": draw, "recordsTotal": 0, "recordsFiltered": 0, "data": [] } per_page = int(post_data.get("length", 10)) page = int(int(post_data.get("start", 0)) / per_page) + 1 search = post_data.get("search[value]", None) if per_page in [10, 25, 50, 100]: tmp_cls = importlib.import_module("application.models", type_) cls = getattr(tmp_cls, type_) # cls = eval(type_ + "()") if not search: res = query_data_from_models(cls, page, per_page, type_ not in not_need_status_list) else: res = cls.query.filter_by(s_name=search, i_status=1).paginate(int(page), int(per_page), error_out=False) ret = { "draw": int(draw), "recordsTotal": res.total, "recordsFiltered": res.total, "data": [foo.to_dict() for foo in res.items], } return ret else: return { "draw": draw, "recordsTotal": 0, "recordsFiltered": 0, "data": [] }
def get_model_paginated_data(model, current, **kwargs): if not check_model_name(model): return {"msg": "invalid model"} condition = {} mod = importlib.import_module("application.models", model) tmp = getattr(mod, model) if 's_name' in kwargs.keys(): condition = {'s_name': kwargs.pop('s_name')} page_res = query_data_from_models(tmp, current, kwargs.pop('pageSize'), condition=condition) result = [item.to_dict() for item in page_res.items] return {'data': result, 'success': True, 'total': page_res.total}
def delete_modal_data(model: str, id_: str): """ Args: model: id_: Returns: """ if not check_model_name(model): return {"msg": "invalid model"} mod = importlib.import_module("application.models", model) query_res = getattr(mod, model).query.get(id_) query_res.fake_delete() return {"msg": "success"}
def get_model_data_of_json_schema(model, id_): """ 获取json_schema数据, 用来渲染模型 Args: model: id_: Returns: """ if not check_model_name(model): return {"msg": "invalid model"} mod = importlib.import_module("application.models", model) field_comment_mapper = get_field_comment_mapper(getattr(mod, model)) if id_ == '$$9527$$': query_res = getattr(mod, model)() else: query_res = getattr(mod, model).query.get(id_) return query_res.json_schema(field_comment_mapper)
def data_query_particular(data: str, cls_str: str) -> dict: """ Args: data: id cls_str: type Returns: """ if not check_model_name(cls_str): return {} id_field = get_cls_primary_key(cls_str) query_data = {id_field: data, "i_status": 1} tmp_cls = importlib.import_module("application.models", cls_str) cls = getattr(tmp_cls, cls_str) query_res = cls.query.filter_by(**query_data).first() if query_res: return query_res.to_dict_particular()
def data_delete(data: str, cls_str: str) -> str: """ 根据id删 Args: cls_str: data: id Returns: """ if not check_model_name(cls_str): return "invalid model" data_for_del = {get_cls_primary_key(cls_str): data} tmp_cls = importlib.import_module("application.models", cls_str) cls = getattr(tmp_cls, cls_str) query_res = cls.query.filter_by(**data_for_del).first() if query_res and query_res.i_status == 1: query_res.fake_delete() return "delete success" else: return "this value(name) is not in the database"
def get_model_columns(model): if not check_model_name(model): return {"msg": "invalid model"} mod = importlib.import_module("application.models", model) return getattr(mod, model).normal_columns()