Added 'clear_engine' method with subscriber on modification event to force removal of volatile attributes
authorThierry Florac <thierry.florac@onf.fr>
Mon, 24 Oct 2016 16:59:33 +0200
changeset 22 9491c1528b80
parent 21 c412c833a9ad
child 23 06e3617fe77d
Added 'clear_engine' method with subscriber on modification event to force removal of volatile attributes
src/pyams_alchemy/engine.py
src/pyams_alchemy/interfaces/__init__.py
--- a/src/pyams_alchemy/engine.py	Mon Oct 24 10:13:43 2016 +0200
+++ b/src/pyams_alchemy/engine.py	Mon Oct 24 16:59:33 2016 +0200
@@ -29,7 +29,7 @@
 # import interfaces
 from pyams_alchemy.interfaces import REQUEST_SESSION_KEY, IAlchemyEngineUtility
 from pyams_utils.interfaces.site import IOptionalUtility
-from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent
+from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
 from zope.schema.interfaces import IVocabularyFactory
 
 # import packages
@@ -163,6 +163,10 @@
                                                                    **kw)
             return engine
 
+    def clear_engine(self):
+        if hasattr(self, '_v_engine'):
+            delattr(self, '_v_engine')
+
 
 class PersistentAlchemyEngineUtility(Persistent, AlchemyEngineUtility, Contained):
     """Persistent implementation of SQLAlchemy engine utility"""
@@ -175,6 +179,12 @@
     manager.registerUtility(event.object, IAlchemyEngineUtility, name=event.object.name or '')
 
 
+@subscriber(IObjectModifiedEvent, context_selector=IAlchemyEngineUtility)
+def handle_modified_engine(event):
+    """Clear SQLAlchemy engine volatile attributes when modified"""
+    IAlchemyEngineUtility(event.object).clear_engine()
+
+
 @subscriber(IObjectRemovedEvent, context_selector=IAlchemyEngineUtility)
 def handle_removed_engine(event):
     """Un-register an SQLAlchemy engine when deleted"""
--- a/src/pyams_alchemy/interfaces/__init__.py	Mon Oct 24 10:13:43 2016 +0200
+++ b/src/pyams_alchemy/interfaces/__init__.py	Mon Oct 24 16:59:33 2016 +0200
@@ -76,3 +76,6 @@
 
     def get_engine(self, use_pool=True):
         """Get SQLAlchemy engine"""
+
+    def clear_engine(self):
+        """Remove inner volatile attributes when utility properties are modified"""