from datetime import timedelta
from optparse import make_option

from django.core.management.base import BaseCommand
from django.db.models import Count

from democracy.models import SectionComment


class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option("--yes-i-know-what-im-doing", dest="nothing_can_go_wrong", action="store_true"),
    )

    def _remove_dupes(self, klass):
        potential_dupes = klass.objects.values('content',).exclude(content='')\
            .exclude(deleted=True).annotate(Count('content'))\
            .filter(content__count__gt=1).order_by('-content__count')

        for d in potential_dupes:
            objs = list(klass.objects.filter(content=d['content']).order_by('created_at'))
            first = objs.pop(0)
            print("%s\n%s" % (first.created_at, first.content))
            for other in objs:
                if other.created_at - first.created_at > timedelta(hours=1):
                    print("\ttoo late %s" % (other.created_at - first.created_at))
                    continue
                if getattr(other, klass.parent_field) != getattr(first, klass.parent_field):
                    print("\tdifferent parent %s" % other)
                    continue
                if other.plugin_data != first.plugin_data:
                    print("\tplugin data differs %s" % other)
                    continue
                print("\tbye bye %s" % other)
                other.soft_delete()

    def handle(self, *args, **options):
        if not options.pop("nothing_can_go_wrong", False):
            raise Exception("You don't know what you're doing.")

        self._remove_dupes(SectionComment)

Implement management command for removing duplicate comments
Implement management command for removing duplicate comments
