Skip to content

Commit 6aa4242

Browse files
committed
strip things down to just warehouse and order
1 parent 235f60b commit 6aa4242

File tree

4 files changed

+3
-304
lines changed

4 files changed

+3
-304
lines changed

domain_model.py

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,5 @@
11
from __future__ import annotations
22
from dataclasses import dataclass
3-
from datetime import date
4-
5-
6-
def allocate(order, warehouse, shipments):
7-
ordered_sources = [warehouse] + sorted(shipments)
8-
allocation = Allocation(order)
9-
for source in ordered_sources:
10-
allocation.supplement_with(source.allocation_for(order))
11-
allocation.decrement_available_quantities()
12-
return allocation
13-
14-
15-
@dataclass(unsafe_hash=True)
16-
class AllocationLine:
17-
sku: str
18-
source: _Stock
19-
20-
21-
class Allocation:
22-
23-
def __init__(self, order):
24-
self.order = order
25-
self.lines = []
26-
27-
def __getitem__(self, sku):
28-
return next(l.source for l in self.lines if l.sku == sku)
29-
30-
def __setitem__(self, sku, source):
31-
line = next(l.source for l in self.lines if l.sku == sku)
32-
line.source = source
33-
34-
def __contains__(self, sku):
35-
return sku in {l.sku for l in self.lines}
36-
37-
def with_sources(self, sources: dict):
38-
self.lines = [AllocationLine(sku, source) for sku, source in sources.items()]
39-
return self
40-
41-
def supplement_with(self, other: Allocation):
42-
for line in other.lines:
43-
if line.sku not in self:
44-
self.lines.append(line)
45-
46-
def decrement_available_quantities(self):
47-
for line in self.lines:
48-
line.source.decrement_available(line.sku, self.order[line.sku])
493

504

515
@dataclass(unsafe_hash=True)
@@ -59,54 +13,12 @@ class _Lines:
5913
def __init__(self, lines: dict):
6014
self.lines = [Line(sku, qty) for sku, qty in lines.items()]
6115

62-
def __getitem__(self, sku):
63-
return next(l.qty for l in self.lines if l.sku == sku)
64-
65-
def __setitem__(self, sku, qty):
66-
try:
67-
line = next(l for l in self.lines if l.sku == sku)
68-
line.qty = qty
69-
except StopIteration:
70-
self.lines.append(Line(sku=sku, qty=qty))
71-
72-
def __contains__(self, sku):
73-
return sku in {l.sku for l in self.lines}
74-
7516

7617

7718
class Order(_Lines):
7819
pass
7920

8021

81-
class _Stock(_Lines):
82-
83-
def decrement_available(self, sku, qty):
84-
self[sku] -= qty
85-
86-
def allocation_for(self, order: Order):
87-
return Allocation(order).with_sources({
88-
line.sku: self
89-
for line in order.lines
90-
if line.sku in self
91-
and self[line.sku] > line.qty
92-
})
93-
94-
95-
class Warehouse(_Stock):
96-
97-
def __repr__(self):
98-
return f'<Warehouse lines={self._lines}>'
99-
100-
101-
102-
class Shipment(_Stock):
103-
104-
def __init__(self, lines: dict, eta: date):
105-
self.eta = eta
106-
super().__init__(lines)
107-
108-
def __repr__(self):
109-
return f'<Shipment eta={self.eta} lines={self._lines}>'
22+
class Warehouse(_Lines):
23+
pass
11024

111-
def __lt__(self, other):
112-
return self.eta < other.eta

orm.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,3 @@
3838
})
3939

4040

41-
shipment = Table(
42-
'shipment', metadata,
43-
Column('id', Integer, primary_key=True, autoincrement=True),
44-
Column('eta', Date),
45-
)
46-
shipment_lines = Table(
47-
'shipment_lines', metadata,
48-
Column('shipment_id', ForeignKey('shipment.id'), primary_key=True),
49-
Column('sku', String(255), primary_key=True),
50-
Column('qty', Integer),
51-
)
52-
53-
shipment_line_mapper = mapper(domain_model.Line, shipment_lines, non_primary=True)
54-
mapper(domain_model.Shipment, shipment, properties={
55-
'lines': relationship(shipment_line_mapper, cascade="all, delete-orphan")
56-
})
57-
58-
allocation = Table(
59-
'allocation', metadata,
60-
Column('id', Integer, primary_key=True, autoincrement=True),
61-
Column('order_id', ForeignKey('order.id'))
62-
)
63-
64-
allocation_lines = Table(
65-
'allocation_lines', metadata,
66-
Column('allocation_id', ForeignKey('allocation.id'), primary_key=True),
67-
Column('sku', String(255), primary_key=True),
68-
Column('shipment_id', ForeignKey('shipment.id'), nullable=True),
69-
)
70-
71-
mapper(domain_model.AllocationLine, allocation_lines)
72-
mapper(domain_model.Allocation, allocation, properties={
73-
'lines': relationship(domain_model.AllocationLine, cascade="all, delete-orphan"),
74-
})

test_allocation.py

Lines changed: 0 additions & 172 deletions
This file was deleted.

test_orm.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import pytest
22
from sqlalchemy import create_engine
33
from sqlalchemy.orm import sessionmaker
4-
from datetime import date
54

65
from orm import metadata
7-
from domain_model import Order, Warehouse, Shipment, Allocation
6+
from domain_model import Order, Warehouse
87

98

109
@pytest.fixture
@@ -98,14 +97,8 @@ def test_order_mapper_can_delete_lines(session):
9897
def test_rest_of_fields(session):
9998
new_order = Order({'sku1': 12, 'sku2': 13})
10099
warehouse = Warehouse({'whsku1': 11, 'whsku2': 12})
101-
# shipment1 = Shipment({'shipsku1': 13, 'shipsku2': 13}, eta=date.today())
102-
# shipment2 = Shipment({'shipsku2': 22, 'shipsku3': 34}, eta=date.today())
103-
# allocation = Allocation({'asku1': warehouse, 'asku2': shipment2})
104100
session.add(new_order)
105101
session.add(warehouse)
106-
# session.add(shipment1)
107-
# session.add(shipment2)
108-
# session.add(allocation)
109102
session.commit()
110103

111104
warehouse_rows = list(session.execute('SELECT * FROM "warehouse_lines"'))

0 commit comments

Comments
 (0)