Skip to content

Commit

Permalink
Use engine and session in future=True mode in till client
Browse files Browse the repository at this point in the history
See #310 on github.
  • Loading branch information
sde1000 committed Jan 24, 2025
1 parent a78dad4 commit 1ee3fdc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
2 changes: 2 additions & 0 deletions quicktill/delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ def accept(self):
f"{delivery.logref}")
stocktype.saleprice = saleprice
qty = int(self.qtyfield.f)
# add_items() creates the StockItem instances and leaves them
# pending in the current ORM session
items = delivery.add_items(stocktype, stockunit, qty, cost, bestbefore)
td.s.flush()
for item in items:
Expand Down
30 changes: 14 additions & 16 deletions quicktill/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,9 @@ def void(self, transaction, user, source):
(not necessarily the same as this line's transaction) that
voids this line and return it.
The new transaction line is not automatically placed in the
ORM session; the caller must do this if desired.
If this transaction line has already been voided, returns
None.
"""
Expand Down Expand Up @@ -1925,22 +1928,17 @@ def add_items(self, stocktype, stockunit, qty, cost, bestbefore=None):
costper = (cost / qty).quantize(penny) if cost else None
remaining_cost = cost
items = []
# It's necessary to disable autoflush here, otherwise some of
# the new StockItem rows may be autoflushed with deliveryid
# still set to None if sqlalchemy has to issue a query to load
# stocktype
with object_session(self).no_autoflush:
while qty > 0:
thiscost = remaining_cost if qty == 1 else costper
remaining_cost = remaining_cost - thiscost if cost else None
item = StockItem(stocktype=stocktype,
description=description,
size=size,
costprice=thiscost,
bestbefore=bestbefore)
self.items.append(item)
items.append(item)
qty -= 1
while qty > 0:
thiscost = remaining_cost if qty == 1 else costper
remaining_cost = remaining_cost - thiscost if cost else None
item = StockItem(stocktype=stocktype,
description=description,
size=size,
costprice=thiscost,
bestbefore=bestbefore)
self.items.append(item) # adds item to object_session(self)
items.append(item)
qty -= 1
return items


Expand Down
4 changes: 2 additions & 2 deletions quicktill/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ def init(database):
log.info("init database \'%s\'", database)
database = parse_database_name(database)
log.info("sqlalchemy engine URL \'%s\'", database)
engine = create_engine(database)
session_factory = sessionmaker(bind=engine, future=False)
engine = create_engine(database, future=True)
session_factory = sessionmaker(bind=engine, future=True)
s = scoped_session(session_factory)


Expand Down
6 changes: 3 additions & 3 deletions quicktill/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class ModelTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Create the test database
engine = create_engine("postgresql+psycopg2:///postgres")
engine = create_engine("postgresql+psycopg2:///postgres", future=True)
raw_connection = engine.raw_connection()
with raw_connection.cursor() as cursor:
cursor.execute('commit')
cursor.execute(f'create database "{TEST_DATABASE_NAME}"')
raw_connection.close()
cls._engine = create_engine(
f"postgresql+psycopg2:///{TEST_DATABASE_NAME}")
f"postgresql+psycopg2:///{TEST_DATABASE_NAME}", future=True)
models.metadata.create_all(cls._engine)
cls._sm = sessionmaker(cls._engine, future=True)

Expand All @@ -29,7 +29,7 @@ def tearDownClass(cls):
# Dispose of the connection pool, closing all checked-in connections
cls._engine.dispose()
del cls._engine
engine = create_engine("postgresql+psycopg2:///postgres")
engine = create_engine("postgresql+psycopg2:///postgres", future=True)
raw_connection = engine.raw_connection()
with raw_connection.cursor() as cursor:
cursor.execute('commit')
Expand Down
3 changes: 2 additions & 1 deletion quicktill/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ def add_token(self):
if not token:
token = UserToken(token=t)
token.last_seen = None
user.tokens.append(token)
user.tokens.append(token) # adds token to ORM session if not present
td.s.flush()
self.tokenfield.set(None)
self.description.set("")
self.reload_tokens()
Expand Down

0 comments on commit 1ee3fdc

Please sign in to comment.