Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Add manage.py delete_stale_comments command
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov authored and ObserverOfTime committed Dec 28, 2020
1 parent fa72e43 commit f5df9fa
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ History

* Forked and rebranded into ``django-commentary``.
* Dropped support for Python<3.6 and Django<3.0.
* Added the ``delete_stale_comments`` management command.

1.9.1 (2019-02-20)
------------------
Expand Down
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions commentary/management/commands/delete_stale_comments.py
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)
1 change: 1 addition & 0 deletions docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Contents
quickstart
models
signals
management_commands
custom
forms
moderation
Expand Down
19 changes: 19 additions & 0 deletions docs/management_commands.txt
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:
32 changes: 32 additions & 0 deletions tests/testapp/tests/test_delete_stale_comments.py
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())

0 comments on commit f5df9fa

Please sign in to comment.