-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make attachments of other models in poweremail.template #177
Open
gpozo-gisce
wants to merge
9
commits into
v5_backport
Choose a base branch
from
69731_add_attach_into_mails
base: v5_backport
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e7bbc17
feat: get ids from the new columns to attach
gpozo-gisce 125d7dc
feat: add column necessary to make the attachments
gpozo-gisce 1218edb
test: add test to check if the attachments is created with the conten…
gpozo-gisce 92d1373
refactor: requested changes
gpozo-gisce fbcb7b7
Merge branch 'v5_backport' into 69731_add_attach_into_mails
gpozo-gisce 2b881e9
test: check attaches for specifics report attach values
gpozo-gisce 767a9d6
fix: create new function to evalute the new column conditions
gpozo-gisce 481a175
fix: change the function for wizard preview to generate_mail_sync()
gpozo-gisce ae9b0a0
Merge branch 'v5_backport' into 69731_add_attach_into_mails
gpozo-gisce File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from tools import config | ||
import pooler | ||
|
||
|
||
def up(cursor, installed_version): | ||
if not installed_version or config.updating_all: | ||
return | ||
|
||
pool = pooler.get_pool(cursor.dbname) | ||
|
||
pool.get("poweremail.templates")._auto_init(cursor, context={'module': 'poweremail'}) | ||
|
||
|
||
def down(cursor, installed_version): | ||
pass | ||
|
||
|
||
migrate = up |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .test_poweremail_mailbox import * | ||
from .test_poweremail_templates import * | ||
from .test_attach_other_models import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
# coding=utf-8 | ||
|
||
from destral.transaction import Transaction | ||
from destral import testing | ||
from mock import patch | ||
from base64 import b64decode | ||
|
||
class TestAttachOtherModels(testing.OOTestCase): | ||
""" | ||
Test the attachment of other models to the email | ||
""" | ||
|
||
def setUp(self): | ||
self.openerp.install_module('giscedata_facturacio') | ||
|
||
def _create_account(self, cursor, uid, extra_vals=None): | ||
acc_obj = self.openerp.pool.get('poweremail.core_accounts') | ||
|
||
vals = { | ||
'name': 'Test account', | ||
'user': uid, | ||
'email_id': '[email protected]', | ||
'smtpserver': 'smtp.example.com', | ||
'smtpport': 587, | ||
'smtpuname': 'test', | ||
'smtppass': 'test', | ||
'company': 'yes' | ||
} | ||
if extra_vals: | ||
vals.update(extra_vals) | ||
|
||
acc_id = acc_obj.create(cursor, uid, vals) | ||
return acc_id | ||
|
||
def _create_template(self, cursor, uid, extra_vals=None): | ||
if extra_vals is None: | ||
extra_vals = {} | ||
|
||
model = extra_vals['model'] if 'model' in extra_vals else None | ||
conditions = extra_vals['conditions'] if 'conditions' in extra_vals else None | ||
|
||
if model and conditions: | ||
report_template_object_reference_value = "('{model}', {conditions})".format( | ||
model=model, | ||
conditions=conditions) | ||
else: | ||
report_template_object_reference_value = None | ||
|
||
|
||
imd_obj = self.openerp.pool.get('ir.model.data') | ||
tmpl_obj = self.openerp.pool.get('poweremail.templates') | ||
acc_id = False | ||
if 'enforce_from_account' not in extra_vals: | ||
acc_id = self.create_account(cursor, uid) | ||
|
||
model_partner = imd_obj.get_object_reference( | ||
cursor, uid, 'base', 'model_res_partner' | ||
)[1] # Retunr ir.model id of res.partner | ||
|
||
# Agafem un report de demo | ||
report_id = imd_obj.get_object_reference( | ||
cursor, uid, 'base', 'report_test' | ||
)[1] | ||
|
||
vals = { | ||
'name': 'Test template', | ||
'object_name': model_partner, | ||
'enforce_from_account': acc_id, | ||
'template_language': 'mako', | ||
'def_to': 'Test to', | ||
'inline': True, | ||
'def_subject': 'Test subject', | ||
'def_cc': 'Test cc', | ||
'def_bcc': 'Test bcc', | ||
'def_body_text': 'Test body text', | ||
'def_priority': '2', | ||
'report_template': report_id, | ||
'report_template_object_reference': report_template_object_reference_value | ||
} | ||
|
||
if extra_vals: | ||
vals.update(extra_vals) | ||
|
||
tmpl_id = tmpl_obj.create(cursor, uid, vals) | ||
return tmpl_id | ||
|
||
@patch('poweremail.poweremail_template.poweremail_templates.create_report', return_value=("content_from_report_template_object_reference", "provapdf")) | ||
def test_generate_attachments_with_report_template_object_reference(self, extra_vals=None): | ||
""" | ||
Test the generation of attachments with a report template object reference | ||
|
||
NOTE: In this case only will generate one attachment, because the conditions are too restrictive to return anything | ||
""" | ||
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') | ||
|
||
acc1_id = self._create_account(cursor, uid, extra_vals={'name': 'acc1', 'email_id': '[email protected]'}) | ||
tmpl_id = self._create_template(cursor, uid, extra_vals={'enforce_from_account': acc1_id, | ||
'name': 'Test template 1', | ||
'model': 'giscedata.facturacio.factura', | ||
'conditions': [('invoice_id', '=', 1)]}) | ||
|
||
mailbox_id = pm_tmp_obj.generate_mail(cursor, uid, tmpl_id, [1], context={'raise_exception': True}) | ||
mail = mailbox_obj.simple_browse(cursor, uid, mailbox_id) | ||
for att in mail.pem_attachments_ids: | ||
self.assertEqual(b64decode(att.datas), 'content_from_report_template_object_reference') # datas is get from the 1r tuple value from create_report method, that in this case is mocked to avoid the real report generation | ||
|
||
@patch('poweremail.poweremail_template.poweremail_templates.create_report', return_value=("content_from_report_template_object_reference", "provapdf")) | ||
def test_generate_attachments_without_report_template_object_reference(self, mock_create_report , extra_vals=None): | ||
""" | ||
Test the generation of attachments without a report template object reference | ||
""" | ||
with Transaction().start(self.database) as txn: | ||
uid = txn.user | ||
cursor = txn.cursor | ||
pm_tmp_obj = self.openerp.pool.get('poweremail.templates') | ||
mailbox_obj = self.openerp.pool.get('poweremail.mailbox') | ||
|
||
acc1_id = self._create_account(cursor, uid, extra_vals={'name': 'acc1', 'email_id': '[email protected]'}) | ||
tmpl_id = self._create_template(cursor, uid, extra_vals={'enforce_from_account': acc1_id, | ||
'name': 'Test template 1', | ||
'model': None, | ||
'conditions': None}) # Without report_template_object_reference | ||
|
||
with patch.object(pm_tmp_obj, '_generate_attach_reports') as mock_generate_attach_reports: | ||
mock_generate_attach_reports.return_value = None | ||
with patch.object(pm_tmp_obj._generate_attach_reports, 'get_value') as mock_get_value_from_generate_attach_reports: # Mock the get_value from the _generate_attach_reports method | ||
mailbox_id = pm_tmp_obj.generate_mail(cursor, uid, tmpl_id, [1], context={'raise_exception': True}) | ||
mock_get_value_from_generate_attach_reports.assert_not_called() | ||
|
||
mail = mailbox_obj.simple_browse(cursor, uid, mailbox_id) | ||
for att in mail.pem_attachments_ids: | ||
self.assertEqual(b64decode(att.datas), 'content_from_report_template_object_reference') | ||
|
||
@patch('poweremail.poweremail_template.poweremail_templates.create_report', return_value=("content_from_report_template_object_reference", "provapdf")) | ||
def test_generate_attachments_with_user(self, mock_create_report, extra_vals=None): | ||
""" | ||
Test the generation of attachments with a user and invoice | ||
""" | ||
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') | ||
|
||
acc1_id = self._create_account(cursor, uid, extra_vals={'name': 'acc1', 'email_id': '[email protected]'}) | ||
tmpl_id = self._create_template(cursor, uid, extra_vals={'enforce_from_account': acc1_id, | ||
'name': 'Test template 1', | ||
'model': 'account.invoice', | ||
'conditions': [('partner_id', '=', 1)]}) # Tiny partner | ||
|
||
mailbox_id = pm_tmp_obj.generate_mail(cursor, uid, tmpl_id, [1], context={'raise_exception': True}) | ||
mail = mailbox_obj.simple_browse(cursor, uid, mailbox_id) | ||
|
||
for att in mail.pem_attachments_ids: | ||
self.assertEqual(b64decode(att.datas), 'content_from_report_template_object_reference') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using eval() can be unsafe if the input is not strictly controlled. Consider replacing it with ast.literal_eval to safely evaluate literal structures.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correcte és insegur utilitzar eval(), per això vam pensar d'usar el tools.safe_eval, però no era capaç d'interpretar el domain correctament.
@Ruben1700