python-2.7openerp-7

How can override write method without executing the super write?


In the membership.py file the class account_invoice_line which inherits 'account.invoice.line' and overrides the write method which will create a new member line when a new line of invoice is created in an existed invoice. For me its not correct when a a new line of invoice is created in an existing invoice it must be related to the existed member line and not create a new member line so I must override the write method, but the problem that the write method in member.py file is always executed.

So can someone tell me how to override the write method without passing by the write method in membership.py file?

This is the code in file membership.py:

class account_invoice_line(osv.osv):
_inherit='account.invoice.line'

def write(self, cr, uid, ids, vals, context=None):
    """Overrides orm write method
    """
    member_line_obj = self.pool.get('membership.membership_line')
    res = super(account_invoice_line, self).write(cr, uid, ids, vals, context=context)
    for line in self.browse(cr, uid, ids, context=context):
        if line.invoice_id.type == 'out_invoice':
            ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
            if line.product_id and line.product_id.membership and not ml_ids:
                # Product line has changed to a membership product
                date_from = line.product_id.membership_date_from
                date_to = line.product_id.membership_date_to
                if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
                    date_from = line.invoice_id.date_invoice
                member_line_obj.create(cr, uid, {
                                'partner': line.invoice_id.partner_id.id,
                                'membership_id': line.product_id.id,
                                'member_price': line.price_unit,
                                'date': time.strftime('%Y-%m-%d'),
                                'date_from': date_from,
                                'date_to': date_to,
                                'account_invoice_line': line.id,
                                }, context=context)
            if line.product_id and not line.product_id.membership and ml_ids:
                # Product line has changed to a non membership product
                member_line_obj.unlink(cr, uid, ml_ids, context=context)
    return res

def unlink(self, cr, uid, ids, context=None):
    """Remove Membership Line Record for Account Invoice Line
    """
    member_line_obj = self.pool.get('membership.membership_line')
    for id in ids:
        ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', id)], context=context)
        member_line_obj.unlink(cr, uid, ml_ids, context=context)
    return super(account_invoice_line, self).unlink(cr, uid, ids, context=context)

def create(self, cr, uid, vals, context=None):
    """Overrides orm create method
    """
    member_line_obj = self.pool.get('membership.membership_line')
    result = super(account_invoice_line, self).create(cr, uid, vals, context=context)
    line = self.browse(cr, uid, result, context=context)
    if line.invoice_id.type == 'out_invoice':
        ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
        if line.product_id and line.product_id.membership and not ml_ids:
            # Product line is a membership product
            date_from = line.product_id.membership_date_from
            date_to = line.product_id.membership_date_to
            if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
                date_from = line.invoice_id.date_invoice
            member_line_obj.create(cr, uid, {
                        'partner': line.invoice_id.partner_id and line.invoice_id.partner_id.id or False,
                        'membership_id': line.product_id.id,
                        'member_price': line.price_unit,
                        'date': time.strftime('%Y-%m-%d'),
                        'date_from': date_from,
                        'date_to': date_to,
                        'account_invoice_line': line.id,
                    }, context=context)
    return result

account_invoice_line()

Solution

  • you can bypass calling of super method as like below.

    Instead of your class name you need to pass osv.osv class to call direct base write method.

    res = super(osv.osv, self).write(cr, uid, ids, vals, context=context)
    

    It will bypass the all super write method and directly called of osv.osv class.

    Note: You can also call any other class instead of osv.osv. For that you have to follow as like below.

    for ex.

    from openerp.addons.account.account_invoice import account_invoice
    
    
    class account_invoice_inherit(osv.osv):
    def write( . .. . )
    
        res = super(account_invoice,self).write(. .. .)
    
        . .. .
    
        return res
    

    Here from this class system will called directly write method of account_invoice class.

    I hope it will help you.