diff --git a/bemade_mailcow_blacklist/__manifest__.py b/bemade_mailcow_blacklist/__manifest__.py index 515e756..89b67d5 100644 --- a/bemade_mailcow_blacklist/__manifest__.py +++ b/bemade_mailcow_blacklist/__manifest__.py @@ -34,9 +34,13 @@ Main Features: "assets": { "web.assets_backend": [ "bemade_mailcow_blacklist/static/src/js/mailcow_mailbox.js", + "bemade_mailcow_blacklist/static/src/js/mailcow_alias.js", + "bemade_mailcow_blacklist/static/src/js/mailcow_blacklist.js", ], "web.assets_qweb": [ "bemade_mailcow_blacklist/static/src/xml/mailcow_mailbox.xml", + "bemade_mailcow_blacklist/static/src/xml/mailcow_alias.xml", + "bemade_mailcow_blacklist/static/src/xml/mailcow_blacklist.xml", ], }, 'demo': [], diff --git a/bemade_mailcow_blacklist/models/mailcow_mailbox.py b/bemade_mailcow_blacklist/models/mailcow_mailbox.py index 64d8b45..920db47 100644 --- a/bemade_mailcow_blacklist/models/mailcow_mailbox.py +++ b/bemade_mailcow_blacklist/models/mailcow_mailbox.py @@ -34,17 +34,18 @@ class MailcowMailbox(models.Model): """ endpoint = '/api/v1/get/mailbox/all' data = self.api_request(endpoint) + domain = self.env['ir.config_parameter'].sudo().get_param('mail.catchall.domain') if data: for item in data: - domain = self.env['ir.config_parameter'].sudo().get_param('web.base.url') if item['domain'] == domain: - self.create({ - 'name': item['name'], - 'local_part': item['local_part'], - 'domain': item['domain'], - 'active': item['active'], - 'password': item['password'], - }) + mailbox = self.env['mail.mailcow.mailbox'].search([('address', '=', f"{item['local_part']}@{item['domain']}")]) + if not mailbox: + self.create({ + 'name': item['name'], + 'local_part': item['local_part'], + 'domain': item['domain'], + 'active': item['active'], + }) @api.model def create(self, vals): @@ -101,9 +102,15 @@ class MailcowMailbox(models.Model): """ Override the unlink function to delete a Mailcow mailbox whenever a user is deleted in Odoo. """ - endpoint = f'/api/v1/delete/mailbox/{self.address}' - self.api_request(endpoint, method='POST') - _logger.info(f'Mailbox {self.address} has been deleted on Mailcow server') + + for mailbox in self: + data = { + 'username': mailbox.address + } + + endpoint = f'/api/v1/delete/mailbox' + mailbox.api_request(endpoint, method='POST', data=data) + _logger.info(f'Mailbox {mailbox.address} has been deleted on Mailcow server') return super().unlink() diff --git a/bemade_mailcow_blacklist/static/src/js/mailcow_alias.js b/bemade_mailcow_blacklist/static/src/js/mailcow_alias.js new file mode 100644 index 0000000..40f9cce --- /dev/null +++ b/bemade_mailcow_blacklist/static/src/js/mailcow_alias.js @@ -0,0 +1,31 @@ +/** @odoo-module **/ + +import ListController from 'web.ListController'; +import ListView from 'web.ListView'; +const viewRegistry = require('web.view_registry'); + +const AliasesListController = ListController.extend({ + // buttons_template must match the t-name on the template for the button (static xml) + buttons_template: 'mail.mailcow_aliases_list_view_buttons', + events: _.extend({}, ListController.prototype.events, { + 'click .o_button_sync_aliases': '_onSyncAliases', + }), + // This may need to be async function() if there needs to be an await this._rpc({ ... }); call to not reload early + _onSyncAliases: function () { + this._rpc({ + model: 'mail.mailcow.alias', + method: 'sync_aliases', + args: [], + }); + // Couldn't test this for real, but it runs the action. May need a this.reload() + }, +}); + +const AliasesListView = ListView.extend({ + config: _.extend({}, ListView.prototype.config, { + Controller: AliasesListController, + }), +}); + +// key must match with the js_class attribute of the tree view you want to modify +viewRegistry.add('mail_mailcow_aliases_tree', AliasesListView); diff --git a/bemade_mailcow_blacklist/static/src/js/mailcow_blacklist.js b/bemade_mailcow_blacklist/static/src/js/mailcow_blacklist.js new file mode 100644 index 0000000..41186c9 --- /dev/null +++ b/bemade_mailcow_blacklist/static/src/js/mailcow_blacklist.js @@ -0,0 +1,31 @@ +/** @odoo-module **/ + +import ListController from 'web.ListController'; +import ListView from 'web.ListView'; +const viewRegistry = require('web.view_registry'); + +const MailboxesListController = ListController.extend({ + // buttons_template must match the t-name on the template for the button (static xml) + buttons_template: 'mail.mailcow_mailbox_list_view_buttons', + events: _.extend({}, ListController.prototype.events, { + 'click .o_button_sync_mailboxes': '_onSyncMailboxes', + }), + // This may need to be async function() if there needs to be an await this._rpc({ ... }); call to not reload early + _onSyncMailboxes: function () { + this._rpc({ + model: 'mail.mailcow.mailbox', + method: 'sync_mailboxes', + args: [], + }); + // Couldn't test this for real, but it runs the action. May need a this.reload() + }, +}); + +const MailboxesListView = ListView.extend({ + config: _.extend({}, ListView.prototype.config, { + Controller: MailboxesListController, + }), +}); + +// key must match with the js_class attribute of the tree view you want to modify +viewRegistry.add('mail_mailcow_mailbox_tree', MailboxesListView); diff --git a/bemade_mailcow_blacklist/static/src/xml/mailcow_alias.xml b/bemade_mailcow_blacklist/static/src/xml/mailcow_alias.xml new file mode 100644 index 0000000..579bcb4 --- /dev/null +++ b/bemade_mailcow_blacklist/static/src/xml/mailcow_alias.xml @@ -0,0 +1,10 @@ + + + + + Sync Aliases + + + \ No newline at end of file diff --git a/bemade_mailcow_blacklist/static/src/xml/mailcow_blacklist.xml b/bemade_mailcow_blacklist/static/src/xml/mailcow_blacklist.xml new file mode 100644 index 0000000..48e1b06 --- /dev/null +++ b/bemade_mailcow_blacklist/static/src/xml/mailcow_blacklist.xml @@ -0,0 +1,10 @@ + + + + + Sync Blacklist + + + \ No newline at end of file diff --git a/bemade_mailcow_blacklist/views/mailcow_alias_views.xml b/bemade_mailcow_blacklist/views/mailcow_alias_views.xml index a4c48fb..457168d 100644 --- a/bemade_mailcow_blacklist/views/mailcow_alias_views.xml +++ b/bemade_mailcow_blacklist/views/mailcow_alias_views.xml @@ -35,7 +35,7 @@ - + Aliases ir.actions.act_window mail.mailcow.alias @@ -53,6 +53,21 @@ + + + Sync Aliases + + code + model.sync_aliases() + + + + + tree + + + + diff --git a/bemade_mailcow_blacklist/views/mailcow_blacklist_views.xml b/bemade_mailcow_blacklist/views/mailcow_blacklist_views.xml index 0a4de07..9c09a78 100644 --- a/bemade_mailcow_blacklist/views/mailcow_blacklist_views.xml +++ b/bemade_mailcow_blacklist/views/mailcow_blacklist_views.xml @@ -29,7 +29,7 @@ - + Blacklist ir.actions.act_window mail.mailcow.blacklist @@ -47,6 +47,20 @@ + + + Sync Blacklist + + code + model.sync_blacklist() + + + + + tree + + + diff --git a/bemade_mailcow_blacklist/views/res_config_settings_views.xml b/bemade_mailcow_blacklist/views/res_config_settings_views.xml index 98cc026..12392f9 100644 --- a/bemade_mailcow_blacklist/views/res_config_settings_views.xml +++ b/bemade_mailcow_blacklist/views/res_config_settings_views.xml @@ -5,35 +5,29 @@ res.config.settings - - + + Mailcow Settings - - + + + Base URL for Mailcow server - - - - API key for Mailcow server - - - - - + - - - - Base URL for Mailcow server - + + + Mailcow API Key + + + - +