Exemplo n.º 1
0
    async def patch(self, element_id, *args, **kwargs):

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = PageElementForm.from_json(param)

        if form.validate():

            try:
                existed_element = await self.application.objects.get(PageElement, id=int(element_id))
                existed_element.element_name = form.element_name.data
                existed_element.operate_type = form.operate_type.data
                existed_element.owner_page = form.owner_page.data
                existed_element.desc = form.desc.data
                await self.application.objects.update(existed_element)
                return self.json(
                    JsonResponse(code=1, data={"id": element_id})
                )

            except PageElement.DoesNotExist:
                self.set_status(404)
                return self.json(JsonResponse(code=10009))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 2
0
    async def patch(self, db_id, *args, **kwargs):
        """
        更新数据配置
        :param db_id: 更新的配置数据库id
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = DBSettingForm.from_json(param)

        if form.validate():

            try:
                existed_db = await self.application.objects.get(DBSetting,
                                                                id=int(db_id))
                existed_db.name = form.name.data
                existed_db.db_user = form.db_user.data
                existed_db.db_type = form.db_type.data
                existed_db.db_host = form.db_host.data
                existed_db.db_password = form.db_password.data
                existed_db.db_port = form.db_port.data
                existed_db.desc = form.desc.data
                await self.application.objects.update(existed_db)
                return self.json(JsonResponse(code=1, data={"id": db_id}))

            except DBSetting.DoesNotExist:
                self.set_status(400)
                return self.json(JsonResponse(code=10009))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 3
0
    async def post(self, *args, **kwargs):
        """
        新增测试环境数据
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = TestEnvironmentForm.from_json(param)
        name = form.name.data

        if form.validate():
            try:
                await self.application.objects.get(TestEnvironment, name=name)
                return self.json(JsonResponse(code=10007, msg='这个测试环境已经被创建!'))

            except TestEnvironment.DoesNotExist:
                environment = await self.application.objects.create(
                    TestEnvironment,
                    name=name,
                    host_address=form.host_address.data,
                    desc=form.desc.data,
                    creator=self.current_user)
                return self.json(
                    JsonResponse(code=1,
                                 data={"EnvironmentId": environment.id}))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 4
0
    async def post(self, *args, **kwargs):
        """
        增加内置函数数据
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = FunctionGeneratorForm.from_json(param)
        name = form.name.data

        if form.validate():
            try:
                await self.application.objects.get(FunctionGenerator,
                                                   name=name)
                return self.json(JsonResponse(code=10007))

            except FunctionGenerator.DoesNotExist:
                function = await self.application.objects.create(
                    FunctionGenerator,
                    name=name,
                    desc=form.desc.data,
                    function=form.function.data,
                    creator=self.current_user)
                return self.json(
                    JsonResponse(code=1, data={"functionId": function.id}))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 5
0
    async def patch(self, project_id, *args, **kwargs):
        """
        更新项目数据
        :param project_id: 更新的项目id
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = ProjectForm.from_json(param)

        if form.validate():

            try:
                existed_project = await self.application.objects.get(
                    Project, id=int(project_id))
                existed_project.name = form.name.data
                existed_project.env = form.env.data
                existed_project.desc = form.desc.data
                await self.application.objects.update(existed_project)
                return self.json(JsonResponse(code=1, data={"id": project_id}))

            except Project.DoesNotExist:
                self.set_status(404)
                return self.json(JsonResponse(code=10009))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 6
0
    async def post(self, *args, **kwargs):

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = PageElementForm.from_json(param)

        if form.validate():

            try:
                await self.application.objects.get(
                    PageElement,
                    element_name=form.element_name.data
                )
                return self.json(
                    JsonResponse(code=10007))

            except PageElement.DoesNotExist:
                page_element = await self.application.objects.create(
                    PageElement,
                    element_name=form.element_name.data,
                    operate_type=form.operate_type.data,
                    owner_page=form.owner_page.data,
                    creator=self.current_user,
                    desc=form.desc.data
                )

                return self.json(
                    JsonResponse(code=1, data={'Id': page_element.id})
                )

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 7
0
    async def patch(self, function_id, *args, **kwargs):
        """
        更新内置函数数据
        :param function_id: 更新内置函数数据的id
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = FunctionGeneratorForm.from_json(param)

        if form.validate():

            try:
                existed_function = await self.application.objects.get(
                    FunctionGenerator, id=int(function_id))
                existed_function.name = form.name.data
                existed_function.function = form.function.data
                existed_function.desc = form.desc.data
                await self.application.objects.update(existed_function)
                return self.json(JsonResponse(code=1, data={"id":
                                                            function_id}))

            except FunctionGenerator.DoesNotExist:
                self.set_status(400)
                return self.json(JsonResponse(code=10009))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004))
Exemplo n.º 8
0
    async def post(self, *args, **kwargs):
        """
        新增项目数据
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = ProjectForm.from_json(param)
        name = form.name.data

        if form.validate():
            try:
                await self.application.objects.get(Project, name=name)
                return self.json(JsonResponse(code=10007))

            except Project.DoesNotExist:
                project = await self.application.objects.create(
                    Project,
                    name=name,
                    desc=form.desc.data,
                    env=form.env.data,
                    creator=self.current_user)
                return self.json(
                    JsonResponse(code=1, data={"projectId": project.id}))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 9
0
    async def post(self, *args, **kwargs):
        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = RegisterForm.from_json(param)
        account = form.account.data
        code = form.code.data
        password = form.password.data

        if form.validate():
            if not self.redis_conn.get(f'{account}_{code}'):
                self.set_status(400)
                return self.json(JsonResponse(code=10006))

            else:
                try:
                    await self.application.objects.get(User, account=account)
                    self.set_status(400)
                    return self.json(JsonResponse(code=10007))

                except User.DoesNotExist:
                    user = await self.application.objects.create(User, account=account, password=password)
                    return self.json(
                        JsonResponse(code=1, data={'id': user.id}))
        else:
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 10
0
    async def patch(self, environment_id, *args, **kwargs):
        """
        更新测试环境数据
        :param environment_id: 更新的测试环境id
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = TestEnvironmentForm.from_json(param)

        if form.validate():

            try:
                existed_environment = await self.application.objects.get(
                    TestEnvironment, id=int(environment_id))
                existed_environment.name = form.name.data
                existed_environment.host = form.host_address.data
                existed_environment.desc = form.desc.data
                await self.application.objects.update(existed_environment)
                return self.json(
                    JsonResponse(code=1, data={"id": environment_id}))

            except TestEnvironment.DoesNotExist:
                self.set_status(400)
                return self.json(JsonResponse(code=10009, msg="该环境不存在!"))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 11
0
    async def post(self, *args, **kwargs):
        """
        更新数据库配置数据
        """

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = DBSettingForm.from_json(param)
        name = form.name.data

        if form.validate():
            try:
                await self.application.objects.get(DBSetting, name=name)
                return self.json(JsonResponse(code=10007))

            except DBSetting.DoesNotExist:
                db = await self.application.objects.create(
                    DBSetting,
                    name=name,
                    db_type=form.db_type.data,
                    db_password=form.db_password.data,
                    db_user=form.db_user.data,
                    db_host=form.db_host.data,
                    db_port=form.db_port.data,
                    desc=form.desc.data,
                    creator=self.current_user)
                return self.json(JsonResponse(code=1, data={"DBId": db.id}))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 12
0
 async def delete(self, image_id, *args, **kwargs):
     try:
         public_params = await self.application.objects.get(
             ImageIdentifyText, id=int(image_id))
         await self.application.objects.delete(public_params)
         return self.json(JsonResponse(code=1, data={"id": image_id}))
     except ImageIdentifyText.DoesNotExist:
         self.set_status(400)
         return self.json(JsonResponse(code=10009))
Exemplo n.º 13
0
 async def delete(self, element_id, *args, **kwargs):
     try:
         element = await self.application.objects.get(PageElement, id=int(element_id))
         await self.application.objects.delete(element)
         return self.json(
             JsonResponse(code=1, data={"id": element_id})
         )
     except PageElement.DoesNotExist:
         self.set_status(400)
         return self.json(JsonResponse(code=10009))
Exemplo n.º 14
0
 async def delete(self, db_id, *args, **kwargs):
     """
     删除数据库配置
     :param db_id: 删除的配置数据库id
     """
     try:
         db = await self.application.objects.get(DBSetting, id=int(db_id))
         await self.application.objects.delete(db)
         return self.json(JsonResponse(code=1, data={"id": db_id}))
     except DBSetting.DoesNotExist:
         self.set_status(400)
         return self.json(JsonResponse(code=10009, msg="该数据库配置尚未创建!"))
Exemplo n.º 15
0
 async def delete(self, project_id, *args, **kwargs):
     """
     删除项目数据
     :param project_id: 删除的项目id
     """
     try:
         project = await self.application.objects.get(Project,
                                                      id=int(project_id))
         await self.application.objects.delete(project)
         return self.json(JsonResponse(code=1, data={"id": project_id}))
     except Project.DoesNotExist:
         self.set_status(400)
         return self.json(JsonResponse(code=10007))
Exemplo n.º 16
0
 async def delete(self, environment_id, *args, **kwargs):
     """
     删除测试环境数据
     :param environment_id: 删除环境的id
     """
     try:
         environment = await self.application.objects.get(
             TestEnvironment, id=int(environment_id))
         await self.application.objects.delete(environment)
         return self.json(JsonResponse(code=1, data={"id": environment_id}))
     except TestEnvironment.DoesNotExist:
         self.set_status(400)
         return self.json(JsonResponse(code=10009, msg="该环境尚未创建!"))
Exemplo n.º 17
0
 async def delete(self, function_id, *args, **kwargs):
     """
     删除内置函数数据
     :param function_id: 删除内置函数数据id
     """
     try:
         function = await self.application.objects.get(FunctionGenerator,
                                                       id=int(function_id))
         await self.application.objects.delete(function)
         return self.json(
             JsonResponse(code=1, data={"functionId": function_id}))
     except FunctionGenerator.DoesNotExist:
         self.set_status(400)
         return self.json(JsonResponse(code=10009, msg="该函数方法尚未创建!"))
Exemplo n.º 18
0
    async def post(self, *args, **kwargs):

        body = self.request.body_arguments
        body = format_arguments(body)
        form = ImageIdentifyTextForm(body)

        if form.validate():
            files_meta = self.request.files.get("image")

            try:
                await self.application.objects.get(
                    ImageIdentifyText, image_name=form.image_name.data)
                return self.json(JsonResponse(code=10007))

            except ImageIdentifyText.DoesNotExist:

                new_filename, content = None, None
                if files_meta:
                    # 完成图片保存并将值设置给对应的记录
                    # 通过aiofiles写文件
                    for meta in files_meta:
                        filename = meta["filename"]
                        new_filename = "{uuid}_{filename}".format(
                            uuid=shortuuid.uuid(), filename=filename)
                        filepath = path.join(settings.TORNADO_CONF.MEDIA_ROOT,
                                             new_filename)
                        async with aiofiles.open(filepath, 'wb') as f:
                            await f.write(meta['body'])

                        # 识别图片文字, 识别速度1~3秒左右
                        words_result = dict(await async_image_identifying_text(
                            filepath)).get('words_result')
                        content = ','.join([
                            word_result.get('words')
                            for word_result in words_result
                        ])

                image = await self.application.objects.create(
                    ImageIdentifyText,
                    image_name=form.image_name.data,
                    desc=form.desc.data,
                    address=new_filename,
                    creator=self.current_user,
                    content=content)

                return self.json(JsonResponse(code=1, data={"Id": image.id}))

        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 19
0
    def post(self, *args, **kwargs):

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = SmsCodeForm.from_json(param)
        account = form.account.data

        if form.validate():
            code = generate_code()
            self.redis_conn.set(f'{account}_{code}', 1, 10 * 60)
            return self.json(
                JsonResponse(code=1, data={'account': account, 'VerCode': code}))
        else:
            self.set_status(404)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 20
0
    async def get(self, *args, **kwargs):

        ret_data = []
        page_element_query = PageElement.extend()

        name = self.get_argument('name', None)
        if name is not None:
            page_element_query = page_element_query.filter(
                page_element_query.element_name == name
            )

        page = self.get_argument('page', None)
        if page is not None:
            page_element_query = page_element_query.filter(
                page_element_query.owner_page == page
            )

        page_element_query = page_element_query.order_by(-PageElement.add_time)
        page_elements = await self.application.objects.execute(page_element_query)

        for page_element in page_elements:
            page_element_dict = model_to_dict(page_element)
            ret_data.append(page_element_dict)

        return self.json(JsonResponse(code=1, data=ret_data))
Exemplo n.º 21
0
    async def get(self, *args, **kwargs):
        ret_data = []
        images_query = ImageIdentifyText.extend()

        name = self.get_argument('name', None)
        if name is not None:
            images_query = images_query.filter(
                ImageIdentifyText.image_name == name)

        images_query = images_query.order_by(-ImageIdentifyText.add_time)
        images = await self.application.objects.execute(images_query)

        for image in images:
            image_dict = model_to_dict(image)
            image_dict['address'] = "{}/media/{}/".format(
                settings.TORNADO_CONF.SITE_URL, image.address)

            # 根据图片中的关键字进行检索
            keywords = self.get_argument('keywords', None)
            if keywords is not None:

                if image.content.__contains__(keywords):
                    ret_data.append(image_dict)
            else:
                ret_data.append(image_dict)

        return self.json(JsonResponse(code=1, data=ret_data))
Exemplo n.º 22
0
    def post(self, *args, **kwargs):
        """
        内置函数-自定义python解释器, 并提供代码运行环境用于debug
        """
        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = FunctionDebugForm.from_json(param)

        if form.validate():
            code = form.function.data
            result = python_running_env(code)

            return self.json(JsonResponse(code=1, data={'RunningRes': result}))
        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 23
0
    async def get(self, *args, **kwargs):
        """
        获取测试环境列表数据
        """
        ret_data = []
        environment_query = TestEnvironment.extend()

        # 根据环境名过滤
        name = self.get_argument('name', None)
        if name is not None:
            environment_query = environment_query.filter(
                TestEnvironment.name == name)

        host = self.get_argument('router', None)
        if host is not None:
            environment_query = environment_query.filter(
                TestEnvironment.host_address == host)

        # 默认排序规则
        environment_query = environment_query.order_by(
            TestEnvironment.add_time.desc())

        environments = await self.application.objects.execute(environment_query
                                                              )
        for environment in environments:
            environment_dict = model_to_dict(environment)
            ret_data.append(environment_dict)

        return self.json(JsonResponse(code=1, data=ret_data))
Exemplo n.º 24
0
    async def get(self, *args, **kwargs):
        """
        项目数据查询
        """
        ret_data = []
        project_query = Project.extend()

        name = self.get_argument('name', None)
        if name is not None:
            project_query = project_query.filter(Project.name == name)

        env = self.get_argument('env', None)
        if env is not None:
            project_query = project_query.filter(TestEnvironment.name == env)

        host = self.get_argument('host', None)
        if host is not None:
            project_query = project_query.filter(
                TestEnvironment.host_address == host)

        project_query = project_query.order_by(-Project.add_time)
        projects = await self.application.objects.execute(project_query)
        for project in projects:
            project_dict = model_to_dict(project)
            ret_data.append(project_dict)

        return self.json(JsonResponse(code=1, data=ret_data))
Exemplo n.º 25
0
    async def get(self, *args, **kwargs):
        """
        获取数据库配置列表数据
        """
        ret_data = []
        db_query = DBSetting.extend()

        name = self.get_argument('name', None)

        if name is not None:
            db_query = db_query.filter(DBSetting.name == name)

        db_type = self.get_argument('type', None)
        if db_type is not None:
            db_query = db_query.filter(DBSetting.db_type == db_type)

        # 默认排序规则
        db_query = db_query.order_by(DBSetting.add_time.desc())

        dbs = await self.application.objects.execute(db_query)
        for db in dbs:
            db_dict = model_to_dict(db)
            ret_data.append(db_dict)

        return self.json(JsonResponse(code=1, data=ret_data))
Exemplo n.º 26
0
    async def post(self, *args, **kwargs):
        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = LoginForm.from_json(param)
        account = form.account.data
        password = form.password.data

        if form.validate():

            try:
                user = await self.application.objects.get(User, account=account)
                if not user.password.check_password(password):
                    return self.json(JsonResponse(code=10008))

                else:
                    payload = {
                        "id": user.id,
                        "nick_name": user.nick_name,
                        "exp": datetime.utcnow()
                    }
                    token = jwt.encode(
                        payload,
                        settings.TORNADO_CONF.secret_key, algorithm='HS256'
                    )

                    nick_name = user.nick_name if user.nick_name is not None else user.account
                    return self.json(
                        JsonResponse(
                            code=1,
                            data={
                                'id': user.id,
                                'nick_name': nick_name,
                                'token': token.decode('utf-8')})
                    )

            except User.DoesNotExist:
                self.set_status(400)
                return self.json(JsonResponse(code=10009))
        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10008))
Exemplo n.º 27
0
    async def post(self, *args, **kwargs):

        param = self.request.body.decode('utf-8')
        param = json.loads(param)
        form = PasswordForm.from_json(param)

        if form.validate():
            # 检查旧密码
            if not self.current_user.password.check_password(form.oldPassword.data):
                self.set_status(400)
                self.json(JsonResponse(code=10008))
            else:
                if form.newPassword.data != form.checkPassword.data:
                    self.set_status(400)
                    self.json(JsonResponse(code=10008))
                else:
                    self.current_user.password = form.newPassword.data
                    await self.application.objects.update(self.current_user)
        else:
            self.set_status(400)
            return self.json(JsonResponse(code=10004, msg=form.errors))
Exemplo n.º 28
0
    async def get(self, *args, **kwargs):
        """
        获取内置函数列表
        """
        ret_data = []
        function_query = FunctionGenerator.extend()

        name = self.get_argument('name', None)
        if name is not None:
            function_query = function_query.filter(
                FunctionGenerator.name == name)

        function_query = function_query.order_by(
            FunctionGenerator.add_time.desc())

        functions = await self.application.objects.execute(function_query)
        for func in functions:
            func_dict = model_to_dict(func)
            ret_data.append(func_dict)

        return self.json(JsonResponse(code=1, data=ret_data))
Exemplo n.º 29
0
 async def get(self, *args, **kwargs):
     keyword = [
         keyword.title().translate(str.maketrans('_', ' '))
         for keyword in SeleniumLibrary().get_keyword_names()
     ]
     return self.json(JsonResponse(code=1, data={'keyword': keyword}))