| 1 |
from django.core.management.base import BaseCommand |
| 2 |
|
| 3 |
from django_comments.models import Comment |
| 4 |
|
| 5 |
|
| 6 |
class Command(BaseCommand): |
| 7 |
help = ("Remove comments for which the related objects " |
| 8 |
"don't exist anymore!") |
| 9 |
|
| 10 |
def add_arguments(self, parser): |
| 11 |
parser.add_argument( |
| 12 |
'-y', '--yes', default='x', action='store_const', const='y', |
| 13 |
dest='answer', help='Automatically confirm deletion', |
| 14 |
) |
| 15 |
|
| 16 |
def handle(self, *args, **kwargs): |
| 17 |
verbose = kwargs['verbosity'] >= 1 |
| 18 |
answer = kwargs['answer'] |
| 19 |
|
| 20 |
|
| 21 |
if not verbose: |
| 22 |
answer = 'y' |
| 23 |
|
| 24 |
for comment in Comment.objects.all(): |
| 25 |
if comment.content_object is None: |
| 26 |
if verbose: |
| 27 |
self.stdout.write( |
| 28 |
"Comment `%s' to non-existing `%s' with PK `%s'" % |
| 29 |
(comment, comment.content_type.model, comment.object_pk)) |
| 30 |
|
| 31 |
while answer not in 'yn': |
| 32 |
answer = input("Do you wish to delete? [yN] ") |
| 33 |
if not answer: |
| 34 |
answer = 'x' |
| 35 |
continue |
| 36 |
answer = answer[0].lower() |
| 37 |
|
| 38 |
if answer == 'y' : |
| 39 |
comment.delete() |
| 40 |
|
| 41 |
if verbose: |
| 42 |
self.stdout.write("Deleted comment `%s'" % comment) |