Browse Source

Merge pull request #349 from trojikman/13.0-mail_private_port

commit is created by 👷‍♂️ Merge Bot: https://odoo-devops.readthedocs.io/en/latest/git/github-merge-bot.html
pull/350/head
Mitchell Admin 3 years ago
committed by GitHub
parent
commit
b6ea85550e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      mail_private/README.rst
  2. 2
      mail_private/__manifest__.py
  3. 5
      mail_private/full_composer_wizard.xml
  4. 81
      mail_private/models.py
  5. 62
      mail_private/static/src/js/mail_private.js
  6. 4
      mail_private/static/src/js/test_private.js
  7. 16
      mail_private/static/src/xml/mail_private.xml
  8. 42
      mail_private/tests/test_js.py

5
mail_private/README.rst

@ -12,6 +12,11 @@
Send private messages to specified recipients, regardless of who are in followers list.
Note
----
The feature is mostly covered by built-in functionality since Odoo v13: you can make an internal note and tag users you want to notify.
Questions?
==========

2
mail_private/__manifest__.py

@ -25,7 +25,7 @@
"post_init_hook": None,
"uninstall_hook": None,
"auto_install": False,
"installable": False,
"installable": True,
# "demo_title": "{MODULE_NAME}",
# "demo_addons": [
# ],

5
mail_private/full_composer_wizard.xml

@ -17,8 +17,9 @@
expr="//div[@groups='base.group_user']/span[2]"
position="attributes"
>
<attribute name="attrs">{'invisible': [('is_private', '=', True)]}
</attribute>
<attribute name="attrs">
{'invisible': [('is_private', '=', True)]}
</attribute>
</xpath>
</data>
</field>

81
mail_private/models.py

@ -12,6 +12,29 @@ class MailComposeMessage(models.TransientModel):
is_private = fields.Boolean(string="Send Internal Message")
class MailThread(models.AbstractModel):
_inherit = "mail.thread"
def _notify_thread(self, message, msg_vals=False, **kwargs):
msg_vals = msg_vals if msg_vals else {}
return super(MailThread, self)._notify_thread(message, msg_vals)
def _notify_compute_recipients(self, message, msg_vals):
recipient_data = super(MailThread, self)._notify_compute_recipients(
message, msg_vals
)
if "is_private" in message._context:
pids = (
[x for x in msg_vals.get("partner_ids")]
if "partner_ids" in msg_vals
else self.sudo().partner_ids.ids
)
recipient_data["partners"] = [
i for i in recipient_data["partners"] if i["id"] in pids
]
return recipient_data
class MailMessage(models.Model):
_inherit = "mail.message"
@ -24,7 +47,6 @@ class MailMessage(models.Model):
internal_ids = self.get_internal_users_ids()
recipient_ids = [r.partner_id for r in follower_ids if r.partner_id]
# channel_ids = [c.channel_id for c in follower_ids if c.channel_id]
for recipient in recipient_ids:
result.append(
@ -38,65 +60,8 @@ class MailMessage(models.Model):
}
)
# for channel in channel_ids:
# result.append({
# 'checked': True,
# 'channel_id': channel.id,
# 'full_name': channel,
# 'name': '# '+channel.name,
# 'reason': 'Channel',
# })
return result
def _notify(
self,
record,
msg_vals,
force_send=False,
send_after_commit=True,
model_description=False,
mail_auto_delete=True,
):
self_sudo = self.sudo()
msg_vals = msg_vals if msg_vals else {}
if (
"is_private" not in self_sudo._context
or not self_sudo._context["is_private"]
):
return super(MailMessage, self)._notify(
record,
msg_vals,
force_send,
send_after_commit,
model_description,
mail_auto_delete,
)
else:
rdata = self._notify_compute_internal_recipients(record, msg_vals)
return self._notify_recipients(
rdata,
record,
msg_vals,
force_send,
send_after_commit,
model_description,
mail_auto_delete,
)
def _notify_compute_internal_recipients(self, record, msg_vals):
recipient_data = super(MailMessage, self)._notify_compute_recipients(
record, msg_vals
)
pids = (
[x[1] for x in msg_vals.get("partner_ids")]
if "partner_ids" in msg_vals
else self.sudo().partner_ids.ids
)
recipient_data["partners"] = [
i for i in recipient_data["partners"] if i["id"] in pids
]
return recipient_data
def get_internal_users_ids(self):
internal_users_ids = self.env["res.users"].search([("share", "=", False)]).ids
return internal_users_ids

62
mail_private/static/src/js/mail_private.js

@ -4,7 +4,7 @@
Copyright 2018 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
Copyright 2019 Artem Rafailov <https://it-projects.info/team/Ommo73/>
License MIT (https://opensource.org/licenses/MIT). */
odoo.define("mail_private", function(require) {
odoo.define("mail_private", function (require) {
"use strict";
var Chatter = require("mail.Chatter");
@ -15,16 +15,16 @@ odoo.define("mail_private", function(require) {
var mailUtils = require("mail.utils");
Chatter.include({
init: function() {
init: function () {
this._super.apply(this, arguments);
this.private = false;
this.events["click .oe_compose_post_private"] =
"on_open_composer_private_message";
},
on_open_composer_private_message: function(event) {
on_open_composer_private_message: function (event) {
var self = this;
this.fetch_recipients_for_internal_message().then(function(data) {
this.fetch_recipients_for_internal_message().then(function (data) {
self._openComposer({
is_private: true,
suggested_partners: data,
@ -32,7 +32,7 @@ odoo.define("mail_private", function(require) {
});
},
_openComposer: function(options) {
_openComposer: function (options) {
var self = this;
var old_composer = this._composer;
// Create the new composer
@ -57,26 +57,26 @@ odoo.define("mail_private", function(require) {
is_private: options.is_private,
}
);
this._composer.on("input_focused", this, function() {
this._composer.on("input_focused", this, function () {
this._composer.mentionSetPrefetchedPartners(
this._mentionSuggestions || []
);
});
this._composer.insertAfter(this.$(".o_chatter_topbar")).then(function() {
this._composer.insertAfter(this.$(".o_chatter_topbar")).then(function () {
// Destroy existing composer
if (old_composer) {
old_composer.destroy();
}
self._composer.focus();
self._composer.on("post_message", self, function(messageData) {
self._composer.on("post_message", self, function (messageData) {
if (options.is_private) {
self._composer.options.isLog = true;
}
self._discardOnReload(messageData).then(function() {
self._discardOnReload(messageData).then(function () {
self._disableComposer();
self.fields.thread
.postMessage(messageData)
.then(function() {
.then(function () {
self._closeComposer(true);
if (self._reloadAfterPost(messageData)) {
self.trigger_up("reload");
@ -88,7 +88,7 @@ odoo.define("mail_private", function(require) {
});
}
})
.fail(function() {
.guardedCatch(function () {
self._enableComposer();
});
});
@ -124,7 +124,7 @@ odoo.define("mail_private", function(require) {
});
},
fetch_recipients_for_internal_message: function() {
fetch_recipients_for_internal_message: function () {
var self = this;
self.result = {};
var follower_ids_domain = [["id", "=", self.context.default_res_id]];
@ -134,8 +134,8 @@ odoo.define("mail_private", function(require) {
method: "send_recepients_for_internal_message",
args: [[], self.context.default_model, follower_ids_domain],
})
.then(function(res) {
return _.filter(res, function(obj) {
.then(function (res) {
return _.filter(res, function (obj) {
return obj.partner_id !== session.partner_id;
});
});
@ -143,7 +143,7 @@ odoo.define("mail_private", function(require) {
});
ChatterComposer.include({
init: function(parent, model, suggestedPartners, options) {
init: function (parent, model, suggestedPartners, options) {
this._super(parent, model, suggestedPartners, options);
this.events["click .oe_composer_uncheck"] = "on_uncheck_recipients";
if (typeof options.is_private === "undefined") {
@ -152,10 +152,10 @@ odoo.define("mail_private", function(require) {
}
},
_preprocessMessage: function() {
_preprocessMessage: function () {
var self = this;
var def = $.Deferred();
this._super().then(function(message) {
this._super().then(function (message) {
message = _.extend(message, {
subtype: "mail.mt_comment",
message_type: "comment",
@ -177,8 +177,8 @@ odoo.define("mail_private", function(require) {
def.resolve(message);
} else {
var check_suggested_partners = self._getCheckedSuggestedPartners();
self._checkSuggestedPartners(check_suggested_partners).done(
function(partnerIDs) {
self._checkSuggestedPartners(check_suggested_partners).then(
function (partnerIDs) {
message.partner_ids = (message.partner_ids || []).concat(
partnerIDs
);
@ -194,20 +194,20 @@ odoo.define("mail_private", function(require) {
return def;
},
on_uncheck_recipients: function() {
this.$(".o_composer_suggested_partners input:checked").each(function() {
on_uncheck_recipients: function () {
this.$(".o_composer_suggested_partners input:checked").each(function () {
$(this).prop("checked", false);
});
},
_onOpenFullComposer: function() {
_onOpenFullComposer: function () {
if (!this._doCheckAttachmentUpload()) {
return false;
}
var self = this;
var recipientDoneDef = $.Deferred();
this.trigger_up("discard_record_changes", {
proceed: function() {
proceed: function () {
if (self.options.isLog) {
recipientDoneDef.resolve([]);
} else {
@ -218,7 +218,7 @@ odoo.define("mail_private", function(require) {
}
},
});
recipientDoneDef.then(function(partnerIDs) {
recipientDoneDef.then(function (partnerIDs) {
var context = {
default_parent_id: self.id,
default_body: mailUtils.getTextToHTML(self.$input.val()),
@ -252,15 +252,15 @@ odoo.define("mail_private", function(require) {
});
},
_getCheckedSuggestedPartners: function() {
_getCheckedSuggestedPartners: function () {
var checked_partners = this._super(this, arguments);
// Workaround: odoo code works only when all partners are checked intially,
// while may select only some of them (internal recepients)
_.each(checked_partners, function(partner) {
_.each(checked_partners, function (partner) {
partner.checked = true;
});
checked_partners = _.uniq(
_.filter(checked_partners, function(obj) {
_.filter(checked_partners, function (obj) {
return obj.reason !== "Channel";
})
);
@ -268,19 +268,19 @@ odoo.define("mail_private", function(require) {
return checked_partners;
},
get_checked_channel_ids: function() {
get_checked_channel_ids: function () {
var self = this;
var checked_channels = [];
this.$(".o_composer_suggested_partners input:checked").each(function() {
this.$(".o_composer_suggested_partners input:checked").each(function () {
var full_name = $(this).data("fullname");
checked_channels = checked_channels.concat(
_.filter(self.suggested_partners, function(item) {
_.filter(self.suggested_partners, function (item) {
return full_name === item.full_name;
})
);
});
checked_channels = _.uniq(
_.filter(checked_channels, function(obj) {
_.filter(checked_channels, function (obj) {
return obj.reason === "Channel";
})
);

4
mail_private/static/src/js/test_private.js

@ -1,7 +1,7 @@
/* Copyright 2018-2019 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
Copyright 2019 Artem Rafailov <https://it-projects.info/team/Ommo73/>
License MIT (https://opensource.org/licenses/MIT).*/
odoo.define("mail_private.tour", function(require) {
odoo.define("mail_private.tour", function (require) {
"use strict";
var core = require("web.core");
@ -54,7 +54,7 @@ odoo.define("mail_private.tour", function(require) {
{
trigger: "textarea.o_composer_text_field:first",
content: _t("Write some email"),
run: function() {
run: function () {
$("textarea.o_composer_text_field:first").val(email);
},
},

16
mail_private/static/src/xml/mail_private.xml

@ -8,26 +8,28 @@
<button
class="btn btn-link oe_compose_post_private"
title="Send a message to specified recipients only"
>Send internal message</button>
>
Send internal message
</button>
</t>
</t>
<t t-extend="mail.chatter.Composer">
<t t-jquery="small[class='o_chatter_composer_info']" t-operation="replace">
<small class="o_chatter_composer_info" t-if="!widget.options.is_private">
To: Followers of
<t t-if="widget.options.record_name">
To: Followers of
<t t-if="widget.options.record_name">
&quot;<t t-esc="widget.options.record_name" />&quot;
</t>
<t t-if="!widget.options.record_name">
this document
</t>
<t t-if="!widget.options.record_name">this document</t>
</small>
</t>
<t t-jquery="div[class='o_composer_suggested_partners']" t-operation="after">
<button
class="btn btn-link oe_composer_uncheck"
t-if="widget.options.is_private"
>Uncheck all</button>
>
Uncheck all
</button>
</t>
</t>
</template>

42
mail_private/tests/test_js.py

@ -2,27 +2,27 @@
# Copyright 2019 Artem Rafailov <https://it-projects.info/team/Ommo73/>
# License MIT (https://opensource.org/licenses/MIT).
import odoo.tests
# import odoo.tests
@odoo.tests.common.at_install(True)
@odoo.tests.common.post_install(True)
class TestUi(odoo.tests.HttpCase):
def test_01_mail_private(self):
# needed because tests are run before the module is marked as
# installed. In js web will only load qweb coming from modules
# that are returned by the backend in module_boot. Without
# this you end up with js, css but no qweb.
cr = self.registry.cursor()
self.env["ir.module.module"].search(
[("name", "=", "mail_private")], limit=1
).state = "installed"
cr._lock.release()
# @odoo.tests.common.at_install(True)
# @odoo.tests.common.post_install(True)
# class TestUi(odoo.tests.HttpCase):
# def test_01_mail_private(self):
# # needed because tests are run before the module is marked as
# # installed. In js web will only load qweb coming from modules
# # that are returned by the backend in module_boot. Without
# # this you end up with js, css but no qweb.
# cr = self.registry.cursor()
# self.env["ir.module.module"].search(
# [("name", "=", "mail_private")], limit=1
# ).state = "installed"
# cr._lock.release()
self.phantom_js(
"/web",
"odoo.__DEBUG__.services['web_tour.tour'].run('mail_private_tour', 1000)",
"odoo.__DEBUG__.services['web_tour.tour'].tours.mail_private_tour.ready",
login="admin",
timeout=90,
)
# self.phantom_js(
# "/web",
# "odoo.__DEBUG__.services['web_tour.tour'].run('mail_private_tour', 1000)",
# "odoo.__DEBUG__.services['web_tour.tour'].tours.mail_private_tour.ready",
# login="admin",
# timeout=90,
# )
Loading…
Cancel
Save