diff options
| author | Thibault Delavallée <tde@odoo.com> | 2019-02-19 10:41:15 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2019-02-20 12:09:01 +0000 |
| commit | 9ae7f5a2612a2e69a32d33dca5ba78775a8b09a9 (patch) | |
| tree | 7ec70d946e415eda585be1cb673d4570d7c36795 /addons/website_slides/wizard | |
| parent | db6e8f368cd275bef012e21cfe7a7a39dbc607c4 (diff) | |
[IMP] website_slides: perform some code linting and re-order frontend templates
Purpose is to perform some linting in code. No functional change should
occur with this commit, only some code move. Diff may appear huge but
it is just because some files are split.
This cleaning allow to better understand code and templates architecture
now that eLearning feature has landed in Odoo Community. It will ease future
additions, modifications and fixes.
Containing :
* [REF] make some internal slide-related methods private as they are not
intended to be called by outside world;
* [MOV] new channel invite wizard in wizard folder;
* [MOV] split frontend templates in some files to better locate code. There is
now files for homepage, course, lesson, embed and utils in addition to
slide-related profile template;
* [REM] dead (unused) templates;
Commit linked to task ID 1941250 and PR #31272.
Diffstat (limited to 'addons/website_slides/wizard')
| -rw-r--r-- | addons/website_slides/wizard/__init__.py | 4 | ||||
| -rw-r--r-- | addons/website_slides/wizard/slide_channel_invite.py | 131 | ||||
| -rw-r--r-- | addons/website_slides/wizard/slide_channel_invite_views.xml | 37 |
3 files changed, 172 insertions, 0 deletions
diff --git a/addons/website_slides/wizard/__init__.py b/addons/website_slides/wizard/__init__.py new file mode 100644 index 00000000000..4020988908b --- /dev/null +++ b/addons/website_slides/wizard/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import slide_channel_invite diff --git a/addons/website_slides/wizard/slide_channel_invite.py b/addons/website_slides/wizard/slide_channel_invite.py new file mode 100644 index 00000000000..094e82f57b4 --- /dev/null +++ b/addons/website_slides/wizard/slide_channel_invite.py @@ -0,0 +1,131 @@ +# -*- 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, _ +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 _default_email_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 _default_author_id(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', string='Attachments') + template_id = fields.Many2one( + 'mail.template', 'Use template', + domain="[('model', '=', 'slide.channel.partner')]") + # origin + email_from = fields.Char('From', default=_default_email_from) + author_id = fields.Many2one( + 'res.partner', 'Author', + ondelete='set null', default=_default_author_id) + # recipients + partner_ids = fields.Many2many('res.partner', string='Recipients') + # 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): + """ Make the 'subject' and 'body' field match the selected template_id """ + if self.template_id: + self.subject = self.template_id.subject + self.body = self.template_id.body_html + + @api.onchange('partner_ids') + 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.' % + (','.join(invalid_partners.mapped('name'))))) + + @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_members(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 slide channel 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('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 diff --git a/addons/website_slides/wizard/slide_channel_invite_views.xml b/addons/website_slides/wizard/slide_channel_invite_views.xml new file mode 100644 index 00000000000..cdb9ddd79bf --- /dev/null +++ b/addons/website_slides/wizard/slide_channel_invite_views.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + <data> + <record id="slide_channel_invite_view_form" model="ir.ui.view"> + <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> |
