Repetead code across various modules

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??

The code was repeated in order to be able to backport to older versions. On trunk version a new function has introduced to lock the records. This function is generic and can be used to lock any records of any model.

1 Like

What a pleasure read that answer.

Tryton rocks!!!

Thank you.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.