Repetead code across various modules

(Jose Salvador) #1

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 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(, sub_records)
+                query =
+                    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??

(Sergi Almacellas Abellana) #2

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.

(Jose Salvador) #3

What a pleasure read that answer.

Tryton rocks!!!

Thank you.

(system) closed #4

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