Format code using black

This commit is contained in:
Alexandre Iooss 2022-03-02 21:23:40 +01:00
parent 2ad0c8dbc7
commit 59136050fb
14 changed files with 809 additions and 413 deletions

View file

@ -5,26 +5,36 @@ from photologue.models import Gallery
class Command(BaseCommand):
help = 'List all duplicate for chosen galleries'
help = "List all duplicate for chosen galleries"
def add_arguments(self, parser):
parser.add_argument(
'--slugs', nargs='+', help='Try to find duplicate in the selected galleries', default=[])
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')
"--slugs",
nargs="+",
help="Try to find duplicate in the selected galleries",
default=[],
)
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):
# Collect all required galleries
if options['all']:
if options["all"]:
galleries = Gallery.objects.all()
else:
galleries = []
for slug in options['slugs']:
for slug in options["slugs"]:
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.")
raise CommandError(
f"Slug {slug} does not correspond to a "
"gallery in the database."
)
galleries += gallery_query
# Find duplicates in all galleries
@ -32,18 +42,16 @@ class Command(BaseCommand):
duplicates = find_duplicate(gallery)
self.stdout.write(f"Gallery {gallery.slug}:")
for original, copies in duplicates:
self.stdout.write(f" {original.slug} is duplicated:", ending='')
self.stdout.write(f" {original.slug} is duplicated:", ending="")
for copy in copies:
self.stdout.write(f" {copy.slug}")
# Delete them if --delete
if options['delete']:
self.stdout.write(
' Deleting duplicate in {} :'.format(gallery.slug))
if options["delete"]:
self.stdout.write(" Deleting duplicate in {} :".format(gallery.slug))
for (_original, copies) in duplicates:
for copy in copies:
self.stdout.write(
' Deleting {}...'.format(copy.slug))
self.stdout.write(" Deleting {}...".format(copy.slug))
copy.delete()

View file

@ -7,22 +7,21 @@ from photologue.models import ImageModel, PhotoSize
class Command(BaseCommand):
help = 'Manages Photologue cache file for the given sizes.'
help = "Manages Photologue cache file for the given sizes."
def add_arguments(self, parser):
parser.add_argument('sizes',
nargs='*',
type=str,
help='Name of the photosize.')
parser.add_argument('--reset',
action='store_true',
default=False,
dest='reset',
help='Reset photo cache before generating.')
parser.add_argument("sizes", nargs="*", type=str, help="Name of the photosize.")
parser.add_argument(
"--reset",
action="store_true",
default=False,
dest="reset",
help="Reset photo cache before generating.",
)
def handle(self, *args, **options):
reset = options['reset']
sizes = options['sizes']
reset = options["reset"]
sizes = options["sizes"]
if not sizes:
photosizes = PhotoSize.objects.all()
@ -30,13 +29,13 @@ class Command(BaseCommand):
photosizes = PhotoSize.objects.filter(name__in=sizes)
if not len(photosizes):
raise CommandError('No photo sizes were found.')
raise CommandError("No photo sizes were found.")
print('Caching photos, this may take a while...')
print("Caching photos, this may take a while...")
for cls in ImageModel.__subclasses__():
for photosize in photosizes:
print('Cacheing %s size images' % photosize.name)
print("Cacheing %s size images" % photosize.name)
for obj in cls.objects.all():
if reset:
obj.remove_size(photosize)

View file

@ -6,17 +6,15 @@ from photologue.models import PhotoSize
class Command(BaseCommand):
help = ('Creates a new Photologue photo size interactively.')
help = "Creates a new Photologue photo size interactively."
requires_model_validation = True
can_import_settings = True
def add_arguments(self, parser):
parser.add_argument('name',
type=str,
help='Name of the new photo size')
parser.add_argument("name", type=str, help="Name of the new photo size")
def handle(self, *args, **options):
create_photosize(options['name'])
create_photosize(options["name"])
def get_response(msg, func=int, default=None):
@ -27,10 +25,12 @@ def get_response(msg, func=int, default=None):
try:
return func(resp)
except Exception:
print('Invalid input.')
print("Invalid input.")
def create_photosize(name, width=0, height=0, crop=False, pre_cache=False, increment_count=False):
def create_photosize(
name, width=0, height=0, crop=False, pre_cache=False, increment_count=False
):
try:
size = PhotoSize.objects.get(name=name)
exists = True
@ -38,15 +38,20 @@ def create_photosize(name, width=0, height=0, crop=False, pre_cache=False, incre
size = PhotoSize(name=name)
exists = False
if exists:
msg = 'A "%s" photo size already exists. Do you want to replace it? (yes, no):' % name
if not get_response(msg, lambda inp: inp == 'yes', False):
msg = (
'A "%s" photo size already exists. Do you want to replace it? (yes, no):'
% name
)
if not get_response(msg, lambda inp: inp == "yes", False):
return
print('\nWe will now define the "%s" photo size:\n' % size)
w = get_response('Width (in pixels):', lambda inp: int(inp), width)
h = get_response('Height (in pixels):', lambda inp: int(inp), height)
c = get_response('Crop to fit? (yes, no):', lambda inp: inp == 'yes', crop)
p = get_response('Pre-cache? (yes, no):', lambda inp: inp == 'yes', pre_cache)
i = get_response('Increment count? (yes, no):', lambda inp: inp == 'yes', increment_count)
w = get_response("Width (in pixels):", lambda inp: int(inp), width)
h = get_response("Height (in pixels):", lambda inp: int(inp), height)
c = get_response("Crop to fit? (yes, no):", lambda inp: inp == "yes", crop)
p = get_response("Pre-cache? (yes, no):", lambda inp: inp == "yes", pre_cache)
i = get_response(
"Increment count? (yes, no):", lambda inp: inp == "yes", increment_count
)
size.width = w
size.height = h
size.crop = c

View file

@ -6,16 +6,13 @@ from photologue.models import ImageModel, PhotoSize
class Command(BaseCommand):
help = 'Clears the Photologue cache for the given sizes.'
help = "Clears the Photologue cache for the given sizes."
def add_arguments(self, parser):
parser.add_argument('sizes',
nargs='*',
type=str,
help='Name of the photosize.')
parser.add_argument("sizes", nargs="*", type=str, help="Name of the photosize.")
def handle(self, *args, **options):
sizes = options['sizes']
sizes = options["sizes"]
if not sizes:
photosizes = PhotoSize.objects.all()
@ -23,12 +20,12 @@ class Command(BaseCommand):
photosizes = PhotoSize.objects.filter(name__in=sizes)
if not len(photosizes):
raise CommandError('No photo sizes were found.')
raise CommandError("No photo sizes were found.")
print('Flushing cache...')
print("Flushing cache...")
for cls in ImageModel.__subclasses__():
for photosize in photosizes:
print('Flushing %s size images' % photosize.name)
print("Flushing %s size images" % photosize.name)
for obj in cls.objects.all():
obj.remove_size(photosize)

View file

@ -7,17 +7,17 @@ from photologue.models import Gallery
class Command(BaseCommand):
help = 'Rename uploaded media file to match gallery and photo names'
help = "Rename uploaded media file to match gallery and photo names"
def add_arguments(self, parser):
parser.add_argument('--apply', action='store_true')
parser.add_argument("--apply", action="store_true")
def handle(self, *args, **options):
media_dir = Path(settings.MEDIA_ROOT)
for gallery in Gallery.objects.all():
# Create gallery directory
gallery_year = str(gallery.date_start.year)
gallery_dir = Path('photos') / gallery_year / gallery.slug
gallery_dir = Path("photos") / gallery_year / gallery.slug
gallery_path = media_dir / gallery_dir
if not gallery_path.exists():
self.stdout.write(f"Creating {gallery_dir}")