Browse Source

flake8

pull/307/head
notze-nb2 6 years ago
parent
commit
93eb8f1b14
  1. 1
      mail_store_outgoing/__manifest__.py
  2. 27
      mail_store_outgoing/models/ir_mail_server.py
  3. 1
      mail_store_outgoing/tests/__init__.py
  4. 44
      mail_store_outgoing/tests/test_ir_mail.py

1
mail_store_outgoing/__manifest__.py

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Georg Notter, Agent ERP GmbH # Copyright 2017 Georg Notter, Agent ERP GmbH
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{ {

27
mail_store_outgoing/models/ir_mail_server.py

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import logging import logging
import re import re
import imaplib import imaplib
import threading
from odoo import tools from odoo import tools
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo import fields from odoo import fields
@ -10,6 +10,7 @@ from odoo import api
from odoo.tools.translate import _ from odoo.tools.translate import _
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
_test_logger = logging.getLogger('odoo.tests')
class IrMailServer(models.Model): class IrMailServer(models.Model):
@ -38,11 +39,16 @@ class IrMailServer(models.Model):
def test_imap_connection(self): def test_imap_connection(self):
self.ensure_one() self.ensure_one()
imap_pool = self.env['ir.mail.imap.folder'] imap_pool = self.env['ir.mail.imap.folder']
maillib = None
if self.has_separate_imap_server: if self.has_separate_imap_server:
smtp_server = self.separate_imap_server smtp_server = self.separate_imap_server
else: else:
smtp_server = self.smtp_host smtp_server = self.smtp_host
try: try:
if getattr(threading.currentThread(), 'testing', False) or \
self.env.registry.in_test_mode():
_test_logger.info("skip sending email in test mode")
return True
maillib = imaplib.IMAP4_SSL(smtp_server) maillib = imaplib.IMAP4_SSL(smtp_server)
maillib.login(self.smtp_user, self.smtp_pass) maillib.login(self.smtp_user, self.smtp_pass)
typ, mBoxes = maillib.list() typ, mBoxes = maillib.list()
@ -50,7 +56,8 @@ class IrMailServer(models.Model):
for folder in folder_ids: for folder in folder_ids:
folder.unlink() folder.unlink()
for line in mBoxes: for line in mBoxes:
flags, delimiter, mailbox_name = self.parse_list_response(line)
flags, delimiter, \
mailbox_name = self.parse_list_response(line)
res = {'server_id': self.id, 'name': mailbox_name, } res = {'server_id': self.id, 'name': mailbox_name, }
imap_pool.create(res) imap_pool.create(res)
self.write({'imap_mailbox_verified': True}) self.write({'imap_mailbox_verified': True})
@ -59,13 +66,14 @@ class IrMailServer(models.Model):
_("Connection Test Failed! " _("Connection Test Failed! "
"Here is what we got instead:\n %s") % tools.ustr(e)) "Here is what we got instead:\n %s") % tools.ustr(e))
finally: finally:
if maillib:
maillib.logout() maillib.logout()
@api.model @api.model
def send_email(self, message, mail_server_id=None, def send_email(self, message, mail_server_id=None,
smtp_server=None, smtp_port=None, smtp_user=None, smtp_server=None, smtp_port=None, smtp_user=None,
smtp_password=None, smtp_encryption=None, smtp_debug=False,
smtp_session=None):
smtp_password=None, smtp_encryption=None,
smtp_debug=False, smtp_session=None):
res = super(IrMailServer, self).send_email( res = super(IrMailServer, self).send_email(
message, mail_server_id, smtp_server, smtp_port, message, mail_server_id, smtp_server, smtp_port,
smtp_user, smtp_password, smtp_encryption, smtp_debug, smtp_user, smtp_password, smtp_encryption, smtp_debug,
@ -74,9 +82,10 @@ class IrMailServer(models.Model):
return res return res
@api.model @api.model
def _save_sent_message_to_sentbox(self, msg, mail_server_id):
def _save_sent_message_to_sentbox(self, msg, mail_server_id=None):
mail_server = None mail_server = None
smtp_server = None smtp_server = None
maillib = None
if mail_server_id: if mail_server_id:
mail_server = self.sudo().browse(mail_server_id) mail_server = self.sudo().browse(mail_server_id)
else: else:
@ -91,15 +100,21 @@ class IrMailServer(models.Model):
smtp_user = mail_server.smtp_user smtp_user = mail_server.smtp_user
smtp_password = mail_server.smtp_pass smtp_password = mail_server.smtp_pass
try: try:
if getattr(threading.currentThread(), 'testing', False) or \
self.env.registry.in_test_mode():
_test_logger.info("skip sending email in test mode")
return True
maillib = imaplib.IMAP4_SSL(smtp_server) maillib = imaplib.IMAP4_SSL(smtp_server)
maillib.login(smtp_user, smtp_password) maillib.login(smtp_user, smtp_password)
folder = mail_server.imap_mailbox_folder.name.join('""') folder = mail_server.imap_mailbox_folder.name.join('""')
maillib.append(str.encode(folder), r'\Seen', None, str(msg).encode())
maillib.append(str.encode(folder),
r'\Seen', None, str(msg).encode())
except Exception as ex: except Exception as ex:
_logger.error(_( _logger.error(_(
'Failed attaching mail via imap to server %s %s') 'Failed attaching mail via imap to server %s %s')
% (ex, msg)) % (ex, msg))
finally: finally:
if maillib:
maillib.logout() maillib.logout()
return True return True

1
mail_store_outgoing/tests/__init__.py

@ -1,2 +1 @@
from . import test_ir_mail from . import test_ir_mail

44
mail_store_outgoing/tests/test_ir_mail.py

@ -1,20 +1,56 @@
# -*- coding: utf-8 -*-
# Copyright 2018 AGENTERP GMBH # Copyright 2018 AGENTERP GMBH
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from openerp.tests.common import TransactionCase
from odoo.exceptions import ValidationError
from openerp.tests import common
class TestIrMail(TransactionCase):
class TestIrMail(common.TransactionCase):
def setUp(self): def setUp(self):
super(TestIrMail, self).setUp() super(TestIrMail, self).setUp()
self.mail_server = self.env['ir.mail_server'].create({
'smtp_port': '25',
'smtp_host': 'localhost',
'smtp_encryption': 'none',
'name': 'test',
'has_separate_imap_server': True,
'store_outgoing_mail': True,
})
def test_parse_list_response(self): def test_parse_list_response(self):
imap_mailbox = \ imap_mailbox = \
'(\\HasNoChildren \\UnMarked) "." "INBOX.Deleted Messages"'
b'(\\HasNoChildren \\UnMarked) "." "INBOX.Deleted Messages"'
flags, delimiter, mailbox_name = \ flags, delimiter, mailbox_name = \
self.env['ir.mail_server'].parse_list_response(imap_mailbox) self.env['ir.mail_server'].parse_list_response(imap_mailbox)
self.assertEqual(flags, '\\HasNoChildren \\UnMarked') self.assertEqual(flags, '\\HasNoChildren \\UnMarked')
self.assertEqual(delimiter, '.') self.assertEqual(delimiter, '.')
self.assertEqual(mailbox_name, 'INBOX.Deleted Messages') self.assertEqual(mailbox_name, 'INBOX.Deleted Messages')
def test_imap_connection(self):
try:
self.mail_server.test_imap_connection()
except ValidationError as e:
pass
def test_send_mail(self):
msg = self.env['ir.mail_server'].build_email(
email_from='test.from@example.com',
reply_to='test.reply@example.com',
email_to=["test.to@example.com"],
subject="Test Subject",
body="test Bosy",
)
self.env['ir.mail_server'].send_email(
msg, mail_server_id=self.mail_server.id)
def test_save_sent_message_to_sentbox(self):
msg = self.env['ir.mail_server'].build_email(
email_from='test.from@example.com',
reply_to='test.reply@example.com',
email_to=["test.to@example.com"],
subject="Test Subject",
body="test Bosy",
)
msg = self.env['ir.mail_server']._save_sent_message_to_sentbox(
msg, self.mail_server.id)
Loading…
Cancel
Save