From aba550a170af2445f301bd793da9b2e4a17aa257 Mon Sep 17 00:00:00 2001 From: Raf Ven Date: Thu, 2 Jan 2025 11:13:36 +0100 Subject: [PATCH] [18.0-mail] [IMP] mail_environment: Keep existing settings at install of module --- mail_environment/README.rst | 22 +++--- mail_environment/__init__.py | 1 + mail_environment/__manifest__.py | 2 + mail_environment/hooks.py | 79 +++++++++++++++++++ .../static/description/index.html | 36 ++++----- mail_environment/tests/__init__.py | 1 + .../tests/test_existing_mail_server_config.py | 30 +++++++ 7 files changed, 137 insertions(+), 34 deletions(-) create mode 100644 mail_environment/hooks.py create mode 100644 mail_environment/tests/test_existing_mail_server_config.py diff --git a/mail_environment/README.rst b/mail_environment/README.rst index ed0aa41a8..d9afe0be5 100644 --- a/mail_environment/README.rst +++ b/mail_environment/README.rst @@ -1,7 +1,3 @@ -.. image:: https://odoo-community.org/readme-banner-image - :target: https://odoo-community.org/get-involved?utm_source=readme - :alt: Odoo Community Association - ========================================== Mail configuration with server_environment ========================================== @@ -17,7 +13,7 @@ Mail configuration with server_environment .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png :target: https://odoo-community.org/page/development-status :alt: Beta -.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--env-lightgray.png?logo=github @@ -103,8 +99,8 @@ file. Known issues / Roadmap ====================== -- Due to the special nature of this addon, you cannot test it on the OCA - runbot. +- Due to the special nature of this addon, you cannot test it on the + OCA runbot. Bug Tracker =========== @@ -127,12 +123,12 @@ Authors Contributors ------------ -- Nicolas Bessi -- Yannick Vaucher -- Guewen Baconnier -- Joël Grand-Guillaume -- Holger Brunn -- Alexandre Fayolle +- Nicolas Bessi +- Yannick Vaucher +- Guewen Baconnier +- Joël Grand-Guillaume +- Holger Brunn +- Alexandre Fayolle Maintainers ----------- diff --git a/mail_environment/__init__.py b/mail_environment/__init__.py index 0650744f6..f69c7729c 100644 --- a/mail_environment/__init__.py +++ b/mail_environment/__init__.py @@ -1 +1,2 @@ from . import models +from .hooks import post_init_hook, pre_init_hook diff --git a/mail_environment/__manifest__.py b/mail_environment/__manifest__.py index 7bd19de9c..d13bf5291 100644 --- a/mail_environment/__manifest__.py +++ b/mail_environment/__manifest__.py @@ -10,4 +10,6 @@ "license": "AGPL-3", "website": "https://github.com/OCA/server-env", "depends": ["mail", "server_environment"], + "pre_init_hook": "pre_init_hook", + "post_init_hook": "post_init_hook", } diff --git a/mail_environment/hooks.py b/mail_environment/hooks.py new file mode 100644 index 000000000..3f58a295e --- /dev/null +++ b/mail_environment/hooks.py @@ -0,0 +1,79 @@ +from odoo.tools import config + + +def pre_init_hook(env): + if config["test_enable"]: + env["ir.mail_server"].create( + { + "name": "Test Outgoing Mail Server", + "smtp_host": "localhost", + "smtp_port": 25, + "smtp_user": "test", + "smtp_pass": "test123", + "active": False, + } + ) + env.cr.execute( + "SELECT * FROM ir_mail_server where name = 'Test Outgoing Mail Server'" + ) + mail_server = env.cr.dictfetchone() + assert mail_server + assert mail_server["smtp_host"] == "localhost" + assert mail_server["smtp_port"] == 25 + assert mail_server["smtp_user"] == "test" + assert mail_server["smtp_pass"] == "test123" + env["fetchmail.server"].create( + { + "name": "Test Incoming Mail Server", + "server": "localhost", + "port": 143, + "user": "test", + "password": "test123", + "active": False, + } + ) + env.cr.execute( + "SELECT * FROM fetchmail_server where name = 'Test Incoming Mail Server'" + ) + mail_server = env.cr.dictfetchone() + assert mail_server + assert mail_server["server"] == "localhost" + assert mail_server["port"] == 143 + assert mail_server["user"] == "test" + assert mail_server["password"] == "test123" + + +def post_init_hook(env): + # Migrate Outgoing Mail Server data + env.cr.execute("SELECT * FROM ir_mail_server") + for row in env.cr.dictfetchall(): + mail_server = ( + env["ir.mail_server"] + .with_context(active_test=False) + .search([("name", "=", row["name"])]) + ) + if mail_server: + mail_server_values = {} + for field_name, _options in env[ + "ir.mail_server" + ]._server_env_fields.items(): + if field_name in row: + mail_server_values[field_name] = row[field_name] + mail_server.update(mail_server_values) + + # Migrate Incoming Mail Server data + env.cr.execute("SELECT * FROM fetchmail_server") + for row in env.cr.dictfetchall(): + mail_server = ( + env["fetchmail.server"] + .with_context(active_test=False) + .search([("name", "=", row["name"])]) + ) + if mail_server: + mail_server_values = {} + for field_name, _options in env[ + "fetchmail.server" + ]._server_env_fields.items(): + if field_name in row: + mail_server_values[field_name] = row[field_name] + mail_server.update(mail_server_values) diff --git a/mail_environment/static/description/index.html b/mail_environment/static/description/index.html index be52611f6..d2571219a 100644 --- a/mail_environment/static/description/index.html +++ b/mail_environment/static/description/index.html @@ -3,7 +3,7 @@ -README.rst +Mail configuration with server_environment -
+
+

Mail configuration with server_environment

- - -Odoo Community Association - -
-

Mail configuration with server_environment

-

Beta License: AGPL-3 OCA/server-env Translate me on Weblate Try me on Runboat

+

Beta License: AGPL-3 OCA/server-env Translate me on Weblate Try me on Runboat

This module allows to configure the incoming and outgoing mail servers using the server_environment mechanism: you can then have different mail servers for the production and the test environment.

@@ -395,12 +390,12 @@

Mail configuration with server_environment

-

Installation

+

Installation

To install this module, you need to have the server_environment module installed and properly configured.

-

Configuration

+

Configuration

With this module installed, the incoming and outgoing mail servers are configured in the server_environment_files module (which is a module you should provide, see the documentation of server_environment for more @@ -440,20 +435,20 @@

Configuration

mail server with the field name set to “odoo_pop_mail1”.

-

Usage

+

Usage

Once configured, Odoo will read the mail servers values from the configuration file related to each environment defined in the main Odoo file.

-

Known issues / Roadmap

+

Known issues / Roadmap

    -
  • Due to the special nature of this addon, you cannot test it on the OCA -runbot.
  • +
  • Due to the special nature of this addon, you cannot test it on the +OCA runbot.
-

Bug Tracker

+

Bug Tracker

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed @@ -461,15 +456,15 @@

Bug Tracker

Do not contact contributors directly about support or help with technical issues.

-

Credits

+

Credits

-

Authors

+

Authors

  • Camptocamp
-

Maintainers

+

Maintainers

This module is maintained by the OCA.

Odoo Community Association @@ -493,6 +488,5 @@

Maintainers

-
diff --git a/mail_environment/tests/__init__.py b/mail_environment/tests/__init__.py index 82ffeb745..417aa55d3 100644 --- a/mail_environment/tests/__init__.py +++ b/mail_environment/tests/__init__.py @@ -1 +1,2 @@ +from . import test_existing_mail_server_config from . import test_mail_environment diff --git a/mail_environment/tests/test_existing_mail_server_config.py b/mail_environment/tests/test_existing_mail_server_config.py new file mode 100644 index 000000000..dedbf45ce --- /dev/null +++ b/mail_environment/tests/test_existing_mail_server_config.py @@ -0,0 +1,30 @@ +from odoo.tests import tagged + +from odoo.addons.server_environment.tests.common import ServerEnvironmentCase + + +@tagged("post_install", "-at_install") +class TestMailEnvironment(ServerEnvironmentCase): + def test_outgoing_mail_server(self): + mail_server = ( + self.env["ir.mail_server"] + .with_context(active_test=False) + .search([("name", "=", "Test Outgoing Mail Server")]) + ) + self.assertTrue(mail_server) + self.assertEqual(mail_server.smtp_host, "localhost") + self.assertEqual(mail_server.smtp_port, 25) + self.assertEqual(mail_server.smtp_user, "test") + self.assertEqual(mail_server.smtp_pass, "test123") + + def test_incoming_mail_server(self): + mail_server = ( + self.env["fetchmail.server"] + .with_context(active_test=False) + .search([("name", "=", "Test Incoming Mail Server")]) + ) + self.assertTrue(mail_server) + self.assertEqual(mail_server.server, "localhost") + self.assertEqual(mail_server.port, 143) + self.assertEqual(mail_server.user, "test") + self.assertEqual(mail_server.password, "test123")