details: https://code.tryton.org/tryton/commit/9bbe5251be9e
branch: default
user: Cédric Krier <[email protected]>
date: Thu Nov 27 16:26:20 2025 +0100
description:
Use creation position to format warning key for new records
The opposite position in the creation order is used instead of the id
(which is
not stable over transaction retries) to format the warning key when the
record
has been created during the transaction.
This ensures that the formatting of the warning key is stable over
transaction
retries.
Closes #10591
diffstat:
trytond/doc/ref/transaction.rst | 8 ++++++++
trytond/trytond/model/modelsql.py | 2 +-
trytond/trytond/model/modelstorage.py | 2 +-
trytond/trytond/res/user.py | 12 +++++++++++-
trytond/trytond/tests/test_res.py | 15 +++++++++++++++
trytond/trytond/transaction.py | 2 +-
6 files changed, 37 insertions(+), 4 deletions(-)
diffs (102 lines):
diff -r 31105d04576f -r 9bbe5251be9e trytond/doc/ref/transaction.rst
--- a/trytond/doc/ref/transaction.rst Fri Nov 28 15:32:06 2025 +0100
+++ b/trytond/doc/ref/transaction.rst Thu Nov 27 16:26:20 2025 +0100
@@ -40,8 +40,16 @@
.. attribute:: Transaction.create_records
+ A :py:class:`defaultdict <collections.defaultdict>` storing for each
+ :attr:`~trytond.model.Model.__name__` a :py:class:`list` of ids created
+ during the transaction.
+
.. attribute:: Transaction.delete_records
+ A :py:class:`defaultdict <collections.defaultdict>` storing for each
+ :attr:`~trytond.model.Model.__name__` a :py:class:`set` of ids deleted
+ during the transaction.
+
.. attribute:: Transaction.trigger_records
.. attribute:: Transaction.check_warnings
diff -r 31105d04576f -r 9bbe5251be9e trytond/trytond/model/modelsql.py
--- a/trytond/trytond/model/modelsql.py Fri Nov 28 15:32:06 2025 +0100
+++ b/trytond/trytond/model/modelsql.py Thu Nov 27 16:26:20 2025 +0100
@@ -2312,7 +2312,7 @@
def lock(cls, records=None):
transaction = Transaction()
if records is not None:
- new_ids = transaction.create_records[cls.__name__]
+ new_ids = set(transaction.create_records[cls.__name__])
ids = [id_ for id_ in map(int, records) if id_ not in new_ids]
transaction.lock_records(cls._table, ids)
else:
diff -r 31105d04576f -r 9bbe5251be9e trytond/trytond/model/modelstorage.py
--- a/trytond/trytond/model/modelstorage.py Fri Nov 28 15:32:06 2025 +0100
+++ b/trytond/trytond/model/modelstorage.py Thu Nov 27 16:26:20 2025 +0100
@@ -299,7 +299,7 @@
for trigger in triggers:
trigger.queue_trigger_action(records)
- transaction.create_records[cls.__name__].update(ids)
+ transaction.create_records[cls.__name__].extend(ids)
return ids
@classmethod
diff -r 31105d04576f -r 9bbe5251be9e trytond/trytond/res/user.py
--- a/trytond/trytond/res/user.py Fri Nov 28 15:32:06 2025 +0100
+++ b/trytond/trytond/res/user.py Thu Nov 27 16:26:20 2025 +0100
@@ -964,7 +964,17 @@
@classmethod
def format(cls, name, records):
- key = '|'.join(map(str, records)).encode('utf-8')
+ create_records = Transaction().create_records
+
+ def str_(record):
+ name = record.__name__
+ try:
+ # start from 1 to avoid 0 == -0
+ i = create_records[name].index(record.id) + 1
+ return '%s,%s' % (name, -i)
+ except ValueError:
+ return str(record)
+ key = '|'.join(map(str_, records)).encode('utf-8')
return '%s.%s' % (hashlib.md5(key).hexdigest(), name)
@classmethod
diff -r 31105d04576f -r 9bbe5251be9e trytond/trytond/tests/test_res.py
--- a/trytond/trytond/tests/test_res.py Fri Nov 28 15:32:06 2025 +0100
+++ b/trytond/trytond/tests/test_res.py Thu Nov 27 16:26:20 2025 +0100
@@ -82,5 +82,20 @@
self.assertFalse(Warning_.check('test'))
self.assertFalse(Warning_.check('test'))
+ @with_transaction()
+ def test_user_warning_format(self):
+ "Test format of user warning"
+ pool = Pool()
+ Warning_ = pool.get('res.user.warning')
+ Record = pool.get('res.user')
+ transaction = Transaction()
+
+ key1 = Warning_.format('test', [Record(1)])
+
+ transaction.create_records[Record.__name__].append(1)
+ key2 = Warning_.format('test', [Record(1)])
+
+ self.assertNotEqual(key1, key2)
+
del ModuleTestCase
diff -r 31105d04576f -r 9bbe5251be9e trytond/trytond/transaction.py
--- a/trytond/trytond/transaction.py Fri Nov 28 15:32:06 2025 +0100
+++ b/trytond/trytond/transaction.py Thu Nov 27 16:26:20 2025 +0100
@@ -197,7 +197,7 @@
self.readonly = readonly
self.close = close
self.context = ImmutableDict(context or {})
- self.create_records = defaultdict(set)
+ self.create_records = defaultdict(list)
self.delete_records = defaultdict(set)
self.trigger_records = defaultdict(set)
self.log_records = []