This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
forked from django/django-contrib-comments
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add manage.py delete_stale_comments command
- Loading branch information
1 parent
fa72e43
commit f5df9fa
Showing
7 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from django_comments.models import Comment | ||
|
||
|
||
class Command(BaseCommand): | ||
help = ("Remove comments for which the related objects " | ||
"don't exist anymore!") | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'-y', '--yes', default='x', action='store_const', const='y', | ||
dest='answer', help='Automatically confirm deletion', | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
verbose = kwargs['verbosity'] >= 1 | ||
answer = kwargs['answer'] | ||
|
||
# -v0 sets --yes | ||
if not verbose: | ||
answer = 'y' | ||
|
||
for comment in Comment.objects.all(): | ||
if comment.content_object is None: | ||
if verbose: | ||
self.stdout.write( | ||
"Comment `%s' to non-existing `%s' with PK `%s'" % | ||
(comment, comment.content_type.model, comment.object_pk)) | ||
|
||
while answer not in 'yn': | ||
answer = input("Do you wish to delete? [yN] ") | ||
if not answer: | ||
answer = 'x' | ||
continue | ||
answer = answer[0].lower() | ||
|
||
if answer == 'y' : | ||
comment.delete() | ||
|
||
if verbose: | ||
self.stdout.write("Deleted comment `%s'" % comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ Contents | |
quickstart | ||
models | ||
signals | ||
management_commands | ||
custom | ||
forms | ||
moderation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
=================== | ||
Management commands | ||
=================== | ||
|
||
delete_stale_comments | ||
===================== | ||
|
||
Remove comments for which the related objects don't exist anymore! | ||
This is a handy house-keeping command because the comment model | ||
doesn't enforce cascade delete on the database level! Run: | ||
|
||
.. code-block:: shell | ||
manage.py delete_stale_comments | ||
This command supports the ``--yes`` flag to automatically confirm | ||
suggested deletions, suitable for running via cron. | ||
|
||
.. vim:ft=rst: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from django.core.management import call_command | ||
|
||
from django_comments.models import Comment | ||
|
||
from . import CommentTestCase | ||
from testapp.models import Article | ||
|
||
|
||
class CommentManagerTests(CommentTestCase): | ||
|
||
def testDoesNotRemoveWhenNoStaleComments(self): | ||
self.createSomeComments() | ||
initial_count = Comment.objects.count() | ||
|
||
call_command("delete_stale_comments", "--yes", verbosity=0) | ||
|
||
self.assertEqual(initial_count, Comment.objects.count()) | ||
|
||
def testRemovesWhenParentObjectsAreMissing(self): | ||
self.createSomeComments() | ||
initial_count = Comment.objects.count() | ||
article_comments_count = Comment.objects.for_model(Article).count() | ||
self.assertGreater(article_comments_count, 0) | ||
|
||
# removing articles will not remove associated comments | ||
Article.objects.all().delete() | ||
self.assertEqual(initial_count, Comment.objects.count()) | ||
|
||
call_command("delete_stale_comments", "--yes", verbosity=0) | ||
|
||
self.assertEqual(0, Comment.objects.for_model(Article).count()) | ||
self.assertEqual(initial_count - article_comments_count, Comment.objects.count()) |