From bddeb987363e017effdf79acf2f3dbf0f3949f05 Mon Sep 17 00:00:00 2001 From: Qwlouse Date: Tue, 7 Jan 2014 22:19:48 +0100 Subject: [PATCH 1/2] added delete node command --- .../management/commands/delete_node.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 node_storage/management/commands/delete_node.py diff --git a/node_storage/management/commands/delete_node.py b/node_storage/management/commands/delete_node.py new file mode 100644 index 0000000..aa35632 --- /dev/null +++ b/node_storage/management/commands/delete_node.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +# coding=utf-8 +# region License +# Findeco is dually licensed under GPLv3 or later and MPLv2. +# +################################################################################ +# Copyright (c) 2012 Klaus Greff +# This file is part of Findeco. +# +# Findeco is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Findeco is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Findeco. If not, see . +################################################################################ +# +################################################################################ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +#endregion ##################################################################### + +from __future__ import division, print_function, unicode_literals + +from django.core.management import BaseCommand +from node_storage.path_helpers import get_node_for_path +from node_storage.tools import delete_node + + +class Command(BaseCommand): + args = '' + help = 'Delete a node and all children, votes, posts, derivates, ' \ + 'cache entries, arguments, and empty parents associated with it. '\ + 'BE CAREFUL: This operation cannot be undone! Backup your Database!' + + def handle(self, *args, **options): + if len(args) < 1: + print("You have to provide a .") + + if len(args) > 1: + print("Please provide exactly one argument!") + + node_path = args[0] + + node = get_node_for_path(node_path) + assert node, "Node for path '%s' does not exist." % node_path + + delete_node(node) \ No newline at end of file From 918c6d5eaa7495ba9338afb6080832aec27b081a Mon Sep 17 00:00:00 2001 From: Qwlouse Date: Tue, 7 Jan 2014 22:52:05 +0100 Subject: [PATCH 2/2] delete node now also deletes index cache of parents --- node_storage/path_helpers.py | 4 ++++ node_storage/tools.py | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/node_storage/path_helpers.py b/node_storage/path_helpers.py index d5dc9e3..ac957e3 100644 --- a/node_storage/path_helpers.py +++ b/node_storage/path_helpers.py @@ -91,3 +91,7 @@ def get_good_path_for_structure_node(node, slot=None, slot_path=None): return slot_path + '.' + str(node.get_index(slot)) else: return node.get_a_path() + + +def get_all_paths_for_node(node): + return PathCache.objects.filter(node=node).values_list('path', flat=True) \ No newline at end of file diff --git a/node_storage/tools.py b/node_storage/tools.py index a40acf8..de910dc 100644 --- a/node_storage/tools.py +++ b/node_storage/tools.py @@ -28,12 +28,13 @@ from __future__ import division, print_function, unicode_literals from django.core.exceptions import ObjectDoesNotExist from microblogging import delete_posts_referring_to -from node_storage.models import PathCache, TextCache, Vote, Argument +from node_storage.models import TextCache, Vote, Argument from node_storage.models import IndexCache, Node +from node_storage.path_helpers import get_all_paths_for_node def delete_node(node): - paths = PathCache.objects.filter(node=node).all() + paths = get_all_paths_for_node(node) TextCache.objects.filter(path__in=paths).delete() IndexCache.objects.filter(path__in=paths).delete() @@ -56,6 +57,8 @@ def delete_node(node): for p in parents: try: parent = Node.objects.get(id=p.id) + parent_paths = get_all_paths_for_node(parent) + IndexCache.objects.filter(path__in=parent_paths).delete() if parent.children.count() == 0: delete_node(parent) else: