Skip to content

Commit

Permalink
[IMP] Added the possibility to indicate a server action to be executed
Browse files Browse the repository at this point in the history
for the matching record.
  • Loading branch information
zamberjo committed Jul 23, 2024
1 parent ab0bce4 commit 064dd35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
24 changes: 24 additions & 0 deletions fetchmail_attach_from_folder/models/fetchmail_server_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class FetchmailServerFolder(models.Model):
help="The state messages fetched from this folder should be assigned in Odoo",
)
active = fields.Boolean(default=True)
action_id = fields.Many2one(
comodel_name="ir.actions.server",
name="Server action",
help="Optional custom server action to trigger for each incoming "
"mail, on the record that was created or updated by this mail",
)

def button_confirm_folder(self):
self.write({"state": "draft"})
Expand Down Expand Up @@ -226,12 +232,30 @@ def apply_matching(self, connection, msgid):
if match:
thread_id = match.id
self.attach_mail(match, message_dict)
self.run_server_action(thread_id)
matched = True if thread_id else False
self.update_msg(connection, msgid, matched=matched)
if self.archive_path:
self._archive_msg(connection, msgid)
return thread_id # Can be None if no match found.

def run_server_action(self, matched_object_ids):
self.ensure_one()
action = self.action_id
if not action:
return
records = self.env[self.model_id.model].browse(matched_object_ids)
for record in records:
if not record.exists():
continue
action.with_context(
**{
"active_id": record.id,
"active_ids": record.ids,
"active_model": self.model_id.model,
}
).run()

def fetch_msg(self, connection, msgid):
"""Select a single message from a folder."""
self.ensure_one()
Expand Down
2 changes: 2 additions & 0 deletions fetchmail_attach_from_folder/views/fetchmail_server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<field name="path" />
<field name="archive_path" />
<field name="model_id" />
<field name="action_id" />
<field name="match_algorithm" />
<field name="model_field" />
<field name="mail_field" />
Expand Down Expand Up @@ -76,6 +77,7 @@
attrs="{'required':
[('match_algorithm','in',['email_exact','email_domain'])]}"
/>
<field name="action_id" />
<field
name="mail_field"
placeholder="to,from"
Expand Down

0 comments on commit 064dd35

Please sign in to comment.