3
votes

How would one go about passing multiple objects to the reporting engine?

I'm trying to create a custom Invoice report where I need to attach data from other application to be displayed on the invoice. I can get the data into OpenERP server using Web Services but how do I pass it to the reporting engine? Perhaps the

set_context or (self.localcontext.update())

method will be useful here as it allows me to pass custom variables to the report but how would one pass a whole object.

What I'm importing from other application is essentially a massive table with potentially 100's of records that are related to the current partner, I don't need to save it in OpenERP database, simply display it while generating an invoice.

EDIT:

To test the object I have in the parser

class test_invoice(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
    super(test_invoice, self).__init__(cr, uid, name, context=context)
    self.localcontext.update({
        'time': time,
        'test_var': 'Worked!',
        'get_list': self._get_list,
    })


def _get_list(self):
    result = []
    ress = {'first': '1',
        'second': '2',
        }
    result.append(ress)
    return result

and in the rml file

...start of rml file
     <section>
        <para>
            [[ repeatIn(get_list(), 'g')]]
            [[ g.first ]]
        </para>
    </section>

  </story>
</document>

but this throws an error "coercing to Unicode: need string or buffer, tuple found". How can we display a custom list in rml?

Thank You.

3

3 Answers

2
votes

you can pass whole objects as far as OpenERP (well, the python you run OpenERP with) knows about that object. Otherwise you have to 'proxy' the object is some manner. Using SQLAlchemy for fetching data from the external db could be an handful solution. You could hand with something like that:

[...somewhere into your parser...]
self.localcontext.update({'external_rows': session.query(MyObject).filter_by(foo='baz')})

or if you are managing CSV data:

self.localcontext.updarte({'external_rows': self._get_myrows_from_csv()})

where _get_myrows_from_csv returns a list of dictionaries for instance.

2
votes

You are doing everything right except consuming the result (a dictionary) in your RML file. Try

[[ g['first'] ]]

instead of

[[ g.first ]]

The problem is that you are trying to access a dictionary value as object property.

1
votes

In RML file:

<story>
    <section>
        <para>[[ repeatIn(products(), 'p') ]]</para>
        <blockTable colWidths="100.0,100.0" >
            <tr>
                <td>
                    <para>[[p.name]]</para>
                </td>

                <td>
                    <para>[[p.list_price]]</para>
                </td>
            </tr>
        </blockTable >
    </section>
</story>

In report parser:

class myreport(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(myreport, self).__init__(cr, uid, name, context=context)

        self.cr = cr
        self.uid = uid
        self.context = context

        self.localcontext.update({
            'products': self._get_products
            })

    def _get_products(self, ids=[85, 81, 89]):
        return [p for p in self.pool.get('product.product').browse(self.cr, self.uid, ids, self.context)]

I hope it will be usefull.