This question has been flagged
7 Replies
7254 Views

Hi guys,

I've inherited the write function of the model project.issue in V9 so I could send out an e-mail when the issue is changed to the final stage. The strange thing however is that this code goes of twice, but I don't see why.
The code:

@api.multi def write(self, vals): res = super(project_issue, self).write(vals) self.ensure_one() _logger.critical('stage name: ' + str(self.stage_id.name)) if self.stage_id.name == self.project_id.final_fase_id.name: """ This means the issue has entered its final stage and is done. We should now notify the user by e-mail of completion.""" email_template = self.env['mail.template'].search([('name', '=', 'Issue solved')]) email_template.send_mail(self.id, force_send=True, raise_exception=True) return res

The terminal output:

2016-02-18 08:46:33,185 15538 INFO MyDb werkzeug: 127.0.0.1 - - [18/Feb/2016 08:46:33] "POST /web/dataset/call_kw/project.task.type/name_get HTTP/1.1" 200 -

2016-02-18 08:46:34,847 15538 CRITICAL MyDb openerp.addons.MyModule.models.project_issue: stage name: Done

2016-02-18 08:46:36,050 15538 INFO MyDb openerp.addons.mail.models.mail_mail: Mail with ID 19 and Message-Id u'<1455785194.900032924652100.686587842139294-openerp-20-project.issue@odoo8>' successfully sent

2016-02-18 08:46:36,057 15538 INFO MyDb openerp.models.unlink: User #1 deleted mail.mail records with IDs: [19]

2016-02-18 08:46:36,070 15538 INFO MyDb openerp.models.unlink: User #1 deleted mail.message records with IDs: [142]

2016-02-18 08:46:36,077 15538 CRITICAL MyDb openerp.addons.MyAddon.models.project_issue: stage name: Done

2016-02-18 08:46:37,264 15538 INFO MyDb openerp.addons.mail.models.mail_mail: Mail with ID 20 and Message-Id u'<1455785196.118980884552002.189632093322604-openerp-20-project.issue@odoo8>' successfully sent

So why does this write function goes of twice?

Yenthe

Avatar
Discard
Author Best Answer

Hi guys,

Eventually I solved this by inheriting the default _track_subtype function from the module project_issue and by adding some custom code in it to fix my automatic e-mail. The code:

# We will inherit the default track_subtype function from the module project_issue # in order to send out our e-mail in the correct state. def _track_subtype(self, cr, uid, ids, init_values, context=None): res = super(project_issue, self)._track_subtype(cr, uid, ids, init_values, context=context) record = self.browse(cr, uid, ids[0], context=context) if record.stage_id.name == record.project_id.final_fase_id.name: """ This means the issue has entered its final stage and is done. We should now notify the user by e-mail of completion.""" template = self.pool.get('ir.model.data').get_object(cr, uid, 'your_module_name', 'template_email_id') mail_id = self.pool.get('mail.template').send_mail(cr, uid, template.id, record.id , force_send=True) if 'kanban_state' in init_values and record.kanban_state == 'blocked': return 'project_issue.mt_issue_blocked' elif 'kanban_state' in init_values and record.kanban_state == 'done': return 'project_issue.mt_issue_ready' elif 'user_id' in init_values and record.user_id: # assigned -> new return 'project_issue.mt_issue_new' elif 'stage_id' in init_values and record.stage_id and record.stage_id.sequence <= 1: # start stage -> new return 'project_issue.mt_issue_new' elif 'stage_id' in init_values: return 'project_issue.mt_issue_stage' return res


Yenthe

Avatar
Discard
Best Answer

Probably one mail is sent by _track (stage change - see model project.issue ) and the second sending itself.

Avatar
Discard
Author

I was thinking about that too but then the question is how should I prevent this behaviour from only sending one email? I'd rather not create another field with a datetime just to validate one wasn't sent out seconds before..

You change _track conditions, or send only with _track.

Author

@zbik which one is the best fit? It should send out one e-mail on creation only. How do i trigger the _track so that it only sends one?

Go to menu /Settings/Email/Subytpes and disable tracks for model.

Author

@zbik it is indeed coming from the track (track_visbility as seen here: https://github.com/odoo/odoo/blob/9.0/addons/project_issue/project_issue.py#L162-L164) how should I correctly handle this? Should I override this Python function and re-write it to make the e-mail go off here or should I change some other behaviour? Any example / approach? I'm not sure how I should ideally handle this.

Author

@zbik posted an example. What do you think of this approach?