在odoo v8中打印qweb-report时遇到问题

时间:2016-09-09 03:21:00

标签: report odoo odoo-8 qweb

下面是我用来从向导打印报告的函数的代码。

class manager_monthly_salary_report_wizard(osv.osv_memory):
    _name = "manager.monthly.salary.report.wizard"

    _columns = {
        'month': fields.integer("Month"),
        'year': fields.integer("Year"),
    }

    def check_report(self, cr, uid, ids, context=None):
        print "context -> ", context
        print "ids -> ", ids

        if context is None:
            context = {}

        self_read = self.read(cr, uid, ids)[0]

        if (self_read['month'] >= 12 or self_read['month'] <= 0) and len(str(self_read['year'])) != 4:
            raise osv.except_osv(_("Warning"),
                                 _("You have invalid month / year ! please select correct one ... "))
        else:
            print "working normally ... "

        if self_read['month'] in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
            month_year = str("0" + str(self_read['month']) or self_read['month'])
        elif self_read['month'] in [10, 11, 12]:
            month_year = str(self_read['month'])

        month_year = month_year + " - " + str(self_read['year'])
        print "month_year  ", month_year

        departments = []
        office_staff = []

        for deprt in first_level_department_brws:
            office_staff_ids = empl_obj.search(cr, uid, [('department_id', '=', deprt.id), '|', ('manager', '=', True), ('office_staff', '=', True)])
            office_staff_brw = empl_obj.browse(cr, uid, office_staff_ids)
            print " office_staff_brw ", office_staff_brw

            department = {
                'name': deprt.name,
                'office_staff': office_staff
            }
            for dep_employee in office_staff_brw:

                office_staff.append({
                    'name': dep_employee.name,...
                })
                department['office_staff'] = office_staff
                print "office_staff -> ", office_staff

            office_staff = []
            departments.append(department)
            print "Departments-> ", office_staff
            department = {}
            print "\n"

        data = {
            'ids': context.get('active_ids', []),
            'model': context.get('active_model', 'ir.ui.menu'),
            'form': departments
        }
        print "data -> ", data
        print "__end__\n\n"

        return self.pool['report'].get_action(cr, uid, [], 'custom_module.my_report', data=data, context=context)

报告xml文件看起来像..

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="my_report">
            <t t-call="report.html_container">
                <div class="page">
                    <!--header -->
                    <!--<t t-call="report.internal_layout">-->
                    <!--content-->

                    <div class="row">
                        <span >"====="</span>
                        <span t-esc="Departments"/>
                        <t t-esc="data"/>

                        <t t-set="model" t-value="data['model']"/>
                        <t t-set="data" t-value="data['form']"/>
                        <t t-esc="data"/>
                        <span >"++++++"</span>
                    </div>
                </div>
            </t>
        </template>
    </data>
</openerp>

如果数据['表单']中的departmens是一种字典,我可以打印,但现在我已更改为列表,并且在打印报告时我遇到错误

Traceback (most recent call last):
  File "/home/demo/project/odoo/odoo_8/addons/report/controllers/main.py", line 120, in report_download
    response = self.report_routes(reportname, converter='pdf', **dict(data))
  File "/home/demo/project/odoo/odoo_8/openerp/http.py", line 410, in response_wrap
    response = f(*args, **kw)
  File "/home/demo/project/odoo/odoo_8/addons/report/controllers/main.py", line 65, in report_routes
    pdf = report_obj.get_pdf(cr, uid, docids, reportname, data=options_data, context=context)
  File "/home/demo/project/odoo/odoo_8/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/demo/project/odoo/odoo_8/addons/report/models/report.py", line 192, in get_pdf
    html = self.get_html(cr, uid, ids, report_name, data=data, context=context)
  File "/home/demo/project/odoo/odoo_8/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/demo/project/odoo/odoo_8/addons/report/models/report.py", line 167, in get_html
    return particularreport_obj.render_html(cr, uid, ids, data=data, context=context)
  File "/home/demo/project/odoo/odoo_8/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/demo/project/odoo/odoo_8/addons/report/models/abstract_report.py", line 35, in render_html
    if data and data.get('form', {}).get('landscape'):
AttributeError: 'list' object has no attribute 'get'

在py代码中更新

我刚刚改为

    data = {
        'ids': context.get('active_ids', []),
        'model': context.get('active_model', 'ir.ui.menu'),
        'form': {'departments': departments}
    }

并且报告正在打印....问题是,如果我使用

,他们无法直接打印报告
    data = {
        'ids': context.get('active_ids', []),
        'model': context.get('active_model', 'ir.ui.menu'),
        'form': departments
    }

列出而不是data['form']中的词典......?

1 个答案:

答案 0 :(得分:0)

我不确定我是否认为您使用数据存在冲突[&#39;表格&#39;]。在abstract_report.py文件中提到了错误中引用的文件。如果您按照Odoo Reports的文档进行操作,他们会像这样描述覆盖render_html函数。不要在qweb中使用数据[&#39;形式&#39;]而是直接使用部门作为变量应该可用。

class MyReport(models.AbstractModel):
    _name = 'report.custom_module.my_report'

    @api.multi
    def render_html(self, data=None):
        departments = []
        office_staff = []

        for deprt in first_level_department_brws:
            office_staff_ids = empl_obj.search(cr, uid, [('department_id', '=', deprt.id), '|', ('manager', '=', True), ('office_staff', '=', True)])
            office_staff_brw = empl_obj.browse(cr, uid, office_staff_ids)
            print " office_staff_brw ", office_staff_brw

            department = {
                'name': deprt.name,
                'office_staff': office_staff
            }

            for dep_employee in office_staff_brw:
                office_staff.append({
                    'name': dep_employee.name, ...
                })

            department['office_staff'] = office_staff
            print "office_staff -> ", office_staff

            office_staff = []
            departments.append(department)
            print "Departments-> ", office_staff
            department = {}

        report = self.env['report']._get_report_from_name('custom_addon.my_report')
        docs = self.env['custom_addon.model_name'].browse(context.get('active_ids', []))

        docargs = {
            'doc_model': report.model,
            'docs': docs,
            'departments': departments
        }
        return report_obj.render('custom_addon.my_report', docargs)

尝试定义另一个qweb varable var或除数据之外的任何内容。有几个数据引用,我想知道是否存在冲突。您的原始方法看起来并不完全错误,但使用odoo已经使用的变量名称(如数据和表单)可能会产生冲突。如果您查看/addons/report/abstract_report.py,您会注意到他们使用数据[&#39;表格&#39;]但它看起来与您作为表单传递的内容完全不同。他们正在尝试确定表单是否为横向,并且您的数据[&#39;表单&#39;]似乎与您为其创建报​​告的记录相关。不是报告布局。

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <template id="my_report">
            <t t-call="report.html_container">
                <div class="page">
                    <!--header -->
                    <!--<t t-call="report.internal_layout">-->
                    <!--content-->

                    <div class="row">
                        <span >"====="</span>
                        <span>Departments</span>
                        <t t-raw="departments"/>

                        <span >"++++++"</span>
                    </div>
                </div>
            </t>
        </template>
    </data>
</openerp>