Esempio n. 1
0
    def dao_find_page(self, page, error_out=False):
        """
        分页条件查询
        :param page:
        :param error_out:
        :return:
        """
        # 条件查询
        filter = []

        if is_not_empty(self.type):
            filter.append(SysDict.type == self.type)
        if is_not_empty(self.description):
            filter.append(
                SysDict.description.like(
                    '%{description}%'.format(description=self.description)))

        pagination = self.query.filter(*filter).\
            order_by(SysDict.sort.asc(), SysDict.type.asc(), SysDict.create_date.asc()).\
            paginate(page=page.page, per_page=page.page_size, error_out=error_out)
        page.init_pagination(pagination)

        # 数据序列化 json
        data_dict, errors = SysDictSchema().dump(page.data, many=True)
        Assert.is_true(is_empty(errors), errors)
        page.data = data_dict
        return page, pagination
Esempio n. 2
0
 def dao_update(self, subtransactions=False, nested=False):
     with db.auto_commit_db(subtransactions=subtransactions, nested=nested) as s:
         if is_not_empty(current_user) and is_not_empty(current_user.id):
             self.update_by = current_user.id
         self.update_date = datetime.utcnow()
         # 将对象添加到session中,解决从缓存读取更新报 not in session 异常
         s.merge(self)
Esempio n. 3
0
    def dao_find_page(self, page, error_out=False):
        """
        分页条件查询
        :param page:
        :param error_out:
        :return:
        """
        # 条件查询
        filter = []

        if is_not_empty(self.img_data_id):
            filter.append(ImgDetailModel.img_data_id == self.img_data_id)
        if is_not_empty(self.is_handle):
            filter.append(ImgDetailModel.is_handle == self.is_handle)

        pagination = self.query.filter(*filter).\
            order_by(ImgDetailModel.create_date.desc()).\
            paginate(page=page.page, per_page=page.page_size, error_out=error_out)
        page.init_pagination(pagination)

        # 数据序列化 json
        img_detail_dict, errors = ImgDetailSchema(
            only=ImgDetailSchema().dump_only_page()).dump(page.data, many=True)
        Assert.is_true(is_empty(errors), errors)
        page.data = img_detail_dict
        return page, pagination
Esempio n. 4
0
 def filter_img_details(cls, img_details, filter_fields):
     """
     过滤图片明细指定字段
     :param list img_details: 图片明细字典列表
     :param list filter_fields: 待过滤字段列表
     :return:
     """
     if is_not_empty(img_details) and is_not_empty(filter_fields):
         for img_detail in img_details:
             for filter_field in filter_fields:
                 img_detail.pop(filter_field)
     return img_details
Esempio n. 5
0
def put_menu(menu):
    """
    更新系统菜单
    :param menu:
    :return:
    """
    to_do_menu = Menu().dao_get(menu.id)
    Assert.is_true(is_not_empty(to_do_menu), '无效的菜单ID:{0}'.format(menu.id))
    parent_menu = Menu().dao_get(menu.parent_id)
    Assert.is_true(is_not_empty(parent_menu),
                   '无效的父级菜单ID:{0}'.format(menu.parent_id))

    to_do_menu.dao_put(menu)
    return render_info(MyResponse(msg='更新成功'))
Esempio n. 6
0
    def _init(self):
        fee = self.problem.fee
        self._filter_pay_requests(self.problem.payRequests, fee)
        if is_empty(self._payRequests):
            raise RuntimeError("No pay request")

        # utxos = utxos - cost of using it in tx
        utxos = map(lambda v: v - fee * utils.inputSizeBytes,
                    self.problem.utxos)
        utxos = filter(lambda v: v > 0, utxos)
        utxos = list(utxos)
        if is_empty(utxos):
            raise RuntimeError("Insufficient funds")

        amount = utils.get_tx_amount(self._payRequests, fee)
        if amount > sum(utxos):
            raise RuntimeError("Insufficient funds")
        largeUtxos = list(filter(lambda v: v >= amount, utxos))
        if is_not_empty(largeUtxos):
            minUtxo = min(largeUtxos)
            change = minUtxo - amount
            solution = Solution(change, [minUtxo],
                                utils.get_solution_expense(fee, change))
            utxos = list(filter(lambda v: v < minUtxo, utxos))
        else:
            solution = Solver._build_solution_min_inputs_with_change(
                utxos, amount, fee)
        # solution - the best solution so far
        if solution is None or len(solution.inputs) > self._inputCountMax:
            raise RuntimeError("No solution")

        self.utxos = utxos
        self.amount = amount
        self.solution = solution
Esempio n. 7
0
def render_info(info, template=None, status=codes.ok, **kwargs):
    """
    客户端返回信息
    :param status: 请求状态码
    :param template: 模板路径
    :param info: 信息体(字典格式)
    :type kwargs: 模板参数对象
    :return:
    """
    if not isinstance(info, dict):
        info = info.__dict__

    if codes.ok != status and is_not_empty(info.get('code')):
        info['code'] = str(status)
    if request.headers.get(Headers.CONTENT_TYPE.value) and \
            request.headers.get(Headers.CONTENT_TYPE.value) == ContentType.JSON.value:
        # 判断请求响应类型是否为 JSON
        return jsonify(info), status
    if template:
        # 判断模板路径是否存在
        try:
            return render_template(template_name_or_list=template,
                                   info=info,
                                   **kwargs), status
        except TemplateNotFound:
            return render_template(
                template_name_or_list='errors/404.html',
                info=MyResponse(code=codes.server_error,
                                msg='模板路径不存在:{0}'.format(
                                    template)).__dict__), codes.not_found
    return jsonify(MyResponse(code=codes.not_allowed,
                              msg='不支持的请求头').__dict__), codes.not_allowed
Esempio n. 8
0
    def dao_init_file(self,
                      file_path,
                      id=None,
                      file_name=None,
                      subtransactions=False,
                      nested=False):
        """
        根据路径解析入库
        :param nested:
        :param subtransactions:
        :param file_name
        :param file_path: 文件路径
        :param id:
        :return:
        """
        with db.auto_commit_db(subtransactions=subtransactions,
                               nested=nested) as s:
            super().dao_create(id)
            # 计算文件 md5
            self.md5_id = FileUtil.get_md5_path(file_path)
            # noinspection PyAttributeOutsideInit
            self.file_path = file_path
            _, self.file_name, self.file_format = FileUtil.get_path_name_ext(
                file_path)

            if is_not_empty(file_name):
                self.file_name = file_name

            self.file_size = FileUtil.get_file_size(file_path)
            s.add(self)
Esempio n. 9
0
def init_log(project_name, lever, log_dir_name='logs'):
    """
    初始化日志
    :param lever: 日志级别
    :param project_name: 项目名,不可为空
    :param log_dir_name: 日志父目录名
    :return:
    """
    Assert.is_true(is_not_empty(project_name), '初始化日志。项目名不可为空.')
    log_file_name = 'logger'
    log_file_folder = os.path.abspath(
        os.path.join(
            os.path.dirname(__file__), os.pardir,
            os.pardir)) + os.sep + log_dir_name + os.sep + project_name
    FileUtil.creat_dirs(log_file_folder)
    log_file = log_file_folder + os.sep + log_file_name

    file_handler = MyLoggerHandler(log_file,
                                   when='D',
                                   encoding=Unicode.UTF_8.value,
                                   suffix='_%Y-%m-%d.log',
                                   extMatch=r'^_\d{4}-\d{2}-\d{2}.log$')
    file_handler.setLevel(lever)  # 日志输出级别

    fmt = '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s'
    formatter = logging.Formatter(fmt)
    file_handler.setFormatter(formatter)
    return file_handler
Esempio n. 10
0
def img_files_flow_page(page, img_detail):
    """
    图片文件流加载
    :param page:
    :param img_detail:
    :return:
    """
    page, pagination = img_detail.dao_find_page(page)

    flow_img_details = []
    for img_detail in pagination.items:
        url = url_for('file.file_download',
                      id=img_detail.file_id,
                      md5_id=img_detail.file_md5)
        if not img_detail.is_handle:
            alt = '待处理'
        elif is_not_empty(img_detail.img_type):
            alt = img_detail.img_type.type_explain
        else:
            alt = img_detail.err_msg
        flow_info = FlowInfo(url, url, alt, img_detail.file_id)
        flow_img_details.append(flow_info)

    flow_img_details_dict, errors = FlowInfoSchema().dump(flow_img_details,
                                                          many=True)
    Assert.is_true(is_empty(errors), errors)

    page_dict, page_errors = PageSchema().dump(page)
    Assert.is_true(is_empty(page_errors), page_errors)
    page_dict['data'] = flow_img_details_dict

    return render_info(page_dict)
Esempio n. 11
0
    def dao_create(self, id=None):
        self.id = id if id else IdGen.uuid()
        self.create_date = datetime.utcnow()
        self.update_date = datetime.utcnow()

        if is_empty(self.create_by):
            self.create_by = current_user.id if is_not_empty(current_user) else None
        self.update_by = self.create_by
Esempio n. 12
0
 def file_md5(self):
     """
     图片文件md5值
     :return:
     """
     from models.file import FileModel
     file_model = FileModel().dao_get(self.file_id)  # type: FileModel
     return file_model.md5_id if is_not_empty(file_model) else None
Esempio n. 13
0
 def file_path(self):
     """
     图片文件路径
     :return:
     """
     from models.file import FileModel
     file_model = FileModel().dao_get(self.file_id)  # type: FileModel
     return file_model.file_path if is_not_empty(file_model) else None
Esempio n. 14
0
def del_menu(menu):
    """
    删除系统菜单
    :param menu:
    :return:
    """
    menu = Menu().dao_get(menu.id)
    if is_not_empty(menu):
        menu.dao_delete()
    return render_info(MyResponse(msg='删除成功'))
Esempio n. 15
0
def add_menu(menu):
    """
    添加系统菜单
    :param menu:
    :return:
    """
    parent_menu = Menu().dao_get(menu.parent_id)
    Assert.is_true(is_not_empty(parent_menu),
                   '无效的父级菜单ID:{0}'.format(menu.parent_id))
    menu.dao_add()
    return render_info(MyResponse(msg='添加成功'))
Esempio n. 16
0
 def app_sys_code(self):
     """
     应用系统代号
     :return:
     """
     if self.app_sys_id:
         app_sys = SysDict().dao_get(self.app_sys_id)
         return app_sys.value if is_not_empty(app_sys) else '查无此来源系统.'
     elif self._app_sys_code:
         return self._app_sys_code
     return None
Esempio n. 17
0
def patch_img_detail_type(img_detail):
    """
    更新图片明细类型
    :param ImgDetailModel img_detail:
    :return:
    """
    img_type = ImgTypeModel().dao_get_by_code(img_detail.img_type_code)
    Assert.is_true(is_not_empty(img_type),
                   '无效的图片类型:{0}'.format(img_detail.img_type_code),
                   codes.unprocessable)

    img_detail_id = img_detail.id
    # 接口提交的更新者
    update_by = img_detail.update_by
    img_detail = ImgDetailModel().dao_get(
        img_detail_id)  # type: ImgDetailModel
    Assert.is_true(is_not_empty(img_detail),
                   '无查无此数据:{0}'.format(img_detail_id), codes.no_data)

    img_detail.update_by = update_by
    img_detail.dao_update_type(img_type.id)
    return render_info(MyResponse('更新成功'))
Esempio n. 18
0
def render_json(info, status=codes.ok):
    """
    客户端返回json信息
    :param status: 请求状态码
    :param info: 信息体(字典格式)
    :return:
    """
    if not isinstance(info, dict):
        info = info.__dict__

    if codes.ok != status and is_not_empty(info.get('code')):
        info['code'] = str(status)
    return jsonify(info), status
Esempio n. 19
0
 def __init__(self,
              api_key=None,
              secret_key=None,
              token=None,
              baidu_cloud=None):
     """
     百度云OCR识别
     :param api_key: 应用的API Key
     :param secret_key: 应用的Secret Key
     :param token:
     :param baidu_cloud:
     """
     if is_not_empty(token):
         self.token = token
     elif is_not_empty(api_key) and is_not_empty(secret_key):
         baidu_cloud = BaiduCloud(api_key, secret_key)
         baidu_cloud.init_token()
         self.token = baidu_cloud.token
     elif is_not_empty(baidu_cloud) and is_not_empty(baidu_cloud.token):
         self.token = baidu_cloud.token
     else:
         raise MyError('缺失鉴权 Token 参数.')
Esempio n. 20
0
def get_img_data(img_data, id):
    """
    图片流水查询
    :param img_data:
    :param id:
    :return:
    """
    # 查询图片流水
    img_data = ImgDataModel().dao_get(img_data.id)  # type: ImgDataModel
    Assert.is_true(is_not_empty(img_data), '查无此数据', codes.no_data)
    img_data_dict, errors = ImgDataSchema(
        only=ImgDataSchema().dump_only_get()).dump(img_data)
    Assert.is_true(is_empty(errors), errors)
    return render_info(MyResponse(msg='查询成功', imgData=img_data_dict))
Esempio n. 21
0
def files_handle_html(img_data, id):
    """
    文件处理页面
    :param img_data:
    :param id: 图片流水号
    :return:
    """
    source_files = img_data.dao_get_source_files(img_data.id)
    Assert.is_true(is_not_empty(source_files), '查无此数据', codes.no_data)

    for source_file in source_files:
        children = ImgDetailModel().dao_get_children(source_file.id)
        source_file.img_page = 0 if is_empty(children) else len(children)
    return render_template('ai/img/files_handle.html',
                           img_data_id=img_data.id,
                           source_files=source_files)
Esempio n. 22
0
def get_img(img_data, id):
    """
    根据 ID 流水号查询图片流水(附带路径)
    :param img_data:
    :param id:
    :return:
    """
    # 查询图片流水
    img_data = ImgDataModel.query.get(img_data.id)  # type: ImgDataModel
    Assert.is_true(is_not_empty(img_data), '查无此数据', codes.no_data)
    img_data_dict, errors = ImgDataSchema().dump(img_data)
    Assert.is_true(is_empty(errors), errors)

    # 过滤图片明细字典字段
    ImgDataSchema().filter_img_details(img_data_dict.get('imgDetails', []),
                                       ['fileData'])
    return render_info(MyResponse(msg='查询成功', imgData=img_data_dict))
Esempio n. 23
0
    def pre_load_data(self, data):
        """
        预处理数据
        :param data:
        :return:
        """
        issue_date = data.get('issue_date')
        if is_not_empty(issue_date):
            # noinspection PyBroadException
            try:
                date.str_to_time(issue_date)
            except:
                data['issue_date'] = None

        data['tax_exclusive_price'] = str_util.amount_formatting(data.get('tax_exclusive_price'), self.__AMOUNT)
        data['tax'] = str_util.amount_formatting(data.get('tax'), self.__AMOUNT)
        data['total'] = str_util.amount_formatting(data.get('total'), self.__AMOUNT)
        return data
Esempio n. 24
0
def login_validate(user):
    """
    登录验证
    :param user:
    :return:
    """
    if current_user.is_authenticated:
        return render_info(MyResponse(msg='登录成功'))

    real_user = User().dao_get_by_login_name(user.login_name)  # type: User
    Assert.is_true(is_not_empty(real_user),
                   assert_code=codes.login_fail,
                   assert_msg='账号未注册')
    if real_user.validate_password(user.password):
        # 密码验证成功
        login_user(real_user, False)
        return render_info(MyResponse(msg='登录成功'))
    return render_info(MyResponse(code=codes.login_fail, msg='用户名或密码不正确'))
Esempio n. 25
0
 def _filter_pay_requests(self, payRequests, fee):
     """
 check which pay requests can be included in transaction
 requirement: fee should not be large than transaction value
 """
     inputCountMin = 3  # minimum number of inputs to consider
     payRequests = sorted(payRequests,
                          key=lambda v: v["amount"],
                          reverse=True)
     while is_not_empty(payRequests):
         amount = sum(map(lambda v: v["amount"], payRequests))
         commission = fee * (utils.containerSizeBytes +
                             (len(payRequests) + 1) * utils.outputSizeBytes)
         inputCountMax = math.floor(
             (amount - commission) / utils.inputSizeBytes)
         if inputCountMax >= inputCountMin:
             break
         payRequests.pop()
     self._payRequests = payRequests
     self._inputCountMax = 0 if is_empty(payRequests) else inputCountMax
Esempio n. 26
0
def ocr(ocrFile):
    """
    机动车销售发票 OCR识别
    :return:
    """
    app_sys = get_app_sys(ocrFile.app_sys_code)
    Assert.is_true(is_not_empty(app_sys),
                   '无效的应用系统:{0}'.format(ocrFile.app_sys_code),
                   codes.unprocessable)
    # 通过外键添加
    ocrFile.app_sys_id = app_sys.id

    file_model = FileModel()
    file_model.dao_create()

    # 资料写入磁盘
    file_path = encodes.base64_to_file(ocrFile.file_data.file_base64,
                                       current_app.config.get('OCR_FILE_DIR'),
                                       file_model.id,
                                       ocrFile.file_data.file_format)

    # OCR识别
    token = SysData().dao_get_key(system.SysKey.HUAWEI_CLOUD_TOKEN.value).value
    mvsi_result = OCR(token=token)\
        .mvsi(image_bs64=img.ImgUtil.img_compress(path=file_path, threshold=5))
    Assert.is_true(
        is_empty(mvsi_result.error_code),
        'OCR FAILED: 【{0}】【{1}】'.format(mvsi_result.error_code,
                                        mvsi_result.error_msg))
    mvsi_result_json, errors = MvsiResultSchema(
        only=MvsiResultSchema().only_success()).dump(mvsi_result)
    Assert.is_true(is_empty(errors), errors)

    # 信息入库
    mvsi = Mvsi(**mvsi_result_json)
    mvsi.dao_add(ocrFile, file_model, file_path)

    # json 序列化
    mvsi_json, errors = MvsiSchema().dump(mvsi)
    Assert.is_true(is_empty(errors), errors)
    return render_info(MyResponse('OCR SUCCESS', results=mvsi_json))
Esempio n. 27
0
    def __call__(self, value):
        super().__call__(value)

        if not self.not_empty and utils.is_empty(value):
            raise ValidationError(self.message_not_empty)

        if utils.is_not_empty(self.encode_str):
            utils.Assert.is_true(self.encode_str in self.__encodes,
                                 '无效的编码{0}'.format(self.encode_str))

            encode_len = len(str(value).encode(self.encode_str))

            if self.min is not None and encode_len < self.min:
                message = self.message_min if self.max is None else self.message_all
                raise ValidationError(self._format_error(value, message))

            if self.max is not None and encode_len > self.max:
                message = self.message_max if self.min is None else self.message_all
                raise ValidationError(self._format_error(value, message))

        return value
Esempio n. 28
0
def add_img(img_data):
    """
    图片文件入库
    :param ImgDataModel img_data:
    :return:
    """
    img_data.dao_create()
    app_sys = get_app_sys(img_data.app_sys_code)
    Assert.is_true(is_not_empty(app_sys),
                   '无效的应用系统:{0}'.format(img_data.app_sys_code),
                   codes.unprocessable)

    # 通过外键添加
    img_data.app_sys_id = app_sys.id

    # 创建资料目录
    loan_dir = FileUtil.path_join(current_app.config.get('DATA_DIR'),
                                  img_data.id)

    # 信息入库
    handle_info = img_data.dao_add_info(loan_dir)
    return render_info(MyResponse(msg='接收资料成功', handle_info=handle_info))
Esempio n. 29
0
 def __init__(self, user_name=None, password=None, domain_name=None, token=None, huawei_cloud=None):
     """
     华为云OCR识别
     :param user_name: 用户名称
     :param password: 用户的登录密码
     :param domain_name: 用户所属的账号名称
     :param huawei_cloud:
     """
     if is_not_empty(token):
         self.token = token
     elif is_not_empty(user_name) and is_not_empty(password) and is_not_empty(domain_name):
         huawei_cloud = HuaweiCloud(user_name, password, domain_name)
         huawei_cloud.init_token()
         self.token = huawei_cloud.token
     elif is_not_empty(huawei_cloud) and is_not_empty(huawei_cloud.token):
         self.token = huawei_cloud.token
     else:
         raise MyError('缺失鉴权 Token 参数.')
Esempio n. 30
0
def img_source_files_flow_page(img_data, id):
    """
    源文件流加载
    :param img_data:
    :param id:
    :return:
    """
    source_files = img_data.dao_get_source_files(img_data.id)
    Assert.is_true(is_not_empty(source_files), '查无此数据', codes.no_data)

    file_datas = []
    for source_file in source_files:
        url = url_for('file.file_download',
                      id=source_file.id,
                      md5_id=source_file.md5_id)
        if source_file.file_format.upper() == FileFormat.PDF.value:
            url = url + '/' + source_file.id
        flow_info = FlowInfo(url, url, source_file.file_name, source_file.id,
                             source_file.file_format)
        file_datas.append(flow_info)

    file_datas_dict, errors = FlowInfoSchema().dump(file_datas, many=True)
    Assert.is_true(is_empty(errors), errors)
    return render_info(MyResponse('查询成功', data=file_datas_dict))