Commits

Chad Dombrova  committed 79a2028

Small updates to backend and listener code

  • Participants
  • Parent commits 91d756f
  • Branches orms

Comments (0)

Files changed (2)

File denormalize/backend/base.py

 
     collections = None
 
-    # Keep references to listeners to prevent garbage collection
-    _listeners = None
-
     # This will be shared by all subclasses. It keeps track of all active
     # backends.
     _registry = weakref.WeakValueDictionary()
         self.collections[collection.name] = collection
         self._setup_listeners(collection)
 
-    def _set_affected(self, ns, collection, instance, affected_set):
+    @staticmethod
+    def _set_affected(ns, collection, instance, affected_set):
         """Used in pre_* handlers. Annotates an instance with
         the ids of the root objects it affects, so that we can include
         these in the final updates in the post_* handlers.
         new = current | affected_set
         setattr(instance, attname, new)
 
-    def _get_affected(self, ns, collection, instance):
+    @staticmethod
+    def _get_affected(ns, collection, instance):
         """See _set_affected for an explanation."""
         attname = '_denormalize_extra_affected_{0}_{1}'.format(collection.name, ns)
         affected_set = getattr(instance, attname, set())
         return affected_set
 
-    def _get_collection(self, collection_name_or_obj):
+    @staticmethod
+    def _get_collection(collection_name_or_obj):
         if isinstance(collection_name_or_obj, DocumentCollection):
             name = collection_name_or_obj.name
         elif isinstance(collection_name_or_obj, basestring):
     # ORM updates you need to commit to the backend.
 
     def deleted(self, collection, doc_id):
-        """Called when the root object for a document was deleted in the Django ORM"""
+        """Called when the root object for a document was deleted in the ORM"""
         raise NotImplementedError()
 
     def added(self, collection, doc_id, doc):
-        """Called when the root object for a document was added in the Django ORM"""
+        """Called when the root object for a document was added in the ORM"""
         raise NotImplementedError()
 
     def changed(self, collection, doc_id, doc):
-        """Called when any data in a document was changed in the Django ORM"""
+        """Called when any data in a document was changed in the ORM"""
         raise NotImplementedError()
 
     def get_doc(self, collection, doc_id):

File denormalize/orms/django.py

             return value
 
 class DjangoConnector(object):
+    # Keep references to listeners to prevent garbage collection
+    _listeners = None
+
+    @staticmethod
+    def _get_affected_instances(filter_path, collection, instance):
+        # Find all affected root model instance ids
+        # These are the objects that reference the changed instance
+        if filter_path:
+            # Submodel
+            filt = {filter_path: instance}
+            affected = set(collection.queryset(prefetch=False).filter(
+                **filt).values_list('id', flat=True))
+        else:
+            # This is the root model
+            affected = set([instance.id])
+        return affected
+
     def add_listeners(self, backend, collection, filter_path, submodel, info):
         """Connect the Django ORM signals to given dependency
 
                 log.debug("pre_save:   looks new, no action needed")
                 return
 
-            # Find all affected root model instance ids
-            # These are the objects that reference the changed instance
+            affected = self._get_affected_instances(filter_path, collection,
+                                                    instance)
             if filter_path:
-                # Submodel only
-                filt = {filter_path: instance}
-                affected = set(collection.queryset(prefetch=False).filter(
-                    **filt).values_list('id', flat=True))
                 log.debug("pre_save:   affected documents: %r", affected)
 
                 # Instead of calling the update functions directly, we make
                 log.warn("post_save: raw=True, so no document sync performed!")
                 return
 
-            # Find all affected root model instance ids
-            # These are the objects that reference the changed instance
-            if filter_path:
-                # Submodel
-                filt = {filter_path: instance}
-                affected = set(collection.queryset(prefetch=False).filter(
-                    **filt).values_list('id', flat=True))
-            else:
-                # This is the root model
-                affected = set([instance.id])
+            affected = self._get_affected_instances(filter_path, collection,
+                                                    instance)
             log.debug("post_save:   affected documents: %r", affected)
 
             # This can be passed by the pre_save handler
                 collection.name, submodel.__name__, filter_path or '^',
                 instance.id)
 
-            # Find all affected root model instance ids
-            # These are the objects that reference the changed instance
-            if filter_path:
-                # Submodel
-                filt = {filter_path: instance}
-                affected = set(collection.queryset(prefetch=False).filter(
-                    **filt).values_list('id', flat=True))
-            else:
-                # This is the root model
-                affected = set([instance.id])
+            affected = self._get_affected_instances(filter_path, collection,
+                                                    instance)
             log.debug("pre_delete:   affected documents: %r", affected)
 
             # Instead of calling the update functions directly, we make
 
             # Optimization: if either side is our root object, send out changes
             # without querying.
-            if instance_model is collection.model:
+            if instance_model is collection.model._model:
                 backend._queue_changed(collection, instance.id)
                 return
 
-            elif model is collection.model and pk_set:
+            elif model is collection.model._model and pk_set:
                 for pk in pk_set:
                     backend._queue_changed(collection, pk)
                 return
 
 
         # We need to keep a reference, because signal connections are weak
-        backend._listeners.append(pre_save)
-        backend._listeners.append(post_save)
-        backend._listeners.append(pre_delete)
-        backend._listeners.append(post_delete)
+        self._listeners.append(pre_save)
+        self._listeners.append(post_save)
+        self._listeners.append(pre_delete)
+        self._listeners.append(post_delete)
         # Connect to the save signal
         signals.pre_save.connect(pre_save, sender=submodel)
         signals.post_save.connect(post_save, sender=submodel)
             backend._listeners.append(m2m_changed)
             through_model = info['through']._model
             signals.m2m_changed.connect(m2m_changed, sender=through_model)
+