Gerardo Parrello
Gerardo Parrello

Reputation: 55

In Odoo8, how do I import a module's .py file into a custom module?

Ok, so here comes. Brace yourselves.

I'm trying to overwrite a method (get_followup_table_html) from a class (res_partner) located in a module (account_followup) in Odoo 8, by creating a custom module (account_followup_upgrade) that inherits the class and defines and redefines the method.

The changes are very small, as the method's only purpose is to create an html table to be included in emails sent to customers; I have just copied the original method and modified the html code parts.

That being said, it works, mostly. My files, in my custom module's folder, include:

__openerp__.py (not full file, just the important bit):

'depends': ['account_followup'],

__init__.py:

 # -*- coding: utf-8 -*-

import mymodule

mymodule.py:

# -*- coding: utf-8 -*-

from openerp.osv import osv

# I need to modify a method in this class
class res_partner(osv.osv):
    # So I inherit it
    _inherit = 'res.partner'

    # And define a method called the same as the original, with the same arguments
    def get_followup_table_html(self, cr, uid, ids, context=None):

        """
        Build the html tables to be included in emails send to partners,
        when reminding them their overdue invoices.
        :param ids: [id] of the partner for whom we are building the tables
        :rtype: string
        """

        try:
            from report import account_followup_print
        except ImportError:
            return 'import failed'

        # The code that follows generates an html table
        assert len(ids) == 1
        if context is None:
            context = {}
        partner = self.browse(cr, uid, ids[0], context=context).commercial_partner_id
        #copy the context to not change global context. Overwrite it because _() looks for the lang in local variable 'context'.
        #Set the language to use = the partner language
        context = dict(context, lang=partner.lang)
        followup_table = ''
        if partner.unreconciled_aml_ids:
            company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
            current_date = fields.date.context_today(self, cr, uid, context=context)
            rml_parse = account_followup_print.report_rappel(cr, uid, "followup_rml_parser")
            final_res = rml_parse._lines_get_with_partner(partner, company.id)

            for currency_dict in final_res:
                currency = currency_dict.get('line', [{'currency_id': company.currency_id}])[0]['currency_id']
                # My changes start here
                followup_table += '''
                <table border="0" width=100%>
                <tr style="background-color:#ea6153;color:White">
                    <td><b>''' + _("Invoice Date") + '''</b></td>
                    <td><b>''' + _("Description") + '''</b></td>
                    <td><b>''' + _("Due Date") + '''</b></td>
                    <td><b>''' + _("Amount") + " (%s)" % (currency.symbol) + '''</b></td>
                </tr>
                ''' 
                total = 0
                strbegin = "<TD>"
                strend = "</TD>"
                empty_cell = strbegin + strend
                for aml in currency_dict['line']:
                    date = aml['date_maturity'] or aml['date']
                    if date <= current_date and aml['balance'] > 0:
                        total += aml['balance']
                        followup_table +="<TR>" + strbegin + str(aml['date']) + strend + strbegin + aml['name'] + strend + strbegin + str(date) + strend + strbegin + '{0:.2f}'.format(aml['balance']) + strend + "</TR>"

                #total = reduce(lambda x, y: x+y['balance'], currency_dict['line'], 0.00)
                followup_table +='''<TR style="background-color:#e9e9e9">''' + empty_cell + empty_cell + strbegin +"<B>TOTAL</B>" + strend + strbegin + "<B>"  + '{0:.2f}'.format(total) + "</B>" + strend + "</TR>"
                followup_table +="</table>"

        return followup_table

Now, I know the module is working as a I get 'import error' inside my emails, it is actually overwriting the original method (otherwise I would get a working, if ugly, html table). The problem is the import:

from report import account_followup_print

This file, account_followup_print.py, is located inside the folder report, in the original account_followup module, and I don't know how to import it (or inherit it). I need it because it is called once during the html table generation...

How can I reference this file from my custom module?

I know this is a wall of text, so thank you for reading!

Upvotes: 2

Views: 687

Answers (1)

Charif DZ
Charif DZ

Reputation: 14721

To import it use the full path

from openerp.addons.account_followup.report import account_followup_print

I Think this will work but if not just change it a little bit and it will work you need to specify the full path to the model report take a look at this question too:

Odoo overwrite inherited method

Upvotes: 2

Related Questions