Help

2

How to override a method using inherit?

Avatar
Anabela Damas

Hi,

I'm trying to override a method in the class calendar_event, I see in here that is needed to re-define columns/fields but my function don have any field defined in the columns...

So what I've tried was this :

class calendar_event(osv.osv):

    _name = 'calendar.event'
    _inherit = "calendar.event"


    def create_attendees(self, cr, uid, ids, context):
        att_obj = self.pool.get('calendar.attendee')

        print 'JUST Print something to see if it uses this function '

        user_obj = self.pool.get('res.users')

        current_user = user_obj.browse(cr, uid, uid, context=context)
        for event in self.browse(cr, uid, ids, context):
            attendees = {}
            for att in event.attendee_ids:
                attendees[att.partner_id.id] = True
            new_attendees = []
            mail_to = ""
            for partner in event.partner_ids:
                if partner.id in attendees:
                    continue
                att_id = self.pool.get('calendar.attendee').create(cr, uid, {
                    'partner_id': partner.id,
                    'user_id': partner.user_ids and partner.user_ids[0].id or False,
                    'ref': self._name+','+str(event.id),
                    'email': partner.email
                }, context=context)
                if partner.email:
                    mail_to = mail_to + " " + partner.email
                self.write(cr, uid, [event.id], {
                    'attendee_ids': [(4, att_id)]
                }, context=context)
                new_attendees.append(att_id)

            if mail_to and current_user.email:
                att_obj._send_mail(cr, uid, new_attendees, mail_to,
                    email_from = current_user.email, context=context)
        return True


calendar_event()

How can I do this?

Avatar
Discard
2 Answers
6
Avatar
Carlos Vasquez
Best Answer

Hi Anabela,

You only need to redefine columns when you are overwritting a method for a function field. In this case you are not. This method is not used to calculate a field, but to do some other functionality.

The way you are doing this inherit is right. Just a couple of recommendations (assuming you are on v7):

  • Use python standards for class name (camelCase). For your class it would be calendarEvent.
  • Use the new orm.Model object instead of osv.osv. For this you have to import osv.orm first.
  • You don't need anymore the explicit call at the end of the class.
  • For maintainability reasons, always try to implement you inherited methods calling the original one, instead of just copy-pasting it and changing it in your code. This can be done with the super method. In your case it would be:

    super(calendarEvent, self).create_attendees(cr, uid, ids, context)

Regards

11 Comments
Avatar
Discard
Avatar
Anabela Damas
-

Hi Carlos,

I'm a newbie in python, I do stuff like I see in other modules =)

Thanks for the recommendations ;)

Some doubts...

In the second dot I did like this :

from openerp.osv import fields, orm

class calendar_event(orm.Model):

#_name = 'calendar.event'
_inherit = 'calendar.event'

And nothing happened

As I am newbie in python the dot four is very hard because I never know where to put the code, and copy and paste all code is much easier =) Because I want to change some lines in the middle of the function.

Avatar
Anabela Damas
-

So I try everything and the openerp is still using the original def create_attendees(self, cr, uid, ids, context) .... I don't know what I'm doing wrong =(

Thanks

Avatar
Carlos Vasquez
-

Have you created the __init__.py for your module? In this __init__.py you need to have a simple line with: "import file". (change file with the name of your py file without the .py)

Avatar
Carlos Vasquez
-

Also, when you make changes to the __openerp__.py and the __init__.py, it is usually necessary to update your module in your database.

Avatar
Anabela Damas
-

Unfortunately yes, have all of that ... I really don't know what is wrong, I was doing this inherit in a module that does other stuff.

And for tests I created a new single module just with this (mymodule.py), a __init__.py and the __openerp__.py files. This function for some reason that I don't understand doesn't override the original function.... what is weird is that I've made this to other functions and it's working.... =(

Thanks

Avatar
Carlos Vasquez
-

One possible explanation is that there is another module installed that is also inheriting this method. If that other module takes precedence, and doesn't do a super call (as I explained in my answer) it will only run that particular module's code. If you are doing this for other methods with a positive result, than it can be either a typo somewhere, or something a little more complicated. It would take a closer look to your code, server and database to find the problem.

Avatar
Anabela Damas
-

Thanks for all =) I've done a workaround =)

Avatar
Anabela Damas
-

The workaround has a bug, so I tried again to find the problem I've made a grep to find where this function appears and the only places is my code and the original...

Avatar
Carlos Llamacho
-

Hi, i have always used the osv.osv model to do my classes but now i read that the new orm.Model is prefered. There is documentation regarding the differences between them?

Avatar
fussions
-

Carlos, can you tell me where I can read about using orm.Model object instead osv.osv. Is it mentioned in OpenERP documentation or one can only derive it from the OpenErp source code?

Avatar
Carlos Llamacho
-

I haven't found any official documentation regarding orm.Model as better to use than osv.osv. I have however asked the same question in an Openerp Google group and they recommend to start using orm.Model instead of osv.osv as the later is going to be replaced. Only maintain osv.osv for compatibility issues with former versions.

1
Avatar
patrick
Best Answer

No need to define _name, as your module will inherit the class, with the name.

After making your module, you have to install it. The module (with other files and folders) needs to be in /addons directory (on Ubuntu: /usr/lib/pymodules/python2.7/openerp/addons). Now you go to Settings -> Update Module List, than Settings -> Installed modules. Remove the filter 'installed', and search for your module. Click on it, and click on the button 'Install'.

If you want to see some output, don't use print, but use logging.warning('your text here'). You have to import logging first, before usage.

4 Comments
Avatar
Discard
Avatar
Anabela Damas
-

from openerp.osv import logging

2013-06-20 19:25:32,114 15008 CRITICAL teste20jun openerp.modules.module: Couldn't load module gestao_ideias 2013-06-20 19:25:32,114 15008 CRITICAL teste20jun openerp.modules.module: cannot import name logging 2013-06-20 19:25:32,115 15008 ERROR teste20jun openerp.netsvc: cannot import name logging

logging.warning('your text here')

I try this way but didn't work...

Avatar
patrick
-

I use the text 'import logging', and it is working. It is a default python thing you import, not something from openERP.

Avatar
Anabela Damas
-

I tried this:

        import logging
        logging.warning('Something')

and nothing happened. Thanks

Avatar
patrick
-

The result of the warning should be in your logging file (on Ubuntu: /vaar/log/openerp/openerp-server.log).