def do_filter(self, **kwargs): session = kwargs.get("session", None) if session is None: return None pk = kwargs.get("id", None) try: filter_obj = session.query(self._model) if pk is not None: return filter_obj.filter_by(id=pk) value = {k: self._clean_value(kwargs[k]) for k in self._filter_value(kwargs)} filter_obj = filter_obj.filter_by(**value) kv = kwargs.get("like", None) if kv is not None: k, v = kv.split("^") filter_obj = filter_obj.filter(getattr(self._model, k).like("%%%s%%" % v)) id_list = kwargs.get("id_list", None) if id_list is not None: filter_obj = filter_obj.filter(getattr(self._model, "id").in_(id_list)) filter_condition = kwargs.get('filter_condition', None) if filter_condition is not None: filter_obj = filter_obj.filter(filter_condition) return filter_obj except Exception as e: logger.error(u"查询%s出错. %s, %s" % (self._model.__tablename__, e, kwargs)) return None
def _do_put(self): try: if self._controller_obj.update_item(**self._input): self._db_session.commit() return True else: self._db_session.rollback() return False except Exception as e: logger.error("put method error: %s" % e) return False
def get_model_pk(self): """ 获取model主键 :return: Column """ try: ins = inspect(self._model) return ins.primary_key[0] except Exception as e: logger.error("获取主键失败e" % e) return None
def _do_get(self, disable_output=None, transform_json=True): try: data, total = self._controller_obj.filter_item(**self._input) self._data["total"] = total if transform_json: self._data["list"] = utility.to_obj(data, without_fields=disable_output) else: self._data["list"] = data return True except Exception as e: traceback.print_exc() logger.error("get method error: %s" % e) return False
def _do_post(self): try: if self._controller_obj.new_item(**self._input): self._db_session.commit() return True else: self._db_session.rollback() logger.error("post method error") return False except Exception as e: self._ret, self._msg = error_msg.SERVER_ERROR logger.error("post method error: %s" % e) return False
def delete_item(self, **kwargs): session = kwargs.get("session", None) if session is None: return False try: filter_obj = self.do_filter(**kwargs) if filter_obj: filter_obj.delete() return True else: return False except Exception as e: logger.error(u"删除%s出错. %s, %s" % (self._model.__tablename__, e, kwargs)) return False
def filter_item(self, **kwargs): session = kwargs.get("session", None) if session is None: return None, None start = int(kwargs.get("start", -1)) end = int(kwargs.get("end", -1)) compare_key = kwargs.get("compare_key", None) gte = kwargs.get("gte", None) gt = kwargs.get("gt", None) lte = kwargs.get("lte", None) lt = kwargs.get("lt", None) pk = kwargs.get("id", None) try: filter_obj = self.do_filter(**kwargs) if not filter_obj: return None, None if compare_key and hasattr(self._model, compare_key): if gte: filter_obj = filter_obj.filter(getattr(self._model, compare_key) >= gte) if gt: filter_obj = filter_obj.filter(getattr(self._model, compare_key) > gt) if lte: filter_obj = filter_obj.filter(getattr(self._model, compare_key) <= lte) if lt: filter_obj = filter_obj.filter(getattr(self._model, compare_key) < lt) if pk: return filter_obj.first(), 1 order_by = kwargs.get("order_by", None) order_method = kwargs.get("order_method", None) if order_by is None: filter_obj = filter_obj.order_by(desc("id")) else: if order_method == "desc": filter_obj = filter_obj.order_by(desc(order_by)) else: filter_obj = filter_obj.order_by(order_by) if start == -1 and end == -1: data = filter_obj.all() return data, len(data) else: pk = self.get_model_pk() if pk is None: return None, None return filter_obj[start:end], filter_obj.with_entities(func.count(pk)).scalar() except Exception as e: logger.error(u"查询%s出错. %s, %s" % (self._model.__tablename__, e, kwargs)) return None, None
def new_item(self, **kwargs): session = kwargs.get("session", None) if session is None: return False try: value = {k: self._clean_value(kwargs[k]) for k in self._filter_value(kwargs)} if kwargs.get("primary_key"): value["id"] = kwargs["primary_key"] if 'password' in value: # sha1加密密码字符 value['password'] = self.__encryption(value['password']) new_obj = self._model(**value) session.add(new_obj) return new_obj except Exception as e: logger.error(u"新建%s出错. %s, %s" % (self._model.__tablename__, e, kwargs)) return False
def update_item(self, **kwargs): session = kwargs.get("session", None) if session is None: return False try: value = {k: self._clean_value(kwargs[k]) for k in self._filter_value(kwargs)} if 'password' in value: # sha1加密密码字符 value['password'] = self.__encryption(value['password']) filter_obj = self.do_filter(**kwargs) if filter_obj: filter_obj.update(value) return True else: return False except Exception as e: logger.error(u"修改%s出错. %s, %s" % (self._model.__tablename__, e, kwargs)) return False
def dispatch(self, fun_name=None): self.get_input_arguments() self._input["session"] = self._db_session try: if fun_name: func = getattr(self, fun_name, None) if func: return func() else: return self._response(error_msg.PARAMS_ERROR) if self._request.method == "GET": action = self._input.get("action", None) if action: func = getattr(self, "get_action_%s" % action, None) if func: return func() else: return self._response(error_msg.PARAMS_ERROR) else: return self.get() elif self._request.method == "POST": action = self._input.get("action", None) if action: func = getattr(self, "post_action_%s" % action, None) if func: return func() else: return self._response(error_msg.PARAMS_ERROR) else: return self.post() elif self._request.method == "PUT": return self.put() elif self._request.method == "DELETE": return self.delete() except Exception as e: traceback.print_exc() logger.error("dispatch method error: %s" % e) return self._response(error_msg.PARAMS_ERROR)
def get(self): logger.error("测试") do_something.delay() return self._response()