-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
357 additions
and
20 deletions.
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
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
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,4 +1,5 @@ | ||
from emission_tests import * | ||
from investment_tests import * | ||
from partner_tests import * | ||
from somenergia_soci_tests import * | ||
from somenergia_soci_tests import * | ||
from test_wizard_baixa_soci 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
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,97 @@ | ||
# -*- coding: utf-8 -*- | ||
from destral import testing | ||
from destral.transaction import Transaction | ||
from osv import osv | ||
from datetime import datetime | ||
import poweremail | ||
import mock | ||
|
||
|
||
class TestWizardBaixaSoci(testing.OOTestCase): | ||
def setUp(self): | ||
self.txn = Transaction().start(self.database) | ||
self.cursor = self.txn.cursor | ||
self.uid = self.txn.user | ||
self.Investment = self.openerp.pool.get('generationkwh.investment') | ||
self.IrModelData = self.openerp.pool.get('ir.model.data') | ||
self.Soci = self.openerp.pool.get('somenergia.soci') | ||
self.WizardBaixaSoci = self.openerp.pool.get('wizard.baixa.soci') | ||
self.AccountObj = self.openerp.pool.get('poweremail.core_accounts') | ||
|
||
def tearDown(self): | ||
self.txn.stop() | ||
|
||
def test__baixa_soci__allowed(self): | ||
member_id = self.IrModelData.get_object_reference( | ||
self.cursor, self.uid, 'som_generationkwh', 'soci_0003' | ||
)[1] | ||
self.Soci.write(self.cursor, self.uid, [member_id], {'baixa': False, 'data_baixa_soci': None}) | ||
context = {'active_ids':[member_id]} | ||
|
||
wiz_id = self.WizardBaixaSoci.create(self.cursor, self.uid, {'info':''}, context=context) | ||
self.WizardBaixaSoci.baixa_soci(self.cursor, self.uid, wiz_id, context=context, send_mail=False) | ||
|
||
baixa = self.Soci.read(self.cursor, self.uid, member_id, ['baixa'])['baixa'] | ||
self.assertTrue(baixa) | ||
|
||
def test__baixa_soci__notAllowed(self): | ||
member_id = self.IrModelData.get_object_reference( | ||
self.cursor, self.uid, 'som_generationkwh', 'soci_0001' | ||
)[1] | ||
self.Soci.write(self.cursor, self.uid, [member_id], {'baixa': False, 'data_baixa_soci': None}) | ||
context = {'active_ids':[member_id]} | ||
|
||
wiz_id = self.WizardBaixaSoci.create(self.cursor, self.uid, {'info':''}, context=context) | ||
self.WizardBaixaSoci.baixa_soci(self.cursor, self.uid, wiz_id, context=context, send_mail=False) | ||
|
||
baixa = self.Soci.read(self.cursor, self.uid, member_id, ['baixa'])['baixa'] | ||
self.assertFalse(baixa) | ||
|
||
@mock.patch("poweremail.poweremail_send_wizard.poweremail_send_wizard.send_mail") | ||
def test__baixa_soci_and_send_mail__allowed(self, mocked_send_mail): | ||
member_id = self.IrModelData.get_object_reference( | ||
self.cursor, self.uid, 'som_generationkwh', 'soci_0003' | ||
)[1] | ||
self.Soci.write(self.cursor, self.uid, [member_id], {'baixa': False, 'data_baixa_soci': None}) | ||
context = {'active_ids':[member_id]} | ||
|
||
wiz_id = self.WizardBaixaSoci.create(self.cursor, self.uid, {'info':''}, context=context) | ||
self.WizardBaixaSoci.baixa_soci(self.cursor, self.uid, wiz_id, context=context, send_mail=True) | ||
|
||
baixa = self.Soci.read(self.cursor, self.uid, member_id, ['baixa'])['baixa'] | ||
self.assertTrue(baixa) | ||
|
||
template_id = self.IrModelData.get_object_reference( | ||
self.cursor, self.uid, 'som_generationkwh', 'email_baixa_soci' | ||
)[1] | ||
|
||
email_from = self.AccountObj.search(self.cursor, self.uid, [('name', '=', 'Info Som Energia')])[0] | ||
|
||
expected_ctx = { | ||
'active_ids': [member_id], | ||
'active_id': member_id, | ||
'template_id': template_id, | ||
'src_model': 'somenergia.soci', | ||
'src_rec_ids': [member_id], | ||
'from': email_from, | ||
'state': 'single', | ||
'priority': '0', | ||
} | ||
|
||
mocked_send_mail.assert_called_with(self.cursor, self.uid, mock.ANY, expected_ctx) | ||
|
||
@mock.patch("poweremail.poweremail_send_wizard.poweremail_send_wizard.send_mail") | ||
def test__baixa_soci_and_send_mail__notAllowed(self, mocked_send_mail): | ||
member_id = self.IrModelData.get_object_reference( | ||
self.cursor, self.uid, 'som_generationkwh', 'soci_0001' | ||
)[1] | ||
self.Soci.write(self.cursor, self.uid, [member_id], {'baixa': False, 'data_baixa_soci': None}) | ||
|
||
context = {'active_ids':[member_id]} | ||
|
||
wiz_id = self.WizardBaixaSoci.create(self.cursor, self.uid, {'info':''}, context=context) | ||
self.WizardBaixaSoci.baixa_soci_and_send_mail(self.cursor, self.uid, wiz_id, context=context) | ||
|
||
baixa = self.Soci.read(self.cursor, self.uid, member_id, ['baixa'])['baixa'] | ||
self.assertFalse(baixa) | ||
mocked_send_mail.assert_not_called() |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from osv import osv, fields | ||
from tools.translate import _ | ||
import logging | ||
|
||
class WizardBaixaSoci(osv.osv): | ||
|
||
_name = 'wizard.baixa.soci' | ||
_columns = { | ||
'state': fields.char('State', size=16), | ||
'info': fields.text('Info') | ||
} | ||
_defaults = { | ||
'state': 'init' | ||
} | ||
|
||
def baixa_soci(self, cursor, uid, ids, context=None, send_mail=False): | ||
soci_obj = self.pool.get('somenergia.soci') | ||
soci_id = context['active_ids'] | ||
|
||
if not isinstance(soci_id, list): | ||
soci_id = [soci_id] | ||
if len(soci_id) != 1: | ||
raise "Has de seleccionar un sol soci" | ||
|
||
try: | ||
soci_obj.verifica_baixa_soci(cursor, uid, soci_id[0], context) | ||
except osv.except_osv as e: | ||
self.write(cursor, uid, ids, {'state':'error', | ||
'info': e.message}) | ||
else: | ||
if send_mail: | ||
self.send_mail(cursor, uid, soci_id[0]) | ||
self.write(cursor, uid, ids, {'state':'ok'}) | ||
|
||
|
||
def baixa_soci_and_send_mail(self, cursor, uid, ids, context=None): | ||
self.baixa_soci(cursor, uid, ids, context, send_mail=True) | ||
|
||
def send_mail(self, cursor, uid, soci_id, context=None): | ||
ir_model_data = self.pool.get('ir.model.data') | ||
account_obj = self.pool.get('poweremail.core_accounts') | ||
power_email_tmpl_obj = self.pool.get('poweremail.templates') | ||
|
||
template_id = ir_model_data.get_object_reference( | ||
cursor, uid, 'som_generationkwh', 'email_baixa_soci' | ||
)[1] | ||
template = power_email_tmpl_obj.read(cursor, uid, template_id) | ||
|
||
email_from = False | ||
template_name = 'Info Som Energia' | ||
|
||
if template.get(template_name, False): | ||
email_from = template.get('enforce_from_account')[0] | ||
|
||
if not email_from: | ||
email_from = account_obj.search(cursor, uid, [('name', '=', template_name)])[0] | ||
|
||
email_params = dict({ | ||
'email_from': email_from, | ||
'template_id': template_id | ||
}) | ||
|
||
logger = logging.getLogger('openerp.poweremail') | ||
|
||
try: | ||
wiz_send_obj = self.pool.get('poweremail.send.wizard') | ||
ctx = { | ||
'active_ids': [soci_id], | ||
'active_id': soci_id, | ||
'template_id': email_params['template_id'], | ||
'src_model': 'somenergia.soci', | ||
'src_rec_ids': [soci_id], | ||
'from': email_params['email_from'], | ||
'state': 'single', | ||
'priority': '0', | ||
} | ||
|
||
params = {'state': 'single', 'priority': '0', 'from': ctx['from']} | ||
wiz_id = wiz_send_obj.create(cursor, uid, params, ctx) | ||
return wiz_send_obj.send_mail(cursor, uid, [wiz_id], ctx) | ||
|
||
except Exception as e: | ||
logger.info( | ||
'ERROR sending email to member {soci_id}: {exc}'.format( | ||
soci_id=soci_id, | ||
exc=e | ||
) | ||
) | ||
|
||
|
||
WizardBaixaSoci() | ||
|
Oops, something went wrong.