2016-09-09 100 views
0

下面印刷qweb报告获取问题是我使用从向导打印报告的函数的代码..在odoo V8

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 
    } 

list代替字典在data['form'] ...?

回答

0

我不确定但是我认为你在使用数据['form']时有冲突。它在您的错误中引用的abstract_report.py文件中提及。如果你按照Odoo Reports的文档,他们描述了像这样覆盖render_html函数。而不是在你的qweb中使用数据['form']尝试直接使用部门,因为变量应该是可用的。

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,你会注意到它们使用了一个data ['form'],但它看起来不像你通过表单传递的东西。他们试图确定表单是否为横向格式,并且您的数据['form']似乎与您正在创建报告的记录有关。不是报告布局。

<?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> 
+0

谢谢,但是,这是我的格式是 '数据= { '形式':[{ 'A':1, 'B':[{..}]},{ 'A':1 ,'b':[{..}]},..]' 和我刚刚尝试打印的数据['form']'这是一个列表,不访问任何元素... '...用你的例子我只是试图打印'form',然后才能做''...... – Bhuro

+0

使用t-raw,看看我的编辑 –

+0

同样的错误......'<! - < t t-esc =“data”/> - ><! - - ><! - - - ><! - - >'... – Bhuro