forked from valhuber/python-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
129 lines (101 loc) · 4.12 KB
/
Copy path__init__.py
File metadata and controls
129 lines (101 loc) · 4.12 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from shutil import copyfile
import sqlalchemy
from sqlalchemy import event
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import session
from logic_engine.rule_bank import rule_bank_withdraw # FIXME design why required to avoid circular imports??
from logic_engine.rule_bank import rule_bank_setup
from nw.nw_logic.nw_rules_bank import activate_basic_check_credit_rules
from nw.nw_logic.order_code import order_commit_dirty, order_flush_dirty, order_flush_new, order_flush_delete
from nw.nw_logic.order_detail_code import order_detail_flush_new, order_detail_flush_delete
from logic_engine.util import prt
from nw.nw_logic.models import Order
'''
These listeners are part of the hand-coded logic alternative.
'''
def nw_before_commit(a_session: session):
print("logic: before commit!")
# for obj in versioned_objects(a_session.dirty):
for obj in a_session.dirty:
print("logic: before commit! --> " + str(obj))
obj_class = obj.__tablename__
if obj_class == "Order":
order_commit_dirty(obj, a_session)
elif obj_class == "OrderDetail":
print("Stub")
print("logic called: before commit! EXIT")
def nw_before_flush(a_session: session, a_flush_context, an_instances):
print("nw_before_flush")
for each_instance in a_session.dirty:
print("nw_before_flush flushing Dirty! --> " + str(each_instance))
obj_class = each_instance.__tablename__
if obj_class == "Order":
order_flush_dirty(each_instance, a_session)
elif obj_class == "OrderDetail":
print("Stub")
for each_instance in a_session.new:
print("nw_before_flush flushing New! --> " + str(each_instance))
obj_class = each_instance.__tablename__
if obj_class == "OrderDetail":
order_detail_flush_new(each_instance, a_session)
elif obj_class == "Order":
order_flush_new(each_instance, a_session)
for each_instance in a_session.deleted:
print("nw_before_flush flushing New! --> " + str(each_instance))
obj_class = each_instance.__tablename__
if obj_class == "OrderDetail":
order_detail_flush_delete(each_instance, a_session)
elif obj_class == "Order":
order_flush_delete(each_instance, a_session)
print("nw_before_flush EXIT")
""" Initialization
1 - Connect
2 - Register listeners (either hand-coded ones above, or the logic-engine listeners).
"""
basedir = os.path.abspath(os.path.dirname(__file__))
basedir = os.path.dirname(basedir)
basedir = os.path.dirname(basedir)
"""
IMPORTANT - create nw.db from a fresh copy
"""
nw_loc = os.path.join(basedir, "nw-app/nw.db")
nw_source = os.path.join(basedir, "nw-app/nw.db copy")
copyfile(src=nw_source, dst=nw_loc)
conn_string = "sqlite:///" + nw_loc
engine = sqlalchemy.create_engine(conn_string, echo=False) # sqlalchemy sqls...
session_maker = sqlalchemy.orm.sessionmaker()
session_maker.configure(bind=engine)
session = session_maker()
by_rules = True # True => use rules, False => use hand code (for comparison)
rule_list = None
db = None
if by_rules:
rule_bank_setup.setup(session, engine)
activate_basic_check_credit_rules()
rule_bank_setup.validate(session, engine) # checks for cycles, etc
else:
# target, modifier, function
event.listen(session, "before_commit", nw_before_commit)
event.listen(session, "before_flush", nw_before_flush)
print("\n" + prt("session created, listeners registered\n"))
''' *** Exploring alternate listener strategies - ignore ***
@event.listens_for(models.Order.ShippedDate, 'modified')
def receive_modified(target, initiator):
print('Order Modified (Decorator - __init__')
'''
'''
@event.listens_for(Order, 'before_update')
def before_update(mapper, connection, target):
state = db.inspect(target)
changes = {}
for attr in state.attrs:
hist = attr.load_history()
if not hist.has_changes():
continue
# hist.deleted holds old value
# hist.added holds new value
changes[attr.key] = hist.added
# now changes map keys to new values
print ("before update")
'''