[REF] refactor the code in order to split it in several file

This commit is contained in:
Sébastien BEAU
2019-01-11 11:07:29 +01:00
committed by Chafique
parent f13068d356
commit 5ec76d12d0
14 changed files with 159 additions and 133 deletions

View File

@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from . import mail
from . import tools
from . import mail_template
from . import mail_message
from . import models
from . import wizard

View File

@@ -26,8 +26,9 @@ Small usability improvements on mails:
'website': 'http://www.akretion.com',
'depends': ['mail'],
'data': [
'mail_view.xml',
'mail_data.xml',
'views/mail_view.xml',
'data/mail_data.xml',
'wizard/email_template_preview_view.xml',
],
'installable': True,
}

View File

@@ -1,108 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016-2017 Akretion (http://www.akretion.com)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields, api
class ResPartner(models.Model):
_inherit = 'res.partner'
notify_email = fields.Selection(
selection_add=[
('all_except_notification', 'All Messages Except Notifications')],
default='all_except_notification')
def _should_be_notify_by_email(self, message):
if message.message_type == 'notification':
if self.notify_email == 'always':
return True
else:
return False
else:
return True
def _notify_by_email(
self, message, force_send=False, send_after_commit=True,
user_signature=True):
# use an empty layout for notification by default
if not self._context.get('custom_layout'):
self = self.with_context(
custom_layout='mail_usability.mail_template_notification')
# Filter the partner that should receive the notification
filtered_partners = self.filtered(
lambda p: p._should_be_notify_by_email(message)
)
return super(ResPartner, filtered_partners)._notify_by_email(
message, force_send=force_send,
send_after_commit=send_after_commit,
user_signature=user_signature)
def _notify_prepare_email_values(self, message):
res = super(ResPartner, self)._notify_prepare_email_values(message)
# Never auto delete notification email
# fucking to hard to debug when message have been delete
res['auto_delete'] = False
return res
class TemplatePreview(models.TransientModel):
_inherit = "email_template.preview"
res_id = fields.Integer(compute='_compute_res_id')
object_id = fields.Reference(selection='_reference_models')
@api.model
def default_get(self, fields):
result = super(TemplatePreview, self).default_get(fields)
if result.get('model_id'):
model = self.env['ir.model'].browse(result['model_id'])
result['object_id'] = model.model
return result
def _reference_models(self):
result = self.default_get(['model_id'])
if result.get('model_id'):
model = self.env['ir.model'].browse(result['model_id'])
return [(model.model, model.name)]
else:
models = self.env['ir.model'].search([('state', '!=', 'manual')])
return [(model.model, model.name)
for model in models
if not model.model.startswith('ir.')]
@api.depends('object_id')
def _compute_res_id(self):
for record in self:
if self.object_id:
record.res_id = self.object_id.id
def send(self):
template = self.env['mail.template'].browse(
self._context['template_id'])
template.send_mail(
self.res_id, force_send=True, raise_exception=True)
class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
@api.multi
@api.returns('self', lambda value: value.id)
def message_post(self, body='', subject=None, message_type='notification',
subtype=None, parent_id=False, attachments=None,
content_subtype='html', **kwargs):
if not 'mail_create_nosubscribe' in self._context:
# Do not implicitly follow an object by just sending a message
self = self.with_context(mail_create_nosubscribe=True)
return super(MailThread,
self.with_context(mail_create_nosubscribe=True)
).message_post(
body=body, subject=subject, message_type=message_type,
subtype=subtype, parent_id=parent_id, attachments=attachments,
content_subtype=content_subtype, **kwargs)

View File

@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from . import mail
from . import tools
from . import mail_template
from . import mail_message
from . import res_partner

View File

@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016-2017 Akretion (http://www.akretion.com)
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, api
class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
@api.multi
@api.returns('self', lambda value: value.id)
def message_post(self, body='', subject=None, message_type='notification',
subtype=None, parent_id=False, attachments=None,
content_subtype='html', **kwargs):
if not 'mail_create_nosubscribe' in self._context:
# Do not implicitly follow an object by just sending a message
self = self.with_context(mail_create_nosubscribe=True)
return super(MailThread,
self.with_context(mail_create_nosubscribe=True)
).message_post(
body=body, subject=subject, message_type=message_type,
subtype=subtype, parent_id=parent_id, attachments=attachments,
content_subtype=content_subtype, **kwargs)

View File

@@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016-2019 Akretion (http://www.akretion.com)
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models, fields
class ResPartner(models.Model):
_inherit = 'res.partner'
notify_email = fields.Selection(
selection_add=[
('all_except_notification', 'All Messages Except Notifications')],
default='all_except_notification')
def _should_be_notify_by_email(self, message):
if message.message_type == 'notification':
if self.notify_email == 'always':
return True
else:
return False
else:
return True
def _notify_by_email(
self, message, force_send=False, send_after_commit=True,
user_signature=True):
# use an empty layout for notification by default
if not self._context.get('custom_layout'):
self = self.with_context(
custom_layout='mail_usability.mail_template_notification')
# Filter the partner that should receive the notification
filtered_partners = self.filtered(
lambda p: p._should_be_notify_by_email(message)
)
return super(ResPartner, filtered_partners)._notify_by_email(
message, force_send=force_send,
send_after_commit=send_after_commit,
user_signature=user_signature)
def _notify_prepare_email_values(self, message):
res = super(ResPartner, self)._notify_prepare_email_values(message)
# Never auto delete notification email
# fucking to hard to debug when message have been delete
res['auto_delete'] = False
return res

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
© 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="view_mail_tree" model="ir.ui.view">
<field name="model">mail.mail</field>
<field name="inherit_id" ref="mail.view_mail_tree"/>
<field name="arch" type="xml">
<field name="email_from" position="replace"/>
<field name="date" position="after">
<field name="email_from"/>
<field name="email_to"/>
</field>
</field>
</record>
</odoo>

View File

@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import email_template_preview

View File

@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Akretion (http://www.akretion.com).
# @author Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp import api, fields, models
class TemplatePreview(models.TransientModel):
_inherit = "email_template.preview"
res_id = fields.Integer(compute='_compute_res_id')
object_id = fields.Reference(selection='_reference_models')
@api.model
def default_get(self, fields):
result = super(TemplatePreview, self).default_get(fields)
if result.get('model_id'):
model = self.env['ir.model'].browse(result['model_id'])
result['object_id'] = model.model
return result
def _reference_models(self):
result = self.default_get(['model_id'])
if result.get('model_id'):
model = self.env['ir.model'].browse(result['model_id'])
return [(model.model, model.name)]
else:
models = self.env['ir.model'].search([('state', '!=', 'manual')])
return [(model.model, model.name)
for model in models
if not model.model.startswith('ir.')]
@api.depends('object_id')
def _compute_res_id(self):
for record in self:
if self.object_id:
record.res_id = self.object_id.id
def send(self):
template = self.env['mail.template'].browse(
self._context['template_id'])
template.send_mail(
self.res_id, force_send=True, raise_exception=True)

View File

@@ -1,24 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
© 2016 Akretion (Alexis de Lattre <alexis.delattre@akretion.com>)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_mail_tree" model="ir.ui.view">
<field name="name">mail_usability.mail.tree</field>
<field name="model">mail.mail</field>
<field name="inherit_id" ref="mail.view_mail_tree"/>
<field name="arch" type="xml">
<field name="email_from" position="replace"/>
<field name="date" position="after">
<field name="email_from"/>
<field name="email_to"/>
</field>
</field>
</record>
<record id="email_template_preview_form" model="ir.ui.view">
<field name="model">email_template.preview</field>
<field name="inherit_id" ref="mail.email_template_preview_form"/>
@@ -38,4 +20,5 @@
</footer>
</field>
</record>
</odoo>