Skip to content

Commit 92352c7

Browse files
committed
Use the ORM instead [view_using_orm]
1 parent 407abb7 commit 92352c7

File tree

3 files changed

+7
-19
lines changed

3 files changed

+7
-19
lines changed

src/allocation/adapters/repository.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,3 @@ def _get_by_batchref(self, batchref):
5757
return self.session.query(model.Product).join(model.Batch).filter(
5858
orm.batches.c.reference == batchref,
5959
).first()
60-
61-
62-
def for_order(self, orderid):
63-
order_lines = self.session.query(model.OrderLine).filter_by(orderid=orderid)
64-
skus = {l.sku for l in order_lines}
65-
return self.session.query(model.Product).join(model.Batch).filter(
66-
model.Batch.sku.in_(skus)
67-
)

src/allocation/domain/model.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ def __gt__(self, other):
7373
return True
7474
return self.eta > other.eta
7575

76-
@property
77-
def orderids(self):
78-
return {l.orderid for l in self._allocations}
79-
8076
def allocate(self, line: OrderLine):
8177
if self.can_allocate(line):
8278
self._allocations.add(line)

src/allocation/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
from allocation.domain import model
12
from allocation.service_layer import unit_of_work
23

34
def allocations(orderid: str, uow: unit_of_work.SqlAlchemyUnitOfWork):
45
with uow:
5-
products = uow.products.for_order(orderid=orderid)
6-
batches = [b for p in products for b in p.batches]
7-
return [
8-
{'sku': b.sku, 'batchref': b.reference}
9-
for b in batches
10-
if orderid in b.orderids
11-
]
6+
batches = uow.session.query(model.Batch).join(
7+
model.OrderLine, model.Batch._allocations
8+
).filter(
9+
model.OrderLine.orderid == orderid
10+
)
11+
return [{'sku': b.sku, 'batchref': b.reference} for b in batches]

0 commit comments

Comments
 (0)