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

Why does this inherited write function goes of twice?

By
Yenthe
on 2/18/16, 3:53 AM 659 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

0

Yenthe

--Yenthe--
6696
| 7 7 8
Turnhout, Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 3/9/16, 9:06 AM

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

0

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 2/18/16, 5:27 AM

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

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..

Yenthe
on 2/18/16, 5:39 AM

You change _track conditions, or send only with _track.

zbik
on 2/18/16, 5:44 AM

@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?

Yenthe
on 2/18/16, 6:05 AM

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

zbik
on 2/18/16, 7:30 AM

@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.

Yenthe
on 3/9/16, 7:49 AM

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

Yenthe
on 3/9/16, 9:17 AM

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

1 follower(s)

Stats

Asked: 2/18/16, 3:53 AM
Seen: 659 times
Last updated: 3/9/16, 9:06 AM