changeset a91d134fa7e2 in modules/purchase:4.4
details: https://hg.tryton.org/modules/purchase?cmd=changeset;node=a91d134fa7e2
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 d89192ddc63d -r a91d134fa7e2 purchase.py
--- a/purchase.py Thu Jul 05 12:11:12 2018 +0200
+++ b/purchase.py Wed Feb 06 22:51:18 2019 +0100
@@ -817,6 +817,7 @@
@ModelView.button
def process(cls, purchases):
process, done = [], []
+ cls.__lock(purchases)
for purchase in purchases:
purchase.create_invoice()
purchase.set_invoice_state()
@@ -837,6 +838,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'