forked from cosmicpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorm.py
More file actions
59 lines (48 loc) · 1.58 KB
/
orm.py
File metadata and controls
59 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from sqlalchemy import (
Table, MetaData, Column, Integer, String, Date, ForeignKey,
event,
)
from sqlalchemy.orm import mapper, relationship
from allocation import model
metadata = MetaData()
order_lines = Table(
'order_lines', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('sku', String(255)),
Column('qty', Integer, nullable=False),
Column('orderid', String(255)),
)
products = Table(
'products', metadata,
Column('sku', String(255), primary_key=True),
Column('version_number', Integer, nullable=False, server_default='0'),
)
batches = Table(
'batches', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('reference', String(255)),
Column('sku', ForeignKey('products.sku')),
Column('_purchased_quantity', Integer, nullable=False),
Column('eta', Date, nullable=True),
)
allocations = Table(
'allocations', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('orderline_id', ForeignKey('order_lines.id')),
Column('batch_id', ForeignKey('batches.id')),
)
def start_mappers():
lines_mapper = mapper(model.OrderLine, order_lines)
batches_mapper = mapper(model.Batch, batches, properties={
'_allocations': relationship(
lines_mapper,
secondary=allocations,
collection_class=set,
)
})
mapper(model.Product, products, properties={
'batches': relationship(batches_mapper)
})
@event.listens_for(model.Product, 'load')
def receive_load(product, _):
product.events = []