Bundle trimed down alternative to photologue

This commit is contained in:
Alexandre Iooss 2022-01-30 08:20:03 +01:00
parent 2da3419b8d
commit 5368a51a76
40 changed files with 2652 additions and 70 deletions

View file

@ -0,0 +1,43 @@
# Based on https://github.com/richardbarran/django-photologue/
# by Richard Barran, BSD-3 licensed
from django.core.management.base import BaseCommand, CommandError
from photologue.models import ImageModel, PhotoSize
class Command(BaseCommand):
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.')
def handle(self, *args, **options):
reset = options['reset']
sizes = options['sizes']
if not sizes:
photosizes = PhotoSize.objects.all()
else:
photosizes = PhotoSize.objects.filter(name__in=sizes)
if not len(photosizes):
raise CommandError('No photo sizes were found.')
print('Caching photos, this may take a while...')
for cls in ImageModel.__subclasses__():
for photosize in photosizes:
print('Cacheing %s size images' % photosize.name)
for obj in cls.objects.all():
if reset:
obj.remove_size(photosize)
obj.create_size(photosize)

View file

@ -0,0 +1,57 @@
# Based on https://github.com/richardbarran/django-photologue/
# by Richard Barran, BSD-3 licensed
from django.core.management.base import BaseCommand
from photologue.models import PhotoSize
class Command(BaseCommand):
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')
def handle(self, *args, **options):
create_photosize(options['name'])
def get_response(msg, func=int, default=None):
while True:
resp = input(msg)
if not resp and default is not None:
return default
try:
return func(resp)
except Exception:
print('Invalid input.')
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
except PhotoSize.DoesNotExist:
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):
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)
size.width = w
size.height = h
size.crop = c
size.pre_cache = p
size.increment_count = i
size.save()
print('\nA "%s" photo size has been created.\n' % name)
return size

View file

@ -0,0 +1,34 @@
# Based on https://github.com/richardbarran/django-photologue/
# by Richard Barran, BSD-3 licensed
from django.core.management.base import BaseCommand, CommandError
from photologue.models import ImageModel, PhotoSize
class Command(BaseCommand):
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.')
def handle(self, *args, **options):
sizes = options['sizes']
if not sizes:
photosizes = PhotoSize.objects.all()
else:
photosizes = PhotoSize.objects.filter(name__in=sizes)
if not len(photosizes):
raise CommandError('No photo sizes were found.')
print('Flushing cache...')
for cls in ImageModel.__subclasses__():
for photosize in photosizes:
print('Flushing %s size images' % photosize.name)
for obj in cls.objects.all():
obj.remove_size(photosize)