Commits

Gustavo Picon committed bd4bdcb

refactored mp_tree.MP_NodeQuerySet.delete() to avoid an extra argument.

Comments (0)

Files changed (1)

     Needed only for the customized delete method.
     """
 
-    def delete(self, known_children=False):
+    def delete(self):
         """
         Custom delete method, will remove all descendant nodes to ensure a
         consistent tree (no orphans)
 
         :returns: ``None``
         """
-        if known_children:
-            # we already know the children, let's call the default django
-            # delete method and let it handle the removal of the user's
-            # foreign keys...
-            super(MP_NodeQuerySet, self).delete()
-        else:
-            # we'll have to manually run through all the nodes that are going
-            # to be deleted and remove nodes from the list if an ancestor is
-            # already getting removed, since that would be redundant
-            removed = {}
-            for node in self.order_by('depth', 'path'):
-                found = False
-                for depth in range(1, len(node.path) / node.steplen):
-                    path = node._get_basepath(node.path, depth)
-                    if path in removed:
-                        # we are already removing a parent of this node
-                        # skip
-                        found = True
-                        break
-                if not found:
-                    removed[node.path] = node
+        # we'll have to manually run through all the nodes that are going
+        # to be deleted and remove nodes from the list if an ancestor is
+        # already getting removed, since that would be redundant
+        removed = {}
+        for node in self.order_by('depth', 'path'):
+            found = False
+            for depth in range(1, len(node.path) / node.steplen):
+                path = node._get_basepath(node.path, depth)
+                if path in removed:
+                    # we are already removing a parent of this node
+                    # skip
+                    found = True
+                    break
+            if not found:
+                removed[node.path] = node
 
-            # ok, got the minimal list of nodes to remove...
-            # we must also remove their children
-            # and update every parent node's numchild attribute
-            # LOTS OF FUN HERE!
-            parents = {}
-            toremove = []
-            for path, node in removed.items():
-                parentpath = node._get_basepath(node.path, node.depth - 1)
-                if parentpath:
-                    if parentpath not in parents:
-                        parents[parentpath] = node.get_parent(True)
-                    parent = parents[parentpath]
-                    if parent and parent.numchild > 0:
-                        parent.numchild -= 1
-                        parent.save()
-                if not node.is_leaf():
-                    toremove.append(Q(path__startswith=node.path))
-                else:
-                    toremove.append(Q(path=node.path))
-            # uh, django will handle this as a SELECT and then a DELETE of
-            # ids..
-            # status: NOT SURE IF WANT, maybe add custom sql here
-            if toremove:
-                self.model.objects.filter(
-                    reduce(operator.or_, toremove)).delete(known_children=True)
+        # ok, got the minimal list of nodes to remove...
+        # we must also remove their children
+        # and update every parent node's numchild attribute
+        # LOTS OF FUN HERE!
+        parents = {}
+        toremove = []
+        for path, node in removed.items():
+            parentpath = node._get_basepath(node.path, node.depth - 1)
+            if parentpath:
+                if parentpath not in parents:
+                    parents[parentpath] = node.get_parent(True)
+                parent = parents[parentpath]
+                if parent and parent.numchild > 0:
+                    parent.numchild -= 1
+                    parent.save()
+            if not node.is_leaf():
+                toremove.append(Q(path__startswith=node.path))
+            else:
+                toremove.append(Q(path=node.path))
+
+        # Django will handle this as a SELECT and then a DELETE of
+        # ids, and will deal with removing related objects
+        if toremove:
+            qset = self.model.objects.filter(reduce(operator.or_, toremove))
+            super(MP_NodeQuerySet, qset).delete()
         transaction.commit_unless_managed()