Commit 6ccd08e0 by Bridger Maxwell

Moved subtree delete to a model method with transactions.

parent c2cfcc2a
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.db import models from django.db import models, transaction
from django.db.models.signals import pre_delete, post_save from django.db.models.signals import pre_delete, post_save
from django.utils.translation import ugettext_lazy as _, ugettext from django.utils.translation import ugettext_lazy as _, ugettext
...@@ -16,6 +18,8 @@ from wiki.conf import settings ...@@ -16,6 +18,8 @@ from wiki.conf import settings
from wiki.core.exceptions import NoRootURL, MultipleRootURLs from wiki.core.exceptions import NoRootURL, MultipleRootURLs
from wiki.models.article import ArticleRevision, ArticleForObject, Article from wiki.models.article import ArticleRevision, ArticleForObject, Article
log = logging.getLogger(__name__)
class URLPath(MPTTModel): class URLPath(MPTTModel):
""" """
Strategy: Very few fields go here, as most has to be managed through an Strategy: Very few fields go here, as most has to be managed through an
...@@ -89,6 +93,24 @@ class URLPath(MPTTModel): ...@@ -89,6 +93,24 @@ class URLPath(MPTTModel):
return ancestor return ancestor
return None return None
@transaction.commit_manually
def delete_subtree(self):
"""
NB! This deletes this urlpath, its children, and ALL of the related
articles. This is a purged delete and CANNOT be undone.
"""
try:
for descendant in self.get_descendants(include_self=True).order_by("-level"):
print "deleting " , descendant
descendant.article.delete()
transaction.commit()
except:
transaction.rollback()
log.exception("Exception deleting article subtree.")
@classmethod @classmethod
def root(cls): def root(cls):
site = Site.objects.get_current() site = Site.objects.get_current()
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
{% if error_type == "ancestors_missing" %} {% if error_type == "ancestors_missing" %}
<div class="missing"> <div class="missing">
<p>This article was not found, and neither was its parent article. <a href="{% url 'wiki:get' path='' %}">Go back to the main wiki article</a></p> <p>This article was not found, and neither was its parent article. <a href="{% url 'wiki:get' path='' %}">Go to the main wiki article</a></p>
</div> </div>
{% else %} {% else %}
......
...@@ -176,8 +176,7 @@ class Delete(FormView, ArticleMixin): ...@@ -176,8 +176,7 @@ class Delete(FormView, ArticleMixin):
if can_moderate and purge: if can_moderate and purge:
# First, remove children # First, remove children
if self.urlpath: if self.urlpath:
for descendant in self.urlpath.get_descendants(include_self=True): self.urlpath.delete_subtree()
descendant.article.delete()
else: else:
self.article.delete() self.article.delete()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment