Merge lp:~therp-nl/therp-addons/7.0-add-mail_error into lp:~therp-nl/therp-addons/7.0

Proposed by Stefan Rijnhart (Opener)
Status: Merged
Merged at revision: 103
Proposed branch: lp:~therp-nl/therp-addons/7.0-add-mail_error
Merge into: lp:~therp-nl/therp-addons/7.0
Diff against target: 300 lines (+257/-0)
8 files modified
mail_error/__init__.py (+1/-0)
mail_error/__openerp__.py (+37/-0)
mail_error/i18n/mail_error.pot (+28/-0)
mail_error/i18n/nl.po (+40/-0)
mail_error/model/__init__.py (+2/-0)
mail_error/model/mail_mail.py (+27/-0)
mail_error/model/mail_mail_monkeypatch.py (+103/-0)
mail_error/view/mail_mail.xml (+19/-0)
To merge this branch: bzr merge lp:~therp-nl/therp-addons/7.0-add-mail_error
Reviewer Review Type Date Requested Status
Holger Brunn (Therp) code review Approve
Review via email: mp+215186@code.launchpad.net

Description of the change

Proposing in therp-addons because of ugly monkeypatch

To post a comment you must log in.
Revision history for this message
Holger Brunn (Therp) (hbrunn) :
review: Approve (code review)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'mail_error'
2=== added file 'mail_error/__init__.py'
3--- mail_error/__init__.py 1970-01-01 00:00:00 +0000
4+++ mail_error/__init__.py 2014-04-10 14:06:49 +0000
5@@ -0,0 +1,1 @@
6+from . import model
7
8=== added file 'mail_error/__openerp__.py'
9--- mail_error/__openerp__.py 1970-01-01 00:00:00 +0000
10+++ mail_error/__openerp__.py 2014-04-10 14:06:49 +0000
11@@ -0,0 +1,37 @@
12+# -*- coding: utf-8 -*-
13+##############################################################################
14+#
15+# OpenERP, Open Source Management Solution
16+# This module copyright (C) 2014 Therp BV (<http://therp.nl>).
17+#
18+# This program is free software: you can redistribute it and/or modify
19+# it under the terms of the GNU Affero General Public License as
20+# published by the Free Software Foundation, either version 3 of the
21+# License, or (at your option) any later version.
22+#
23+# This program is distributed in the hope that it will be useful,
24+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26+# GNU Affero General Public License for more details.
27+#
28+# You should have received a copy of the GNU Affero General Public License
29+# along with this program. If not, see <http://www.gnu.org/licenses/>.
30+#
31+##############################################################################
32+{
33+ 'name' : 'Register errors on emails',
34+ 'version' : '0.1',
35+ 'author' : 'Therp BV',
36+ "description": """
37+This module adds a field to the mail model to store any error that may
38+occur when sending an email. The field is filled in a monkeypatched method
39+of the model.
40+ """,
41+ 'category' : 'Tools',
42+ 'depends' : [
43+ 'mail',
44+ ],
45+ 'data' : [
46+ 'view/mail_mail.xml',
47+ ],
48+}
49
50=== added directory 'mail_error/i18n'
51=== added file 'mail_error/i18n/mail_error.pot'
52--- mail_error/i18n/mail_error.pot 1970-01-01 00:00:00 +0000
53+++ mail_error/i18n/mail_error.pot 2014-04-10 14:06:49 +0000
54@@ -0,0 +1,28 @@
55+# Translation of OpenERP Server.
56+# This file contains the translation of the following modules:
57+# * mail_error
58+#
59+msgid ""
60+msgstr ""
61+
62+#. module: mail_error
63+#: field:mail.mail,error_msg:0
64+msgid "Error message"
65+msgstr ""
66+
67+#. module: mail_error
68+#: code:_description:0
69+#, python-format
70+msgid "Mail model monkeypatch"
71+msgstr ""
72+
73+#. module: mail_error
74+#: code:_description:0 model:ir.model,name:mail_error.model_mail_mail
75+#, python-format
76+msgid "Outgoing Mails"
77+msgstr ""
78+
79+#. module: mail_error
80+#: model:ir.model,name:mail_error.model_mail_mail_monkeypatch
81+msgid "mail.mail.monkeypatch"
82+msgstr ""
83
84=== added file 'mail_error/i18n/nl.po'
85--- mail_error/i18n/nl.po 1970-01-01 00:00:00 +0000
86+++ mail_error/i18n/nl.po 2014-04-10 14:06:49 +0000
87@@ -0,0 +1,40 @@
88+# Translation of OpenERP Server.
89+# This file contains the translation of the following modules:
90+# * mail_error
91+#
92+msgid ""
93+msgstr ""
94+"Project-Id-Version: OpenERP Server 7.0\n"
95+"Report-Msgid-Bugs-To: \n"
96+"POT-Creation-Date: 2014-03-31 18:01+0000\n"
97+"PO-Revision-Date: 2014-03-31 18:01+0000\n"
98+"Last-Translator: <>\n"
99+"Language-Team: \n"
100+"MIME-Version: 1.0\n"
101+"Content-Type: text/plain; charset=UTF-8\n"
102+"Content-Transfer-Encoding: \n"
103+"Plural-Forms: \n"
104+
105+#. module: mail_error
106+#: field:mail.mail,error_msg:0
107+msgid "Error message"
108+msgstr "Opgetreden fout"
109+
110+#. module: mail_error
111+#: code:_description:0
112+#, python-format
113+msgid "Mail model monkeypatch"
114+msgstr "Mail model monkeypatch"
115+
116+#. module: mail_error
117+#: code:_description:0
118+#: model:ir.model,name:mail_error.model_mail_mail
119+#, python-format
120+msgid "Outgoing Mails"
121+msgstr "Uitgaande E-mails"
122+
123+#. module: mail_error
124+#: model:ir.model,name:mail_error.model_mail_mail_monkeypatch
125+msgid "mail.mail.monkeypatch"
126+msgstr "mail.mail.monkeypatch"
127+
128
129=== added directory 'mail_error/model'
130=== added file 'mail_error/model/__init__.py'
131--- mail_error/model/__init__.py 1970-01-01 00:00:00 +0000
132+++ mail_error/model/__init__.py 2014-04-10 14:06:49 +0000
133@@ -0,0 +1,2 @@
134+from . import mail_mail
135+from . import mail_mail_monkeypatch
136
137=== added file 'mail_error/model/mail_mail.py'
138--- mail_error/model/mail_mail.py 1970-01-01 00:00:00 +0000
139+++ mail_error/model/mail_mail.py 2014-04-10 14:06:49 +0000
140@@ -0,0 +1,27 @@
141+# -*- coding: utf-8 -*-
142+##############################################################################
143+#
144+# Copyright (C) 2014 Therp BV (<http://therp.nl>).
145+#
146+# This program is free software: you can redistribute it and/or modify
147+# it under the terms of the GNU Affero General Public License as
148+# published by the Free Software Foundation, either version 3 of the
149+# License, or (at your option) any later version.
150+#
151+# This program is distributed in the hope that it will be useful,
152+# but WITHOUT ANY WARRANTY; without even the implied warranty of
153+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
154+# GNU Affero General Public License for more details.
155+#
156+# You should have received a copy of the GNU Affero General Public License
157+# along with this program. If not, see <http://www.gnu.org/licenses/>.
158+#
159+##############################################################################
160+from openerp.osv import orm, fields
161+
162+
163+class MailMail(orm.Model):
164+ _inherit = 'mail.mail'
165+ _columns = {
166+ 'error_msg': fields.char('Error message', readonly=True),
167+ }
168
169=== added file 'mail_error/model/mail_mail_monkeypatch.py'
170--- mail_error/model/mail_mail_monkeypatch.py 1970-01-01 00:00:00 +0000
171+++ mail_error/model/mail_mail_monkeypatch.py 2014-04-10 14:06:49 +0000
172@@ -0,0 +1,103 @@
173+# -*- coding: utf-8 -*-
174+##############################################################################
175+#
176+# Copyright (C) 2014 Therp BV (<http://therp.nl>).
177+# Most of the code in the method below Copyright (C) OpenERP S.A.
178+#
179+# This program is free software: you can redistribute it and/or modify
180+# it under the terms of the GNU Affero General Public License as
181+# published by the Free Software Foundation, either version 3 of the
182+# License, or (at your option) any later version.
183+#
184+# This program is distributed in the hope that it will be useful,
185+# but WITHOUT ANY WARRANTY; without even the implied warranty of
186+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
187+# GNU Affero General Public License for more details.
188+#
189+# You should have received a copy of the GNU Affero General Public License
190+# along with this program. If not, see <http://www.gnu.org/licenses/>.
191+#
192+##############################################################################
193+import base64
194+from openerp import tools
195+from openerp import SUPERUSER_ID
196+from openerp.osv import orm
197+from openerp.addons.mail.mail_mail import mail_mail, _logger
198+
199+def send(self, cr, uid, ids, auto_commit=False, recipient_ids=None, context=None):
200+ """
201+ Copy of this method in mail.mail. If an error occurs when sending
202+ a mail, write the error in the mail record itself.
203+
204+ This method is monkeypatched into the original model, to preserve
205+ inheritance of this method.
206+ """
207+ ir_mail_server = self.pool.get('ir.mail_server')
208+ for mail in self.browse(cr, uid, ids, context=context):
209+ try:
210+ # handle attachments
211+ attachments = []
212+ for attach in mail.attachment_ids:
213+ attachments.append((attach.datas_fname, base64.b64decode(attach.datas)))
214+ # specific behavior to customize the send email for notified partners
215+ email_list = []
216+ if recipient_ids:
217+ partner_obj = self.pool.get('res.partner')
218+ existing_recipient_ids = partner_obj.exists(cr, SUPERUSER_ID, recipient_ids, context=context)
219+ for partner in partner_obj.browse(cr, SUPERUSER_ID, existing_recipient_ids, context=context):
220+ email_list.append(self.send_get_email_dict(cr, uid, mail, partner=partner, context=context))
221+ else:
222+ email_list.append(self.send_get_email_dict(cr, uid, mail, context=context))
223+
224+ # build an RFC2822 email.message.Message object and send it without queuing
225+ res = None
226+ for email in email_list:
227+ msg = ir_mail_server.build_email(
228+ email_from = mail.email_from,
229+ email_to = email.get('email_to'),
230+ subject = email.get('subject'),
231+ body = email.get('body'),
232+ body_alternative = email.get('body_alternative'),
233+ email_cc = tools.email_split(mail.email_cc),
234+ reply_to = email.get('reply_to'),
235+ attachments = attachments,
236+ message_id = mail.message_id,
237+ references = mail.references,
238+ object_id = mail.res_id and ('%s-%s' % (mail.res_id, mail.model)),
239+ subtype = 'html',
240+ subtype_alternative = 'plain')
241+ res = ir_mail_server.send_email(cr, uid, msg,
242+ mail_server_id=mail.mail_server_id.id, context=context)
243+ if res:
244+ mail.write({'state': 'sent', 'message_id': res})
245+ mail_sent = True
246+ else:
247+ mail.write({'state': 'exception'})
248+ mail_sent = False
249+
250+ # /!\ can't use mail.state here, as mail.refresh() will cause an error
251+ # see revid:odo@openerp.com-20120622152536-42b2s28lvdv3odyr in 6.1
252+ if mail_sent:
253+ self._postprocess_sent_message(cr, uid, mail, context=context)
254+ except Exception, e:
255+ _logger.exception('failed sending mail.mail %s', mail.id)
256+ ### mail_error start changes
257+ vals = {'state': 'exception'}
258+ if 'error_msg' in mail._columns and e.args:
259+ vals['error_msg'] = e.args[-1]
260+ mail.write(vals)
261+ ### mail_error stop changes
262+
263+ if auto_commit == True:
264+ cr.commit()
265+ return True
266+
267+
268+class MailMailMonkeypatch(orm.AbstractModel):
269+ _name = 'mail.mail.monkeypatch'
270+ _description = 'Mail model monkeypatch'
271+
272+
273+ def _register_hook(self, cr):
274+ mail_mail.send = send
275+ return super(MailMailMonkeypatch, self)._register_hook(cr)
276
277=== added directory 'mail_error/view'
278=== added file 'mail_error/view/mail_mail.xml'
279--- mail_error/view/mail_mail.xml 1970-01-01 00:00:00 +0000
280+++ mail_error/view/mail_mail.xml 2014-04-10 14:06:49 +0000
281@@ -0,0 +1,19 @@
282+<?xml version="1.0"?>
283+<openerp>
284+ <data>
285+ <record model="ir.ui.view" id="view_mail_form">
286+ <field name="name">Add error message to mail form view</field>
287+ <field name="model">mail.mail</field>
288+ <field name="inherit_id" ref="mail.view_mail_form" />
289+ <field name="arch" type="xml">
290+ <xpath expr='//notebook/page[@string="Message Details"]/..'
291+ position="before">
292+ <div attrs="{'invisible': [('state', '!=', 'exception')]}">
293+ <label for="error_msg" />
294+ <field name="error_msg" />
295+ </div>
296+ </xpath>
297+ </field>
298+ </record>
299+ </data>
300+</openerp>

Subscribers

People subscribed via source and target branches

to all changes: