Added output of found duplicate
Fixed find_duplicate
This commit is contained in:
parent
dbb71d088a
commit
ff50845a13
1 changed files with 17 additions and 16 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
from photologue_custom.models import Gallery
|
from photologue_custom.models import Gallery
|
||||||
import argparse
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -28,8 +28,15 @@ class Command(BaseCommand):
|
||||||
raise CommandError(
|
raise CommandError(
|
||||||
'Slug {} does not correspond to a gallery in the database.'.format(slug))
|
'Slug {} does not correspond to a gallery in the database.'.format(slug))
|
||||||
print('error')
|
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):
|
def find_duplicate(gallery):
|
||||||
|
|
@ -40,22 +47,16 @@ def find_duplicate(gallery):
|
||||||
|
|
||||||
for photo in gallery.photos.all():
|
for photo in gallery.photos.all():
|
||||||
h0 = hashlib.sha256(photo.image.read()).digest()
|
h0 = hashlib.sha256(photo.image.read()).digest()
|
||||||
if photo not in non_duplicate:
|
if h0 not in non_duplicate:
|
||||||
# Photo is not a duplicate
|
# Photo is not a duplicate
|
||||||
non_duplicate[h0] = photo
|
non_duplicate[h0] = photo
|
||||||
elif len(photo.slug) > len(non_duplicate[h0.slug()]):
|
elif h0 in duplicate:
|
||||||
# Photo is a duplicate and photo slug is longer
|
if len(photo.slug) > len(duplicate[h0][0].slug):
|
||||||
if non_duplicate[h0].slug in duplicate:
|
|
||||||
duplicate[h0][1] += [photo]
|
duplicate[h0][1] += [photo]
|
||||||
else:
|
else:
|
||||||
duplicate[h0] = (non_duplicate[h0], [photo])
|
duplicate[h0][1] += [duplicate[h0][0]]
|
||||||
else:
|
|
||||||
# Photo is a duplicate and photo slug is shorter
|
|
||||||
if non_duplicate[h0].slug in duplicate:
|
|
||||||
duplicate[h0][0] = photo
|
duplicate[h0][0] = photo
|
||||||
duplicate[h0][1] += [non_duplicate[h0]]
|
else:
|
||||||
else:
|
duplicate[h0] = [non_duplicate[h0], [photo]]
|
||||||
duplicate[h0] = (photo, [non_duplicate[h0]])
|
# Return only value because hash aren't usefull
|
||||||
non_duplicate[h0] += [photo]
|
|
||||||
# Return values because hash aren't need anymore
|
|
||||||
return duplicate.values()
|
return duplicate.values()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue