From 8b814947d30e9aba25cc3200be369b4fa2e15d71 Mon Sep 17 00:00:00 2001 From: std-odoo Date: Mon, 5 Jul 2021 08:56:12 +0000 Subject: [PATCH] [ADD] website_mail_group: re-add the website snippets Purpose ======= This snippets was originally in the "Website Mail Channel" module, but it has been removed. The snippet allow the user to subscribe to a specific mailing list. If the current user has no access to the mailing list (because of ACL rule) the widget will be invisible for him. Links ===== Task-2510267 See odoo/pull/71599 See odoo/enterprise/pull/19296 See odoo/upgrade/pull/2600 --- addons/mail_group/static/src/js/mail_group.js | 2 + .../{s_channel.svg => s_group.svg} | 2 +- addons/website/views/snippets/snippets.xml | 4 +- addons/website_mail_group/__init__.py | 4 + addons/website_mail_group/__manifest__.py | 23 ++++ .../controllers/__init__.py | 4 + addons/website_mail_group/controllers/main.py | 34 ++++++ .../static/src/snippets/s_group/000.js | 49 +++++++++ .../static/src/snippets/s_group/options.js | 104 ++++++++++++++++++ .../views/snippets/s_group.xml | 34 ++++++ .../views/snippets/snippets.xml | 12 ++ 11 files changed, 269 insertions(+), 3 deletions(-) rename addons/website/static/src/img/snippets_thumbs/{s_channel.svg => s_group.svg} (99%) create mode 100644 addons/website_mail_group/__init__.py create mode 100644 addons/website_mail_group/__manifest__.py create mode 100644 addons/website_mail_group/controllers/__init__.py create mode 100644 addons/website_mail_group/controllers/main.py create mode 100644 addons/website_mail_group/static/src/snippets/s_group/000.js create mode 100644 addons/website_mail_group/static/src/snippets/s_group/options.js create mode 100644 addons/website_mail_group/views/snippets/s_group.xml create mode 100644 addons/website_mail_group/views/snippets/snippets.xml diff --git a/addons/mail_group/static/src/js/mail_group.js b/addons/mail_group/static/src/js/mail_group.js index 0437cd549b2..e5d6686d326 100644 --- a/addons/mail_group/static/src/js/mail_group.js +++ b/addons/mail_group/static/src/js/mail_group.js @@ -84,4 +84,6 @@ publicWidget.registry.MailGroup = publicWidget.Widget.extend({ }, }); + +return publicWidget.registry.MailGroup; }); diff --git a/addons/website/static/src/img/snippets_thumbs/s_channel.svg b/addons/website/static/src/img/snippets_thumbs/s_group.svg similarity index 99% rename from addons/website/static/src/img/snippets_thumbs/s_channel.svg rename to addons/website/static/src/img/snippets_thumbs/s_group.svg index 0e4f3d32ee3..430f1f0a584 100644 --- a/addons/website/static/src/img/snippets_thumbs/s_channel.svg +++ b/addons/website/static/src/img/snippets_thumbs/s_group.svg @@ -18,7 +18,7 @@ - + diff --git a/addons/website/views/snippets/snippets.xml b/addons/website/views/snippets/snippets.xml index fa0b6494473..8bb56ef67a0 100644 --- a/addons/website/views/snippets/snippets.xml +++ b/addons/website/views/snippets/snippets.xml @@ -127,7 +127,7 @@ celebration, launch - + @@ -173,7 +173,7 @@ - + diff --git a/addons/website_mail_group/__init__.py b/addons/website_mail_group/__init__.py new file mode 100644 index 00000000000..dd15b5c32e5 --- /dev/null +++ b/addons/website_mail_group/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import controllers diff --git a/addons/website_mail_group/__manifest__.py b/addons/website_mail_group/__manifest__.py new file mode 100644 index 00000000000..ea1bcd57e26 --- /dev/null +++ b/addons/website_mail_group/__manifest__.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +{ + 'name': "Website Mail Group", + 'summary': "Add a website snippet for the mail groups.", + 'description': "", + 'version': '1.0', + 'depends': ['mail_group', 'website'], + 'auto_install': True, + 'data': [ + 'views/snippets/s_group.xml', + 'views/snippets/snippets.xml', + ], + 'assets': { + 'website.assets_wysiwyg': [ + 'website_mail_group/static/src/snippets/s_group/options.js', + ], + 'web.assets_frontend': [ + 'website_mail_group/static/src/snippets/s_group/000.js', + ], + }, +} diff --git a/addons/website_mail_group/controllers/__init__.py b/addons/website_mail_group/controllers/__init__.py new file mode 100644 index 00000000000..5d4b25db9c0 --- /dev/null +++ b/addons/website_mail_group/controllers/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import main diff --git a/addons/website_mail_group/controllers/main.py b/addons/website_mail_group/controllers/main.py new file mode 100644 index 00000000000..5fe2c44c357 --- /dev/null +++ b/addons/website_mail_group/controllers/main.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import http +from odoo.exceptions import AccessError +from odoo.http import request + + +class WebsiteMailGroup(http.Controller): + @http.route('/groups/is_member', type='json', auth='public', website=True) + def groups_is_member(self, group_id=0, email=None, **kw): + """Return the email of the member if found, otherwise None.""" + group = request.env['mail.group'].browse(int(group_id)).exists() + if not group: + return + + try: + group.check_access_rights('read') + group.check_access_rule('read') + except AccessError: + return + + if not request.env.user._is_public(): + email = request.env.user.email + partner_id = request.env.user.partner_id.id + else: + partner_id = None + + member = group.sudo()._find_member(email, partner_id) + + return { + 'is_member': bool(member), + 'email': member.email if member else email, + } diff --git a/addons/website_mail_group/static/src/snippets/s_group/000.js b/addons/website_mail_group/static/src/snippets/s_group/000.js new file mode 100644 index 00000000000..a21346bd745 --- /dev/null +++ b/addons/website_mail_group/static/src/snippets/s_group/000.js @@ -0,0 +1,49 @@ +odoo.define('website_mail_group.mail_group', function (require) { +'use strict'; + +const core = require('web.core'); +const _t = core._t; +const MailGroup = require('mail_group.mail_group'); + +MailGroup.include({ + willStart: async function () { + await this._super(...arguments); + + // Can not be done in the template of the snippets + // Because it's rendered only once when the admin add the snippets + // for the first time, we make a RPC call to setup the widget properly + const email = (new URL(document.location.href)).searchParams.get('email'); + const response = await this._rpc({ + route: '/groups/is_member', + params: { + 'group_id': this.$target.data('id'), + 'email': email, + }, + }); + + if (!response) { + // We do not access to the mail group, just remove the widget + this.$el.empty(); + return; + } + + this.$el.removeClass('d-none'); + + const userEmail = response.email; + const isMember = response.is_member; + + if (userEmail && userEmail.length) { + const emailInput = this.$el.find('.o_mg_subscribe_email'); + emailInput.val(userEmail); + emailInput.attr('readonly', 1); + } + + if (isMember) { + this.$target.find('.o_mg_subscribe_btn').text(_t('Unsubscribe')); + } + + this.$el.data('isMember', isMember); + }, +}); + +}); diff --git a/addons/website_mail_group/static/src/snippets/s_group/options.js b/addons/website_mail_group/static/src/snippets/s_group/options.js new file mode 100644 index 00000000000..255d0c34b00 --- /dev/null +++ b/addons/website_mail_group/static/src/snippets/s_group/options.js @@ -0,0 +1,104 @@ +odoo.define('website_mail_group.s_group_options', function (require) { +'use strict'; + +const core = require('web.core'); +const options = require('web_editor.snippets.options'); +const wUtils = require('website.utils'); +const _t = core._t; + +options.registry.Group = options.Class.extend({ + /** + * @override + */ + async start() { + await this._super(...arguments); + this.mailGroups = await this._getMailGroups(); + }, + /** + * If we have already created groups => select the first one + * else => modal prompt (create a new group) + * + * @override + */ + onBuilt() { + if (this.mailGroups.length) { + this.$target[0].dataset.id = this.mailGroups[0][0]; + } else { + const widget = this._requestUserValueWidgets('create_mail_group_opt')[0]; + widget.$el.click(); + } + }, + + cleanForSave: function () { + // Hide the element by default, this class will be removed + // if the current user has access to the group + this.$target.addClass('d-none'); + + const emailInput = this.$el.find('.o_mg_subscribe_email'); + emailInput.val(''); + emailInput.removeAttr('readonly'); + }, + + //-------------------------------------------------------------------------- + // Options + //-------------------------------------------------------------------------- + + /** + * Creates a new mail.group through a modal prompt. + * + * @see this.selectClass for parameters + */ + createGroup: async function (previewMode, widgetValue, params) { + const result = await wUtils.prompt({ + id: "editor_new_mail_group_subscribe", + window_title: _t("New Mail Group"), + input: _t("Name"), + }); + + const name = result.val; + if (!name) { + return; + } + + const groupId = await this._rpc({ + model: 'mail.group', + method: 'create', + args: [{ + name: name, + }], + }); + + this.$target.attr("data-id", groupId); + return this._rerenderXML(); + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @override + */ + async _renderCustomXML(uiFragment) { + const groups = await this._getMailGroups(); + const menuEl = uiFragment.querySelector('.select_discussion_list'); + for (const group of groups) { + const el = document.createElement('we-button'); + el.dataset.selectDataAttribute = group[0]; + el.textContent = group[1]; + menuEl.appendChild(el); + } + }, + /** + * @private + * @return {Promise} + */ + _getMailGroups() { + return this._rpc({ + model: 'mail.group', + method: 'name_search', + args: [''], + }); + }, +}); +}); diff --git a/addons/website_mail_group/views/snippets/s_group.xml b/addons/website_mail_group/views/snippets/s_group.xml new file mode 100644 index 00000000000..141933909ea --- /dev/null +++ b/addons/website_mail_group/views/snippets/s_group.xml @@ -0,0 +1,34 @@ + + + + + + Group 000 JS + web.assets_frontend + website_mail_group/static/src/snippets/s_group/000.js + + diff --git a/addons/website_mail_group/views/snippets/snippets.xml b/addons/website_mail_group/views/snippets/snippets.xml new file mode 100644 index 00000000000..2b0e36edd81 --- /dev/null +++ b/addons/website_mail_group/views/snippets/snippets.xml @@ -0,0 +1,12 @@ + + + + + +