[ADD] website_mail_group: re-add the <mail.group> 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
This commit is contained in:
std-odoo
2021-07-09 12:32:28 +00:00
committed by Thibault Delavallée
parent 2d359b909b
commit 8b814947d3
11 changed files with 269 additions and 3 deletions
@@ -84,4 +84,6 @@ publicWidget.registry.MailGroup = publicWidget.Widget.extend({
},
});
return publicWidget.registry.MailGroup;
});
@@ -18,7 +18,7 @@
</linearGradient>
</defs>
<g fill="none" fill-rule="evenodd" class="snippets_thumbs">
<g class="s_channel">
<g class="s_group">
<rect width="82" height="60" class="bg"/>
<g class="group" transform="translate(12 26)">
<g class="shape">

Before

Width:  |  Height:  |  Size: 3.3 KiB

After

Width:  |  Height:  |  Size: 3.3 KiB

+2 -2
View File
@@ -127,7 +127,7 @@
<t t-snippet="website.s_countdown" t-thumbnail="/website/static/src/img/snippets_thumbs/s_countdown.svg">
<keywords>celebration, launch</keywords>
</t>
<t id="mail_channel_discussion_group_hook"/>
<t id="mail_group_hook"/>
<t id="twitter_favorite_tweets_hook"/>
<t t-snippet="website.s_embed_code" t-thumbnail="/website/static/src/img/snippets_thumbs/s_embed_code.svg"/>
</div>
@@ -173,7 +173,7 @@
<xpath expr="//div[@id='snippet_effect']//t[@t-snippet][last()]" position="after">
<t t-install="website_form" string="Form Builder" t-thumbnail="/website/static/src/img/snippets_thumbs/s_website_form.svg"/>
<t id="newsletter_popup_snippet" t-install="mass_mailing" string="Newsletter Popup" t-thumbnail="/website/static/src/img/snippets_thumbs/newsletter_subscribe_popup.svg"/>
<t t-install="website_mail_channel" string="Discussion Group" t-thumbnail="/website/static/src/img/snippets_thumbs/s_channel.svg"/>
<t t-install="website_mail_group" string="Discussion Group" t-thumbnail="/website/static/src/img/snippets_thumbs/s_group.svg"/>
<t t-install="website_twitter" string="Twitter Scroller" t-thumbnail="/website/static/src/img/snippets_thumbs/s_twitter_scroll.svg"/>
</xpath>
<xpath expr="//div[@id='snippet_content']//t[@t-snippet][last()]" position="after">
+4
View File
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import controllers
+23
View File
@@ -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',
],
},
}
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import main
@@ -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,
}
@@ -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);
},
});
});
@@ -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: [''],
});
},
});
});
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="s_group" name="Discussion Group">
<div class="s_group o_mail_group"
data-id="0" data-object="mail.group" data-follow="off">
<div class="input-group o_mg_subscribe_form">
<input class="o_mg_subscribe_email form-control" type="email" name="email" placeholder="your email..."/>
<span class="input-group-append">
<button href="#" class="btn btn-primary o_mg_subscribe_btn">Subscribe</button>
</span>
</div>
</div>
</template>
<template id="s_group_options" inherit_id="website.snippet_options">
<xpath expr="." position="inside">
<div data-js='Group'
data-selector=".s_group"
data-drop-near="p, h1, h2, h3, blockquote, .card">
<we-row>
<we-select class="select_discussion_list" data-attribute-name="id" data-no-preview="true">
<!-- 'we-button' added programmatically with DB data -->
</we-select>
<we-button class="fa fa-fw fa-plus" title="Create a public discussion group in your backend"
data-create-group="" data-no-preview="true" data-name="create_mail_group_opt"/>
</we-row>
</div>
</xpath>
</template>
<record id="website_mail_group.s_group_000_js" model="ir.asset">
<field name="name">Group 000 JS</field>
<field name="bundle">web.assets_frontend</field>
<field name="path">website_mail_group/static/src/snippets/s_group/000.js</field>
</record>
</odoo>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="remove_external_snippets" inherit_id="website.external_snippets">
<xpath expr="//t[@t-install='website_mail_group']" position="replace"/>
</template>
<template id="snippets" inherit_id="website.snippets" name="Snippet Subscribe">
<xpath expr="//t[@id='mail_group_hook']" position="replace">
<t t-snippet="website_mail_group.s_group" t-thumbnail="/website/static/src/img/snippets_thumbs/s_group.svg"/>
</xpath>
</template>
</odoo>