def test_get_env(self):
        ''' 测试 get_env 方法 '''
        doc = DocxTemplate(misc.file_open('sell/template/sell.order.docx').name)
        data = self.env['sell.order'].search([('name', '=', 'SO00001')])

        ctx={'obj':data,'tpl':doc}

        jinja_env = report_helper.get_env()
        doc.render(ctx,jinja_env)
Exemplo n.º 2
0
    def create_source_docx_partner(self,
                                   cr,
                                   uid,
                                   ids,
                                   report,
                                   records,
                                   init_pay,
                                   context=None):
        # 2016-11-2 支持了图片
        # 1.导入依赖,python3语法
        # from . import report_helper
        # 2. 需要添加一个"tpl"属性获得模版对象
        tempname = tempfile.mkdtemp()
        temp_out_file = self.generate_temp_file(tempname)
        doc = DocxTemplate(misc.file_open(report.template_file).name)

        env = api.Environment(cr, uid, context)
        partner = env.get('partner').search([('id', '=',
                                              context.get('partner_id'))])
        simple_dict = {
            'partner_name': partner.name,
            'from_date': context.get('from_date'),
            'to_date': context.get('to_date'),
            'report_line': [],
            'init_pay': {},
            'final_pay': {}
        }
        if not records:
            if init_pay:
                simple_dict['init_pay'] = init_pay
                simple_dict['final_pay'] = init_pay
            doc.render({
                'obj': simple_dict,
                'tpl': doc
            }, report_helper.get_env())
            doc.save(temp_out_file)

            report_stream = ''
            with open(temp_out_file, 'rb') as input_stream:
                report_stream = input_stream.read()

            os.remove(temp_out_file)
            return report_stream, report.output_type

        data = DataModelProxy(records)
        for p_value in data:
            simple_dict['report_line'].append({
                'date':
                p_value.date,
                'name':
                p_value.name,
                'note':
                p_value.note,
                'amount':
                p_value.amount,
                'pay_amount':
                p_value.pay_amount,
                'discount_money':
                p_value.discount_money,
                'balance_amount':
                p_value.balance_amount
            })
        if data:
            simple_dict['init_pay'] = data[0].balance_amount - data[
                0].amount + data[0].pay_amount - data[0].discount_money
            simple_dict['final_pay'] = data[-1].balance_amount

        doc.render({'obj': simple_dict, 'tpl': doc}, report_helper.get_env())
        doc.save(temp_out_file)

        if report.output_type == 'pdf':
            temp_file = self.render_to_pdf(temp_out_file)
        else:
            temp_file = temp_out_file

        report_stream = ''
        with open(temp_file, 'rb') as input_stream:
            report_stream = input_stream.read()

        os.remove(temp_file)
        return report_stream, report.output_type
    def create_source_docx_partner(self, cr, uid, ids, report, records, init_pay, context=None):
        # 2016-11-2 支持了图片
        # 1.导入依赖,python3语法
        # from . import report_helper
        # 2. 需要添加一个"tpl"属性获得模版对象
        tempname = tempfile.mkdtemp()
        temp_out_file = self.generate_temp_file(tempname)
        doc = DocxTemplate(misc.file_open(report.template_file).name)

        env = api.Environment(cr, uid, context)
        partner = env.get('partner').search([('id', '=', context.get('partner_id'))])
        simple_dict = {'partner_name': partner.name,
                       'from_date': context.get('from_date'),
                       'to_date': context.get('to_date'),
                       'report_line': [],
                       'init_pay': {},
                       'final_pay': {}}
        if not records:
            if init_pay:
                simple_dict['init_pay'] = init_pay
                simple_dict['final_pay'] = init_pay
            doc.render({'obj': simple_dict, 'tpl': doc}, report_helper.get_env())
            doc.save(temp_out_file)

            report_stream = ''
            with open(temp_out_file, 'rb') as input_stream:
                report_stream = input_stream.read()

            os.remove(temp_out_file)
            return report_stream, report.output_type

        data = DataModelProxy(records)
        for p_value in data:
            simple_dict['report_line'].append({
                'date': p_value.date,
                'name': p_value.name,
                'note': p_value.note,
                'amount': p_value.amount,
                'pay_amount': p_value.pay_amount,
                'discount_money': p_value.discount_money,
                'balance_amount': p_value.balance_amount
            })
        if data:
            simple_dict['init_pay'] = data[0].balance_amount - data[0].amount + data[0].pay_amount - data[
                0].discount_money
            simple_dict['final_pay'] = data[-1].balance_amount

        doc.render({'obj': simple_dict, 'tpl': doc}, report_helper.get_env())
        doc.save(temp_out_file)

        if report.output_type == 'pdf':
            temp_file = self.render_to_pdf(temp_out_file)
        else:
            temp_file = temp_out_file

        report_stream = ''
        with open(temp_file, 'rb') as input_stream:
            report_stream = input_stream.read()

        os.remove(temp_file)
        return report_stream, report.output_type