Browse Source

flake8

pull/307/head
notze-nb2 6 years ago
parent
commit
93eb8f1b14
  1. 1
      mail_store_outgoing/__manifest__.py
  2. 33
      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
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{

33
mail_store_outgoing/models/ir_mail_server.py

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import logging
import re
import imaplib
import threading
from odoo import tools
from odoo.exceptions import ValidationError
from odoo import fields
@ -10,6 +10,7 @@ from odoo import api
from odoo.tools.translate import _
_logger = logging.getLogger(__name__)
_test_logger = logging.getLogger('odoo.tests')
class IrMailServer(models.Model):
@ -38,11 +39,16 @@ class IrMailServer(models.Model):
def test_imap_connection(self):
self.ensure_one()
imap_pool = self.env['ir.mail.imap.folder']
maillib = None
if self.has_separate_imap_server:
smtp_server = self.separate_imap_server
else:
smtp_server = self.smtp_host
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.login(self.smtp_user, self.smtp_pass)
typ, mBoxes = maillib.list()
@ -50,22 +56,24 @@ class IrMailServer(models.Model):
for folder in folder_ids:
folder.unlink()
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, }
imap_pool.create(res)
self.write({'imap_mailbox_verified': True})
except Exception as e:
raise ValidationError(
_("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:
maillib.logout()
if maillib:
maillib.logout()
@api.model
def send_email(self, message, mail_server_id=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(
message, mail_server_id, smtp_server, smtp_port,
smtp_user, smtp_password, smtp_encryption, smtp_debug,
@ -74,9 +82,10 @@ class IrMailServer(models.Model):
return res
@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
smtp_server = None
maillib = None
if mail_server_id:
mail_server = self.sudo().browse(mail_server_id)
else:
@ -91,16 +100,22 @@ class IrMailServer(models.Model):
smtp_user = mail_server.smtp_user
smtp_password = mail_server.smtp_pass
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.login(smtp_user, smtp_password)
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:
_logger.error(_(
'Failed attaching mail via imap to server %s %s')
% (ex, msg))
finally:
maillib.logout()
if maillib:
maillib.logout()
return True

1
mail_store_outgoing/tests/__init__.py

@ -1,2 +1 @@
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
# 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):
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):
imap_mailbox = \
'(\\HasNoChildren \\UnMarked) "." "INBOX.Deleted Messages"'
b'(\\HasNoChildren \\UnMarked) "." "INBOX.Deleted Messages"'
flags, delimiter, mailbox_name = \
self.env['ir.mail_server'].parse_list_response(imap_mailbox)
self.assertEqual(flags, '\\HasNoChildren \\UnMarked')
self.assertEqual(delimiter, '.')
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