@@ -0,0 +1,1419 @@
+import csv%0Afrom optparse import make_option%0Afrom django.core.management.base import BaseCommand%0Afrom django_digest.models import PartialDigest%0Afrom dimagi.utils.decorators.log_exception import log_exception%0A%0A%0Aclass Command(BaseCommand):%0A    %22%22%22%0A    Cleans up duplicate partial digests in the system.%0A    %22%22%22%0A    option_list = BaseCommand.option_list + (%0A        make_option('--cleanup',%0A                    action='store_true',%0A                    dest='cleanup',%0A                    default=False,%0A                    help=%22Clean up (delete) the affected cases.%22),%0A    )%0A%0A    @log_exception()%0A    def handle(self, *args, **options):%0A        with open('duplicate-digests.csv', 'wb') as f:%0A            writer = csv.writer(f, dialect=csv.excel)%0A            # headings%0A            writer.writerow(%5B%0A                'user id',%0A                'username',%0A                'email',%0A                'digest id',%0A                'login',%0A            %5D)%0A            for digest in PartialDigest.objects.all():%0A                if digest.user.username.lower() != digest.login.lower():%0A                    writer.writerow(%5B%0A                        digest.user.pk,%0A                        digest.user.username,%0A                        digest.user.email,%0A                        digest.pk,%0A                        digest.login,%0A                    %5D)%0A                    if options%5B'cleanup'%5D:%0A                        digest.delete()%0A

add management command to cleanup partial digests