Permalink
Browse files

[IMP] website_slides: invite improve me

  • Loading branch information...
awa-odoo committed Feb 8, 2019
1 parent 627ed0b commit 492b560775bde6a294bf0ce563440b49edf2e210
@@ -29,6 +29,7 @@
'views/website_slides_embed_templates.xml',
'views/slide_slide_views.xml',
'views/slide_channel_views.xml',
'views/slide_channel_invite_views.xml',
'views/website_slides_menu_views.xml',
'data/website_slides_ir_data.xml',
'data/mail_data.xml',
@@ -259,10 +259,10 @@ def slide_send_share_email(self, slide_id, email):
def slide_channel_join(self, channel_id):
if request.website.is_public_user():
return {'error': 'public_user'}
joined = request.env['slide.channel'].browse(channel_id).action_add_member()
if not joined:
join_success = request.env['slide.channel'].browse(channel_id).action_add_member()
if not join_success:
return {'error': 'join_done'}
return joined.ids
return join_success

@http.route(['/slides/dialog_preview'], type='json', auth='user', methods=['POST'], website=True)
def dialog_preview(self, **data):
@@ -60,5 +60,30 @@
<field name="default" eval="True"/>
<field name="description">Presentation Published</field>
</record>

<!-- 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 ${object.channel_id.name}</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 ${object.partner_id.name or 'participant'}<br/><br/>
You have been invited to join a new channel: ${object.channel_id.name}.

<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.
</a>
</div>
Thank you.
</p>
</div>
</field>
<field name="auto_delete" eval="True"/>
<field name="user_signature" eval="False"/>
</record>
</data>
</odoo>
@@ -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 odoo.tools.translate 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)],
}

@api.multi
def action_channel_invite(self):
self.ensure_one()

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(
self.env.context,
default_channel_id=self.id,
default_use_template=bool(template),
default_template_id=template and template.id or False,
notif_layout='mail.mail_notification_light',
)
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'slide.channel.invite',
'target': 'new',
'context': local_context,
}

# ---------------------------------------------------------
# ORM Overrides
# ---------------------------------------------------------
@@ -220,21 +246,22 @@ def message_post(self, parent_id=False, subtype=None, **kwargs):
# Rating Mixin API
# ---------------------------------------------------------

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

def _action_add_member(self, target_partner=False, **member_values):
existing = self.env['slide.channel.partner'].sudo().search([
('channel_id', 'in', self.ids),
('partner_id', '=', target_partner.id)
])
to_join = (self - existing.mapped('channel_id'))._filter_add_member(target_partner, **member_values)
if to_join:
self.env['slide.channel.partner'].sudo().create([
sudo_channel_partners = self.env['slide.channel.partner'].sudo().create([
dict(channel_id=channel.id, partner_id=target_partner.id, **member_values)
for channel in to_join
])
return to_join
return False
return sudo_channel_partners
return self.env['slide.channel.partner']

def _filter_add_member(self, target_partner, **member_values):
allowed = self.filtered(lambda channel: channel.visibility == 'public')
@@ -0,0 +1,123 @@
# -*- 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 = 'slide.channel.invite'
_description = 'Channel Invitation Wizard'

@api.model
def _get_default_from(self):
if self.env.user.email:
return formataddr((self.env.user.name, self.env.user.email))
raise UserError(_("Unable to post message, please configure the sender's email address."))

@api.model
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', 'survey_mail_compose_message_ir_attachments_rel', 'wizard_id', 'attachment_id',
string='Attachments')
template_id = fields.Many2one(
'mail.template', 'Use template', index=True,
domain="[('model', '=', 'survey.user_input')]")
# 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', 'survey_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('slide.channel', string='Slide channel', required=True)
channel_url = fields.Char(related="channel_id.website_url", readonly=True)

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

@api.model
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)

@api.multi
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 """
self.ensure_one()

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:
mail_values.append(self._prepare_mail_values(slide_channel_partner))

# TODO awa: change me to create multi when mail.mail supports it
for mail_value in mail_values:
self.env['mail.mail'].sudo().create(mail_value)

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, 'slide.channel.partner', slide_channel_partner.id, post_process=True)
body = self.env['mail.template']._render_template(self.body, 'slide.channel.partner', slide_channel_partner.id, post_process=True)
# post the message
mail_values = {
'email_from': self.email_from,
'author_id': self.author_id.id,
'model': None,
'res_id': None,
'subject': subject,
'body_html': body,
'attachment_ids': [(4, att.id) for att in self.attachment_ids],
'auto_delete': True,
'recipient_ids': [(4, slide_channel_partner.partner_id.id)]
}

# optional support of notif_layout in context
notif_layout = self.env.context.get('notif_layout', self.env.context.get('custom_layout'))
if notif_layout:
try:
template = self.env.ref(notif_layout, raise_if_not_found=True)
except ValueError:
_logger.warning('QWeb template %s not found when sending survey mails. Sending without layouting.' % (notif_layout))
else:
template_ctx = {
'message': self.env['mail.message'].sudo().new(dict(body=mail_values['body_html'], record_name=self.channel_id.name)),
'model_description': self.env['ir.model']._get('survey.survey').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"?>
<odoo>
<data>
<record model="ir.ui.view" id="slide_channel_invite_view_form">
<field name="name">slide.channel.invite.view.form</field>
<field name="model">slide.channel.invite</field>
<field name="arch" type="xml">
<form string="Compose Email">
<group col="1">
<group col="2">
<field name="partner_ids"
widget="many2many_tags_email"
placeholder="Add existing contacts..."
context="{'force_email':True, 'show_email':True, 'no_create_edit': True}"/>
</group>
<group col="2">
<field name="subject" placeholder="Subject..."/>
</group>
<field name="body" options="{'style-inline': true}"/>
<group>
<group>
<field name="attachment_ids" widget="many2many_binary"/>
</group>
<group>
<field name="template_id" label="Use template"/>
</group>
</group>
</group>
<footer>
<button string="Send" name="action_invite" type="object" class="btn-primary"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
</data>
</odoo>
@@ -47,6 +47,9 @@
<field name="model">slide.channel</field>
<field name="arch" type="xml">
<form string="Channels">
<header>
<button name="action_channel_invite" string="Invite" type="object" class="oe_highlight" attrs="{'invisible': [('visibility', '!=', 'invite')]}"/>
</header>
<sheet>
<div class="oe_button_box" name="button_box">
<button name="action_redirect_to_members"

0 comments on commit 492b560

Please sign in to comment.