Add notify_user to ModelStorage

Closes #14667
This commit is contained in:
Cédric Krier committed 2026-03-11 10:01:51 +01:00
1 parent 27dff955f1
commit 03655a335a
7 files changed
+71 -1

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
* Add notify_user to ModelStorage
* Check button states when testing access
* Remove the files of Binary fields from the Filestore
* Add delete and delete_many to Filestore
+5
View File
@@ -356,6 +356,11 @@ Class methods:
Log event for records.
.. classmethod:: ModelStorage.notify_user(records, icon, label[, description[, user[, \**extra]]])
Notify user about records.
.. classmethod:: ModelStorage.preprocess_values(cls, mode, values)
Returns a copy of ``values`` to be used for :meth:`~ModelStorage.create` and
+2
View File
@@ -54,6 +54,8 @@ Transaction
.. attribute:: Transaction.log_records
.. attribute:: Transaction.user_notifications
.. attribute:: Transaction.check_warnings
The dictionary of warnings already checked with user and warning
+20
View File
@@ -5,6 +5,7 @@ import base64
import csv
import datetime
import decimal
import json
import math
import random
import time
@@ -245,6 +246,25 @@ class ModelStorage(Model):
resource=record, event=event, target=target,
user=user, **extra))
@dualmethod
def notify_user(
cls, records, icon, label, description=None, user=None, **extra):
pool = Pool()
Notification = pool.get('res.notification')
for transaction, sub_records in groupby(
records, lambda r: r._transaction):
with Transaction().set_current_transaction(transaction):
if user is None:
user = transaction.user
transaction.user_notifications.append(Notification(
user=user,
icon=icon,
label=label,
description=description,
model=cls.__name__,
records=json.dumps([r.id for r in records]),
**extra))
@classmethod
def preprocess_values(cls, mode, values):
assert mode in {'create', 'write'}
+1 -1
View File
@@ -54,7 +54,7 @@ class Notification(
@classmethod
def __setup__(cls):
super().__setup__()
cls._order = [('id', 'DESC')]
cls.__rpc__.update({
'get': RPC(),
'get_count': RPC(),
@@ -743,6 +743,30 @@ class ModelStorageTestCase(TestCase):
Model.delete([record])
@with_transaction()
def test_notify_user(self):
"Test notify user"
pool = Pool()
Model = pool.get('test.modelstorage')
Notification = pool.get('res.notification')
transaction = Transaction()
record = Model(name="Foo")
record.save()
record.notify_user(
'tryton-notification',
"Test notification",
"Long description")
transaction._store_user_notifications()
notification, = Notification.search([])
self.assertEqual(notification.user.id, transaction.user)
self.assertEqual(notification.icon, 'tryton-notification')
self.assertEqual(notification.label, "Test notification")
self.assertEqual(notification.description, "Long description")
class EvalEnvironmentTestCase(TestCase):
"Test EvalEnvironment"
+18
View File
@@ -130,6 +130,7 @@ class Transaction(object):
instance.delete_records = None
instance.trigger_records = None
instance.log_records = None
instance.user_notifications = None
instance.check_warnings = None
instance.timestamp = None
instance.started_at = None
@@ -198,6 +199,7 @@ class Transaction(object):
self.delete_records = defaultdict(set)
self.trigger_records = defaultdict(set)
self.log_records = []
self.user_notifications = []
self.check_warnings = defaultdict(set)
self.timestamp = {}
self.counter = 0
@@ -265,6 +267,7 @@ class Transaction(object):
self.delete_records = None
self.trigger_records = None
self.log_records = None
self.user_notifications = None
self.timestamp = None
self._datamanagers = []
@@ -340,6 +343,19 @@ class Transaction(object):
if self.log_records:
self.log_records.clear()
def _store_user_notifications(self):
from trytond.pool import Pool
if self.user_notifications:
pool = Pool()
Notification = pool.get('res.notification')
with without_check_access():
Notification.save(self.user_notifications)
self._clear_user_notifications()
def _clear_user_notifications(self):
if self.user_notifications:
self.user_notifications.clear()
def _remove_warnings(self):
from trytond.pool import Pool
if self.check_warnings:
@@ -358,6 +374,7 @@ class Transaction(object):
from trytond.cache import Cache
try:
self._store_log_records()
self._store_user_notifications()
self._remove_warnings()
if self._datamanagers:
for datamanager in self._datamanagers:
@@ -391,6 +408,7 @@ class Transaction(object):
datamanager.tpc_abort(self)
Cache.rollback(self)
self._clear_log_records()
self._clear_user_notifications()
self._clear_warnings()
if self.connection:
self.connection.rollback()