0

Below is a code of a function I am using to print report from wizard ..

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)

THe report xml file looks like ..

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

If departmens in data['form'] is a type of dictionary, I was able to print, but now I have changed to a list and I ma getting below error while printing report

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'

update in py code

I have just changed to

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

and the report is getting printed.... The question is why they can't directly print the report if I use

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

list instead of dict in data['form'] ... ??

ChesuCR
  • 9,352
  • 5
  • 51
  • 114
Bhuro
  • 348
  • 4
  • 22

1 Answers1

0

I am not sure however I think there is a conflict in your use of data['form']. It is mentioned in the abstract_report.py file which is referenced in your error. If you follow the documentation for Odoo Reports they describe overriding the render_html function like this. Instead of using data['form'] in your qweb try using departments directly as the variable should be available.

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)

Try defining another qweb varable var or anything but data. There are several references to data and I am wondering if there is a conflict. Your original method does not look altogether wrong but the use of variable names that odoo is already using such as data and form could be a conflict. If you look at /addons/report/abstract_report.py you notice they use a data['form'] but it looks nothing like what you are passing as form. They are trying to determine if the form is landscape or not and your data['form'] seems to be related to the records you are creating a report for. Not the report layout.

<?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>
Bhuro
  • 348
  • 4
  • 22
Phillip Stack
  • 3,308
  • 1
  • 15
  • 24
  • Thank, But this is what my format is `data = {'form': [{'a':1,'b':[{..}]},{'a':1,'b':[{..}]},.. ]` and in the report I've just try to print `data['form']` which is a list, not accessing any element ... ` ` ...with your example I'm just trying to print `form` before I can do`` ... – Bhuro Sep 09 '16 at 03:48
  • Same error ... ` ` ... – Bhuro Sep 09 '16 at 03:54
  • Dont reassign data using t-set. Run t-raw on it before you use t-set to reassign it. – Phillip Stack Sep 09 '16 at 03:55
  • I've updated my previous comment where I have commented the lines reassigning using t-set .... but same error ... – Bhuro Sep 09 '16 at 03:57
  • Sorry, your use of data dictionary in your python file threw me off. If you look at the error it is odoo's data variable not the data variable from your file. – Phillip Stack Sep 09 '16 at 04:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122950/discussion-between-phillipstack-and-bhuro). – Phillip Stack Sep 09 '16 at 04:08
  • I am using `wizard to calculate some values`, and then running my main forloop to print the data, I have updated the question with full class description ... so How would I get `month_year` from wizard to `your suggested Report class` ... by just passing `month_year as `data` in `get_action` and then doing my main forloop in `Report class as you suggest`??? – Bhuro Sep 10 '16 at 02:58