javascipt working

This commit is contained in:
Benoît Vézina 2023-06-30 13:03:57 -04:00
parent e17f32dfaf
commit cf124a758f
9 changed files with 150 additions and 34 deletions

View file

@ -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': [],

View file

@ -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()

View file

@ -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);

View file

@ -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);

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates>
<t t-extend="ListView.buttons" t-name="mail.mailcow_alias_list_view_buttons"> <!-- t-name must match with js controller button template -->
<t t-jquery="button.o_list_button_add" t-operation="after">
<button type="button"
class="btn btn-primary ml4 o_button_sync_aliases"
title="Sync Aliases"> Sync Aliases </button>
</t>
</t>
</templates>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates>
<t t-extend="ListView.buttons" t-name="mail.mailcow_blacklist_list_view_buttons"> <!-- t-name must match with js controller button template -->
<t t-jquery="button.o_list_button_add" t-operation="after">
<button type="button"
class="btn btn-primary ml4 o_button_sync_blacklist"
title="Sync Blacklist"> Sync Blacklist </button>
</t>
</t>
</templates>

View file

@ -35,7 +35,7 @@
</field>
</record>
<record id="mailcow_alias_action" model="ir.actions.act_window">
<record id="action_mailcow_alias" model="ir.actions.act_window">
<field name="name">Aliases</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">mail.mailcow.alias</field>
@ -53,6 +53,21 @@
<menuitem id="mailcow_menu_alias"
name="Aliases"
parent="mailcow_menu"
action="mailcow_alias_action"
action="action_mailcow_alias"
sequence="20"/>
<record id="action_server_sync_aliases" model="ir.actions.server">
<field name="name">Sync Aliases</field>
<field name="model_id" ref="model_mail_mailcow_alias"/>
<field name="state">code</field>
<field name="code">model.sync_aliases()</field>
</record>
<record id="action_sync_aliases" model="ir.actions.act_window.view">
<field name="sequence" eval="10"/>
<field name="view_mode">tree</field>
<field name="act_window_id" ref="action_mailcow_alias"/>
<field name="view_id" ref="mailcow_alias_view_tree"/>
</record>
</odoo>

View file

@ -29,7 +29,7 @@
</field>
</record>
<record id="mailcow_blacklist_action" model="ir.actions.act_window">
<record id="action_mailcow_blacklist" model="ir.actions.act_window">
<field name="name">Blacklist</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">mail.mailcow.blacklist</field>
@ -47,6 +47,20 @@
<menuitem id="mailcow_menu_blacklist"
name="Blacklist"
parent="mailcow_menu"
action="mailcow_blacklist_action"
action="action_mailcow_blacklist"
sequence="30"/>
<record id="action_server_sync_blacklist" model="ir.actions.server">
<field name="name">Sync Blacklist</field>
<field name="model_id" ref="model_mail_mailcow_blacklist"/>
<field name="state">code</field>
<field name="code">model.sync_blacklist()</field>
</record>
<record id="action_sync_blacklist" model="ir.actions.act_window.view">
<field name="sequence" eval="10"/>
<field name="view_mode">tree</field>
<field name="act_window_id" ref="action_mailcow_blacklist"/>
<field name="view_id" ref="mailcow_blacklist_view_form"/>
</record>
</odoo>

View file

@ -5,35 +5,29 @@
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="base_setup.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[hasclass('settings')]" position="inside">
<div class="app_settings_block" data-string="Mailcow" string="Mailcow" data-key="mailcow" groups="base.group_system">
<div id="emails" position='after'>
<div id="mailcow">
<h2>Mailcow Settings</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane">
<field name="mailcow_api_key"/>
<label for="mailcow_base_url" string="Mailcow URL"/>
<div class="text-muted">
Base URL for Mailcow server
</div>
<div class="o_setting_right_pane">
<label for="mailcow_api_key"/>
<div class="text-muted">
API key for Mailcow server
</div>
</div>
</div>
<div class="col-12 col-lg-6 o_setting_box">
<div class="o_setting_left_pane">
<div class="content-group">
<field name="mailcow_base_url"/>
</div>
<div class="o_setting_right_pane">
<label for="mailcow_base_url"/>
<div class="text-muted">
Base URL for Mailcow server
</div>
<label for="mailcow_api_key" string="API Key"/>
<div class="text-muted">
Mailcow API Key
</div>
<div class="content-group">
<field name="mailcow_api_key"/>
</div>
</div>
</div>
</div>
</xpath>
</div>
</field>
</record>
</odoo>