- added flag to mark any attribute as "modified"

This commit is contained in:
Mike Bayer
2010-12-23 14:10:14 -05:00
parent 08ef0e21e0
commit e9d1b5b8de
2 changed files with 45 additions and 0 deletions
+12
View File
@@ -1247,3 +1247,15 @@ def del_attribute(instance, key):
state, dict_ = instance_state(instance), instance_dict(instance)
state.manager[key].impl.delete(state, dict_)
def flag_modified(instance, key):
"""Mark an attribute on an instance as 'modified'.
This sets the 'modified' flag on the instance and
establishes an unconditional change event for the given attribute.
"""
state, dict_ = instance_state(instance), instance_dict(instance)
impl = state.manager[key].impl
state.modified_event(dict_, impl, NO_VALUE)
+33
View File
@@ -1123,7 +1123,40 @@ class HistoryTest(_base.ORMTest):
attributes.instance_state(f).commit(attributes.instance_dict(f), ['someattr'])
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [{'foo':'old'}], ()))
def test_flag_modified(self):
class Foo(_base.BasicEntity):
pass
instrumentation.register_class(Foo)
attributes.register_attribute(Foo, 'someattr', uselist=False, useobject=False)
f = Foo()
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), (), ()))
f.someattr = {'a':'b'}
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([{'a':'b'},], (), ()))
attributes.instance_state(f).commit_all(attributes.instance_dict(f))
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [{'a':'b'},], ()))
f.someattr['a'] = 'c'
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [{'a':'c'},], ()))
attributes.flag_modified(f, 'someattr')
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([{'a':'c'},], (), ()))
f.someattr = ['a']
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([['a']], (), ()))
attributes.instance_state(f).commit_all(attributes.instance_dict(f))
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [['a']], ()))
f.someattr[0] = 'b'
f.someattr.append('c')
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ((), [['b', 'c']], ()))
attributes.flag_modified(f, 'someattr')
eq_(attributes.get_state_history(attributes.instance_state(f), 'someattr'), ([['b', 'c']], (), ()))
def test_use_object(self):
class Foo(_base.BasicEntity):
pass