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

        issue8012
        review60511002
        (grafted from fbf01ede212f025da32d4fa8bacc32eac34eaec7)
diffstat:

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

diffs (37 lines):

diff -r 44708caa51f4 -r 1b8662862eec purchase.py
--- a/purchase.py       Thu Jul 05 12:08:56 2018 +0200
+++ b/purchase.py       Wed Feb 06 22:51:18 2019 +0100
@@ -835,6 +835,7 @@
     @ModelView.button
     def process(cls, purchases):
         process, done = [], []
+        cls.__lock(purchases)
         for purchase in purchases:
             purchase.create_invoice()
             purchase.set_invoice_state()
@@ -855,6 +856,25 @@
         if done:
             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)
+
 
 class PurchaseIgnoredInvoice(ModelSQL):
     'Purchase - Ignored Invoice'

Reply via email to