events.py 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  1. # orm/events.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """ORM event interfaces.
  8. """
  9. from .. import event, exc, util
  10. from .base import _mapper_or_none
  11. import inspect
  12. import weakref
  13. from . import interfaces
  14. from . import mapperlib, instrumentation
  15. from .session import Session, sessionmaker
  16. from .scoping import scoped_session
  17. from .attributes import QueryableAttribute
  18. from .query import Query
  19. from sqlalchemy.util.compat import inspect_getargspec
  20. class InstrumentationEvents(event.Events):
  21. """Events related to class instrumentation events.
  22. The listeners here support being established against
  23. any new style class, that is any object that is a subclass
  24. of 'type'. Events will then be fired off for events
  25. against that class. If the "propagate=True" flag is passed
  26. to event.listen(), the event will fire off for subclasses
  27. of that class as well.
  28. The Python ``type`` builtin is also accepted as a target,
  29. which when used has the effect of events being emitted
  30. for all classes.
  31. Note the "propagate" flag here is defaulted to ``True``,
  32. unlike the other class level events where it defaults
  33. to ``False``. This means that new subclasses will also
  34. be the subject of these events, when a listener
  35. is established on a superclass.
  36. .. versionchanged:: 0.8 - events here will emit based
  37. on comparing the incoming class to the type of class
  38. passed to :func:`.event.listen`. Previously, the
  39. event would fire for any class unconditionally regardless
  40. of what class was sent for listening, despite
  41. documentation which stated the contrary.
  42. """
  43. _target_class_doc = "SomeBaseClass"
  44. _dispatch_target = instrumentation.InstrumentationFactory
  45. @classmethod
  46. def _accept_with(cls, target):
  47. if isinstance(target, type):
  48. return _InstrumentationEventsHold(target)
  49. else:
  50. return None
  51. @classmethod
  52. def _listen(cls, event_key, propagate=True, **kw):
  53. target, identifier, fn = \
  54. event_key.dispatch_target, event_key.identifier, \
  55. event_key._listen_fn
  56. def listen(target_cls, *arg):
  57. listen_cls = target()
  58. if propagate and issubclass(target_cls, listen_cls):
  59. return fn(target_cls, *arg)
  60. elif not propagate and target_cls is listen_cls:
  61. return fn(target_cls, *arg)
  62. def remove(ref):
  63. key = event.registry._EventKey(
  64. None, identifier, listen,
  65. instrumentation._instrumentation_factory)
  66. getattr(instrumentation._instrumentation_factory.dispatch,
  67. identifier).remove(key)
  68. target = weakref.ref(target.class_, remove)
  69. event_key.\
  70. with_dispatch_target(instrumentation._instrumentation_factory).\
  71. with_wrapper(listen).base_listen(**kw)
  72. @classmethod
  73. def _clear(cls):
  74. super(InstrumentationEvents, cls)._clear()
  75. instrumentation._instrumentation_factory.dispatch._clear()
  76. def class_instrument(self, cls):
  77. """Called after the given class is instrumented.
  78. To get at the :class:`.ClassManager`, use
  79. :func:`.manager_of_class`.
  80. """
  81. def class_uninstrument(self, cls):
  82. """Called before the given class is uninstrumented.
  83. To get at the :class:`.ClassManager`, use
  84. :func:`.manager_of_class`.
  85. """
  86. def attribute_instrument(self, cls, key, inst):
  87. """Called when an attribute is instrumented."""
  88. class _InstrumentationEventsHold(object):
  89. """temporary marker object used to transfer from _accept_with() to
  90. _listen() on the InstrumentationEvents class.
  91. """
  92. def __init__(self, class_):
  93. self.class_ = class_
  94. dispatch = event.dispatcher(InstrumentationEvents)
  95. class InstanceEvents(event.Events):
  96. """Define events specific to object lifecycle.
  97. e.g.::
  98. from sqlalchemy import event
  99. def my_load_listener(target, context):
  100. print "on load!"
  101. event.listen(SomeClass, 'load', my_load_listener)
  102. Available targets include:
  103. * mapped classes
  104. * unmapped superclasses of mapped or to-be-mapped classes
  105. (using the ``propagate=True`` flag)
  106. * :class:`.Mapper` objects
  107. * the :class:`.Mapper` class itself and the :func:`.mapper`
  108. function indicate listening for all mappers.
  109. .. versionchanged:: 0.8.0 instance events can be associated with
  110. unmapped superclasses of mapped classes.
  111. Instance events are closely related to mapper events, but
  112. are more specific to the instance and its instrumentation,
  113. rather than its system of persistence.
  114. When using :class:`.InstanceEvents`, several modifiers are
  115. available to the :func:`.event.listen` function.
  116. :param propagate=False: When True, the event listener should
  117. be applied to all inheriting classes as well as the
  118. class which is the target of this listener.
  119. :param raw=False: When True, the "target" argument passed
  120. to applicable event listener functions will be the
  121. instance's :class:`.InstanceState` management
  122. object, rather than the mapped instance itself.
  123. """
  124. _target_class_doc = "SomeClass"
  125. _dispatch_target = instrumentation.ClassManager
  126. @classmethod
  127. def _new_classmanager_instance(cls, class_, classmanager):
  128. _InstanceEventsHold.populate(class_, classmanager)
  129. @classmethod
  130. @util.dependencies("sqlalchemy.orm")
  131. def _accept_with(cls, orm, target):
  132. if isinstance(target, instrumentation.ClassManager):
  133. return target
  134. elif isinstance(target, mapperlib.Mapper):
  135. return target.class_manager
  136. elif target is orm.mapper:
  137. return instrumentation.ClassManager
  138. elif isinstance(target, type):
  139. if issubclass(target, mapperlib.Mapper):
  140. return instrumentation.ClassManager
  141. else:
  142. manager = instrumentation.manager_of_class(target)
  143. if manager:
  144. return manager
  145. else:
  146. return _InstanceEventsHold(target)
  147. return None
  148. @classmethod
  149. def _listen(cls, event_key, raw=False, propagate=False, **kw):
  150. target, identifier, fn = \
  151. event_key.dispatch_target, event_key.identifier, \
  152. event_key._listen_fn
  153. if not raw:
  154. def wrap(state, *arg, **kw):
  155. return fn(state.obj(), *arg, **kw)
  156. event_key = event_key.with_wrapper(wrap)
  157. event_key.base_listen(propagate=propagate, **kw)
  158. if propagate:
  159. for mgr in target.subclass_managers(True):
  160. event_key.with_dispatch_target(mgr).base_listen(
  161. propagate=True)
  162. @classmethod
  163. def _clear(cls):
  164. super(InstanceEvents, cls)._clear()
  165. _InstanceEventsHold._clear()
  166. def first_init(self, manager, cls):
  167. """Called when the first instance of a particular mapping is called.
  168. This event is called when the ``__init__`` method of a class
  169. is called the first time for that particular class. The event
  170. invokes before ``__init__`` actually proceeds as well as before
  171. the :meth:`.InstanceEvents.init` event is invoked.
  172. """
  173. def init(self, target, args, kwargs):
  174. """Receive an instance when its constructor is called.
  175. This method is only called during a userland construction of
  176. an object, in conjunction with the object's constructor, e.g.
  177. its ``__init__`` method. It is not called when an object is
  178. loaded from the database; see the :meth:`.InstanceEvents.load`
  179. event in order to intercept a database load.
  180. The event is called before the actual ``__init__`` constructor
  181. of the object is called. The ``kwargs`` dictionary may be
  182. modified in-place in order to affect what is passed to
  183. ``__init__``.
  184. :param target: the mapped instance. If
  185. the event is configured with ``raw=True``, this will
  186. instead be the :class:`.InstanceState` state-management
  187. object associated with the instance.
  188. :param args: positional arguments passed to the ``__init__`` method.
  189. This is passed as a tuple and is currently immutable.
  190. :param kwargs: keyword arguments passed to the ``__init__`` method.
  191. This structure *can* be altered in place.
  192. .. seealso::
  193. :meth:`.InstanceEvents.init_failure`
  194. :meth:`.InstanceEvents.load`
  195. """
  196. def init_failure(self, target, args, kwargs):
  197. """Receive an instance when its constructor has been called,
  198. and raised an exception.
  199. This method is only called during a userland construction of
  200. an object, in conjunction with the object's constructor, e.g.
  201. its ``__init__`` method. It is not called when an object is loaded
  202. from the database.
  203. The event is invoked after an exception raised by the ``__init__``
  204. method is caught. After the event
  205. is invoked, the original exception is re-raised outwards, so that
  206. the construction of the object still raises an exception. The
  207. actual exception and stack trace raised should be present in
  208. ``sys.exc_info()``.
  209. :param target: the mapped instance. If
  210. the event is configured with ``raw=True``, this will
  211. instead be the :class:`.InstanceState` state-management
  212. object associated with the instance.
  213. :param args: positional arguments that were passed to the ``__init__``
  214. method.
  215. :param kwargs: keyword arguments that were passed to the ``__init__``
  216. method.
  217. .. seealso::
  218. :meth:`.InstanceEvents.init`
  219. :meth:`.InstanceEvents.load`
  220. """
  221. def load(self, target, context):
  222. """Receive an object instance after it has been created via
  223. ``__new__``, and after initial attribute population has
  224. occurred.
  225. This typically occurs when the instance is created based on
  226. incoming result rows, and is only called once for that
  227. instance's lifetime.
  228. Note that during a result-row load, this method is called upon
  229. the first row received for this instance. Note that some
  230. attributes and collections may or may not be loaded or even
  231. initialized, depending on what's present in the result rows.
  232. :param target: the mapped instance. If
  233. the event is configured with ``raw=True``, this will
  234. instead be the :class:`.InstanceState` state-management
  235. object associated with the instance.
  236. :param context: the :class:`.QueryContext` corresponding to the
  237. current :class:`.Query` in progress. This argument may be
  238. ``None`` if the load does not correspond to a :class:`.Query`,
  239. such as during :meth:`.Session.merge`.
  240. .. seealso::
  241. :meth:`.InstanceEvents.init`
  242. :meth:`.InstanceEvents.refresh`
  243. :meth:`.SessionEvents.loaded_as_persistent`
  244. """
  245. def refresh(self, target, context, attrs):
  246. """Receive an object instance after one or more attributes have
  247. been refreshed from a query.
  248. Contrast this to the :meth:`.InstanceEvents.load` method, which
  249. is invoked when the object is first loaded from a query.
  250. :param target: the mapped instance. If
  251. the event is configured with ``raw=True``, this will
  252. instead be the :class:`.InstanceState` state-management
  253. object associated with the instance.
  254. :param context: the :class:`.QueryContext` corresponding to the
  255. current :class:`.Query` in progress.
  256. :param attrs: sequence of attribute names which
  257. were populated, or None if all column-mapped, non-deferred
  258. attributes were populated.
  259. .. seealso::
  260. :meth:`.InstanceEvents.load`
  261. """
  262. def refresh_flush(self, target, flush_context, attrs):
  263. """Receive an object instance after one or more attributes have
  264. been refreshed within the persistence of the object.
  265. This event is the same as :meth:`.InstanceEvents.refresh` except
  266. it is invoked within the unit of work flush process, and the values
  267. here typically come from the process of handling an INSERT or
  268. UPDATE, such as via the RETURNING clause or from Python-side default
  269. values.
  270. .. versionadded:: 1.0.5
  271. :param target: the mapped instance. If
  272. the event is configured with ``raw=True``, this will
  273. instead be the :class:`.InstanceState` state-management
  274. object associated with the instance.
  275. :param flush_context: Internal :class:`.UOWTransaction` object
  276. which handles the details of the flush.
  277. :param attrs: sequence of attribute names which
  278. were populated.
  279. """
  280. def expire(self, target, attrs):
  281. """Receive an object instance after its attributes or some subset
  282. have been expired.
  283. 'keys' is a list of attribute names. If None, the entire
  284. state was expired.
  285. :param target: the mapped instance. If
  286. the event is configured with ``raw=True``, this will
  287. instead be the :class:`.InstanceState` state-management
  288. object associated with the instance.
  289. :param attrs: sequence of attribute
  290. names which were expired, or None if all attributes were
  291. expired.
  292. """
  293. def pickle(self, target, state_dict):
  294. """Receive an object instance when its associated state is
  295. being pickled.
  296. :param target: the mapped instance. If
  297. the event is configured with ``raw=True``, this will
  298. instead be the :class:`.InstanceState` state-management
  299. object associated with the instance.
  300. :param state_dict: the dictionary returned by
  301. :class:`.InstanceState.__getstate__`, containing the state
  302. to be pickled.
  303. """
  304. def unpickle(self, target, state_dict):
  305. """Receive an object instance after its associated state has
  306. been unpickled.
  307. :param target: the mapped instance. If
  308. the event is configured with ``raw=True``, this will
  309. instead be the :class:`.InstanceState` state-management
  310. object associated with the instance.
  311. :param state_dict: the dictionary sent to
  312. :class:`.InstanceState.__setstate__`, containing the state
  313. dictionary which was pickled.
  314. """
  315. class _EventsHold(event.RefCollection):
  316. """Hold onto listeners against unmapped, uninstrumented classes.
  317. Establish _listen() for that class' mapper/instrumentation when
  318. those objects are created for that class.
  319. """
  320. def __init__(self, class_):
  321. self.class_ = class_
  322. @classmethod
  323. def _clear(cls):
  324. cls.all_holds.clear()
  325. class HoldEvents(object):
  326. _dispatch_target = None
  327. @classmethod
  328. def _listen(cls, event_key, raw=False, propagate=False, **kw):
  329. target, identifier, fn = \
  330. event_key.dispatch_target, event_key.identifier, event_key.fn
  331. if target.class_ in target.all_holds:
  332. collection = target.all_holds[target.class_]
  333. else:
  334. collection = target.all_holds[target.class_] = {}
  335. event.registry._stored_in_collection(event_key, target)
  336. collection[event_key._key] = (event_key, raw, propagate)
  337. if propagate:
  338. stack = list(target.class_.__subclasses__())
  339. while stack:
  340. subclass = stack.pop(0)
  341. stack.extend(subclass.__subclasses__())
  342. subject = target.resolve(subclass)
  343. if subject is not None:
  344. # we are already going through __subclasses__()
  345. # so leave generic propagate flag False
  346. event_key.with_dispatch_target(subject).\
  347. listen(raw=raw, propagate=False, **kw)
  348. def remove(self, event_key):
  349. target, identifier, fn = \
  350. event_key.dispatch_target, event_key.identifier, event_key.fn
  351. if isinstance(target, _EventsHold):
  352. collection = target.all_holds[target.class_]
  353. del collection[event_key._key]
  354. @classmethod
  355. def populate(cls, class_, subject):
  356. for subclass in class_.__mro__:
  357. if subclass in cls.all_holds:
  358. collection = cls.all_holds[subclass]
  359. for event_key, raw, propagate in collection.values():
  360. if propagate or subclass is class_:
  361. # since we can't be sure in what order different
  362. # classes in a hierarchy are triggered with
  363. # populate(), we rely upon _EventsHold for all event
  364. # assignment, instead of using the generic propagate
  365. # flag.
  366. event_key.with_dispatch_target(subject).\
  367. listen(raw=raw, propagate=False)
  368. class _InstanceEventsHold(_EventsHold):
  369. all_holds = weakref.WeakKeyDictionary()
  370. def resolve(self, class_):
  371. return instrumentation.manager_of_class(class_)
  372. class HoldInstanceEvents(_EventsHold.HoldEvents, InstanceEvents):
  373. pass
  374. dispatch = event.dispatcher(HoldInstanceEvents)
  375. class MapperEvents(event.Events):
  376. """Define events specific to mappings.
  377. e.g.::
  378. from sqlalchemy import event
  379. def my_before_insert_listener(mapper, connection, target):
  380. # execute a stored procedure upon INSERT,
  381. # apply the value to the row to be inserted
  382. target.calculated_value = connection.scalar(
  383. "select my_special_function(%d)"
  384. % target.special_number)
  385. # associate the listener function with SomeClass,
  386. # to execute during the "before_insert" hook
  387. event.listen(
  388. SomeClass, 'before_insert', my_before_insert_listener)
  389. Available targets include:
  390. * mapped classes
  391. * unmapped superclasses of mapped or to-be-mapped classes
  392. (using the ``propagate=True`` flag)
  393. * :class:`.Mapper` objects
  394. * the :class:`.Mapper` class itself and the :func:`.mapper`
  395. function indicate listening for all mappers.
  396. .. versionchanged:: 0.8.0 mapper events can be associated with
  397. unmapped superclasses of mapped classes.
  398. Mapper events provide hooks into critical sections of the
  399. mapper, including those related to object instrumentation,
  400. object loading, and object persistence. In particular, the
  401. persistence methods :meth:`~.MapperEvents.before_insert`,
  402. and :meth:`~.MapperEvents.before_update` are popular
  403. places to augment the state being persisted - however, these
  404. methods operate with several significant restrictions. The
  405. user is encouraged to evaluate the
  406. :meth:`.SessionEvents.before_flush` and
  407. :meth:`.SessionEvents.after_flush` methods as more
  408. flexible and user-friendly hooks in which to apply
  409. additional database state during a flush.
  410. When using :class:`.MapperEvents`, several modifiers are
  411. available to the :func:`.event.listen` function.
  412. :param propagate=False: When True, the event listener should
  413. be applied to all inheriting mappers and/or the mappers of
  414. inheriting classes, as well as any
  415. mapper which is the target of this listener.
  416. :param raw=False: When True, the "target" argument passed
  417. to applicable event listener functions will be the
  418. instance's :class:`.InstanceState` management
  419. object, rather than the mapped instance itself.
  420. :param retval=False: when True, the user-defined event function
  421. must have a return value, the purpose of which is either to
  422. control subsequent event propagation, or to otherwise alter
  423. the operation in progress by the mapper. Possible return
  424. values are:
  425. * ``sqlalchemy.orm.interfaces.EXT_CONTINUE`` - continue event
  426. processing normally.
  427. * ``sqlalchemy.orm.interfaces.EXT_STOP`` - cancel all subsequent
  428. event handlers in the chain.
  429. * other values - the return value specified by specific listeners.
  430. """
  431. _target_class_doc = "SomeClass"
  432. _dispatch_target = mapperlib.Mapper
  433. @classmethod
  434. def _new_mapper_instance(cls, class_, mapper):
  435. _MapperEventsHold.populate(class_, mapper)
  436. @classmethod
  437. @util.dependencies("sqlalchemy.orm")
  438. def _accept_with(cls, orm, target):
  439. if target is orm.mapper:
  440. return mapperlib.Mapper
  441. elif isinstance(target, type):
  442. if issubclass(target, mapperlib.Mapper):
  443. return target
  444. else:
  445. mapper = _mapper_or_none(target)
  446. if mapper is not None:
  447. return mapper
  448. else:
  449. return _MapperEventsHold(target)
  450. else:
  451. return target
  452. @classmethod
  453. def _listen(
  454. cls, event_key, raw=False, retval=False, propagate=False, **kw):
  455. target, identifier, fn = \
  456. event_key.dispatch_target, event_key.identifier, \
  457. event_key._listen_fn
  458. if identifier in ("before_configured", "after_configured") and \
  459. target is not mapperlib.Mapper:
  460. util.warn(
  461. "'before_configured' and 'after_configured' ORM events "
  462. "only invoke with the mapper() function or Mapper class "
  463. "as the target.")
  464. if not raw or not retval:
  465. if not raw:
  466. meth = getattr(cls, identifier)
  467. try:
  468. target_index = \
  469. inspect_getargspec(meth)[0].index('target') - 1
  470. except ValueError:
  471. target_index = None
  472. def wrap(*arg, **kw):
  473. if not raw and target_index is not None:
  474. arg = list(arg)
  475. arg[target_index] = arg[target_index].obj()
  476. if not retval:
  477. fn(*arg, **kw)
  478. return interfaces.EXT_CONTINUE
  479. else:
  480. return fn(*arg, **kw)
  481. event_key = event_key.with_wrapper(wrap)
  482. if propagate:
  483. for mapper in target.self_and_descendants:
  484. event_key.with_dispatch_target(mapper).base_listen(
  485. propagate=True, **kw)
  486. else:
  487. event_key.base_listen(**kw)
  488. @classmethod
  489. def _clear(cls):
  490. super(MapperEvents, cls)._clear()
  491. _MapperEventsHold._clear()
  492. def instrument_class(self, mapper, class_):
  493. r"""Receive a class when the mapper is first constructed,
  494. before instrumentation is applied to the mapped class.
  495. This event is the earliest phase of mapper construction.
  496. Most attributes of the mapper are not yet initialized.
  497. This listener can either be applied to the :class:`.Mapper`
  498. class overall, or to any un-mapped class which serves as a base
  499. for classes that will be mapped (using the ``propagate=True`` flag)::
  500. Base = declarative_base()
  501. @event.listens_for(Base, "instrument_class", propagate=True)
  502. def on_new_class(mapper, cls_):
  503. " ... "
  504. :param mapper: the :class:`.Mapper` which is the target
  505. of this event.
  506. :param class\_: the mapped class.
  507. """
  508. def mapper_configured(self, mapper, class_):
  509. r"""Called when a specific mapper has completed its own configuration
  510. within the scope of the :func:`.configure_mappers` call.
  511. The :meth:`.MapperEvents.mapper_configured` event is invoked
  512. for each mapper that is encountered when the
  513. :func:`.orm.configure_mappers` function proceeds through the current
  514. list of not-yet-configured mappers.
  515. :func:`.orm.configure_mappers` is typically invoked
  516. automatically as mappings are first used, as well as each time
  517. new mappers have been made available and new mapper use is
  518. detected.
  519. When the event is called, the mapper should be in its final
  520. state, but **not including backrefs** that may be invoked from
  521. other mappers; they might still be pending within the
  522. configuration operation. Bidirectional relationships that
  523. are instead configured via the
  524. :paramref:`.orm.relationship.back_populates` argument
  525. *will* be fully available, since this style of relationship does not
  526. rely upon other possibly-not-configured mappers to know that they
  527. exist.
  528. For an event that is guaranteed to have **all** mappers ready
  529. to go including backrefs that are defined only on other
  530. mappings, use the :meth:`.MapperEvents.after_configured`
  531. event; this event invokes only after all known mappings have been
  532. fully configured.
  533. The :meth:`.MapperEvents.mapper_configured` event, unlike
  534. :meth:`.MapperEvents.before_configured` or
  535. :meth:`.MapperEvents.after_configured`,
  536. is called for each mapper/class individually, and the mapper is
  537. passed to the event itself. It also is called exactly once for
  538. a particular mapper. The event is therefore useful for
  539. configurational steps that benefit from being invoked just once
  540. on a specific mapper basis, which don't require that "backref"
  541. configurations are necessarily ready yet.
  542. :param mapper: the :class:`.Mapper` which is the target
  543. of this event.
  544. :param class\_: the mapped class.
  545. .. seealso::
  546. :meth:`.MapperEvents.before_configured`
  547. :meth:`.MapperEvents.after_configured`
  548. """
  549. # TODO: need coverage for this event
  550. def before_configured(self):
  551. """Called before a series of mappers have been configured.
  552. The :meth:`.MapperEvents.before_configured` event is invoked
  553. each time the :func:`.orm.configure_mappers` function is
  554. invoked, before the function has done any of its work.
  555. :func:`.orm.configure_mappers` is typically invoked
  556. automatically as mappings are first used, as well as each time
  557. new mappers have been made available and new mapper use is
  558. detected.
  559. This event can **only** be applied to the :class:`.Mapper` class
  560. or :func:`.mapper` function, and not to individual mappings or
  561. mapped classes. It is only invoked for all mappings as a whole::
  562. from sqlalchemy.orm import mapper
  563. @event.listens_for(mapper, "before_configured")
  564. def go():
  565. # ...
  566. Constrast this event to :meth:`.MapperEvents.after_configured`,
  567. which is invoked after the series of mappers has been configured,
  568. as well as :meth:`.MapperEvents.mapper_configured`, which is invoked
  569. on a per-mapper basis as each one is configured to the extent possible.
  570. Theoretically this event is called once per
  571. application, but is actually called any time new mappers
  572. are to be affected by a :func:`.orm.configure_mappers`
  573. call. If new mappings are constructed after existing ones have
  574. already been used, this event will likely be called again. To ensure
  575. that a particular event is only called once and no further, the
  576. ``once=True`` argument (new in 0.9.4) can be applied::
  577. from sqlalchemy.orm import mapper
  578. @event.listens_for(mapper, "before_configured", once=True)
  579. def go():
  580. # ...
  581. .. versionadded:: 0.9.3
  582. .. seealso::
  583. :meth:`.MapperEvents.mapper_configured`
  584. :meth:`.MapperEvents.after_configured`
  585. """
  586. def after_configured(self):
  587. """Called after a series of mappers have been configured.
  588. The :meth:`.MapperEvents.after_configured` event is invoked
  589. each time the :func:`.orm.configure_mappers` function is
  590. invoked, after the function has completed its work.
  591. :func:`.orm.configure_mappers` is typically invoked
  592. automatically as mappings are first used, as well as each time
  593. new mappers have been made available and new mapper use is
  594. detected.
  595. Contrast this event to the :meth:`.MapperEvents.mapper_configured`
  596. event, which is called on a per-mapper basis while the configuration
  597. operation proceeds; unlike that event, when this event is invoked,
  598. all cross-configurations (e.g. backrefs) will also have been made
  599. available for any mappers that were pending.
  600. Also constrast to :meth:`.MapperEvents.before_configured`,
  601. which is invoked before the series of mappers has been configured.
  602. This event can **only** be applied to the :class:`.Mapper` class
  603. or :func:`.mapper` function, and not to individual mappings or
  604. mapped classes. It is only invoked for all mappings as a whole::
  605. from sqlalchemy.orm import mapper
  606. @event.listens_for(mapper, "after_configured")
  607. def go():
  608. # ...
  609. Theoretically this event is called once per
  610. application, but is actually called any time new mappers
  611. have been affected by a :func:`.orm.configure_mappers`
  612. call. If new mappings are constructed after existing ones have
  613. already been used, this event will likely be called again. To ensure
  614. that a particular event is only called once and no further, the
  615. ``once=True`` argument (new in 0.9.4) can be applied::
  616. from sqlalchemy.orm import mapper
  617. @event.listens_for(mapper, "after_configured", once=True)
  618. def go():
  619. # ...
  620. .. seealso::
  621. :meth:`.MapperEvents.mapper_configured`
  622. :meth:`.MapperEvents.before_configured`
  623. """
  624. def before_insert(self, mapper, connection, target):
  625. """Receive an object instance before an INSERT statement
  626. is emitted corresponding to that instance.
  627. This event is used to modify local, non-object related
  628. attributes on the instance before an INSERT occurs, as well
  629. as to emit additional SQL statements on the given
  630. connection.
  631. The event is often called for a batch of objects of the
  632. same class before their INSERT statements are emitted at
  633. once in a later step. In the extremely rare case that
  634. this is not desirable, the :func:`.mapper` can be
  635. configured with ``batch=False``, which will cause
  636. batches of instances to be broken up into individual
  637. (and more poorly performing) event->persist->event
  638. steps.
  639. .. warning::
  640. Mapper-level flush events only allow **very limited operations**,
  641. on attributes local to the row being operated upon only,
  642. as well as allowing any SQL to be emitted on the given
  643. :class:`.Connection`. **Please read fully** the notes
  644. at :ref:`session_persistence_mapper` for guidelines on using
  645. these methods; generally, the :meth:`.SessionEvents.before_flush`
  646. method should be preferred for general on-flush changes.
  647. :param mapper: the :class:`.Mapper` which is the target
  648. of this event.
  649. :param connection: the :class:`.Connection` being used to
  650. emit INSERT statements for this instance. This
  651. provides a handle into the current transaction on the
  652. target database specific to this instance.
  653. :param target: the mapped instance being persisted. If
  654. the event is configured with ``raw=True``, this will
  655. instead be the :class:`.InstanceState` state-management
  656. object associated with the instance.
  657. :return: No return value is supported by this event.
  658. .. seealso::
  659. :ref:`session_persistence_events`
  660. """
  661. def after_insert(self, mapper, connection, target):
  662. """Receive an object instance after an INSERT statement
  663. is emitted corresponding to that instance.
  664. This event is used to modify in-Python-only
  665. state on the instance after an INSERT occurs, as well
  666. as to emit additional SQL statements on the given
  667. connection.
  668. The event is often called for a batch of objects of the
  669. same class after their INSERT statements have been
  670. emitted at once in a previous step. In the extremely
  671. rare case that this is not desirable, the
  672. :func:`.mapper` can be configured with ``batch=False``,
  673. which will cause batches of instances to be broken up
  674. into individual (and more poorly performing)
  675. event->persist->event steps.
  676. .. warning::
  677. Mapper-level flush events only allow **very limited operations**,
  678. on attributes local to the row being operated upon only,
  679. as well as allowing any SQL to be emitted on the given
  680. :class:`.Connection`. **Please read fully** the notes
  681. at :ref:`session_persistence_mapper` for guidelines on using
  682. these methods; generally, the :meth:`.SessionEvents.before_flush`
  683. method should be preferred for general on-flush changes.
  684. :param mapper: the :class:`.Mapper` which is the target
  685. of this event.
  686. :param connection: the :class:`.Connection` being used to
  687. emit INSERT statements for this instance. This
  688. provides a handle into the current transaction on the
  689. target database specific to this instance.
  690. :param target: the mapped instance being persisted. If
  691. the event is configured with ``raw=True``, this will
  692. instead be the :class:`.InstanceState` state-management
  693. object associated with the instance.
  694. :return: No return value is supported by this event.
  695. .. seealso::
  696. :ref:`session_persistence_events`
  697. """
  698. def before_update(self, mapper, connection, target):
  699. """Receive an object instance before an UPDATE statement
  700. is emitted corresponding to that instance.
  701. This event is used to modify local, non-object related
  702. attributes on the instance before an UPDATE occurs, as well
  703. as to emit additional SQL statements on the given
  704. connection.
  705. This method is called for all instances that are
  706. marked as "dirty", *even those which have no net changes
  707. to their column-based attributes*. An object is marked
  708. as dirty when any of its column-based attributes have a
  709. "set attribute" operation called or when any of its
  710. collections are modified. If, at update time, no
  711. column-based attributes have any net changes, no UPDATE
  712. statement will be issued. This means that an instance
  713. being sent to :meth:`~.MapperEvents.before_update` is
  714. *not* a guarantee that an UPDATE statement will be
  715. issued, although you can affect the outcome here by
  716. modifying attributes so that a net change in value does
  717. exist.
  718. To detect if the column-based attributes on the object have net
  719. changes, and will therefore generate an UPDATE statement, use
  720. ``object_session(instance).is_modified(instance,
  721. include_collections=False)``.
  722. The event is often called for a batch of objects of the
  723. same class before their UPDATE statements are emitted at
  724. once in a later step. In the extremely rare case that
  725. this is not desirable, the :func:`.mapper` can be
  726. configured with ``batch=False``, which will cause
  727. batches of instances to be broken up into individual
  728. (and more poorly performing) event->persist->event
  729. steps.
  730. .. warning::
  731. Mapper-level flush events only allow **very limited operations**,
  732. on attributes local to the row being operated upon only,
  733. as well as allowing any SQL to be emitted on the given
  734. :class:`.Connection`. **Please read fully** the notes
  735. at :ref:`session_persistence_mapper` for guidelines on using
  736. these methods; generally, the :meth:`.SessionEvents.before_flush`
  737. method should be preferred for general on-flush changes.
  738. :param mapper: the :class:`.Mapper` which is the target
  739. of this event.
  740. :param connection: the :class:`.Connection` being used to
  741. emit UPDATE statements for this instance. This
  742. provides a handle into the current transaction on the
  743. target database specific to this instance.
  744. :param target: the mapped instance being persisted. If
  745. the event is configured with ``raw=True``, this will
  746. instead be the :class:`.InstanceState` state-management
  747. object associated with the instance.
  748. :return: No return value is supported by this event.
  749. .. seealso::
  750. :ref:`session_persistence_events`
  751. """
  752. def after_update(self, mapper, connection, target):
  753. """Receive an object instance after an UPDATE statement
  754. is emitted corresponding to that instance.
  755. This event is used to modify in-Python-only
  756. state on the instance after an UPDATE occurs, as well
  757. as to emit additional SQL statements on the given
  758. connection.
  759. This method is called for all instances that are
  760. marked as "dirty", *even those which have no net changes
  761. to their column-based attributes*, and for which
  762. no UPDATE statement has proceeded. An object is marked
  763. as dirty when any of its column-based attributes have a
  764. "set attribute" operation called or when any of its
  765. collections are modified. If, at update time, no
  766. column-based attributes have any net changes, no UPDATE
  767. statement will be issued. This means that an instance
  768. being sent to :meth:`~.MapperEvents.after_update` is
  769. *not* a guarantee that an UPDATE statement has been
  770. issued.
  771. To detect if the column-based attributes on the object have net
  772. changes, and therefore resulted in an UPDATE statement, use
  773. ``object_session(instance).is_modified(instance,
  774. include_collections=False)``.
  775. The event is often called for a batch of objects of the
  776. same class after their UPDATE statements have been emitted at
  777. once in a previous step. In the extremely rare case that
  778. this is not desirable, the :func:`.mapper` can be
  779. configured with ``batch=False``, which will cause
  780. batches of instances to be broken up into individual
  781. (and more poorly performing) event->persist->event
  782. steps.
  783. .. warning::
  784. Mapper-level flush events only allow **very limited operations**,
  785. on attributes local to the row being operated upon only,
  786. as well as allowing any SQL to be emitted on the given
  787. :class:`.Connection`. **Please read fully** the notes
  788. at :ref:`session_persistence_mapper` for guidelines on using
  789. these methods; generally, the :meth:`.SessionEvents.before_flush`
  790. method should be preferred for general on-flush changes.
  791. :param mapper: the :class:`.Mapper` which is the target
  792. of this event.
  793. :param connection: the :class:`.Connection` being used to
  794. emit UPDATE statements for this instance. This
  795. provides a handle into the current transaction on the
  796. target database specific to this instance.
  797. :param target: the mapped instance being persisted. If
  798. the event is configured with ``raw=True``, this will
  799. instead be the :class:`.InstanceState` state-management
  800. object associated with the instance.
  801. :return: No return value is supported by this event.
  802. .. seealso::
  803. :ref:`session_persistence_events`
  804. """
  805. def before_delete(self, mapper, connection, target):
  806. """Receive an object instance before a DELETE statement
  807. is emitted corresponding to that instance.
  808. This event is used to emit additional SQL statements on
  809. the given connection as well as to perform application
  810. specific bookkeeping related to a deletion event.
  811. The event is often called for a batch of objects of the
  812. same class before their DELETE statements are emitted at
  813. once in a later step.
  814. .. warning::
  815. Mapper-level flush events only allow **very limited operations**,
  816. on attributes local to the row being operated upon only,
  817. as well as allowing any SQL to be emitted on the given
  818. :class:`.Connection`. **Please read fully** the notes
  819. at :ref:`session_persistence_mapper` for guidelines on using
  820. these methods; generally, the :meth:`.SessionEvents.before_flush`
  821. method should be preferred for general on-flush changes.
  822. :param mapper: the :class:`.Mapper` which is the target
  823. of this event.
  824. :param connection: the :class:`.Connection` being used to
  825. emit DELETE statements for this instance. This
  826. provides a handle into the current transaction on the
  827. target database specific to this instance.
  828. :param target: the mapped instance being deleted. If
  829. the event is configured with ``raw=True``, this will
  830. instead be the :class:`.InstanceState` state-management
  831. object associated with the instance.
  832. :return: No return value is supported by this event.
  833. .. seealso::
  834. :ref:`session_persistence_events`
  835. """
  836. def after_delete(self, mapper, connection, target):
  837. """Receive an object instance after a DELETE statement
  838. has been emitted corresponding to that instance.
  839. This event is used to emit additional SQL statements on
  840. the given connection as well as to perform application
  841. specific bookkeeping related to a deletion event.
  842. The event is often called for a batch of objects of the
  843. same class after their DELETE statements have been emitted at
  844. once in a previous step.
  845. .. warning::
  846. Mapper-level flush events only allow **very limited operations**,
  847. on attributes local to the row being operated upon only,
  848. as well as allowing any SQL to be emitted on the given
  849. :class:`.Connection`. **Please read fully** the notes
  850. at :ref:`session_persistence_mapper` for guidelines on using
  851. these methods; generally, the :meth:`.SessionEvents.before_flush`
  852. method should be preferred for general on-flush changes.
  853. :param mapper: the :class:`.Mapper` which is the target
  854. of this event.
  855. :param connection: the :class:`.Connection` being used to
  856. emit DELETE statements for this instance. This
  857. provides a handle into the current transaction on the
  858. target database specific to this instance.
  859. :param target: the mapped instance being deleted. If
  860. the event is configured with ``raw=True``, this will
  861. instead be the :class:`.InstanceState` state-management
  862. object associated with the instance.
  863. :return: No return value is supported by this event.
  864. .. seealso::
  865. :ref:`session_persistence_events`
  866. """
  867. class _MapperEventsHold(_EventsHold):
  868. all_holds = weakref.WeakKeyDictionary()
  869. def resolve(self, class_):
  870. return _mapper_or_none(class_)
  871. class HoldMapperEvents(_EventsHold.HoldEvents, MapperEvents):
  872. pass
  873. dispatch = event.dispatcher(HoldMapperEvents)
  874. class SessionEvents(event.Events):
  875. """Define events specific to :class:`.Session` lifecycle.
  876. e.g.::
  877. from sqlalchemy import event
  878. from sqlalchemy.orm import sessionmaker
  879. def my_before_commit(session):
  880. print "before commit!"
  881. Session = sessionmaker()
  882. event.listen(Session, "before_commit", my_before_commit)
  883. The :func:`~.event.listen` function will accept
  884. :class:`.Session` objects as well as the return result
  885. of :class:`~.sessionmaker()` and :class:`~.scoped_session()`.
  886. Additionally, it accepts the :class:`.Session` class which
  887. will apply listeners to all :class:`.Session` instances
  888. globally.
  889. """
  890. _target_class_doc = "SomeSessionOrFactory"
  891. _dispatch_target = Session
  892. @classmethod
  893. def _accept_with(cls, target):
  894. if isinstance(target, scoped_session):
  895. target = target.session_factory
  896. if not isinstance(target, sessionmaker) and \
  897. (
  898. not isinstance(target, type) or
  899. not issubclass(target, Session)
  900. ):
  901. raise exc.ArgumentError(
  902. "Session event listen on a scoped_session "
  903. "requires that its creation callable "
  904. "is associated with the Session class.")
  905. if isinstance(target, sessionmaker):
  906. return target.class_
  907. elif isinstance(target, type):
  908. if issubclass(target, scoped_session):
  909. return Session
  910. elif issubclass(target, Session):
  911. return target
  912. elif isinstance(target, Session):
  913. return target
  914. else:
  915. return None
  916. def after_transaction_create(self, session, transaction):
  917. """Execute when a new :class:`.SessionTransaction` is created.
  918. This event differs from :meth:`~.SessionEvents.after_begin`
  919. in that it occurs for each :class:`.SessionTransaction`
  920. overall, as opposed to when transactions are begun
  921. on individual database connections. It is also invoked
  922. for nested transactions and subtransactions, and is always
  923. matched by a corresponding
  924. :meth:`~.SessionEvents.after_transaction_end` event
  925. (assuming normal operation of the :class:`.Session`).
  926. :param session: the target :class:`.Session`.
  927. :param transaction: the target :class:`.SessionTransaction`.
  928. To detect if this is the outermost
  929. :class:`.SessionTransaction`, as opposed to a "subtransaction" or a
  930. SAVEPOINT, test that the :attr:`.SessionTransaction.parent` attribute
  931. is ``None``::
  932. @event.listens_for(session, "after_transaction_create")
  933. def after_transaction_create(session, transaction):
  934. if transaction.parent is None:
  935. # work with top-level transaction
  936. To detect if the :class:`.SessionTransaction` is a SAVEPOINT, use the
  937. :attr:`.SessionTransaction.nested` attribute::
  938. @event.listens_for(session, "after_transaction_create")
  939. def after_transaction_create(session, transaction):
  940. if transaction.nested:
  941. # work with SAVEPOINT transaction
  942. .. seealso::
  943. :class:`.SessionTransaction`
  944. :meth:`~.SessionEvents.after_transaction_end`
  945. """
  946. def after_transaction_end(self, session, transaction):
  947. """Execute when the span of a :class:`.SessionTransaction` ends.
  948. This event differs from :meth:`~.SessionEvents.after_commit`
  949. in that it corresponds to all :class:`.SessionTransaction`
  950. objects in use, including those for nested transactions
  951. and subtransactions, and is always matched by a corresponding
  952. :meth:`~.SessionEvents.after_transaction_create` event.
  953. :param session: the target :class:`.Session`.
  954. :param transaction: the target :class:`.SessionTransaction`.
  955. To detect if this is the outermost
  956. :class:`.SessionTransaction`, as opposed to a "subtransaction" or a
  957. SAVEPOINT, test that the :attr:`.SessionTransaction.parent` attribute
  958. is ``None``::
  959. @event.listens_for(session, "after_transaction_create")
  960. def after_transaction_end(session, transaction):
  961. if transaction.parent is None:
  962. # work with top-level transaction
  963. To detect if the :class:`.SessionTransaction` is a SAVEPOINT, use the
  964. :attr:`.SessionTransaction.nested` attribute::
  965. @event.listens_for(session, "after_transaction_create")
  966. def after_transaction_end(session, transaction):
  967. if transaction.nested:
  968. # work with SAVEPOINT transaction
  969. .. seealso::
  970. :class:`.SessionTransaction`
  971. :meth:`~.SessionEvents.after_transaction_create`
  972. """
  973. def before_commit(self, session):
  974. """Execute before commit is called.
  975. .. note::
  976. The :meth:`~.SessionEvents.before_commit` hook is *not* per-flush,
  977. that is, the :class:`.Session` can emit SQL to the database
  978. many times within the scope of a transaction.
  979. For interception of these events, use the
  980. :meth:`~.SessionEvents.before_flush`,
  981. :meth:`~.SessionEvents.after_flush`, or
  982. :meth:`~.SessionEvents.after_flush_postexec`
  983. events.
  984. :param session: The target :class:`.Session`.
  985. .. seealso::
  986. :meth:`~.SessionEvents.after_commit`
  987. :meth:`~.SessionEvents.after_begin`
  988. :meth:`~.SessionEvents.after_transaction_create`
  989. :meth:`~.SessionEvents.after_transaction_end`
  990. """
  991. def after_commit(self, session):
  992. """Execute after a commit has occurred.
  993. .. note::
  994. The :meth:`~.SessionEvents.after_commit` hook is *not* per-flush,
  995. that is, the :class:`.Session` can emit SQL to the database
  996. many times within the scope of a transaction.
  997. For interception of these events, use the
  998. :meth:`~.SessionEvents.before_flush`,
  999. :meth:`~.SessionEvents.after_flush`, or
  1000. :meth:`~.SessionEvents.after_flush_postexec`
  1001. events.
  1002. .. note::
  1003. The :class:`.Session` is not in an active transaction
  1004. when the :meth:`~.SessionEvents.after_commit` event is invoked,
  1005. and therefore can not emit SQL. To emit SQL corresponding to
  1006. every transaction, use the :meth:`~.SessionEvents.before_commit`
  1007. event.
  1008. :param session: The target :class:`.Session`.
  1009. .. seealso::
  1010. :meth:`~.SessionEvents.before_commit`
  1011. :meth:`~.SessionEvents.after_begin`
  1012. :meth:`~.SessionEvents.after_transaction_create`
  1013. :meth:`~.SessionEvents.after_transaction_end`
  1014. """
  1015. def after_rollback(self, session):
  1016. """Execute after a real DBAPI rollback has occurred.
  1017. Note that this event only fires when the *actual* rollback against
  1018. the database occurs - it does *not* fire each time the
  1019. :meth:`.Session.rollback` method is called, if the underlying
  1020. DBAPI transaction has already been rolled back. In many
  1021. cases, the :class:`.Session` will not be in
  1022. an "active" state during this event, as the current
  1023. transaction is not valid. To acquire a :class:`.Session`
  1024. which is active after the outermost rollback has proceeded,
  1025. use the :meth:`.SessionEvents.after_soft_rollback` event, checking the
  1026. :attr:`.Session.is_active` flag.
  1027. :param session: The target :class:`.Session`.
  1028. """
  1029. def after_soft_rollback(self, session, previous_transaction):
  1030. """Execute after any rollback has occurred, including "soft"
  1031. rollbacks that don't actually emit at the DBAPI level.
  1032. This corresponds to both nested and outer rollbacks, i.e.
  1033. the innermost rollback that calls the DBAPI's
  1034. rollback() method, as well as the enclosing rollback
  1035. calls that only pop themselves from the transaction stack.
  1036. The given :class:`.Session` can be used to invoke SQL and
  1037. :meth:`.Session.query` operations after an outermost rollback
  1038. by first checking the :attr:`.Session.is_active` flag::
  1039. @event.listens_for(Session, "after_soft_rollback")
  1040. def do_something(session, previous_transaction):
  1041. if session.is_active:
  1042. session.execute("select * from some_table")
  1043. :param session: The target :class:`.Session`.
  1044. :param previous_transaction: The :class:`.SessionTransaction`
  1045. transactional marker object which was just closed. The current
  1046. :class:`.SessionTransaction` for the given :class:`.Session` is
  1047. available via the :attr:`.Session.transaction` attribute.
  1048. .. versionadded:: 0.7.3
  1049. """
  1050. def before_flush(self, session, flush_context, instances):
  1051. """Execute before flush process has started.
  1052. :param session: The target :class:`.Session`.
  1053. :param flush_context: Internal :class:`.UOWTransaction` object
  1054. which handles the details of the flush.
  1055. :param instances: Usually ``None``, this is the collection of
  1056. objects which can be passed to the :meth:`.Session.flush` method
  1057. (note this usage is deprecated).
  1058. .. seealso::
  1059. :meth:`~.SessionEvents.after_flush`
  1060. :meth:`~.SessionEvents.after_flush_postexec`
  1061. :ref:`session_persistence_events`
  1062. """
  1063. def after_flush(self, session, flush_context):
  1064. """Execute after flush has completed, but before commit has been
  1065. called.
  1066. Note that the session's state is still in pre-flush, i.e. 'new',
  1067. 'dirty', and 'deleted' lists still show pre-flush state as well
  1068. as the history settings on instance attributes.
  1069. :param session: The target :class:`.Session`.
  1070. :param flush_context: Internal :class:`.UOWTransaction` object
  1071. which handles the details of the flush.
  1072. .. seealso::
  1073. :meth:`~.SessionEvents.before_flush`
  1074. :meth:`~.SessionEvents.after_flush_postexec`
  1075. :ref:`session_persistence_events`
  1076. """
  1077. def after_flush_postexec(self, session, flush_context):
  1078. """Execute after flush has completed, and after the post-exec
  1079. state occurs.
  1080. This will be when the 'new', 'dirty', and 'deleted' lists are in
  1081. their final state. An actual commit() may or may not have
  1082. occurred, depending on whether or not the flush started its own
  1083. transaction or participated in a larger transaction.
  1084. :param session: The target :class:`.Session`.
  1085. :param flush_context: Internal :class:`.UOWTransaction` object
  1086. which handles the details of the flush.
  1087. .. seealso::
  1088. :meth:`~.SessionEvents.before_flush`
  1089. :meth:`~.SessionEvents.after_flush`
  1090. :ref:`session_persistence_events`
  1091. """
  1092. def after_begin(self, session, transaction, connection):
  1093. """Execute after a transaction is begun on a connection
  1094. :param session: The target :class:`.Session`.
  1095. :param transaction: The :class:`.SessionTransaction`.
  1096. :param connection: The :class:`~.engine.Connection` object
  1097. which will be used for SQL statements.
  1098. .. seealso::
  1099. :meth:`~.SessionEvents.before_commit`
  1100. :meth:`~.SessionEvents.after_commit`
  1101. :meth:`~.SessionEvents.after_transaction_create`
  1102. :meth:`~.SessionEvents.after_transaction_end`
  1103. """
  1104. def before_attach(self, session, instance):
  1105. """Execute before an instance is attached to a session.
  1106. This is called before an add, delete or merge causes
  1107. the object to be part of the session.
  1108. .. versionadded:: 0.8. Note that :meth:`~.SessionEvents.after_attach`
  1109. now fires off after the item is part of the session.
  1110. :meth:`.before_attach` is provided for those cases where
  1111. the item should not yet be part of the session state.
  1112. .. seealso::
  1113. :meth:`~.SessionEvents.after_attach`
  1114. :ref:`session_lifecycle_events`
  1115. """
  1116. def after_attach(self, session, instance):
  1117. """Execute after an instance is attached to a session.
  1118. This is called after an add, delete or merge.
  1119. .. note::
  1120. As of 0.8, this event fires off *after* the item
  1121. has been fully associated with the session, which is
  1122. different than previous releases. For event
  1123. handlers that require the object not yet
  1124. be part of session state (such as handlers which
  1125. may autoflush while the target object is not
  1126. yet complete) consider the
  1127. new :meth:`.before_attach` event.
  1128. .. seealso::
  1129. :meth:`~.SessionEvents.before_attach`
  1130. :ref:`session_lifecycle_events`
  1131. """
  1132. @event._legacy_signature("0.9",
  1133. ["session", "query", "query_context", "result"],
  1134. lambda update_context: (
  1135. update_context.session,
  1136. update_context.query,
  1137. update_context.context,
  1138. update_context.result))
  1139. def after_bulk_update(self, update_context):
  1140. """Execute after a bulk update operation to the session.
  1141. This is called as a result of the :meth:`.Query.update` method.
  1142. :param update_context: an "update context" object which contains
  1143. details about the update, including these attributes:
  1144. * ``session`` - the :class:`.Session` involved
  1145. * ``query`` -the :class:`.Query` object that this update operation
  1146. was called upon.
  1147. * ``context`` The :class:`.QueryContext` object, corresponding
  1148. to the invocation of an ORM query.
  1149. * ``result`` the :class:`.ResultProxy` returned as a result of the
  1150. bulk UPDATE operation.
  1151. """
  1152. @event._legacy_signature("0.9",
  1153. ["session", "query", "query_context", "result"],
  1154. lambda delete_context: (
  1155. delete_context.session,
  1156. delete_context.query,
  1157. delete_context.context,
  1158. delete_context.result))
  1159. def after_bulk_delete(self, delete_context):
  1160. """Execute after a bulk delete operation to the session.
  1161. This is called as a result of the :meth:`.Query.delete` method.
  1162. :param delete_context: a "delete context" object which contains
  1163. details about the update, including these attributes:
  1164. * ``session`` - the :class:`.Session` involved
  1165. * ``query`` -the :class:`.Query` object that this update operation
  1166. was called upon.
  1167. * ``context`` The :class:`.QueryContext` object, corresponding
  1168. to the invocation of an ORM query.
  1169. * ``result`` the :class:`.ResultProxy` returned as a result of the
  1170. bulk DELETE operation.
  1171. """
  1172. def transient_to_pending(self, session, instance):
  1173. """Intercept the "transient to pending" transition for a specific object.
  1174. This event is a specialization of the
  1175. :meth:`.SessionEvents.after_attach` event which is only invoked
  1176. for this specific transition. It is invoked typically during the
  1177. :meth:`.Session.add` call.
  1178. :param session: target :class:`.Session`
  1179. :param instance: the ORM-mapped instance being operated upon.
  1180. .. versionadded:: 1.1
  1181. .. seealso::
  1182. :ref:`session_lifecycle_events`
  1183. """
  1184. def pending_to_transient(self, session, instance):
  1185. """Intercept the "pending to transient" transition for a specific object.
  1186. This less common transition occurs when an pending object that has
  1187. not been flushed is evicted from the session; this can occur
  1188. when the :meth:`.Session.rollback` method rolls back the transaction,
  1189. or when the :meth:`.Session.expunge` method is used.
  1190. :param session: target :class:`.Session`
  1191. :param instance: the ORM-mapped instance being operated upon.
  1192. .. versionadded:: 1.1
  1193. .. seealso::
  1194. :ref:`session_lifecycle_events`
  1195. """
  1196. def persistent_to_transient(self, session, instance):
  1197. """Intercept the "persistent to transient" transition for a specific object.
  1198. This less common transition occurs when an pending object that has
  1199. has been flushed is evicted from the session; this can occur
  1200. when the :meth:`.Session.rollback` method rolls back the transaction.
  1201. :param session: target :class:`.Session`
  1202. :param instance: the ORM-mapped instance being operated upon.
  1203. .. versionadded:: 1.1
  1204. .. seealso::
  1205. :ref:`session_lifecycle_events`
  1206. """
  1207. def pending_to_persistent(self, session, instance):
  1208. """Intercept the "pending to persistent"" transition for a specific object.
  1209. This event is invoked within the flush process, and is
  1210. similar to scanning the :attr:`.Session.new` collection within
  1211. the :meth:`.SessionEvents.after_flush` event. However, in this
  1212. case the object has already been moved to the persistent state
  1213. when the event is called.
  1214. :param session: target :class:`.Session`
  1215. :param instance: the ORM-mapped instance being operated upon.
  1216. .. versionadded:: 1.1
  1217. .. seealso::
  1218. :ref:`session_lifecycle_events`
  1219. """
  1220. def detached_to_persistent(self, session, instance):
  1221. """Intercept the "detached to persistent" transition for a specific object.
  1222. This event is a specialization of the
  1223. :meth:`.SessionEvents.after_attach` event which is only invoked
  1224. for this specific transition. It is invoked typically during the
  1225. :meth:`.Session.add` call, as well as during the
  1226. :meth:`.Session.delete` call if the object was not previously
  1227. associated with the
  1228. :class:`.Session` (note that an object marked as "deleted" remains
  1229. in the "persistent" state until the flush proceeds).
  1230. .. note::
  1231. If the object becomes persistent as part of a call to
  1232. :meth:`.Session.delete`, the object is **not** yet marked as
  1233. deleted when this event is called. To detect deleted objects,
  1234. check the ``deleted`` flag sent to the
  1235. :meth:`.SessionEvents.persistent_to_detached` to event after the
  1236. flush proceeds, or check the :attr:`.Session.deleted` collection
  1237. within the :meth:`.SessionEvents.before_flush` event if deleted
  1238. objects need to be intercepted before the flush.
  1239. :param session: target :class:`.Session`
  1240. :param instance: the ORM-mapped instance being operated upon.
  1241. .. versionadded:: 1.1
  1242. .. seealso::
  1243. :ref:`session_lifecycle_events`
  1244. """
  1245. def loaded_as_persistent(self, session, instance):
  1246. """Intercept the "loaded as persistent" transition for a specific object.
  1247. This event is invoked within the ORM loading process, and is invoked
  1248. very similarly to the :meth:`.InstanceEvents.load` event. However,
  1249. the event here is linkable to a :class:`.Session` class or instance,
  1250. rather than to a mapper or class hierarchy, and integrates
  1251. with the other session lifecycle events smoothly. The object
  1252. is guaranteed to be present in the session's identity map when
  1253. this event is called.
  1254. :param session: target :class:`.Session`
  1255. :param instance: the ORM-mapped instance being operated upon.
  1256. .. versionadded:: 1.1
  1257. .. seealso::
  1258. :ref:`session_lifecycle_events`
  1259. """
  1260. def persistent_to_deleted(self, session, instance):
  1261. """Intercept the "persistent to deleted" transition for a specific object.
  1262. This event is invoked when a persistent object's identity
  1263. is deleted from the database within a flush, however the object
  1264. still remains associated with the :class:`.Session` until the
  1265. transaction completes.
  1266. If the transaction is rolled back, the object moves again
  1267. to the persistent state, and the
  1268. :meth:`.SessionEvents.deleted_to_persistent` event is called.
  1269. If the transaction is committed, the object becomes detached,
  1270. which will emit the :meth:`.SessionEvents.deleted_to_detached`
  1271. event.
  1272. Note that while the :meth:`.Session.delete` method is the primary
  1273. public interface to mark an object as deleted, many objects
  1274. get deleted due to cascade rules, which are not always determined
  1275. until flush time. Therefore, there's no way to catch
  1276. every object that will be deleted until the flush has proceeded.
  1277. the :meth:`.SessionEvents.persistent_to_deleted` event is therefore
  1278. invoked at the end of a flush.
  1279. .. versionadded:: 1.1
  1280. .. seealso::
  1281. :ref:`session_lifecycle_events`
  1282. """
  1283. def deleted_to_persistent(self, session, instance):
  1284. """Intercept the "deleted to persistent" transition for a specific object.
  1285. This transition occurs only when an object that's been deleted
  1286. successfully in a flush is restored due to a call to
  1287. :meth:`.Session.rollback`. The event is not called under
  1288. any other circumstances.
  1289. .. versionadded:: 1.1
  1290. .. seealso::
  1291. :ref:`session_lifecycle_events`
  1292. """
  1293. def deleted_to_detached(self, session, instance):
  1294. """Intercept the "deleted to detached" transition for a specific object.
  1295. This event is invoked when a deleted object is evicted
  1296. from the session. The typical case when this occurs is when
  1297. the transaction for a :class:`.Session` in which the object
  1298. was deleted is committed; the object moves from the deleted
  1299. state to the detached state.
  1300. It is also invoked for objects that were deleted in a flush
  1301. when the :meth:`.Session.expunge_all` or :meth:`.Session.close`
  1302. events are called, as well as if the object is individually
  1303. expunged from its deleted state via :meth:`.Session.expunge`.
  1304. .. versionadded:: 1.1
  1305. .. seealso::
  1306. :ref:`session_lifecycle_events`
  1307. """
  1308. def persistent_to_detached(self, session, instance):
  1309. """Intercept the "persistent to detached" transition for a specific object.
  1310. This event is invoked when a persistent object is evicted
  1311. from the session. There are many conditions that cause this
  1312. to happen, including:
  1313. * using a method such as :meth:`.Session.expunge`
  1314. or :meth:`.Session.close`
  1315. * Calling the :meth:`.Session.rollback` method, when the object
  1316. was part of an INSERT statement for that session's transaction
  1317. :param session: target :class:`.Session`
  1318. :param instance: the ORM-mapped instance being operated upon.
  1319. :param deleted: boolean. If True, indicates this object moved
  1320. to the detached state because it was marked as deleted and flushed.
  1321. .. versionadded:: 1.1
  1322. .. seealso::
  1323. :ref:`session_lifecycle_events`
  1324. """
  1325. class AttributeEvents(event.Events):
  1326. """Define events for object attributes.
  1327. These are typically defined on the class-bound descriptor for the
  1328. target class.
  1329. e.g.::
  1330. from sqlalchemy import event
  1331. def my_append_listener(target, value, initiator):
  1332. print "received append event for target: %s" % target
  1333. event.listen(MyClass.collection, 'append', my_append_listener)
  1334. Listeners have the option to return a possibly modified version
  1335. of the value, when the ``retval=True`` flag is passed
  1336. to :func:`~.event.listen`::
  1337. def validate_phone(target, value, oldvalue, initiator):
  1338. "Strip non-numeric characters from a phone number"
  1339. return re.sub(r'\D', '', value)
  1340. # setup listener on UserContact.phone attribute, instructing
  1341. # it to use the return value
  1342. listen(UserContact.phone, 'set', validate_phone, retval=True)
  1343. A validation function like the above can also raise an exception
  1344. such as :exc:`ValueError` to halt the operation.
  1345. Several modifiers are available to the :func:`~.event.listen` function.
  1346. :param active_history=False: When True, indicates that the
  1347. "set" event would like to receive the "old" value being
  1348. replaced unconditionally, even if this requires firing off
  1349. database loads. Note that ``active_history`` can also be
  1350. set directly via :func:`.column_property` and
  1351. :func:`.relationship`.
  1352. :param propagate=False: When True, the listener function will
  1353. be established not just for the class attribute given, but
  1354. for attributes of the same name on all current subclasses
  1355. of that class, as well as all future subclasses of that
  1356. class, using an additional listener that listens for
  1357. instrumentation events.
  1358. :param raw=False: When True, the "target" argument to the
  1359. event will be the :class:`.InstanceState` management
  1360. object, rather than the mapped instance itself.
  1361. :param retval=False: when True, the user-defined event
  1362. listening must return the "value" argument from the
  1363. function. This gives the listening function the opportunity
  1364. to change the value that is ultimately used for a "set"
  1365. or "append" event.
  1366. """
  1367. _target_class_doc = "SomeClass.some_attribute"
  1368. _dispatch_target = QueryableAttribute
  1369. @staticmethod
  1370. def _set_dispatch(cls, dispatch_cls):
  1371. dispatch = event.Events._set_dispatch(cls, dispatch_cls)
  1372. dispatch_cls._active_history = False
  1373. return dispatch
  1374. @classmethod
  1375. def _accept_with(cls, target):
  1376. # TODO: coverage
  1377. if isinstance(target, interfaces.MapperProperty):
  1378. return getattr(target.parent.class_, target.key)
  1379. else:
  1380. return target
  1381. @classmethod
  1382. def _listen(cls, event_key, active_history=False,
  1383. raw=False, retval=False,
  1384. propagate=False):
  1385. target, identifier, fn = \
  1386. event_key.dispatch_target, event_key.identifier, \
  1387. event_key._listen_fn
  1388. if active_history:
  1389. target.dispatch._active_history = True
  1390. if not raw or not retval:
  1391. def wrap(target, value, *arg):
  1392. if not raw:
  1393. target = target.obj()
  1394. if not retval:
  1395. fn(target, value, *arg)
  1396. return value
  1397. else:
  1398. return fn(target, value, *arg)
  1399. event_key = event_key.with_wrapper(wrap)
  1400. event_key.base_listen(propagate=propagate)
  1401. if propagate:
  1402. manager = instrumentation.manager_of_class(target.class_)
  1403. for mgr in manager.subclass_managers(True):
  1404. event_key.with_dispatch_target(
  1405. mgr[target.key]).base_listen(propagate=True)
  1406. def append(self, target, value, initiator):
  1407. """Receive a collection append event.
  1408. :param target: the object instance receiving the event.
  1409. If the listener is registered with ``raw=True``, this will
  1410. be the :class:`.InstanceState` object.
  1411. :param value: the value being appended. If this listener
  1412. is registered with ``retval=True``, the listener
  1413. function must return this value, or a new value which
  1414. replaces it.
  1415. :param initiator: An instance of :class:`.attributes.Event`
  1416. representing the initiation of the event. May be modified
  1417. from its original value by backref handlers in order to control
  1418. chained event propagation.
  1419. .. versionchanged:: 0.9.0 the ``initiator`` argument is now
  1420. passed as a :class:`.attributes.Event` object, and may be
  1421. modified by backref handlers within a chain of backref-linked
  1422. events.
  1423. :return: if the event was registered with ``retval=True``,
  1424. the given value, or a new effective value, should be returned.
  1425. """
  1426. def remove(self, target, value, initiator):
  1427. """Receive a collection remove event.
  1428. :param target: the object instance receiving the event.
  1429. If the listener is registered with ``raw=True``, this will
  1430. be the :class:`.InstanceState` object.
  1431. :param value: the value being removed.
  1432. :param initiator: An instance of :class:`.attributes.Event`
  1433. representing the initiation of the event. May be modified
  1434. from its original value by backref handlers in order to control
  1435. chained event propagation.
  1436. .. versionchanged:: 0.9.0 the ``initiator`` argument is now
  1437. passed as a :class:`.attributes.Event` object, and may be
  1438. modified by backref handlers within a chain of backref-linked
  1439. events.
  1440. :return: No return value is defined for this event.
  1441. """
  1442. def set(self, target, value, oldvalue, initiator):
  1443. """Receive a scalar set event.
  1444. :param target: the object instance receiving the event.
  1445. If the listener is registered with ``raw=True``, this will
  1446. be the :class:`.InstanceState` object.
  1447. :param value: the value being set. If this listener
  1448. is registered with ``retval=True``, the listener
  1449. function must return this value, or a new value which
  1450. replaces it.
  1451. :param oldvalue: the previous value being replaced. This
  1452. may also be the symbol ``NEVER_SET`` or ``NO_VALUE``.
  1453. If the listener is registered with ``active_history=True``,
  1454. the previous value of the attribute will be loaded from
  1455. the database if the existing value is currently unloaded
  1456. or expired.
  1457. :param initiator: An instance of :class:`.attributes.Event`
  1458. representing the initiation of the event. May be modified
  1459. from its original value by backref handlers in order to control
  1460. chained event propagation.
  1461. .. versionchanged:: 0.9.0 the ``initiator`` argument is now
  1462. passed as a :class:`.attributes.Event` object, and may be
  1463. modified by backref handlers within a chain of backref-linked
  1464. events.
  1465. :return: if the event was registered with ``retval=True``,
  1466. the given value, or a new effective value, should be returned.
  1467. """
  1468. def init_scalar(self, target, value, dict_):
  1469. """Receive a scalar "init" event.
  1470. This event is invoked when an uninitialized, unpersisted scalar
  1471. attribute is accessed. A value of ``None`` is typically returned
  1472. in this case; no changes are made to the object's state.
  1473. The event handler can alter this behavior in two ways.
  1474. One is that a value other than ``None`` may be returned. The other
  1475. is that the value may be established as part of the object's state,
  1476. which will also have the effect that it is persisted.
  1477. Typical use is to establish a specific default value of an attribute
  1478. upon access::
  1479. SOME_CONSTANT = 3.1415926
  1480. @event.listens_for(
  1481. MyClass.some_attribute, "init_scalar",
  1482. retval=True, propagate=True)
  1483. def _init_some_attribute(target, dict_, value):
  1484. dict_['some_attribute'] = SOME_CONSTANT
  1485. return SOME_CONSTANT
  1486. Above, we initialize the attribute ``MyClass.some_attribute`` to the
  1487. value of ``SOME_CONSTANT``. The above code includes the following
  1488. features:
  1489. * By setting the value ``SOME_CONSTANT`` in the given ``dict_``,
  1490. we indicate that the value is to be persisted to the database.
  1491. **The given value is only persisted to the database if we
  1492. explicitly associate it with the object**. The ``dict_`` given
  1493. is the ``__dict__`` element of the mapped object, assuming the
  1494. default attribute instrumentation system is in place.
  1495. * By establishing the ``retval=True`` flag, the value we return
  1496. from the function will be returned by the attribute getter.
  1497. Without this flag, the event is assumed to be a passive observer
  1498. and the return value of our function is ignored.
  1499. * The ``propagate=True`` flag is significant if the mapped class
  1500. includes inheriting subclasses, which would also make use of this
  1501. event listener. Without this flag, an inheriting subclass will
  1502. not use our event handler.
  1503. When we establish the value in the given dictionary, the value will
  1504. be used in the INSERT statement established by the unit of work.
  1505. Normally, the default returned value of ``None`` is not established as
  1506. part of the object, to avoid the issue of mutations occurring to the
  1507. object in response to a normally passive "get" operation, and also
  1508. sidesteps the issue of whether or not the :meth:`.AttributeEvents.set`
  1509. event should be awkwardly fired off during an attribute access
  1510. operation. This does not impact the INSERT operation since the
  1511. ``None`` value matches the value of ``NULL`` that goes into the
  1512. database in any case; note that ``None`` is skipped during the INSERT
  1513. to ensure that column and SQL-level default functions can fire off.
  1514. The attribute set event :meth:`.AttributeEvents.set` as well as the
  1515. related validation feature provided by :obj:`.orm.validates` is
  1516. **not** invoked when we apply our value to the given ``dict_``. To
  1517. have these events to invoke in response to our newly generated
  1518. value, apply the value to the given object as a normal attribute
  1519. set operation::
  1520. SOME_CONSTANT = 3.1415926
  1521. @event.listens_for(
  1522. MyClass.some_attribute, "init_scalar",
  1523. retval=True, propagate=True)
  1524. def _init_some_attribute(target, dict_, value):
  1525. # will also fire off attribute set events
  1526. target.some_attribute = SOME_CONSTANT
  1527. return SOME_CONSTANT
  1528. When multiple listeners are set up, the generation of the value
  1529. is "chained" from one listener to the next by passing the value
  1530. returned by the previous listener that specifies ``retval=True``
  1531. as the ``value`` argument of the next listener.
  1532. The :meth:`.AttributeEvents.init_scalar` event may be used to
  1533. extract values from the default values and/or callables established on
  1534. mapped :class:`.Column` objects. See the "active column defaults"
  1535. example in :ref:`examples_instrumentation` for an example of this.
  1536. .. versionadded:: 1.1
  1537. :param target: the object instance receiving the event.
  1538. If the listener is registered with ``raw=True``, this will
  1539. be the :class:`.InstanceState` object.
  1540. :param value: the value that is to be returned before this event
  1541. listener were invoked. This value begins as the value ``None``,
  1542. however will be the return value of the previous event handler
  1543. function if multiple listeners are present.
  1544. :param dict_: the attribute dictionary of this mapped object.
  1545. This is normally the ``__dict__`` of the object, but in all cases
  1546. represents the destination that the attribute system uses to get
  1547. at the actual value of this attribute. Placing the value in this
  1548. dictionary has the effect that the value will be used in the
  1549. INSERT statement generated by the unit of work.
  1550. .. seealso::
  1551. :ref:`examples_instrumentation` - see the
  1552. ``active_column_defaults.py`` example.
  1553. """
  1554. def init_collection(self, target, collection, collection_adapter):
  1555. """Receive a 'collection init' event.
  1556. This event is triggered for a collection-based attribute, when
  1557. the initial "empty collection" is first generated for a blank
  1558. attribute, as well as for when the collection is replaced with
  1559. a new one, such as via a set event.
  1560. E.g., given that ``User.addresses`` is a relationship-based
  1561. collection, the event is triggered here::
  1562. u1 = User()
  1563. u1.addresses.append(a1) # <- new collection
  1564. and also during replace operations::
  1565. u1.addresses = [a2, a3] # <- new collection
  1566. :param target: the object instance receiving the event.
  1567. If the listener is registered with ``raw=True``, this will
  1568. be the :class:`.InstanceState` object.
  1569. :param collection: the new collection. This will always be generated
  1570. from what was specified as
  1571. :paramref:`.RelationshipProperty.collection_class`, and will always
  1572. be empty.
  1573. :param collection_adpater: the :class:`.CollectionAdapter` that will
  1574. mediate internal access to the collection.
  1575. .. versionadded:: 1.0.0 the :meth:`.AttributeEvents.init_collection`
  1576. and :meth:`.AttributeEvents.dispose_collection` events supersede
  1577. the :class:`.collection.linker` hook.
  1578. """
  1579. def dispose_collection(self, target, collection, collection_adpater):
  1580. """Receive a 'collection dispose' event.
  1581. This event is triggered for a collection-based attribute when
  1582. a collection is replaced, that is::
  1583. u1.addresses.append(a1)
  1584. u1.addresses = [a2, a3] # <- old collection is disposed
  1585. The mechanics of the event will typically include that the given
  1586. collection is empty, even if it stored objects while being replaced.
  1587. .. versionadded:: 1.0.0 the :meth:`.AttributeEvents.init_collection`
  1588. and :meth:`.AttributeEvents.dispose_collection` events supersede
  1589. the :class:`.collection.linker` hook.
  1590. """
  1591. class QueryEvents(event.Events):
  1592. """Represent events within the construction of a :class:`.Query` object.
  1593. The events here are intended to be used with an as-yet-unreleased
  1594. inspection system for :class:`.Query`. Some very basic operations
  1595. are possible now, however the inspection system is intended to allow
  1596. complex query manipulations to be automated.
  1597. .. versionadded:: 1.0.0
  1598. """
  1599. _target_class_doc = "SomeQuery"
  1600. _dispatch_target = Query
  1601. def before_compile(self, query):
  1602. """Receive the :class:`.Query` object before it is composed into a
  1603. core :class:`.Select` object.
  1604. This event is intended to allow changes to the query given::
  1605. @event.listens_for(Query, "before_compile", retval=True)
  1606. def no_deleted(query):
  1607. for desc in query.column_descriptions:
  1608. if desc['type'] is User:
  1609. entity = desc['entity']
  1610. query = query.filter(entity.deleted == False)
  1611. return query
  1612. The event should normally be listened with the ``retval=True``
  1613. parameter set, so that the modified query may be returned.
  1614. """
  1615. @classmethod
  1616. def _listen(
  1617. cls, event_key, retval=False, **kw):
  1618. fn = event_key._listen_fn
  1619. if not retval:
  1620. def wrap(*arg, **kw):
  1621. if not retval:
  1622. query = arg[0]
  1623. fn(*arg, **kw)
  1624. return query
  1625. else:
  1626. return fn(*arg, **kw)
  1627. event_key = event_key.with_wrapper(wrap)
  1628. event_key.base_listen(**kw)