From 16174e2d5be39ba1520e8797d07de1b1b3a75a77 Mon Sep 17 00:00:00 2001 From: "Aung Ko Ko Lin (Quartile)" <45355704+AungKoKoLin1997@users.noreply.github.com> Date: Wed, 27 Nov 2024 09:05:32 +0700 Subject: [PATCH] [3509][ADD] mail_message_restrict (#80) [3509][ADD] mail_message_restrict #80 --- mail_message_restrict/README.rst | 86 ++++ mail_message_restrict/__init__.py | 1 + mail_message_restrict/__manifest__.py | 13 + mail_message_restrict/i18n/ja.po | 45 ++ mail_message_restrict/models/__init__.py | 2 + mail_message_restrict/models/mail_message.py | 31 ++ .../models/mail_message_subtype.py | 15 + mail_message_restrict/readme/CONFIGURE.rst | 2 + mail_message_restrict/readme/CONTRIBUTORS.rst | 3 + mail_message_restrict/readme/DESCRIPTION.rst | 3 + .../static/description/index.html | 434 ++++++++++++++++++ mail_message_restrict/tests/__init__.py | 1 + .../tests/test_mail_message_restrict.py | 38 ++ .../views/mail_message_subtype_views.xml | 18 + .../odoo/addons/mail_message_restrict | 1 + setup/mail_message_restrict/setup.py | 6 + 16 files changed, 699 insertions(+) create mode 100644 mail_message_restrict/README.rst create mode 100644 mail_message_restrict/__init__.py create mode 100644 mail_message_restrict/__manifest__.py create mode 100644 mail_message_restrict/i18n/ja.po create mode 100644 mail_message_restrict/models/__init__.py create mode 100644 mail_message_restrict/models/mail_message.py create mode 100644 mail_message_restrict/models/mail_message_subtype.py create mode 100644 mail_message_restrict/readme/CONFIGURE.rst create mode 100644 mail_message_restrict/readme/CONTRIBUTORS.rst create mode 100644 mail_message_restrict/readme/DESCRIPTION.rst create mode 100644 mail_message_restrict/static/description/index.html create mode 100644 mail_message_restrict/tests/__init__.py create mode 100644 mail_message_restrict/tests/test_mail_message_restrict.py create mode 100644 mail_message_restrict/views/mail_message_subtype_views.xml create mode 120000 setup/mail_message_restrict/odoo/addons/mail_message_restrict create mode 100644 setup/mail_message_restrict/setup.py diff --git a/mail_message_restrict/README.rst b/mail_message_restrict/README.rst new file mode 100644 index 0000000..2ae23e4 --- /dev/null +++ b/mail_message_restrict/README.rst @@ -0,0 +1,86 @@ +===================== +Mail Message Restrict +===================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:7a2c5066d19ba0def08c1512dd8d44f66d1d844d2db78258a8ba35bbe72392a2 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/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%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/15.0/mail_message_restrict + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-15-0/social-15-0-mail_message_restrict + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/social&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module limits the creation of non-internal messages in the chatter to certain "allowed" models. +This means that, upon installation of the module, users will get an error message when they try to create +a message in the chatter, unless the model of the chatter is part of the "allowed" models. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +#. Navigate to *Settings > Technical > Email > Subtypes*. +#. In the "Allow Send Model" field, select the models for which you want to allow sending mail. + +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 +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Quartile Limited + +Contributors +~~~~~~~~~~~~ + +* `Quartile `__: + + * Aung Ko Ko Lin + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_message_restrict/__init__.py b/mail_message_restrict/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/mail_message_restrict/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_message_restrict/__manifest__.py b/mail_message_restrict/__manifest__.py new file mode 100644 index 0000000..2f92021 --- /dev/null +++ b/mail_message_restrict/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2023 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Mail Message Restrict", + "category": "Mail", + "license": "AGPL-3", + "author": "Quartile Limited, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/social", + "version": "15.0.1.0.0", + "depends": ["mail"], + "data": ["views/mail_message_subtype_views.xml"], + "installable": True, +} diff --git a/mail_message_restrict/i18n/ja.po b/mail_message_restrict/i18n/ja.po new file mode 100644 index 0000000..8418bd0 --- /dev/null +++ b/mail_message_restrict/i18n/ja.po @@ -0,0 +1,45 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mail_message_restrict +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0+e\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-06-27 04:46+0000\n" +"PO-Revision-Date: 2023-06-27 04:46+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: mail_message_restrict +#: model:ir.model.fields,field_description:mail_message_restrict.field_mail_message_subtype__allow_send_model_ids +msgid "Allow Send Model" +msgstr "メール送信可モデル" + +#. module: mail_message_restrict +#: code:addons/mail_message_restrict/models/mail_message.py:0 +#, python-format +msgid "" +"Creating a message in this model is blocked.Please contact the system " +"administrator as necessary." +msgstr "こちらのモデルよりメッセージを作成するには許可されません。" +"必要に応じて、システム管理者にお問い合わせください。" + +#. module: mail_message_restrict +#: model:ir.model.fields,field_description:mail_message_restrict.field_mail_message_subtype__hide_allow_send_model +msgid "Hide Allow Send Model" +msgstr "メール送信可モデル非表示" + +#. module: mail_message_restrict +#: model:ir.model,name:mail_message_restrict.model_mail_message +msgid "Message" +msgstr "メッセージ" + +#. module: mail_message_restrict +#: model:ir.model,name:mail_message_restrict.model_mail_message_subtype +msgid "Message subtypes" +msgstr "メッセージのサブタイプ" diff --git a/mail_message_restrict/models/__init__.py b/mail_message_restrict/models/__init__.py new file mode 100644 index 0000000..0bfba89 --- /dev/null +++ b/mail_message_restrict/models/__init__.py @@ -0,0 +1,2 @@ +from . import mail_message_subtype +from . import mail_message diff --git a/mail_message_restrict/models/mail_message.py b/mail_message_restrict/models/mail_message.py new file mode 100644 index 0000000..f02bf67 --- /dev/null +++ b/mail_message_restrict/models/mail_message.py @@ -0,0 +1,31 @@ +# Copyright 2023 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, api, models +from odoo.exceptions import UserError +from odoo.tools import config + + +class MailMessage(models.Model): + _inherit = "mail.message" + + @api.model + def create(self, vals): + """Ignore blocking for other tests""" + if config["test_enable"] and not self.env.context.get( + "test_mail_message_restrict" + ): + return super(MailMessage, self).create(vals) + if vals.get("message_type") == "comment": + subtype = self.env["mail.message.subtype"].browse(vals.get("subtype_id")) + if ( + vals.get("model") not in subtype.allow_send_model_ids.mapped("model") + and not subtype.internal + ): + raise UserError( + _( + "Creating a message in this model is blocked." + "Please contact the system administrator as necessary." + ) + ) + return super(MailMessage, self).create(vals) diff --git a/mail_message_restrict/models/mail_message_subtype.py b/mail_message_restrict/models/mail_message_subtype.py new file mode 100644 index 0000000..15710fe --- /dev/null +++ b/mail_message_restrict/models/mail_message_subtype.py @@ -0,0 +1,15 @@ +# Copyright 2023 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class MailMessageSubtype(models.Model): + _inherit = "mail.message.subtype" + + allow_send_model_ids = fields.Many2many("ir.model") + hide_allow_send_model = fields.Boolean(compute="_compute_hide_allow_send_model") + + def _compute_hide_allow_send_model(self): + for rec in self: + rec.hide_allow_send_model = rec != self.env.ref("mail.mt_comment") diff --git a/mail_message_restrict/readme/CONFIGURE.rst b/mail_message_restrict/readme/CONFIGURE.rst new file mode 100644 index 0000000..8bbf8e7 --- /dev/null +++ b/mail_message_restrict/readme/CONFIGURE.rst @@ -0,0 +1,2 @@ +#. Navigate to *Settings > Technical > Email > Subtypes*. +#. In the "Allow Send Model" field, select the models for which you want to allow sending mail. diff --git a/mail_message_restrict/readme/CONTRIBUTORS.rst b/mail_message_restrict/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..cd4e44c --- /dev/null +++ b/mail_message_restrict/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Quartile `__: + + * Aung Ko Ko Lin diff --git a/mail_message_restrict/readme/DESCRIPTION.rst b/mail_message_restrict/readme/DESCRIPTION.rst new file mode 100644 index 0000000..6141831 --- /dev/null +++ b/mail_message_restrict/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This module limits the creation of non-internal messages in the chatter to certain "allowed" models. +This means that, upon installation of the module, users will get an error message when they try to create +a message in the chatter, unless the model of the chatter is part of the "allowed" models. diff --git a/mail_message_restrict/static/description/index.html b/mail_message_restrict/static/description/index.html new file mode 100644 index 0000000..dce5256 --- /dev/null +++ b/mail_message_restrict/static/description/index.html @@ -0,0 +1,434 @@ + + + + + + +Mail Message Restrict + + + +
+

Mail Message Restrict

+ + +

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

+

This module limits the creation of non-internal messages in the chatter to certain “allowed” models. +This means that, upon installation of the module, users will get an error message when they try to create +a message in the chatter, unless the model of the chatter is part of the “allowed” models.

+

Table of contents

+ +
+

Configuration

+
    +
  1. Navigate to Settings > Technical > Email > Subtypes.
  2. +
  3. In the “Allow Send Model” field, select the models for which you want to allow sending mail.
  4. +
+
+
+

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 +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile Limited
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/social project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/mail_message_restrict/tests/__init__.py b/mail_message_restrict/tests/__init__.py new file mode 100644 index 0000000..927b99d --- /dev/null +++ b/mail_message_restrict/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_message_restrict diff --git a/mail_message_restrict/tests/test_mail_message_restrict.py b/mail_message_restrict/tests/test_mail_message_restrict.py new file mode 100644 index 0000000..7efcc25 --- /dev/null +++ b/mail_message_restrict/tests/test_mail_message_restrict.py @@ -0,0 +1,38 @@ +# Copyright 2023 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.exceptions import UserError +from odoo.tests import common + + +class TestMailMessageRestrict(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.message_model = cls.env["mail.message"] + cls.model_1 = cls.env["ir.model"].search([], limit=1) + cls.model_2 = cls.env["ir.model"].search([], offset=1, limit=1) + # Setup subtype + cls.subtype_comment = cls.env.ref("mail.mt_comment") + cls.subtype_comment.allow_send_model_ids = [(6, 0, cls.model_1.ids)] + + def test_create_message_with_comment_type_allowed_model(self): + # Creating a message with comment type and allowed model + self.message_model.with_context(test_mail_message_restrict=True).create( + { + "message_type": "comment", + "model": self.model_1.model, + "subtype_id": self.subtype_comment.id, + } + ) + + def test_create_message_with_comment_type_not_allowed_model(self): + # Creating a message with comment type and not allowed model + with self.assertRaises(UserError): + self.message_model.with_context(test_mail_message_restrict=True).create( + { + "message_type": "comment", + "model": self.model_2.model, + "subtype_id": self.subtype_comment.id, + } + ) diff --git a/mail_message_restrict/views/mail_message_subtype_views.xml b/mail_message_restrict/views/mail_message_subtype_views.xml new file mode 100644 index 0000000..a41acaf --- /dev/null +++ b/mail_message_restrict/views/mail_message_subtype_views.xml @@ -0,0 +1,18 @@ + + + + mail.message.subtype.form + mail.message.subtype + + + + + + + + + diff --git a/setup/mail_message_restrict/odoo/addons/mail_message_restrict b/setup/mail_message_restrict/odoo/addons/mail_message_restrict new file mode 120000 index 0000000..bc6fe2f --- /dev/null +++ b/setup/mail_message_restrict/odoo/addons/mail_message_restrict @@ -0,0 +1 @@ +../../../../mail_message_restrict \ No newline at end of file diff --git a/setup/mail_message_restrict/setup.py b/setup/mail_message_restrict/setup.py new file mode 100644 index 0000000..28c57bb --- /dev/null +++ b/setup/mail_message_restrict/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)