From a80314f99febad9152bfe5cb06b1127da84859dc Mon Sep 17 00:00:00 2001 From: xavier Date: Thu, 23 May 2024 13:03:14 +0200 Subject: [PATCH 1/6] Add template body text optional inliner Co-authored-by: Daniel Estanyol i Torres --- poweremail_send_wizard.py | 10 +++++++++- poweremail_template.py | 8 +++++++- poweremail_template_view.xml | 1 + requirements.txt | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/poweremail_send_wizard.py b/poweremail_send_wizard.py index fb48b69..49a2e80 100644 --- a/poweremail_send_wizard.py +++ b/poweremail_send_wizard.py @@ -31,6 +31,7 @@ import tools from .poweremail_template import get_value from .poweremail_core import filter_send_emails, _priority_selection +from premailer import transform class poweremail_send_wizard(osv.osv_memory): @@ -138,7 +139,11 @@ def _get_template_value(self, cr, uid, field, context=None): if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets original template values for multiple email change return getattr(template, field) else: # Simple Mail: Gets computed template values - return self.get_value(cr, uid, template, getattr(template, field), context) + value = self.get_value(cr, uid, template, getattr(template, field), context) + if template.inline and field == 'def_body_text': + value = transform(value) + + return value _columns = { 'state':fields.selection([ @@ -495,6 +500,9 @@ def save_to_mailbox(self, cr, uid, ids, context=None): # Options:'multipart/mixed','multipart/alternative','text/plain','text/html' } ctx = context.copy() + if template.inline: + vals['pem_body_text'] = transform(vals['pem_body_text']) + mail_id = self.create_mail(cr, uid, screen_vals, src_rec_id, vals, context=ctx) mail_ids.append(mail_id) # Ensure report is rendered using template's language. If not found, user's launguage is used. diff --git a/poweremail_template.py b/poweremail_template.py index 7eb3e35..70d2f12 100644 --- a/poweremail_template.py +++ b/poweremail_template.py @@ -512,6 +512,7 @@ def _get_model_data_name_search( relation='ir.attachment', string='Attachments'), 'attach_record_items': fields.boolean('Attach record items', select=2, help=u"Si es marca aquesta opcio, s'enviaran com a fitxers adjunts del email tots els adjunts del registre utilitzat per renderitzar el email."), + 'inline': fields.boolean('Inline HTML', help=u"Si es marca aquesta opcio, l'html passara per un proces d'inline"), 'model_data_name': fields.function( _get_model_data_name, string='Code', type='char', size=250, method=True, @@ -523,7 +524,8 @@ def _get_model_data_name_search( _defaults = { 'ref_ir_act_window': False, 'ref_ir_value': False, - 'def_priority': lambda *a: '1' + 'def_priority': lambda *a: '1', + 'inline': lambda *a: False } _sql_constraints = [ ('name', 'unique (name)', _('The template name must be unique!')) @@ -1039,6 +1041,10 @@ def _generate_mailbox_item_from_template(self, cursor, user, template, record_id 'priority': template.def_priority, 'template_id': template.id, } + + if template.inline: + mailbox_values['pem_body_text'] = transform(mailbox_values['pem_body_text']) + #Use signatures if allowed if template.use_sign: sign = users_obj.read(cursor, user, user, ['signature'], context=context)['signature'] diff --git a/poweremail_template_view.xml b/poweremail_template_view.xml index b47074d..48e8cd2 100644 --- a/poweremail_template_view.xml +++ b/poweremail_template_view.xml @@ -130,6 +130,7 @@ + diff --git a/requirements.txt b/requirements.txt index f568bad..19b758f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ mako qreu>=0.7.5 html2text +premailer==2.9.6 From 628f3d55c1731e085caa836bd64e94f8a023e1ff Mon Sep 17 00:00:00 2001 From: destanyol Date: Fri, 24 May 2024 11:54:15 +0200 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=90=9B=20(inliner):=20Add=20missing?= =?UTF-8?q?=20import?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- poweremail_template.py | 1 + 1 file changed, 1 insertion(+) diff --git a/poweremail_template.py b/poweremail_template.py index 70d2f12..106e372 100644 --- a/poweremail_template.py +++ b/poweremail_template.py @@ -69,6 +69,7 @@ import tools import report import pooler +from premailer import transform from .poweremail_mailbox import _priority_selection from .poweremail_core import get_email_default_lang From 676cf0f309bd00fcb6d362a5e31a813e79b64fbc Mon Sep 17 00:00:00 2001 From: destanyol Date: Fri, 24 May 2024 11:54:44 +0200 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=94=A7=20(inliner):=20ADD=20inliner?= =?UTF-8?q?=20TESTS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_poweremail_mailbox.py | 102 ++++++++++++++++++ tests/test_poweremail_templates.py | 164 ++++++++++++++++++++++++++++- 2 files changed, 265 insertions(+), 1 deletion(-) diff --git a/tests/test_poweremail_mailbox.py b/tests/test_poweremail_mailbox.py index 285a371..033b244 100644 --- a/tests/test_poweremail_mailbox.py +++ b/tests/test_poweremail_mailbox.py @@ -538,3 +538,105 @@ def test_save_to_mailbox(self, mock_function, mock_function_2, mock_function_3, self.assertEqual(len(mail_created_vals['pem_attachments_ids']), 2) self.assertIn(attachment_report_id, mail_created_vals['pem_attachments_ids']) self.assertIn(attachment_id, mail_created_vals['pem_attachments_ids']) + + def test_save_to_mailbox_inlining(self): + with Transaction().start(self.database) as txn: + uid = txn.user + cursor = txn.cursor + mailbox_obj = self.openerp.pool.get('poweremail.mailbox') + pm_tmp_obj = self.openerp.pool.get('poweremail.templates') + imd_obj = self.openerp.pool.get('ir.model.data') + pw_account_obj = self.openerp.pool.get('poweremail.core_accounts') + send_wizard_obj = self.openerp.pool.get('poweremail.send.wizard') + + # Dummy value for an invoice id + fact_id = 6 + # Agafem un template de prova per posar a l'attachment + template_id = imd_obj.get_object_reference( + cursor, uid, 'poweremail', 'default_template_poweremail' + )[1] + + # Creem un wizard 'poweremail_send_wizard' + body_text = """ + + +

Peter

+

Hej

+ + """ + + wizard_vals = { + 'rel_model_ref': fact_id, + 'requested': 1, + 'from': 1, + 'attachment_ids': [], + 'body_text': body_text, + 'cc': False, + 'body_html': False, + 'bcc': False, + 'priority': '1', + 'to': 'example@example.org', + 'state': 'single', + 'ref_template': template_id, + 'single_email': 0, + 'rel_model': 301, + 'signature': False, + 'report': False, + 'subject': 'Factura electricidad False', + 'generated': False, + 'full_success': False, + } + + # Creem un mailbox + wizard_id = send_wizard_obj.create(cursor, uid, wizard_vals) + + pw_account_id = pw_account_obj.create(cursor, uid, { + 'name': 'test', + 'user': 1, + 'email_id': 'test@email', + 'smtpserver': 'smtp.gmail.com', + 'smtpport': '587', + 'company': 'no', + 'state': 'approved', + }) + + # Escribim el que necessitem als templates + template_vals = { + 'enforce_from_account': pw_account_id, + 'inline': True + } + pm_tmp_obj.write(cursor, uid, template_id, template_vals) + + mail_vals = { + 'pem_from': 'test@email', + 'pem_to': 'example@example.org', + 'pem_cc': False, + 'pem_bcc': False, + 'pem_subject': 'Factura electricidad False', + 'pem_body_text': body_text, + 'pem_body_html': False, + 'pem_account_id': 1, + 'priority': '1', + 'state': 'na', + } + + mailbox_obj.create(cursor, uid, mail_vals) + + context = {} + context['template_id'] = template_id + context['lang'] = False + context['src_rec_id'] = fact_id + context['tz'] = False + context['src_rec_ids'] = [fact_id] + context['active_ids'] = [fact_id] + context['type'] = 'out_invoice' + context['active_id'] = fact_id + + inlined_html = '\n\n\n

Peter

\n

Hej

\n\n\n' + + mail_ids = send_wizard_obj.save_to_mailbox(cursor, uid, [wizard_id], context=context) + pem_body_text = mailbox_obj.read(cursor, uid, mail_ids[0], ['pem_body_text'])['pem_body_text'] + self.assertEqual(pem_body_text, inlined_html) diff --git a/tests/test_poweremail_templates.py b/tests/test_poweremail_templates.py index dc1b2f0..daa6d87 100644 --- a/tests/test_poweremail_templates.py +++ b/tests/test_poweremail_templates.py @@ -96,4 +96,166 @@ def test_send_wizards_gets_default_priority_from_template(self): 'template_id': tmpl_id }) wiz = send_obj.browse(cursor, uid, wiz_id) - self.assertEqual(wiz.priority, '2') \ No newline at end of file + self.assertEqual(wiz.priority, '2') + + def test_inliner_from_template_send_wizard(self): + imd_obj = self.openerp.pool.get('ir.model.data') + tmpl_obj = self.openerp.pool.get('poweremail.templates') + send_obj = self.openerp.pool.get('poweremail.send.wizard') + + cursor = self.cursor + uid = self.uid + + partner_id = imd_obj.get_object_reference( + cursor, uid, 'base', 'res_partner_asus' + )[1] + tmpl_id = self.create_template() + + example_html = """ + + +

Peter

+

Hej

+ + """ + + write_vals = { + 'def_body_text': example_html, + 'inline': True + } + + tmpl_obj.write(cursor, uid, tmpl_id, write_vals) + + wiz_id = send_obj.create(cursor, uid, {}, context={ + 'active_id': partner_id, + 'active_ids': [partner_id], + 'src_rec_ids': [partner_id], + 'src_model': 'res.partner', + 'template_id': tmpl_id + }) + + wiz = send_obj.browse(cursor, uid, wiz_id) + + inlined_html = '\n\n\n

Peter

\n

Hej

\n\n\n' + + self.assertEqual(wiz.body_text, inlined_html) + + def test_no_inliner_from_template_send_wizard(self): + imd_obj = self.openerp.pool.get('ir.model.data') + tmpl_obj = self.openerp.pool.get('poweremail.templates') + send_obj = self.openerp.pool.get('poweremail.send.wizard') + + cursor = self.cursor + uid = self.uid + + partner_id = imd_obj.get_object_reference( + cursor, uid, 'base', 'res_partner_asus' + )[1] + tmpl_id = self.create_template() + + example_html = """ + + +

Peter

+

Hej

+ + """ + + write_vals = { + 'def_body_text': example_html, + 'inline': False + } + + tmpl_obj.write(cursor, uid, tmpl_id, write_vals) + + wiz_id = send_obj.create(cursor, uid, {}, context={ + 'active_id': partner_id, + 'active_ids': [partner_id], + 'src_rec_ids': [partner_id], + 'src_model': 'res.partner', + 'template_id': tmpl_id + }) + + wiz = send_obj.browse(cursor, uid, wiz_id) + + self.assertEqual(wiz.body_text, example_html) + + def test_no_inliner_from_template(self): + imd_obj = self.openerp.pool.get('ir.model.data') + tmpl_obj = self.openerp.pool.get('poweremail.templates') + mailbox_obj = self.openerp.pool.get('poweremail.mailbox') + + cursor = self.cursor + uid = self.uid + + partner_id = imd_obj.get_object_reference( + cursor, uid, 'base', 'res_partner_asus' + )[1] + tmpl_id = self.create_template() + + example_html = """ + + +

Peter

+

Hej

+ + """ + + write_vals = { + 'def_body_text': example_html, + 'inline': False + } + + tmpl_obj.write(cursor, uid, tmpl_id, write_vals) + + mail_id = tmpl_obj.generate_mail(cursor, uid, tmpl_id, [partner_id]) + pem_body_text = mailbox_obj.read(cursor, uid, mail_id, ['pem_body_text'])['pem_body_text'] + + self.assertEqual(pem_body_text, example_html) + + def test_inliner_from_template(self): + imd_obj = self.openerp.pool.get('ir.model.data') + tmpl_obj = self.openerp.pool.get('poweremail.templates') + mailbox_obj = self.openerp.pool.get('poweremail.mailbox') + + cursor = self.cursor + uid = self.uid + + partner_id = imd_obj.get_object_reference( + cursor, uid, 'base', 'res_partner_asus' + )[1] + tmpl_id = self.create_template() + + example_html = """ + + +

Peter

+

Hej

+ + """ + + write_vals = { + 'def_body_text': example_html, + 'inline': True + } + + tmpl_obj.write(cursor, uid, tmpl_id, write_vals) + + mail_id = tmpl_obj.generate_mail(cursor, uid, tmpl_id, [partner_id]) + pem_body_text = mailbox_obj.read(cursor, uid, mail_id, ['pem_body_text'])['pem_body_text'] + + inlined_html = '\n\n\n

Peter

\n

Hej

\n\n\n' + + self.assertEqual(pem_body_text, inlined_html) From 112efb28c2aee27f9f59dc2c2171e19374e65831 Mon Sep 17 00:00:00 2001 From: destanyol Date: Fri, 24 May 2024 11:55:14 +0200 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20(inliner):=20ADD=20?= =?UTF-8?q?migration=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5.0.24.5.0/post-0002_activar_inliner.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 migrations/5.0.24.5.0/post-0002_activar_inliner.py diff --git a/migrations/5.0.24.5.0/post-0002_activar_inliner.py b/migrations/5.0.24.5.0/post-0002_activar_inliner.py new file mode 100644 index 0000000..dc13288 --- /dev/null +++ b/migrations/5.0.24.5.0/post-0002_activar_inliner.py @@ -0,0 +1,37 @@ +# coding=utf-8 +import logging +import pooler + +from oopgrade.oopgrade import load_data_records + + +def up(cursor, installed_version): + if not installed_version: + return + + logger = logging.getLogger("openerp.migration") + logger.info("Creating pooler") + pool = pooler.get_pool(cursor.dbname) + + # Afegir columna a poweremail.templates + logger.info("Updating table table: poweremail.templates") + pool.get("poweremail.templates")._auto_init( + cursor, context={"module": "poweremail"} + ) + logger.info("Table updated succesfully.") + + list_of_records = [ + "poweremail_template_form", + ] + load_data_records( + cursor, "poweremail", "poweremail_template_view.xml", + list_of_records, mode="update" + ) + logger.info("poweremail_template_view.xml successfully updated") + + +def down(cursor, installed_version): + pass + + +migrate = up From b0f1cf02eb0aa67b5c06930f8d16bd5b621b22ee Mon Sep 17 00:00:00 2001 From: destanyol Date: Fri, 24 May 2024 12:11:37 +0200 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20(inlining):=20Insta?= =?UTF-8?q?ll=20package=20in=20migration=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- migrations/5.0.24.5.0/post-0002_activar_inliner.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/migrations/5.0.24.5.0/post-0002_activar_inliner.py b/migrations/5.0.24.5.0/post-0002_activar_inliner.py index dc13288..e989e65 100644 --- a/migrations/5.0.24.5.0/post-0002_activar_inliner.py +++ b/migrations/5.0.24.5.0/post-0002_activar_inliner.py @@ -1,6 +1,7 @@ # coding=utf-8 import logging import pooler +from tools import pip_install from oopgrade.oopgrade import load_data_records @@ -29,6 +30,10 @@ def up(cursor, installed_version): ) logger.info("poweremail_template_view.xml successfully updated") + logger.info('Installing premailer package...') + pip_install('premailer==2.9.6', '--force') + logger.info('Premailer package installed successfully!') + def down(cursor, installed_version): pass From 6f7ea193f841507ea4db418d63667559563b813f Mon Sep 17 00:00:00 2001 From: destanyol Date: Mon, 27 May 2024 11:57:40 +0200 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20(inliner):=20Fix=20migration?= =?UTF-8?q?=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5.0.24.5.0/post-0002_activar_inliner.py | 5 ----- .../5.0.24.5.0/pre-0001_instalar_premailer.py | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 migrations/5.0.24.5.0/pre-0001_instalar_premailer.py diff --git a/migrations/5.0.24.5.0/post-0002_activar_inliner.py b/migrations/5.0.24.5.0/post-0002_activar_inliner.py index e989e65..dc13288 100644 --- a/migrations/5.0.24.5.0/post-0002_activar_inliner.py +++ b/migrations/5.0.24.5.0/post-0002_activar_inliner.py @@ -1,7 +1,6 @@ # coding=utf-8 import logging import pooler -from tools import pip_install from oopgrade.oopgrade import load_data_records @@ -30,10 +29,6 @@ def up(cursor, installed_version): ) logger.info("poweremail_template_view.xml successfully updated") - logger.info('Installing premailer package...') - pip_install('premailer==2.9.6', '--force') - logger.info('Premailer package installed successfully!') - def down(cursor, installed_version): pass diff --git a/migrations/5.0.24.5.0/pre-0001_instalar_premailer.py b/migrations/5.0.24.5.0/pre-0001_instalar_premailer.py new file mode 100644 index 0000000..a44475b --- /dev/null +++ b/migrations/5.0.24.5.0/pre-0001_instalar_premailer.py @@ -0,0 +1,20 @@ +# coding=utf-8 +import logging +from tools import pip_install + + +def up(cursor, installed_version): + if not installed_version: + return + + logger = logging.getLogger("openerp.migration") + logger.info('Installing premailer package...') + pip_install('premailer==2.9.6', '--force') + logger.info('Premailer package installed successfully!') + + +def down(cursor, installed_version): + pass + + +migrate = up