Skip to content

Commit

Permalink
Update for sqlalchemy-2.0
Browse files Browse the repository at this point in the history
Generally:

td.s.query(model).get(pk) -> td.s.get(model, pk)

.join(a, b, c) -> .join(a).join(b).join(c)

Don't pass strings to .joinedload(), .undefer(), etc.

select([a, b], whereclause=cond) converted to select(a, b).where(cond)

See #310 — sqlalchemy-2.0 migration
  • Loading branch information
sde1000 committed Jan 25, 2025
1 parent c9e0ebc commit ee63665
Show file tree
Hide file tree
Showing 32 changed files with 483 additions and 477 deletions.
2 changes: 1 addition & 1 deletion quicktill/barcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class barcode:
def __init__(self, code):
self.code = code
self.binding = td.s.query(Barcode).get(code)
self.binding = td.s.get(Barcode, code)

def feedback(self, valid: bool) -> None:
"""Feedback to scanner user
Expand Down
12 changes: 6 additions & 6 deletions quicktill/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class _cardpopup(ui.dismisspopup):
"""
def __init__(self, paytype_id, reg, transid, amount, refund=False):
self.paytype_id = paytype_id
pt = td.s.query(PayType).get(paytype_id)
pt = td.s.get(PayType, paytype_id)
self.reg = reg
self.transid = transid
self.amount = amount
Expand Down Expand Up @@ -87,7 +87,7 @@ def update_total_amount(self):
f"Total card payment: {tillconfig.fc(self.amount + cashback)}")

def enter(self):
pt = td.s.query(PayType).get(self.paytype_id)
pt = td.s.get(PayType, self.paytype_id)
if self.cashback_in_use:
try:
cashback = Decimal(self.cbfield.f).quantize(penny)
Expand Down Expand Up @@ -122,7 +122,7 @@ def enter(self):

self.dismiss()

trans = td.s.query(Transaction).get(self.transid)
trans = td.s.get(Transaction, self.transid)
user = ui.current_user().dbuser
td.s.add(user)
if not trans or trans.closed:
Expand Down Expand Up @@ -264,7 +264,7 @@ def read_config(self):
self._cashback_method = None
cashback_paytype_id = c.get('cashback_method', None)
if cashback_paytype_id:
cashback_pt = td.s.query(PayType).get(cashback_paytype_id)
cashback_pt = td.s.get(PayType, cashback_paytype_id)
if not cashback_pt:
problem = "Cashback method does not exist"
elif not cashback_pt.active:
Expand Down Expand Up @@ -356,7 +356,7 @@ def start_payment(self, reg, transid, amount, outstanding):
if self._refund_guard:
# Check whether there are any payments of this type in
# the transaction history
trans = td.s.query(Transaction).get(transid)
trans = td.s.get(Transaction, transid)
if not trans:
ui.infopopup(
["Transaction does not exist; cannot refund."],
Expand Down Expand Up @@ -407,7 +407,7 @@ def total(self, sessionid, fields):
"One or more of the total fields has something "
"other than a number in it.")
else:
session = td.s.query(Session).get(sessionid)
session = td.s.get(Session, sessionid)
total = zero
for paytype, amount in session.payment_totals:
if paytype == self.paytype:
Expand Down
4 changes: 2 additions & 2 deletions quicktill/cash.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def start_payment(self, reg, transid, amount, outstanding):
"a till with a cash drawer to take a cash payment."],
title="Error")
return
trans = td.s.query(Transaction).get(transid)
trans = td.s.get(Transaction, transid)
description = self.paytype.description
if amount < zero:
if amount < outstanding:
Expand Down Expand Up @@ -85,7 +85,7 @@ def start_payment(self, reg, transid, amount, outstanding):

@user.permission_required("cancel-cash-payment", "Cancel a cash payment")
def cancel_payment(self, register, pline_instance):
p = td.s.query(Payment).get(pline_instance.payment_id)
p = td.s.get(Payment, pline_instance.payment_id)
if p.amount >= zero:
title = "Cancel payment"
message = [f"Press Cash/Enter to cancel this {p.text} "
Expand Down
4 changes: 2 additions & 2 deletions quicktill/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def preload(cls):
ci._read()

def _read(self):
d = td.s.query(Config).get(self.key)
d = td.s.get(Config, self.key)
if d is None:
# The config option doesn't exist in the database. Initialise it
# with the default.
Expand Down Expand Up @@ -257,7 +257,7 @@ def run(args):
for ci in td.s.query(Config).order_by(Config.key).all():
print(f"{ci.key}: {ci.display_name}: {ci.value}")
return
ci = td.s.query(Config).get(args.key)
ci = td.s.get(Config, args.key)
if not ci:
print(f"Config key {args.key} does not exist")
return 1
Expand Down
19 changes: 9 additions & 10 deletions quicktill/delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, dn=None):
title="Screen width problem")
return
if dn:
d = td.s.query(Delivery).get(dn)
d = td.s.get(Delivery, dn)
else:
d = None
if d:
Expand Down Expand Up @@ -169,7 +169,7 @@ def __init__(self, dn=None):

def update_costfield(self):
if self.dn:
d = td.s.query(Delivery).get(self.dn)
d = td.s.get(Delivery, self.dn)
self.costfield.set(
f"Total cost ex-VAT: {tillconfig.fc(d.costprice)}"
if d.costprice is not None else "Total cost unknown")
Expand All @@ -190,7 +190,7 @@ def update_model(self):
return
if not self.dn:
return
d = td.s.query(Delivery).get(self.dn)
d = td.s.get(Delivery, self.dn)
d.supplier = self.supfield.read()
d.date = date
d.docnumber = self.docnumfield.f
Expand Down Expand Up @@ -291,10 +291,9 @@ def printout(self):
def reallyconfirm(self):
if not self.dn:
return
d = td.s.query(Delivery)\
.options(joinedload("items").joinedload("stocktype")
.joinedload("stocktake"))\
.get(self.dn)
d = td.s.get(Delivery, self.dn, options=[
joinedload(Delivery.items).joinedload(StockItem.stocktype)
.joinedload(StockType.stocktake)])
for si in d.items:
if si.stocktype.stocktake:
ui.infopopup(["You can't confirm this delivery at the moment "
Expand Down Expand Up @@ -380,7 +379,7 @@ def reallydelete(self):
if self.dn is None:
self.dismiss()
return
d = td.s.query(Delivery).get(self.dn)
d = td.s.get(Delivery, self.dn)
for i in d.items:
td.s.delete(i)
user.log(f"Deleted delivery {d.logref}")
Expand Down Expand Up @@ -512,7 +511,7 @@ def accept(self):
stocktype = self.typefield.read()
stockunit = self.unitfield.read()
bestbefore = self.bestbeforefield.read()
delivery = td.s.query(Delivery).get(self.deliveryid)
delivery = td.s.get(Delivery, self.deliveryid)
if stocktype.saleprice != saleprice:
user.log(
f"Changed sale price of {stocktype.logref} from "
Expand Down Expand Up @@ -727,7 +726,7 @@ def __init__(self, func, supplier=None, defaultname=None):

def confirmed(self):
if self.sn:
supplier = td.s.query(Supplier).get(self.sn)
supplier = td.s.get(Supplier, self.sn)
else:
supplier = Supplier()
td.s.add(supplier)
Expand Down
4 changes: 2 additions & 2 deletions quicktill/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ def name(self):
@property
def keycap(self):
from . import td, models
cap = td.s.query(models.KeyCap).get(self.name)
cap = td.s.get(models.KeyCap, self.name)
if cap:
return cap.keycap
return ""

@property
def css_class(self):
from . import td, models
cap = td.s.query(models.KeyCap).get(self.name)
cap = td.s.get(models.KeyCap, self.name)
if cap:
return cap.css_class

Expand Down
2 changes: 1 addition & 1 deletion quicktill/linekeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def _complete_class(m):
result = td.s.execute(
select([KeyCap.css_class]).where(KeyCap.css_class.ilike(m + '%'))
select(KeyCap.css_class).where(KeyCap.css_class.ilike(m + '%'))
)
return [x[0] for x in result]

Expand Down
14 changes: 8 additions & 6 deletions quicktill/managestock.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def stockcheck(dept=None):
.filter(Delivery.checked == True)\
.options(contains_eager(StockItem.stocktype))\
.options(contains_eager(StockItem.delivery))\
.options(undefer('remaining'))\
.options(undefer(StockItem.remaining))\
.order_by(StockItem.id)
if dept:
sq = sq.filter(StockType.dept_id == dept)
Expand Down Expand Up @@ -115,7 +115,8 @@ def stockhistory(dept=None):
.join(StockItem.stocktype)\
.filter(StockItem.finished != None)\
.options(undefer(StockItem.remaining))\
.options(joinedload('stocktype').joinedload('unit'))\
.options(joinedload(StockItem.stocktype)
.joinedload(StockType.unit))\
.order_by(StockItem.id.desc())
if dept:
sq = sq.filter(StockType.dept_id == dept)
Expand Down Expand Up @@ -223,7 +224,7 @@ def stock_purge_internal(source):
finished = td.s.query(StockItem)\
.join(StockItem.stockline)\
.options(contains_eager(StockItem.stockline))\
.options(joinedload('stocktype'))\
.options(joinedload(StockItem.stocktype))\
.filter(StockItem.finished == None)\
.filter(StockLine.linetype == "display")\
.filter(StockItem.remaining == Decimal("0.0"))\
Expand All @@ -235,8 +236,9 @@ def stock_purge_internal(source):
cfinished = td.s.query(StockItem)\
.join(StockLine,
StockItem.stocktype_id == StockLine.stocktype_id)\
.options(joinedload('stocktype'))\
.options(contains_eager('stocktype.stocklines'))\
.options(joinedload(StockItem.stocktype))\
.options(joinedload(StockItem.stocktype)
.contains_eager(StockType.stocklines))\
.filter(StockItem.finished == None)\
.filter(StockLine.linetype == "continuous")\
.filter(StockItem.remaining <= Decimal("0.0"))\
Expand Down Expand Up @@ -331,7 +333,7 @@ def __init__(self, stockitem):
def finish(self):
bb = self.bbfield.read()
if bb:
item = td.s.query(StockItem).get(self.stockid)
item = td.s.get(StockItem, self.stockid)
if not item:
ui.infopopup(["Error: item has gone away!"], title="Error")
return
Expand Down
2 changes: 1 addition & 1 deletion quicktill/managetill.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def enter(self):
if rn is None:
return
self.dismiss()
trans = td.s.query(Transaction).get(rn)
trans = td.s.get(Transaction, rn)
if not trans:
ui.infopopup([f"Transaction {rn} does not exist."],
title="Error")
Expand Down
Loading

0 comments on commit ee63665

Please sign in to comment.