From ff50845a130e604b419940556d1cc26d78f4b07a Mon Sep 17 00:00:00 2001 From: aeltheos Date: Sun, 14 Nov 2021 00:45:44 +0100 Subject: [PATCH] Added output of found duplicate Fixed find_duplicate --- .../management/commands/duplicate.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/photologue_custom/management/commands/duplicate.py b/photologue_custom/management/commands/duplicate.py index 5b08f5c..4719375 100644 --- a/photologue_custom/management/commands/duplicate.py +++ b/photologue_custom/management/commands/duplicate.py @@ -1,6 +1,6 @@ from django.core.management.base import BaseCommand, CommandError from photologue_custom.models import Gallery -import argparse + import hashlib @@ -28,8 +28,15 @@ class Command(BaseCommand): raise CommandError( 'Slug {} does not correspond to a gallery in the database.'.format(slug)) print('error') - # - + # Find duplicates in all galleries + for gallery in galleries: + duplicate = find_duplicate(gallery) + self.stdout.write('Gallery {} :'.format(gallery.slug)) + print(duplicate) + for (original, copies) in duplicate: + for copy in copies: + self.stdout.write('{} is duplicate of {}'.format( + copy.slug, original.slug)) def find_duplicate(gallery): @@ -40,22 +47,16 @@ def find_duplicate(gallery): for photo in gallery.photos.all(): h0 = hashlib.sha256(photo.image.read()).digest() - if photo not in non_duplicate: + if h0 not in non_duplicate: # Photo is not a duplicate non_duplicate[h0] = photo - elif len(photo.slug) > len(non_duplicate[h0.slug()]): - # Photo is a duplicate and photo slug is longer - if non_duplicate[h0].slug in duplicate: + elif h0 in duplicate: + if len(photo.slug) > len(duplicate[h0][0].slug): duplicate[h0][1] += [photo] else: - duplicate[h0] = (non_duplicate[h0], [photo]) - else: - # Photo is a duplicate and photo slug is shorter - if non_duplicate[h0].slug in duplicate: + duplicate[h0][1] += [duplicate[h0][0]] duplicate[h0][0] = photo - duplicate[h0][1] += [non_duplicate[h0]] - else: - duplicate[h0] = (photo, [non_duplicate[h0]]) - non_duplicate[h0] += [photo] - # Return values because hash aren't need anymore + else: + duplicate[h0] = [non_duplicate[h0], [photo]] + # Return only value because hash aren't usefull return duplicate.values()