Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v5_backport' into v5_backport
Browse files Browse the repository at this point in the history
  • Loading branch information
polsala committed Feb 14, 2025
2 parents a99f200 + 57f52d3 commit 7c8b0d1
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 2 deletions.
37 changes: 37 additions & 0 deletions migrations/5.0.24.5.0/post-0002_activar_inliner.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions migrations/5.0.24.5.0/pre-0001_instalar_premailer.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion poweremail_send_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -507,6 +512,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.
Expand Down
9 changes: 8 additions & 1 deletion poweremail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import tools
import report
import pooler
from premailer import transform
from .poweremail_core import get_email_default_lang, _priority_selection
from .utils import Localizer

Expand Down Expand Up @@ -527,6 +528,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"If the option is checked, the CSS will be inlined inside the HTML"),
'record_attachment_categories': fields.many2many('ir.attachment.category',
'template_attachment_category_rel',
'templ_id', 'categ_id',
Expand All @@ -543,7 +545,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!'))
Expand Down Expand Up @@ -1063,6 +1066,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']
Expand Down
1 change: 1 addition & 0 deletions poweremail_template_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<field name="single_email" colspan="4"/>
<field name="use_sign" colspan="4"/>
<field name="save_to_drafts" colspan="4"/>
<field name="inline" colspan="4"/>
</group>
<group>
<separator colspan="4" string="Allowed User Groups" />
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mako
qreu>=0.7.5
html2text
premailer==2.9.6
102 changes: 102 additions & 0 deletions tests/test_poweremail_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<html>
<style type="text/css">
h1 { border:1px solid black }
p { color:red;}
</style>
<h1 style="font-weight:bolder">Peter</h1>
<p>Hej</p>
</html>
"""

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': '[email protected]',
'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': '[email protected]',
'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 = '<html>\n<head></head>\n<body>\n<h1 style="border:1px solid black; font-weight:bolder">Peter</h1>\n<p style="color:red">Hej</p>\n</body>\n</html>\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)
Loading

0 comments on commit 7c8b0d1

Please sign in to comment.