module linker

This commit is contained in:
Benoît Vézina 2023-07-20 13:20:02 -04:00
parent c70e35f42c
commit 6405e7fdfc
14 changed files with 148 additions and 31 deletions

View file

@ -6,12 +6,10 @@
'summary': 'Module for adding blacklist functionality to the helpdesk.',
'description': """
Helpdesk Mailcow Blacklist
==========================
This module extends the functionality of the Helpdesk and Mailcow Blacklist modules by adding an action to blacklist the sender of a Helpdesk ticket.
Main Features:
--------------
- Adds a button on Helpdesk tickets to blacklist the email sender.
- This button is only visible when an email is associated with the ticket.
- Blacklisting an email sender automatically moves the Helpdesk ticket to a new 'Spam' stage, which is marked as closed.
@ -21,9 +19,12 @@
'license': 'GPL-3',
'author': 'Bemade',
'website': 'https://www.bemade.org',
'depends': ['helpdesk', 'bemade_mailcow_blacklist'],
'depends': [
'helpdesk',
'bemade_mailcow_integration'
],
'data': [
'security/ir.model.access.csv',
# 'security/ir.model.access.csv',
'data/helpdesk_stages.xml',
'views/helpdesk_ticket_views.xml',
],

View file

@ -4,7 +4,7 @@
<record id="helpdesk_stage_spam" model="helpdesk.stage">
<field name="name">Spam</field>
<field name="sequence">50</field>
<field name="is_closed">True</field>
<field name="is_close">True</field>
</record>
</data>
</odoo>

View file

@ -1,16 +1,19 @@
from odoo import api, fields, models
from odoo.exceptions import UserError
import re
class HelpdeskTicket(models.Model):
_inherit = 'helpdesk.ticket'
def add_blacklist(self):
def action_add_blacklist(self):
self.ensure_one()
email_regex = r'<([^<>]+)>'
email_to_blacklist = re.findall(email_regex, self.email)[0]
# Create a new blacklist record
blacklist = self.env['mailcow.blacklist'].create({
'email': self.email,
blacklist = self.env['mail.mailcow.blacklist'].create({
'email': email_to_blacklist,
})
# Assign 'spam' as a closed stage

View file

@ -6,7 +6,7 @@
<field name="inherit_id" ref="helpdesk.helpdesk_ticket_view_form"/>
<field name="arch" type="xml">
<header position="inside">
<button name="action_add_blacklist" type="object" string="Add to Blacklist" class="oe_highlight" attrs="{'invisible': [('email_from', '=', False)]}"/>
<button name="action_add_blacklist" type="object" string="Add to Blacklist" class="oe_highlight" attrs="{'invisible': [('partner_email', '=', False)]}"/>
</header>
</field>
</record>

View file

@ -6,22 +6,24 @@
'summary': 'Module for integrating Mailcow email server with Odoo.',
'description': """
Mailcow Integration
===================
This module integrates the Mailcow email server with Odoo, providing a seamless email communication solution for your Odoo instance. It allows for syncing of mailboxes and email aliases from Mailcow to Odoo and vice versa.
Main Features:
--------------
* Synchronize Mailcow mailboxes with Odoo users.
* Synchronize Mailcow email aliases with Odoo.
* Configuration of Mailcow API credentials in Odoo settings.
* Automatically create and manage mailboxes and aliases in Mailcow when they are created in Odoo.
Synchronize Mailcow mailboxes with Odoo users.
Synchronize Mailcow email aliases with Odoo.
Configuration of Mailcow API credentials in Odoo settings.
Automatically create and manage mailboxes and aliases in Mailcow when they are created in Odoo.
""",
'sequence': 10,
'license': 'GPL-3',
'author': 'Bemade',
'website': 'https://www.bemade.org',
'depends': ['hr', 'mail', 'bemade_user_password_bundle'],
'depends': [
'hr',
'mail',
'bemade_user_password_bundle'
],
'data': [
'security/ir.model.access.csv',
'views/res_config_settings_views.xml',
@ -32,10 +34,10 @@
],
"assets": {
"web.assets_backend": [
"bemade_mailcow_blacklist/static/src/js/mailcow.js",
"bemade_mailcow_integration/static/src/js/mailcow.js",
],
"web.assets_qweb": [
"bemade_mailcow_blacklist/static/src/xml/mailcow_templates.xml",
"bemade_mailcow_integration/static/src/xml/mailcow_templates.xml",
],
},
'demo': [],

View file

@ -16,10 +16,8 @@ class MailMailcow(models.AbstractModel):
def get_credentials(self):
params = self.env['ir.config_parameter'].sudo()
base_url = params.get_param('mailcow.base_url'),
base_url = base_url[0]
api_key = params.get_param('mailcow.api_key'),
api_key = api_key[0]
base_url = params.get_param('mailcow.base_url')[0],
api_key = params.get_param('mailcow.api_key')[0],
if not base_url or not api_key:
_logger.error('No API key or base URL is set in the system parameters')

View file

@ -18,20 +18,26 @@ class MailcowBlacklist(models.Model):
"""
Overridden create method to add the new blacklist entry to the Mailcow server.
"""
res = super().create(vals)
domain = self.env['ir.config_parameter'].sudo().get_param('mail.catchall.domain')
endpoint_add = '/api/v1/add/domain-policy'
endpoint_get_bl = f"/api/v1/get/policy_bl_domain/{domain}"
data = {
'domain': domain,
'object_from': res.email,
'object_from': vals['email'],
'object_list': 'bl'
}
res.api_request(endpoint_add, 'POST', data)
self.api_request(endpoint_add, 'POST', data)
_logger.info(f'Added {vals["email"]} to Mailcow blacklist')
res.api_request(endpoint_get_bl, 'GET', None)
allblemail = self.api_request(endpoint_get_bl, 'GET', None)
mc_id = [d['prefid'] for d in allblemail if d['value'] == vals['email']]
vals['mc_id'] = mc_id[0]
res = super().create(vals)
return res
@ -60,11 +66,12 @@ class MailcowBlacklist(models.Model):
Overridden unlink method to remove the blacklist entry from the Mailcow server.
"""
for record in self:
endpoint = '/api/v1/delete/blacklist'
endpoint = '/api/v1/delete/domain-policy'
data = {
'items': [record.email]
'items': [record.mc_id]
}
record.api_request(endpoint, 'POST', data)
result = record.api_request(endpoint, 'POST', data)
print(result)
_logger.info(f'Removed {record.email} from Mailcow blacklist')
return super().unlink()

View file

@ -57,7 +57,7 @@
<menuitem id="mailcow_menu"
name="Mailcow"
sequence="10"
web_icon="bemade_mailcow_blacklist,static/description/icon.png"/>
web_icon="bemade_mailcow_integration,static/description/icon.png"/>
<menuitem id="mailcow_menu_mailbox"
name="Mailboxes"

View file

@ -0,0 +1,12 @@
from . import models
def post_init_hook(cr, registry):
# Look for .gitmodules file
gitmodules_file = os.path.join(os.getcwd(), '.gitmodules')
if os.path.exists(gitmodules_file):
# Read .gitmodules file
with open(gitmodules_file) as f:
content = f.read()
# Extract installed directory
installed_directory = re.search('[submodule "(.*)"]', content).group(1)
print(installed_directory)

View file

@ -0,0 +1,24 @@
{
'name': 'Module Linker',
'version': '15.0.1.0.0',
'category': 'Extra Tools',
'summary': 'Link modules from external repositories',
'description': """
This module allows users to link modules from external repositories.
""",
'depends': [
'base',
'base_setup'
],
'data': [
'views/res_config_settings_views.xml',
],
'license': 'AGPL-3',
'author': 'Bemade',
'website': 'https://bemade.org/',
'installable': True,
'auto_install': False,
'hooks': [
'post_init_hook',
],
}

View file

@ -0,0 +1 @@
from . import res_config_settings

View file

@ -0,0 +1,8 @@
from odoo import models, fields, api, _
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
root_repos_directory = fields.Char(string="Root Repos Directory")
enabled_addons_directory = fields.Char(string="Enabled Addons Directory")

View file

@ -0,0 +1,28 @@
from odoo import fields, models, api
from odoo.exceptions import ValidationError
import os
class ResModuleLinks(models.Model):
_name = 'res.modules.links'
_description = 'Module Links'
name = fields.Char('Name', required=True)
module_id = fields.Many2one('ir.module.module', string='Module')
active = fields.Boolean(default=False)
repo_name = fields.Char('Repo Name', required=True)
@api.onchange('active')
def on_change_active(self):
params = self.env['ir.config_parameter'].sudo()
from_dir = params.get_param('root_repos_directory')[0],
to_dir = params.get_param('enabled_addons_directory')[0],
if self.active:
os.unlink(os.getcwd() + self.repo_name + '/' + self.name)
self.module_id.state = 'installed'
else:
if self.module_id.state == 'installed':
raise ValidationError('You must uninstall the module before deactivating it.')
else:
os.symlink(os.getcwd() + self.repo_name + '/' + self.name, os.getcwd() + self.repo_name + '/' + self.name)

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="view_settings_module_linker" model="ir.ui.view">
<field name="name">Settings Module Linker</field>
<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[@id='about']" position="before">
<div id="module_linker">
<h2>Module Linker Settings</h2>
<div class="row mt16 o_settings_container">
<div class="col-12 col-lg-6 o_setting_box">
<label for="root_repos_directory" string="Root Repos Directory"/>
<div class="text-muted">
Directory where repos are cloned
</div>
<div class="content-group">
<field name="root_repos_directory"/>
</div>
<label for="enabled_addons_directory" string="Enabled Addons Directory"/>
<div class="text-muted">
Enabled Addons Directory
</div>
<div class="content-group">
<field name="enabled_addons_directory"/>
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>