changeset fbf01ede212f in modules/purchase:5.0
details: https://hg.tryton.org/modules/purchase?cmd=changeset;node=fbf01ede212f
description:
        Lock records when processing

        issue8012
        review60511002
diffstat:

 purchase.py |  20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)

diffs (37 lines):

diff -r 517473403e77 -r fbf01ede212f purchase.py
--- a/purchase.py       Sat Jan 26 01:12:03 2019 +0100
+++ b/purchase.py       Wed Feb 06 22:51:18 2019 +0100
@@ -846,6 +846,7 @@
     @ModelView.button
     def process(cls, purchases):
         process, done = [], []
+        cls.__lock(purchases)
         for purchase in purchases:
             purchase.create_invoice()
             purchase.set_invoice_state()
@@ -867,6 +868,25 @@
             cls.do(done)
 
     @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)
+
+    @classmethod
     @ModelView.button_action('purchase.wizard_modify_header')
     def modify_header(cls, purchases):
         pass

Reply via email to