From af798e3fcc5813a34cd928cf63554d1fffe84243 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Sun, 14 Nov 2021 09:08:37 +0100 Subject: [PATCH] Simplify filtering gallery by slug --- .../management/commands/duplicate.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/photologue_custom/management/commands/duplicate.py b/photologue_custom/management/commands/duplicate.py index 634e057..93a9150 100644 --- a/photologue_custom/management/commands/duplicate.py +++ b/photologue_custom/management/commands/duplicate.py @@ -19,15 +19,14 @@ class Command(BaseCommand): if options['all']: galleries = Gallery.objects.all() else: + galleries = [] 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') + gallery_query = Gallery.objects.filter(slug=slug) + if not gallery_query: + raise CommandError(f"Slug {slug} does not correspond to a " + "gallery in the database.") + galleries += gallery_query + # Find duplicates in all galleries for gallery in galleries: duplicates = find_duplicate(gallery)