From dddba4f6f64680a047d99292f8acf65ae57238cf Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 15 Oct 2018 12:00:00 +0200 Subject: Command - reassociate similar images: add an option to delete document with missing images --- .../management/commands/reassociate_similar_images.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'ishtar_common/management') diff --git a/ishtar_common/management/commands/reassociate_similar_images.py b/ishtar_common/management/commands/reassociate_similar_images.py index 992e7f5ea..f6d432327 100644 --- a/ishtar_common/management/commands/reassociate_similar_images.py +++ b/ishtar_common/management/commands/reassociate_similar_images.py @@ -37,6 +37,9 @@ class Command(BaseCommand): parser.add_argument( '--ignore-reference', dest='ignore-reference', action='store_true', help='Ignore the reference on diff between models.') + parser.add_argument( + '--delete-missing', dest='delete-missing', action='store_true', + default=False, help='Delete document with missing images.') parser.add_argument( '--quiet', dest='quiet', action='store_true', help='Quiet output.') @@ -44,6 +47,7 @@ class Command(BaseCommand): def handle(self, *args, **options): quiet = options['quiet'] ignore_ref = options['ignore-reference'] + delete_missing = options['delete-missing'] merged_title = options['merged-title'] output_path = options['output-path'] @@ -69,8 +73,21 @@ class Command(BaseCommand): if hexdigest not in hashes: hashes[hexdigest] = [] hashes[hexdigest].append(doc.pk) + nb_missing_images = len(missing_images) if not quiet: - out.write("\n* {} missing images\n".format(len(missing_images))) + out.write("\n* {} missing images\n".format(nb_missing_images)) + + if missing_images and delete_missing: + for nb, idx in enumerate(missing_images): + if not quiet: + out.write( + "\r* delete document with missing images: {} %".format( + int(float(nb + 1) / nb_missing_images * 100))) + out.flush() + doc = Document.objects.get(pk=idx) + doc.delete() + if not quiet: + out.write("\n") attributes = [ 'title', 'associated_file', 'internal_reference', 'source_type', -- cgit v1.2.3