In advance excuse my ignorance please, I just want to understand and learn.
A very last commit to tryton (Lock records when processing) caught my attention around that code:
+ @classmethod
+ def __lock(cls, records):
+ from trytond.tools import grouped_slice, reduce_ids
+ from sql import Literal, For
+ transaction = Transaction()
+ database = transaction.database
+ connection = transaction.connection
+ table = cls.__table__()
+
+ if database.has_select_for():
+ for sub_records in grouped_slice(records):
+ where = reduce_ids(table.id, sub_records)
+ query = table.select(
+ Literal(1), where=where, for_=For('UPDATE', nowait=True))
+ with connection.cursor() as cursor:
+ cursor.execute(*query)
+ else:
+ database.lock(connection, cls._table)
+
It’s the same useful code repeated across account_payment_stripe, purchase and, at least, sale.
I would like to understand why that code can not be extracted/refactored to be shared among those modules instead to repeat it in each one.
Does not it fit well in another place of tryton’s framework??