Пример #1
0
    def wrapper(*args, **kwargs):
        """Function that wrapped."""
        session = SESS()
        try:
            res = function(sess=session, *args, **kwargs)
        except exc.IntegrityError as exception:
            res = dict(status=1, msg=str(exception.orig))
        except exc.ProgrammingError as exception:
            res = dict(status=2, msg=str(exception.orig))
        except exc.ResourceClosedError as exception:
            res = dict(status=3, msg=str(exception))
        except exc.OperationalError as exception:
            res = dict(status=4, msg=str(exception.orig))
        except UnicodeEncodeError as exception:
            res = dict(status=5, msg=str(exception))
        except:
            dump_error('my exception\n', traceback.format_exc())
            res = dict(status=255, msg='Unknown Error.')
        finally:
            session.close()

        if res and 'status' in res:
            return res
        else:
            return dict(status=0, data=res)
Пример #2
0
    def wait(self, func, worker_mode=True, args=None, kwargs=None):
        """Method to waiting celery result."""
        if worker_mode:
            async_task = func.apply_async(args=args, kwargs=kwargs)

            while True:
                if async_task.status in ['PENDING', 'PROGRESS']:
                    yield gen.sleep(O_O.celery.sleep_time)
                elif async_task.status in ['SUCCESS', 'FAILURE']:
                    break
                else:
                    print('\n\nUnknown status:\n', async_task.status, '\n\n\n')
                    break

            if async_task.status != 'SUCCESS':
                dump_error(f'Task Failed: {func.name}[{async_task.task_id}]',
                           f'    {str(async_task.result)}')
                result = dict(status=1, data=async_task.result)
            else:
                result = async_task.result

            if result.get('status'):
                self.fail(-1, result)
            else:
                return result
        else:
            return func(*args, **kwargs)
Пример #3
0
    def check_auth(self, **kwargs):
        """Check user status."""
        user_id = self.get_current_user()
        params = self.get_parameters()

        def clean_and_fail(code):
            self.set_current_user('')
            self.set_parameters({})
            self.fail(code)

        if not user_id or not params:
            clean_and_fail(3005)

        if user_id != params.user_id:
            clean_and_fail(3006)

        ac_code = yield self.get_session_code()
        if ac_code is not '' and params.ac_code != ac_code:
            clean_and_fail(3007)

        for key in kwargs:
            if params[key] != kwargs[key]:
                dump_error(f'Auth Key Error: {key}')
                self.fail(4003)

        self.set_current_user(self.get_current_user())
        self.set_parameters(self.get_parameters())
        return params
Пример #4
0
    def parse_json_arguments(self, *enforced_keys, **optional_keys):
        """Parse JSON argument like `get_argument`."""
        if O_O.debug:
            dump_in(f'Input: {self.request.method} {self.request.path}',
                    self.request.body.decode()[:500])

        try:
            req = json.loads(self.request.body.decode('utf-8'))
        except json.JSONDecodeError as exception:
            dump_error(self.request.body.decode())
            raise ParseJSONError(exception.doc)

        if not isinstance(req, dict):
            dump_error(self.request.body.decode())
            raise ParseJSONError('Req should be a dictonary.')

        for key in enforced_keys:
            if key not in req:
                dump_error(self.request.body.decode())
                raise MissingArgumentError(key)

        req['remote_ip'] = self.request.remote_ip
        req['request_time'] = int(time.time())

        return Arguments(req)