summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-05-22 15:42:59 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-05-23 15:14:04 -0400
commitf46551de450a76de4105bda3be8d0d5c5fc0d52c (patch)
tree2bf640007f988f6851c359eee7e38886b81239d9 /lib/sqlalchemy
parenta987942761542666be89f40a9ac4a35e001b8265 (diff)
downloadsqlalchemy-f46551de450a76de4105bda3be8d0d5c5fc0d52c.tar.gz
Add AttributeEvents.modified
Added new event handler :meth:`.AttributeEvents.modified` which is triggered when the func:`.attributes.flag_modified` function is invoked, which is common when using the :mod:`sqlalchemy.ext.mutable` extension module. Change-Id: Ic152f1d5c53087d780b24ed7f1f1571527b9e8fc Fixes: #3303
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/ext/mutable.py22
-rw-r--r--lib/sqlalchemy/orm/attributes.py15
-rw-r--r--lib/sqlalchemy/orm/events.py28
3 files changed, 59 insertions, 6 deletions
diff --git a/lib/sqlalchemy/ext/mutable.py b/lib/sqlalchemy/ext/mutable.py
index e721397b3..ccaeb6aa3 100644
--- a/lib/sqlalchemy/ext/mutable.py
+++ b/lib/sqlalchemy/ext/mutable.py
@@ -204,6 +204,28 @@ or more parent objects that are also part of the pickle, the :class:`.Mutable`
mixin will re-establish the :attr:`.Mutable._parents` collection on each value
object as the owning parents themselves are unpickled.
+Receiving Events
+----------------
+
+The :meth:`.AttributeEvents.modified` event handler may be used to receive
+an event when a mutable scalar emits a change event. This event handler
+is called when the :func:`.attributes.flag_modified` function is called
+from within the mutable extension::
+
+ from sqlalchemy.ext.declarative import declarative_base
+ from sqlalchemy import event
+
+ Base = declarative_base()
+
+ class MyDataClass(Base):
+ __tablename__ = 'my_data'
+ id = Column(Integer, primary_key=True)
+ data = Column(MutableDict.as_mutable(JSONEncodedDict))
+
+ @event.listens_for(MyDataClass.data, "modified")
+ def modified_json(instance):
+ print("json value modified:", instance.data)
+
.. _mutable_composites:
Establishing Mutability on Composites
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py
index 23a9f1a8c..f3a5c4735 100644
--- a/lib/sqlalchemy/orm/attributes.py
+++ b/lib/sqlalchemy/orm/attributes.py
@@ -330,6 +330,7 @@ OP_REMOVE = util.symbol("REMOVE")
OP_APPEND = util.symbol("APPEND")
OP_REPLACE = util.symbol("REPLACE")
OP_BULK_REPLACE = util.symbol("BULK_REPLACE")
+OP_MODIFIED = util.symbol("MODIFIED")
class Event(object):
@@ -341,9 +342,9 @@ class Event(object):
operations.
The :class:`.Event` object is sent as the ``initiator`` argument
- when dealing with the :meth:`.AttributeEvents.append`,
+ when dealing with events such as :meth:`.AttributeEvents.append`,
:meth:`.AttributeEvents.set`,
- and :meth:`.AttributeEvents.remove` events.
+ and :meth:`.AttributeEvents.remove`.
The :class:`.Event` object is currently interpreted by the backref
event handlers, and is used to control the propagation of operations
@@ -459,12 +460,18 @@ class AttributeImpl(object):
self.dispatch._active_history = True
self.expire_missing = expire_missing
+ self._modified_token = None
__slots__ = (
'class_', 'key', 'callable_', 'dispatch', 'trackparent',
- 'parent_token', 'send_modified_events', 'is_equal', 'expire_missing'
+ 'parent_token', 'send_modified_events', 'is_equal', 'expire_missing',
+ '_modified_token'
)
+ def _init_modified_token(self):
+ self._modified_token = Event(self, OP_MODIFIED)
+ return self._modified_token
+
def __str__(self):
return "%s.%s" % (self.class_.__name__, self.key)
@@ -1636,6 +1643,8 @@ def flag_modified(instance, key):
"""
state, dict_ = instance_state(instance), instance_dict(instance)
impl = state.manager[key].impl
+ impl.dispatch.modified(
+ state, impl._modified_token or impl._init_modified_token())
state._modified_event(dict_, impl, NO_VALUE, is_userland=True)
diff --git a/lib/sqlalchemy/orm/events.py b/lib/sqlalchemy/orm/events.py
index 1ec898f8c..1d1c347b1 100644
--- a/lib/sqlalchemy/orm/events.py
+++ b/lib/sqlalchemy/orm/events.py
@@ -1877,14 +1877,18 @@ class AttributeEvents(event.Events):
target.dispatch._active_history = True
if not raw or not retval:
- def wrap(target, value, *arg):
+ def wrap(target, *arg):
if not raw:
target = target.obj()
if not retval:
- fn(target, value, *arg)
+ if arg:
+ value = arg[0]
+ else:
+ value = None
+ fn(target, *arg)
return value
else:
- return fn(target, value, *arg)
+ return fn(target, *arg)
event_key = event_key.with_wrapper(wrap)
event_key.base_listen(propagate=propagate)
@@ -2188,6 +2192,24 @@ class AttributeEvents(event.Events):
"""
+ def modified(self, target, initiator):
+ """Receive a 'modified' event.
+
+ This event is triggered when the :func:`.attributes.flag_modified`
+ function is used to trigger a modify event on an attribute without
+ any specific value being set.
+
+ .. versionadded:: 1.2
+
+ :param target: the object instance receiving the event.
+ If the listener is registered with ``raw=True``, this will
+ be the :class:`.InstanceState` object.
+
+ :param initiator: An instance of :class:`.attributes.Event`
+ representing the initiation of the event.
+
+ """
+
class QueryEvents(event.Events):
"""Represent events within the construction of a :class:`.Query` object.