Ejemplo n.º 1
0
    def prepare(self):
        if self.request.method == 'OPTIONS':
            return self.options()

        # 非登录接口Token验证
        if self.request.path not in ["/api/user/login"]:
            token = self.request.headers.get("X-Token", None)
            try:
                assert token is not None
                data = UserInfoService().getUserIdByTonken(token)
                if data is not None:
                    #: 后期权限扩展处 :#
                    self.userId = data['userId']
                    self.token = token
                else:
                    raise CustomException(code=1001)
            except AssertionError as e:
                raise CustomException(code=1002)

        try:
            if self.request.body:
                self.post_arguments = json.loads(
                    self.request.body.decode('utf-8'))
        except Exception as ex:
            return self.json(Result(code=300, msg=str(ex)))
Ejemplo n.º 2
0
    def get(self, *args, **kwargs):
        """ 获取模式 """
        dbname = self.get_argument("dbname", None)
        table = self.get_argument("table", None)
        superclass = self.get_argument("superclass", None)
        prefix = self.get_argument("prefix", "yes")
        if not dbname:
            raise CustomException(code=10001, desc="请选择要操作的数据库")
        if not table:
            raise CustomException(code=10001, desc="请选择要操作的数据表")

        return ToolService().get_schema(dbname, table, superclass, prefix)
Ejemplo n.º 3
0
    def get(self, *args, **kwargs):
        """ 所有表 """
        dbname = self.get_argument('dbname', None)
        if not dbname:
            raise CustomException(code=10001, desc="请选择要操作的数据库")

        return ToolService().get_all_tables(dbname)
Ejemplo n.º 4
0
    def _filter_none_value(doc):
        if not isinstance(doc, dict):
            raise CustomException(code=10001, desc="doc should be a dict")

        for k, v in list(doc.items()):
            if v is None:
                del doc[k]

        return doc
Ejemplo n.º 5
0
    def _validate(cls, doc, required=True):
        if cls._fields is None:
            return

        for k, v in doc.items():
            if '.' in k:
                continue

            if k not in cls._fields:
                raise CustomException(code=10001, desc='unexpected field {}'.format(k))

            type_, _ = cls._fields[k]
            if type_ is not None and not isinstance(v, type_):
                raise CustomException(code=10001, desc='field {} should be a {}'.format(k, type_))

        if required:
            fields = [k for k, v in cls._fields.items() if v[1]]
            for v in fields:
                if v not in doc:
                    raise CustomException(code=10001, desc='field {} is required'.format(v))
Ejemplo n.º 6
0
    def prepare(self):
        if self.request.method == 'OPTIONS':
            return self.options()

        if self.request.path not in ["/user/login"]:
            token = self.request.headers.get("X-Token", None)
            try:
                assert token is not None
                data = UserService().get_user_by_token(token)
                if data is not None:
                    self.uid = data['id']
                    self.token = token
                else:
                    raise CustomException(code=1001)
            except AssertionError as ae:
                raise CustomException(code=1002)

        try:
            self.post_arguments = json.loads(self.request.body.decode('utf-8'))
        except Exception as ex:
            pass
Ejemplo n.º 7
0
 def _deco(self, *args, **kwargs):
     if name is not None and hasattr(self, name):
         session = getattr(self, name)
         if isinstance(session, scoped_session):
             try:
                 res = func(self, *args, **kwargs)
                 session.commit()
                 return res
             except CustomException as ce:
                 session.rollback()
                 raise CustomException(code=ce.code, desc=ce.msg)
             except Exception as e:
                 session.rollback()
                 raise e
     else:
         print("找不到session对象,无法开启自动事务")
Ejemplo n.º 8
0
 def _deco(self, *args, **kwargs):
     raise CustomException(
         code=1003, desc="{path} 已经废弃".format(path=self.request.path))