photo26/photologue/management/commands/alone_photo.py

54 lines
1.6 KiB
Python

# This file is part of photo21
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import hashlib
from django.core.management.base import BaseCommand, CommandError
from photologue.models import Gallery, Photo
class Command(BaseCommand):
help = "List all alone photo, photo that have any galleries"
def add_arguments(self, parser):
parser.add_argument(
"-a",
"--all",
action="store_true",
help="Try to find alone photos",
)
parser.add_argument(
"-c",
"--count",
action="store_true",
help="Count the numerber of alone photos",
)
parser.add_argument("-d", "--delete", action="store_true")
def handle(self, *args, **options):
# Collect all required Photos and do filtering stuff
photos = Photo.objects.all()
photos = photos.filter(galleries=None)
# printing
count = photos.count()
self.stdout.write(f"There is {count} photo alone without a galerie")
if options["count"]:
return # End process
i = 1 # counter
for photo in photos:
self.stdout.write(
"Photo is alone without a galerie : {}".format(photo.slug)
)
# Delete them if --delete
if options["delete"]:
self.stdout.write(" Deleting alone photo {} :".format(photo.slug))
photo.delete()
if i % 10 == 0:
i = 1