import logging

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

from document.models import Document

logger = logging.getLogger(__name__)


class Command(BaseCommand):
    """Remove duplicate documents"""

    def handle(self, *args, **options):
        self.do()

    @transaction.atomic
    def do(self):
        unique_together_fields = ['document_id']
        Command.remove_duplicates(unique_together_fields)
        # unique_together_fields = ['source_url']
        # Command.remove_duplicates(unique_together_fields)
        # unique_together_fields = ['title_short']
        # Command.remove_duplicates(unique_together_fields)
        # relations = Command.find_relations()
        # for relation in relations:
        #     print(relation)

    @staticmethod
    def remove_duplicates(unique_together_fields):
        duplicates = (Document.objects.values(*unique_together_fields)
                      .order_by()
                      .annotate(max_id=models.Max('id'),
                                count_id=models.Count('id'))
                      .filter(count_id__gt=1))

        print('duplicates: ' + str(len(duplicates)))

        for duplicate in duplicates:
            (Document.objects.filter(**{x: duplicate[x] for x in unique_together_fields})
             .exclude(id=duplicate['max_id'])
             .delete())

    @staticmethod
    def find_relations():
        return [
            f for f in Document._meta.get_fields()
            if f.auto_created and not f.concrete
        ]

@@ -153,24 +153,178 @@
  import 
-Document
+Agenda%0Afrom document.models import Document%0Afrom document.models import Kamerstuk%0Afrom document.models import Kamervraag%0Afrom document.models import Kamerantwoord
 %0A%0Alogger
@@ -417,16 +417,48 @@
 ocuments
+ and documents without relations
 %22%22%22%0A%0A   
@@ -654,32 +654,67 @@
 ogether_fields)%0A
+        Command.remove_unrelated()%0A
         # unique
@@ -1033,16 +1033,603 @@
 ation)%0A%0A
+    @staticmethod%0A    def remove_unrelated():%0A        deleted_ids = %5B%5D%0A        for document in Document.objects.all():%0A            if Kamerstuk.objects.filter(document=document):%0A                continue%0A            if Kamervraag.objects.filter(document=document):%0A                continue%0A            if Kamerantwoord.objects.filter(document=document):%0A                continue%0A            if Agenda.objects.filter(document=document):%0A                continue%0A            deleted_ids.append(document.id)%0A            document.delete()%0A        print('deleted: ' + str(len(deleted_ids)))%0A%0A
     @sta

Remove documents without relations