Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
pinae committed Jan 14, 2014
2 parents 63fac68 + 5eff10d commit 75ac2a8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
54 changes: 54 additions & 0 deletions node_storage/management/commands/delete_node.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
# 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 <http://www.gnu.org/licenses/>.
################################################################################
#
################################################################################
# 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 = '<NodePath>'
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 <NodePath>.")

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)
4 changes: 4 additions & 0 deletions node_storage/path_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 5 additions & 2 deletions node_storage/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand Down

0 comments on commit 75ac2a8

Please sign in to comment.