Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

OpenERP - Execute cron job with specific user context

By
Victor Godayer
on 3/14/14, 9:58 AM 2,326 views

I am extending a module to allow an openerp scheduler to call a method automatically once a day.

The module I extend is account_followup, which allows to send mails in order to remind overdue payments to customers. The action of sending reminder emails in the native module is done by clicking on a button in a wizard.

My extension is creating a cron job that calls the method which is called when the user click on the wizard button.

Here are the technicals details of my cron job:

  • object : account_followup.print (there is a table in DB corresponding to this model)
  • method : do_process
  • arguments : [[1]] (this value is part of my problem we'll see below why)

And below is the the begining of the do_process method :

def do_process(self, cr, uid, ids, context=None):
    if context is None:
        context = {} # this is the case when call from cron job

    #Get partners
    tmp = self._get_partners_followp(cr, uid, ids, context=context)

The local scope variables change depending on the origin the method is called from :

  1. Called from the cron job, context is empty, and ids variable is equal to arguments field.

  2. Called by the user clicking on the button, context contains usual informations (lang,tz,uid) and ids which is the record just newly created in DB table account_followup_print. This ids is then used by the method _get_partners_followp which fetch the record newly created in table account_followup_print thanks to ids.

My problem is that I don't understand in case 2, how the record is created whilst the method do_process is called directly from the click event (see below the xml view code).

<button name="do_process" string="Send emails and generate letters" type="object" class="oe_highlight"/>

My cron job call the same do_process method but an error is thrown cause no record matches with "static" id from the field arguments :

2014-03-14 11:37:40,654 31224 ERROR openerp_dev openerp.addons.base.ir.ir_cron: Call of self.pool.get('account_followup.print').do_process(cr, uid, *([1],)) failed in Job 15
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/openerp-7.0_20140210_002654-py2.7.egg/openerp/addons/base/ir/ir_cron.py", line 136, in _callback
method(cr, uid, *args)
File "/usr/local/lib/python2.7/dist-packages/openerp-7.0_20140210_002654-py2.7.egg/openerp/addons/cap_account_followup/wizard/account_followup_print.py", line 231, in do_process
tmp = self._get_partners_followp(cr, uid, ids, context=context)
File "/usr/local/lib/python2.7/dist-packages/openerp-7.0_20140210_002654-py2.7.egg/openerp/addons/cap_account_followup/wizard/account_followup_print.py", line 306, in _get_partners_followp
fup_id = 'followup_id' in context and context['followup_id'] or data.followup_id.id
File "/usr/local/lib/python2.7/dist-packages/openerp-7.0_20140210_002654-py2.7.egg/openerp/osv/orm.py", line 499, in __getattr__
raise AttributeError(e)
AttributeError: 'Field followup_id not found in browse_record(account_followup.print, 1)'

And finally, I can show as well the call stack that are different between case 1 and case 2 :

1.

addons/base/ir/ir_cron.py", line 136, in _callback
  method(cr, uid, *args)
addons/cap_account_followup/wizard/account_followup_print.py", line 229, in do_process

2.

addons/web/session.py", line 89, in send
  return openerp.netsvc.dispatch_rpc(service_name, method, args)
netsvc.py", line 292, in dispatch_rpc
  result = ExportService.getService(service_name).dispatch(method, params)
service/web_services.py", line 626, in dispatch
  res = fn(db, uid, *params)
osv/osv.py", line 190, in execute_kw
  return self.execute(db, uid, obj, method, *args, **kw or {})
osv/osv.py", line 132, in wrapper
  return f(self, dbname, *args, **kwargs)
osv/osv.py", line 199, in execute
  res = self.execute_cr(cr, uid, obj, method, *args, **kw)
osv/osv.py", line 187, in execute_cr
  return getattr(object, method)(cr, uid, *args, **kw)
addons/cap_account_followup/wizard/account_followup_print.py", line 229, in do_process

I d'ont know if my problem is cleary exposed, let me know if it is blurry.

Thank you for your help

Cheers

0

Brett Lehrer

--Brett Lehrer--
1252
| 5 4 8
Milwaukee, United States
--Brett Lehrer--
Director of IT and open source enthusiast.
Brett Lehrer
On 3/14/14, 2:37 PM

Wouldn't editing the cron task work? In the Technical Data tab in the arguments field, add context=<special cron="" context="" dict=""> inside the parenthesis at the end.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 3/14/14, 9:58 AM
Seen: 2326 times
Last updated: 3/16/15, 8:10 AM