Skip to content

Commit

Permalink
[FIX] fetchmail_*: adapt to Odoo 16.0 and python 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
NL66278 committed May 11, 2024
1 parent 7a7b1a6 commit 6a24dec
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 218 deletions.
1 change: 0 additions & 1 deletion fetchmail_attach_from_folder/match_algorithm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import email_exact
from . import email_domain
from . import odoo_standard
14 changes: 0 additions & 14 deletions fetchmail_attach_from_folder/match_algorithm/base.py

This file was deleted.

10 changes: 5 additions & 5 deletions fetchmail_attach_from_folder/match_algorithm/email_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class EmailDomain(EmailExact):
Beware of match_first here, this is most likely to get it wrong (gmail).
"""

def search_matches(self, folder, mail_message):
def search_matches(self, folder, message_dict):
"""Returns recordset of matching objects."""
matches = super().search_matches(folder, mail_message)
matches = super().search_matches(folder, message_dict)
if not matches:
object_model = folder.env[folder.model_id.model]
domains = []
for addr in self._get_mailaddresses(folder, mail_message):
for addr in self._get_mailaddresses(folder, message_dict):
domains.append(addr.split("@")[-1])
matches = object_model.search(
self._get_mailaddress_search_domain(
folder,
mail_message,
message_dict,
operator="like",
values=["%@" + domain for domain in set(domains)],
),
order=folder.model_order,
)
return matches
return matches.ids
21 changes: 10 additions & 11 deletions fetchmail_attach_from_folder/match_algorithm/email_exact.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
from odoo.tools.mail import email_split
from odoo.tools.safe_eval import safe_eval

from .base import Base


class EmailExact(Base):
class EmailExact:
"""Search for exactly the mailadress as noted in the email"""

def _get_mailaddresses(self, folder, mail_message):
def _get_mailaddresses(self, folder, message_dict):
mailaddresses = []
fields = folder.mail_field.split(",")
for field in fields:
if field in mail_message:
mailaddresses += email_split(mail_message[field])
if field in message_dict:
mailaddresses += email_split(message_dict[field])
return [addr.lower() for addr in mailaddresses]

def _get_mailaddress_search_domain(
self, folder, mail_message, operator="=", values=None
self, folder, message_dict, operator="=", values=None
):
mailaddresses = values or self._get_mailaddresses(folder, mail_message)
mailaddresses = values or self._get_mailaddresses(folder, message_dict)
if not mailaddresses:
return [(0, "=", 1)]
search_domain = (
Expand All @@ -30,8 +28,9 @@ def _get_mailaddress_search_domain(
)
return search_domain

def search_matches(self, folder, mail_message):
def search_matches(self, folder, message_dict):
"""Returns recordset of matching objects."""
object_model = folder.env[folder.model_id.model]
search_domain = self._get_mailaddress_search_domain(folder, mail_message)
return object_model.search(search_domain, order=folder.model_order)
search_domain = self._get_mailaddress_search_domain(folder, message_dict)
matches = object_model.search(search_domain, order=folder.model_order)
return matches.ids
23 changes: 0 additions & 23 deletions fetchmail_attach_from_folder/match_algorithm/odoo_standard.py

This file was deleted.

1 change: 1 addition & 0 deletions fetchmail_attach_from_folder/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import fetchmail_server
from . import fetchmail_server_folder
from . import mail_thread
12 changes: 8 additions & 4 deletions fetchmail_attach_from_folder/models/fetchmail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def _compute_folders_available(self):
"""Retrieve available folders from IMAP server."""

def parse_list_response(line):
flags, delimiter, mailbox_name = list_response_pattern.match(line).groups()
string_line = line.decode("utf-8")
flags, delimiter, mailbox_name = list_response_pattern.match(
string_line
).groups()
mailbox_name = mailbox_name.strip('"')
return (flags, delimiter, mailbox_name)

Expand All @@ -34,7 +37,7 @@ def parse_list_response(line):
continue
folders_available = []
for folder_entry in list_result[1]:
folders_available.append(parse_list_response(str(folder_entry))[2])
folders_available.append(parse_list_response(folder_entry)[2])
this.folders_available = "\n".join(folders_available)
connection.logout()

Expand All @@ -47,13 +50,14 @@ def parse_list_response(line):
string="Folders",
context={"active_test": False},
)
object_id = fields.Many2one(required=False) # comodel_name='ir.model'
server_type = fields.Selection(default="imap")
folders_only = fields.Boolean(
string="Only folders, not inbox",
help="Check this field to leave imap inbox alone"
" and only retrieve mail from configured folders.",
)
# Below existing fields, that are modified by this module.
object_id = fields.Many2one(required=False) # comodel_name='ir.model'
server_type = fields.Selection(default="imap")

@api.onchange("server_type", "is_ssl", "object_id")
def onchange_server_type(self):
Expand Down
Loading

0 comments on commit 6a24dec

Please sign in to comment.