Skip to content

Commit 2eafb6a

Browse files
committed
[16.0][ADD] base_notification
1 parent 35822c5 commit 2eafb6a

File tree

20 files changed

+778
-0
lines changed

20 files changed

+778
-0
lines changed

base_notification/README.rst

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
========================
2+
Base Notification Method
3+
========================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:b2d9db82d59881a6a679980ceb906d7da2a71114903585e2119474a7d02c77d3
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
20+
:target: https://github.com/OCA/server-tools/tree/16.0/base_notification
21+
:alt: OCA/server-tools
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-base_notification
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
32+
**Table of contents**
33+
34+
.. contents::
35+
:local:
36+
37+
Bug Tracker
38+
===========
39+
40+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
41+
In case of trouble, please check there if your issue has already been reported.
42+
If you spotted it first, help us to smash it by providing a detailed and welcomed
43+
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_notification%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
44+
45+
Do not contact contributors directly about support or help with technical issues.
46+
47+
Credits
48+
=======
49+
50+
Authors
51+
~~~~~~~
52+
53+
* Kencove
54+
55+
Contributors
56+
~~~~~~~~~~~~
57+
58+
* Mohamed Alkobrosli <https://kencove.com>
59+
60+
61+
Other credits
62+
~~~~~~~~~~~~~
63+
64+
The development of this module has been financially supported by:
65+
66+
* Kencove
67+
68+
69+
Maintainers
70+
~~~~~~~~~~~
71+
72+
This module is maintained by the OCA.
73+
74+
.. image:: https://odoo-community.org/logo.png
75+
:alt: Odoo Community Association
76+
:target: https://odoo-community.org
77+
78+
OCA, or the Odoo Community Association, is a nonprofit organization whose
79+
mission is to support the collaborative development of Odoo features and
80+
promote its widespread use.
81+
82+
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/16.0/base_notification>`_ project on GitHub.
83+
84+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

base_notification/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# from . import models
2+
# from .hooks import post_init_hook

base_notification/__manifest__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# __manifest__.py
2+
{
3+
"name": "Base Notification Method",
4+
"summary": "Generic notifications on create/write/delete or method call",
5+
"version": "16.0.1.0.0",
6+
"author": "Kencove, Odoo Community Association (OCA)",
7+
"website": "https://github.com/OCA/server-tools",
8+
# "category": "Tools",
9+
"license": "LGPL-3",
10+
# "depends": ["base", "mail"],
11+
# "data": [
12+
# "security/ir.model.access.csv",
13+
# "data/base_notification_data.xml",
14+
# "views/base_notification_rule_views.xml",
15+
# ],
16+
# "post_init_hook": "post_init_hook",
17+
# "installable": True,
18+
# "application": False,
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<odoo>
2+
<record id="base_notification_rule_demo" model="base.notification.rule">
3+
<field name="name">Demo: Notify on Partner Create</field>
4+
<field name="model_id" ref="base.model_res_partner" />
5+
<field name="trigger">on_create</field>
6+
<field name="active">True</field>
7+
<field name="partner_ids" eval="[(4, ref('base.user_admin'))]" />
8+
<field name="domain">[]</field>
9+
</record>
10+
</odoo>

base_notification/hooks.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from odoo import SUPERUSER_ID, api
2+
3+
from .utils.notify_runtime_patch import runtime_patch_methods
4+
5+
6+
def post_init_hook(cr, pool):
7+
env = api.Environment(cr, SUPERUSER_ID, {})
8+
runtime_patch_methods(env)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import base_notification_rule
2+
from . import ir_model
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import logging
2+
3+
from odoo import fields, models
4+
from odoo.tools.safe_eval import safe_eval
5+
6+
_logger = logging.getLogger(__name__)
7+
8+
9+
class BaseNotificationRule(models.Model):
10+
_name = "base.notification.rule"
11+
_description = "Generic Notification Rule"
12+
_rec_name = "name"
13+
14+
name = fields.Char(required=True)
15+
model_id = fields.Many2one(
16+
"ir.model",
17+
string="Model",
18+
required=True,
19+
ondelete="cascade",
20+
)
21+
trigger = fields.Selection(
22+
[
23+
("on_create", "On Create"),
24+
("on_write", "On Write"),
25+
("on_unlink", "On Delete"),
26+
("on_method_call", "On Method Call"),
27+
],
28+
required=True,
29+
default="on_write",
30+
)
31+
method_name = fields.Char()
32+
domain = fields.Char("Domain Filter", default="[]")
33+
active = fields.Boolean(default=True)
34+
notify_mode = fields.Selection(
35+
[
36+
("immediate", "Immediate"),
37+
("queued", "Queued (if queue_job installed)"),
38+
],
39+
default="immediate",
40+
)
41+
partner_ids = fields.Many2many("res.partner", string="Recipients")
42+
template_id = fields.Many2one("mail.template", string="Mail Template")
43+
44+
def _execute_notification(self, records):
45+
"""Send the configured notification."""
46+
if not self.active or not records:
47+
return
48+
49+
if self.domain:
50+
try:
51+
domain = safe_eval(self.domain)
52+
records = records.filtered_domain(domain)
53+
except Exception as e:
54+
_logger.warning(
55+
f"Invalid domain in rule {self.name}: {self.domain} ({e})"
56+
)
57+
58+
for rec in records:
59+
if not rec.exists():
60+
continue
61+
62+
if self.template_id:
63+
self.template_id.send_mail(rec.id, force_send=True)
64+
else:
65+
rec.message_post(
66+
body=f"🔔 Notification: {self.name}",
67+
partner_ids=self.partner_ids.ids,
68+
)
69+
70+
def _apply_trigger(self, event_type, records):
71+
"""Called by create/write/unlink hooks."""
72+
rules = self.sudo().search(
73+
[
74+
("model_id.model", "=", records._name),
75+
("trigger", "=", event_type),
76+
("active", "=", True),
77+
]
78+
)
79+
for rule in rules:
80+
rule._execute_notification(records)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from odoo import api, models
2+
3+
4+
class BaseModel(models.AbstractModel):
5+
_inherit = "base"
6+
7+
@api.model_create_multi
8+
def create(self, vals_list):
9+
records = super().create(vals_list)
10+
self.env["base.notification.rule"].sudo()._apply_trigger("on_create", records)
11+
return records
12+
13+
def write(self, vals):
14+
res = super().write(vals)
15+
self.env["base.notification.rule"].sudo()._apply_trigger("on_write", self)
16+
return res
17+
18+
def unlink(self):
19+
self.env["base.notification.rule"].sudo()._apply_trigger("on_unlink", self)
20+
return super().unlink()

base_notification/readme/CONFIGURE.rst

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* Mohamed Alkobrosli <https://kencove.com>
2+

0 commit comments

Comments
 (0)