Added logic to find galleries based on user input
Cleaned up comment in find duplicate
This commit is contained in:
parent
02426942d0
commit
dbb71d088a
1 changed files with 24 additions and 10 deletions
|
|
@ -1,5 +1,5 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from photologue_custom.models import Gallery
|
||||
import argparse
|
||||
import hashlib
|
||||
|
||||
|
|
@ -8,20 +8,34 @@ class Command(BaseCommand):
|
|||
help = 'List all duplicate for chosen galleries'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_arguments(
|
||||
parser.add_argument(
|
||||
'--slugs', nargs='+', help='Try to find duplicate in the selected galleries', default=[])
|
||||
parser.add_arguments('-a', '--all', action='store_true',
|
||||
help='Try to find duplicate in all galleries')
|
||||
parser.add_arguments('-d', '--delete', action='store_true')
|
||||
parser.add_argument('-a', '--all', action='store_true',
|
||||
help='Try to find duplicate in all galleries, overide any slugs given')
|
||||
parser.add_argument('-d', '--delete', action='store_true')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
pass
|
||||
# Collect all required galleries
|
||||
if options['all']:
|
||||
galleries = Gallery.objects.all()
|
||||
else:
|
||||
for slug in options['slugs']:
|
||||
for gallery in Gallery.objects.all():
|
||||
if gallery.slug == slug:
|
||||
galleries += [gallery]
|
||||
break
|
||||
else:
|
||||
raise CommandError(
|
||||
'Slug {} does not correspond to a gallery in the database.'.format(slug))
|
||||
print('error')
|
||||
#
|
||||
|
||||
|
||||
|
||||
def find_duplicate(gallery):
|
||||
# Dict of all already checked photos
|
||||
non_duplicate = {}
|
||||
# Dict of all found duplicate {original.slug:[duplicates]}
|
||||
# Dict of all found duplicate {h0 : (original:[duplicates])}
|
||||
duplicate = {}
|
||||
|
||||
for photo in gallery.photos.all():
|
||||
|
|
@ -34,14 +48,14 @@ def find_duplicate(gallery):
|
|||
if non_duplicate[h0].slug in duplicate:
|
||||
duplicate[h0][1] += [photo]
|
||||
else:
|
||||
duplicate[h0] = [non_duplicate[h0], [photo]]
|
||||
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][0] = photo
|
||||
duplicate[h0][1] += [non_duplicate[h0]]
|
||||
else:
|
||||
duplicate[h0] = [photo, [non_duplicate[h0]]]
|
||||
duplicate[h0] = (photo, [non_duplicate[h0]])
|
||||
non_duplicate[h0] += [photo]
|
||||
# Return values because hash aren't need anymore
|
||||
return duplicate.values()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue