From cfb92ec23ab7f81ff65cb0d231aaa4f9f7b5e88e Mon Sep 17 00:00:00 2001 From: chafique-delli Date: Mon, 18 Sep 2017 15:44:58 +0200 Subject: [PATCH] add many2many_tags_email.js file for mail_usability that replaces addons-mail-fix.diff patch --- mail_usability/__openerp__.py | 2 +- mail_usability/addons-mail-fix.diff | 23 ----- .../static/src/js/many2many_tags_email.js | 91 +++++++++++++++++++ mail_usability/views/mail.xml | 11 +++ 4 files changed, 103 insertions(+), 24 deletions(-) delete mode 100644 mail_usability/addons-mail-fix.diff create mode 100644 mail_usability/static/src/js/many2many_tags_email.js create mode 100644 mail_usability/views/mail.xml diff --git a/mail_usability/__openerp__.py b/mail_usability/__openerp__.py index 1a47d07..b97ea40 100644 --- a/mail_usability/__openerp__.py +++ b/mail_usability/__openerp__.py @@ -42,6 +42,6 @@ Small usability improvements on mails: 'author': 'Akretion', 'website': 'http://www.akretion.com', 'depends': ['mail'], - 'data': [], + 'data': ['views/mail.xml'], 'installable': True, } diff --git a/mail_usability/addons-mail-fix.diff b/mail_usability/addons-mail-fix.diff deleted file mode 100644 index b3e048d..0000000 --- a/mail_usability/addons-mail-fix.diff +++ /dev/null @@ -1,23 +0,0 @@ -diff --git a/addons/mail/static/src/js/many2many_tags_email.js b/addons/mail/static/src/js/many2many_tags_email.js -index c3985a3..307cc0c 100644 ---- a/addons/mail/static/src/js/many2many_tags_email.js -+++ b/addons/mail/static/src/js/many2many_tags_email.js -@@ -17,6 +17,9 @@ instance.web.form.FieldMany2ManyTagsEmail = instance.web.form.FieldMany2ManyTags - this.last_processed_value = []; - - this.on("change:value", this, this.on_change_value_check); -+ // Restore pop-up when trying to send to a partner without email -+ // Pending PR on odoo v8 https://github.com/odoo/odoo/pull/19466 -+ this.on_change_value_check(); - this._super.apply(this, arguments); - }, - -@@ -41,7 +44,7 @@ instance.web.form.FieldMany2ManyTagsEmail = instance.web.form.FieldMany2ManyTags - new instance.web.Model('res.partner').call("search", [[ - ["id", "in", ids], - ["email", "=", false], -- ["notify_email", "=", 'always'] ]], -+ ["notify_email", "in", ['always', 'all_except_notification']] ]], // Adapt code for mail_usability module - {context: this.build_context()}) - .then(function (record_ids) { - var popups_deferreds = []; diff --git a/mail_usability/static/src/js/many2many_tags_email.js b/mail_usability/static/src/js/many2many_tags_email.js new file mode 100644 index 0000000..a30018c --- /dev/null +++ b/mail_usability/static/src/js/many2many_tags_email.js @@ -0,0 +1,91 @@ +openerp_FieldMany2ManyTagsEmail = function(instance) { +var _t = instance.web._t; + +/** + * Extend of FieldMany2ManyTags widget method. + * When the user add a partner and the partner don't have an email, open a popup to purpose to add an email. + * The user can choose to add an email or cancel and close the popup. + */ +instance.web.form.FieldMany2ManyTagsEmail = instance.web.form.FieldMany2ManyTags.extend({ + + start: function() { + this.mutex = new openerp.Mutex(); + + // This widget will indirectly trigger a change:value to it's parent widget + // when setting the value of valid partners. For this reason we have to keep an + // internal state of the last value in order to compute the effective value changes. + this.last_processed_value = []; + + this.on("change:value", this, this.on_change_value_check); + this.on_change_value_check(); + this._super.apply(this, arguments); + }, + + on_change_value_check : function () { + var self = this; + var values = this.get('value').slice(0); // Clone the array + + // We only validate partners emails in case the value is not empty + // and is different from the last processed value + var effective_change = _.difference(values, self.last_processed_value).length; + if (values.length && effective_change) { + this.mutex.exec(function() { + return self._check_email_popup(values); + }); + } + }, + + _check_email_popup: function (ids) { + var self = this; + var valid_partners; + + new instance.web.Model('res.partner').call("search", [[ + ["id", "in", ids], + ["email", "=", false], + ["notify_email", "in", ['always', 'all_except_notification']]]], + {context: this.build_context()}) + .then(function (record_ids) { + var popups_deferreds = []; + self.valid_partners = _.difference(ids, record_ids); + + // Propose the user to correct invalid partners + _.each(record_ids, function (id) { + var popup_def = $.Deferred(); + popups_deferreds.push(popup_def); + + var pop = new instance.web.form.FormOpenPopup(self); + pop.show_element( + 'res.partner', + id, + self.build_context(), + { + title: _t("Please complete partner's informations and Email"), + } + ); + pop.on('write_completed', self, function () { + self.valid_partners.push(id); + }); + pop.on('closed', self, function () { + popup_def.resolve(); + }); + }); + return $.when.apply($, popups_deferreds).then(function() { + // All popups have been processed for the given ids + // It is now time to set the final value with valid partners ids. + var filtered_value = _.uniq(self.valid_partners); + self.last_processed_value = filtered_value; + self.set({'value': filtered_value}); + }); + }); + }, +}); + + +/** + * Registry of form fields + */ +instance.web.form.widgets = instance.web.form.widgets.extend({ + 'many2many_tags_email' : 'instance.web.form.FieldMany2ManyTagsEmail', +}); + +}; diff --git a/mail_usability/views/mail.xml b/mail_usability/views/mail.xml new file mode 100644 index 0000000..0698243 --- /dev/null +++ b/mail_usability/views/mail.xml @@ -0,0 +1,11 @@ + + + + + + +