Browse files

[IMP] website_slides: add invite wizard for 'invite' slide_channel


Currently, the slide_channels having a visibility configured as 'invite' could
only add members manually.

This commit adds an 'Invite' button on top of the slide channel form that opens
a wizard allowing to send emails to contacts and also make them members of the
related slide channel.
  • Loading branch information...
awa-odoo committed Feb 8, 2019
1 parent 98b3f26 commit ee8167dd2f3ff1f165bbf3fd52649e710e535daf
@@ -29,6 +29,7 @@
@@ -60,5 +60,30 @@
<field name="default" eval="True"/>
<field name="description">Presentation Published</field>

<!-- Slide channel invite feature -->
<record id="mail_template_slide_channel_invite" model="mail.template">
<field name="name">Channel: Invite by email</field>
<field name="model_id" ref="model_slide_channel_partner" />
<field name="subject">You have been invited to join ${}</field>
<field name="body_html" type="html">
<div style="margin: 0px; padding: 0px; font-size: 13px;">
<p style="margin: 0px; padding: 0px; font-size: 13px;">
Dear ${ or 'participant'}<br/><br/>
You have been invited to join a new channel: ${}.

<div style="margin: 16px 0px 16px 0px;">
<a href="${(object.channel_id.website_url) | safe}"
style="background-color: #875A7B; padding: 8px 16px 8px 16px; text-decoration: none; color: #fff; border-radius: 5px; font-size:13px;">
Click here to see the content.
Thank you.
<field name="auto_delete" eval="True"/>
<field name="user_signature" eval="False"/>
@@ -3,5 +3,6 @@
from . import gamification_challenge
from . import slide_slide
from . import slide_channel
from . import slide_channel_invite
from . import res_config_settings
from . import website
@@ -5,6 +5,7 @@
from odoo import api, fields, models, SUPERUSER_ID, _
from odoo.addons.http_routing.models.ir_http import slug
from import html_translate
from odoo.exceptions import UserError
from odoo.osv import expression

@@ -163,6 +164,31 @@ def action_redirect_to_members(self):
'domain': [('channel_id', 'in', self.ids)],

def action_channel_invite(self):

if self.visibility != 'invite':
raise UserError(_("You cannot send invitations for channels that are not set as 'invite'."))

template = self.env.ref('website_slides.mail_template_slide_channel_invite', raise_if_not_found=False)

local_context = dict(
default_template_id=template and or False,
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': '',
'target': 'new',
'context': local_context,

# ---------------------------------------------------------
# ORM Overrides
# ---------------------------------------------------------
@@ -221,10 +247,9 @@ def message_post(self, parent_id=False, subtype=None, **kwargs):
# ---------------------------------------------------------

def action_add_member(self, **member_values):
new_cp = self._action_add_member(target_partner=self.env.user.partner_id, **member_values)
return bool(new_cp)
return bool(self._action_add_member(target_partner=self.env.user.partner, **member_values))

def _action_add_member(self, target_partner, **member_values):
def _action_add_member(self, target_partner=False, **member_values):
existing = self.env[''].sudo().search([
('channel_id', 'in', self.ids),
('partner_id', '=',
@@ -0,0 +1,137 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging
import re

from email.utils import formataddr

from odoo import api, fields, models, tools, _
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)

emails_split = re.compile(r"[;,\n\r]+")

class SlideChannelInvite(models.TransientModel):
_name = ''
_description = 'Channel Invitation Wizard'

def _get_default_from(self):
return formataddr((,
raise UserError(_("Unable to post message, please configure the sender's email address."))

def _get_default_author(self):
return self.env.user.partner_id

# composer content
subject = fields.Char('Subject')
body = fields.Html('Contents', default='', sanitize_style=True)
attachment_ids = fields.Many2many(
'ir.attachment', 'slide_channel_mail_compose_message_ir_attachments_rel', 'wizard_id', 'attachment_id',
template_id = fields.Many2one(
'mail.template', 'Use template', index=True,
domain="[('model', '=', '')]")
# origin
email_from = fields.Char('From', default=_get_default_from, help="Email address of the sender.")
author_id = fields.Many2one(
'res.partner', 'Author', index=True,
ondelete='set null', default=_get_default_author,
help="Author of the message.")
# recipients
partner_ids = fields.Many2many(
'res.partner', 'slide_channel_invite_partner_ids', 'invite_id', 'partner_id', string='Recipients')
# technical info
mail_server_id = fields.Many2one('ir.mail_server', 'Outgoing mail server')
# slide channel
channel_id = fields.Many2one('', string='Slide channel', required=True)
channel_url = fields.Char(related="channel_id.website_url", readonly=True)

def _onchange_template_id(self):
""" UPDATE ME """
if self.template_id:
self.subject = self.template_id.subject
self.body = self.template_id.body_html

def _onchange_partner_ids(self):
if self.partner_ids:
signup_allowed = self.env['res.users'].sudo()._get_signup_invitation_scope() == 'b2c'
if not signup_allowed:
invalid_partners = self.env['res.partner'].search([
('user_ids', '=', False),
('id', 'in', self.partner_ids.ids)
if invalid_partners:
raise UserError(
_('The following recipients have no user account: %s. You should create user accounts for them or allow external sign up in configuration.' %

def create(self, values):
if values.get('template_id') and not (values.get('body') or values.get('subject')):
template = self.env['mail.template'].browse(values['template_id'])
if not values.get('subject'):
values['subject'] = template.subject
if not values.get('body'):
values['body'] = template.body_html
return super(SlideChannelInvite, self).create(values)

def action_invite(self):
""" Process the wizard content and proceed with sending the related
email(s), rendering any template patterns on the fly if needed """

mail_values = []
for partner_id in self.partner_ids:
slide_channel_partner = self.channel_id._action_add_member(target_partner=partner_id)
if slide_channel_partner:

# TODO awa: change me to create multi when mail.mail supports it
for mail_value in mail_values:

return {'type': 'ir.actions.act_window_close'}

def _prepare_mail_values(self, slide_channel_partner):
""" Create mail specific for recipient """
subject = self.env['mail.template']._render_template(self.subject, '',, post_process=True)
body = self.env['mail.template']._render_template(self.body, '',, post_process=True)
# post the message
mail_values = {
'email_from': self.email_from,
'model': None,
'res_id': None,
'subject': subject,
'body_html': body,
'attachment_ids': [(4, for att in self.attachment_ids],
'auto_delete': True,
'recipient_ids': [(4,]

# optional support of notif_layout in context
notif_layout = self.env.context.get('notif_layout', self.env.context.get('custom_layout'))
if notif_layout:
template = self.env.ref(notif_layout, raise_if_not_found=True)
except ValueError:
_logger.warning('QWeb template %s not found when sending slide channel mails. Sending without layouting.' % (notif_layout))
template_ctx = {
'message': self.env['mail.message'].sudo().new(dict(body=mail_values['body_html'],,
'model_description': self.env['ir.model']._get('website_slides.slide_channel').display_name,
'company': self.env.user.company_id,
body = template.render(template_ctx, engine='ir.qweb', minimal_qcontext=True)
mail_values['body_html'] = self.env['mail.thread']._replace_local_links(body)

return mail_values
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<record model="ir.ui.view" id="slide_channel_invite_view_form">
<field name="name"></field>
<field name="model"></field>
<field name="arch" type="xml">
<form string="Compose Email">
<group col="1">
<group col="2">
<field name="partner_ids"
placeholder="Add existing contacts..."
context="{'force_email':True, 'show_email':True, 'no_create_edit': True}"/>
<group col="2">
<field name="subject" placeholder="Subject..."/>
<field name="body" options="{'style-inline': true}"/>
<field name="attachment_ids" widget="many2many_binary"/>
<field name="template_id" label="Use template"/>
<button string="Send" name="action_invite" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
@@ -47,6 +47,9 @@
<field name="model"></field>
<field name="arch" type="xml">
<form string="Channels">
<button name="action_channel_invite" string="Invite" type="object" class="oe_highlight" attrs="{'invisible': [('visibility', '!=', 'invite')]}"/>
<div class="oe_button_box" name="button_box">
<button name="action_redirect_to_members"

0 comments on commit ee8167d

Please sign in to comment.