Commits

Chad Dombrova committed 9664f45

Allow a custom listener to be passed to the backend.

Comments (0)

Files changed (4)

denormalize/backend/base.py

     # backends.
     _registry = weakref.WeakValueDictionary()
 
-    def __init__(self, name=None):
+    def __init__(self, name=None, listener_class=None):
         """
 
         :param name: Name for backend instance. This is used to refer to
                 "initialization.".format(name))
         self._registry[name] = self
         self.collections = {}
+        self.listener_class = listener_class
         self._listeners = defaultdict(list)
 
     def register(self, collection):
         :type collection: denormalize.models.DocumentCollectionBase
         """
         for model, filter_paths in collection.get_related_models():
-            self._add_listeners(collection, model, filter_paths)
+            self._add_listener(collection, model, filter_paths)
 
-    def _add_listeners(self, collection, model, filter_paths):
+    def _add_listener(self, collection, model, filter_paths):
         """Connect the Django ORM signals to given dependency
 
         :type collection: denormalize.models.DocumentCollectionBase
         #       database will still contain the old connection.
         # TODO: Consider moving all of this to a Collection or something
         #       separate and just listen to signals from there.
-        collection_listener = collection.model.listener
-        listener = collection_listener(self, collection, model, filter_paths)
+        if self.listener_class:
+            listener_class = self.listener_class
+        else:
+            listener_class = collection.model.listener
+        listener = listener_class(self, collection, model, filter_paths)
         listener.connect()
         self._listeners[collection.name].append(listener)
 

denormalize/backend/cache.py

     #       and update it on the first access. Maybe make this behavior
     #       configurable
 
-    def __init__(self, name="default", timeout=None):
-        super(CacheBackend, self).__init__(name=name)
+    def __init__(self, name="default", timeout=None, listener_class=None):
+        super(CacheBackend, self).__init__(name=name,
+                                           listener_class=listener_class)
         self.cache = cache.get_cache(self.cache_backend)
         if timeout:
             self.cache_timeout = timeout

denormalize/backend/locmem.py

     _dirty = None
     _lock = None
 
-    def __init__(self, name=None):
-        super(LocMemBackend, self).__init__(name=name)
+    def __init__(self, name=None, listener_class=None):
+        super(LocMemBackend, self).__init__(name=name,
+                                            listener_class=listener_class)
         self.data = {}
         self._dirty = {}
         self._lock = threading.Lock()

denormalize/backend/mongodb.py

     connection = None
     db = None
 
-    def __init__(self, name=None, db_name=None, connection_uri=None):
-        super(MongoBackend, self).__init__(name=name)
+    def __init__(self, name=None, db_name=None, connection_uri=None,
+                 listener_class=None):
+        super(MongoBackend, self).__init__(name=name,
+                                           listener_class=listener_class)
         if db_name:
             self.db_name = db_name
         if connection_uri: