diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-12-20 17:50:57 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-12-20 17:50:57 -0500 |
commit | 86e35dfdf6a9c1e715609a9452fef1e1811c8cf2 (patch) | |
tree | 0bb638f46aad9fbe9ddddb1a3e173cb00538c703 /lib/sqlalchemy/orm/unitofwork.py | |
parent | 199c7fb92c13e0957843429ba7295d91c4d266bf (diff) | |
download | sqlalchemy-86e35dfdf6a9c1e715609a9452fef1e1811c8cf2.tar.gz |
- convert built in AttributExtensions to event listener fns
Diffstat (limited to 'lib/sqlalchemy/orm/unitofwork.py')
-rw-r--r-- | lib/sqlalchemy/orm/unitofwork.py | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index ba43b1359..0dd5640a8 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -12,42 +12,37 @@ organizes them in order of dependency, and executes. """ -from sqlalchemy import util +from sqlalchemy import util, event from sqlalchemy.util import topological from sqlalchemy.orm import attributes, interfaces from sqlalchemy.orm import util as mapperutil session = util.importlater("sqlalchemy.orm", "session") -class UOWEventHandler(interfaces.AttributeExtension): - """An event handler added to all relationship attributes which handles - session cascade operations. - """ - - active_history = False +def track_cascade_events(descriptor, prop): + """Establish event listeners on object attributes which handle + cascade-on-set/append. - def __init__(self, key): - self.key = key - - # TODO: migrate these to unwrapped events + """ + key = prop.key - def append(self, state, item, initiator): + def append(state, item, initiator): # process "save_update" cascade rules for when # an instance is appended to the list of another instance sess = session._state_session(state) if sess: - prop = state.manager.mapper._props[self.key] + prop = state.manager.mapper._props[key] item_state = attributes.instance_state(item) if prop.cascade.save_update and \ - (prop.cascade_backrefs or self.key == initiator.key) and \ + (prop.cascade_backrefs or key == initiator.key) and \ not sess._contains_state(item_state): sess._save_or_update_state(item_state) return item - def remove(self, state, item, initiator): + def remove(state, item, initiator): sess = session._state_session(state) if sess: - prop = state.manager.mapper._props[self.key] + prop = state.manager.mapper._props[key] # expunge pending orphans item_state = attributes.instance_state(item) if prop.cascade.delete_orphan and \ @@ -55,7 +50,7 @@ class UOWEventHandler(interfaces.AttributeExtension): prop.mapper._is_orphan(item_state): sess.expunge(item) - def set(self, state, newvalue, oldvalue, initiator): + def set_(state, newvalue, oldvalue, initiator): # process "save_update" cascade rules for when an instance # is attached to another instance if oldvalue is newvalue: @@ -63,11 +58,11 @@ class UOWEventHandler(interfaces.AttributeExtension): sess = session._state_session(state) if sess: - prop = state.manager.mapper._props[self.key] + prop = state.manager.mapper._props[key] if newvalue is not None: newvalue_state = attributes.instance_state(newvalue) if prop.cascade.save_update and \ - (prop.cascade_backrefs or self.key == initiator.key) and \ + (prop.cascade_backrefs or key == initiator.key) and \ not sess._contains_state(newvalue_state): sess._save_or_update_state(newvalue_state) @@ -78,6 +73,10 @@ class UOWEventHandler(interfaces.AttributeExtension): prop.mapper._is_orphan(oldvalue_state): sess.expunge(oldvalue) return newvalue + + event.listen(descriptor, 'on_append', append, raw=True, retval=True) + event.listen(descriptor, 'on_remove', remove, raw=True, retval=True) + event.listen(descriptor, 'on_set', set_, raw=True, retval=True) class UOWTransaction(object): |