2021-05-26 11:52:10 +00:00
|
|
|
|
""" Generate preview images """
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
|
2021-06-18 22:08:36 +00:00
|
|
|
|
from bookwyrm import models, preview_images
|
2021-05-26 11:52:10 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-18 22:08:36 +00:00
|
|
|
|
# pylint: disable=line-too-long
|
2021-05-26 11:52:10 +00:00
|
|
|
|
class Command(BaseCommand):
|
2021-06-18 22:08:36 +00:00
|
|
|
|
"""Creates previews for existing objects"""
|
2021-06-18 22:24:10 +00:00
|
|
|
|
|
2021-05-26 11:52:10 +00:00
|
|
|
|
help = "Generate preview images"
|
2021-05-28 15:25:20 +00:00
|
|
|
|
|
2022-02-17 18:59:28 +00:00
|
|
|
|
# pylint: disable=no-self-use
|
2021-05-28 15:25:20 +00:00
|
|
|
|
def add_arguments(self, parser):
|
2022-02-17 18:59:28 +00:00
|
|
|
|
"""options for how the command is run"""
|
2021-05-28 15:26:43 +00:00
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--all",
|
|
|
|
|
"-a",
|
|
|
|
|
action="store_true",
|
|
|
|
|
help="Generates images for ALL types: site, users and books. Can use a lot of computing power.",
|
|
|
|
|
)
|
2021-05-28 15:25:20 +00:00
|
|
|
|
|
2021-05-26 11:52:10 +00:00
|
|
|
|
# pylint: disable=no-self-use,unused-argument
|
|
|
|
|
def handle(self, *args, **options):
|
2021-05-28 15:25:20 +00:00
|
|
|
|
"""generate preview images"""
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write(
|
|
|
|
|
" | Hello! I will be generating preview images for your instance."
|
|
|
|
|
)
|
|
|
|
|
if options["all"]:
|
2021-05-28 15:25:20 +00:00
|
|
|
|
self.stdout.write(
|
|
|
|
|
"🧑🎨 ⎨ This might take quite long if your instance has a lot of books and users."
|
|
|
|
|
)
|
|
|
|
|
self.stdout.write(" | ✧ Thank you for your patience ✧")
|
|
|
|
|
else:
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write("🧑🎨 ⎨ I will only generate the instance preview image.")
|
2021-05-28 15:25:20 +00:00
|
|
|
|
self.stdout.write(" | ✧ Be right back! ✧")
|
|
|
|
|
|
|
|
|
|
# Site
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write(" → Site preview image: ", ending="")
|
2021-05-28 15:25:20 +00:00
|
|
|
|
preview_images.generate_site_preview_image_task.delay()
|
|
|
|
|
self.stdout.write(" OK 🖼")
|
|
|
|
|
|
2022-02-17 18:59:28 +00:00
|
|
|
|
# pylint: disable=consider-using-f-string
|
2021-05-28 15:26:43 +00:00
|
|
|
|
if options["all"]:
|
2021-05-28 15:25:20 +00:00
|
|
|
|
# Users
|
|
|
|
|
users = models.User.objects.filter(
|
|
|
|
|
local=True,
|
|
|
|
|
is_active=True,
|
|
|
|
|
)
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write(
|
|
|
|
|
" → User preview images ({}): ".format(len(users)), ending=""
|
|
|
|
|
)
|
2021-05-28 15:25:20 +00:00
|
|
|
|
for user in users:
|
|
|
|
|
preview_images.generate_user_preview_image_task.delay(user.id)
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write(".", ending="")
|
2021-05-28 15:25:20 +00:00
|
|
|
|
self.stdout.write(" OK 🖼")
|
|
|
|
|
|
|
|
|
|
# Books
|
|
|
|
|
books = models.Book.objects.select_subclasses().filter()
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write(
|
|
|
|
|
" → Book preview images ({}): ".format(len(books)), ending=""
|
|
|
|
|
)
|
2021-05-28 15:25:20 +00:00
|
|
|
|
for book in books:
|
|
|
|
|
preview_images.generate_edition_preview_image_task.delay(book.id)
|
2021-05-28 15:26:43 +00:00
|
|
|
|
self.stdout.write(".", ending="")
|
2021-05-28 15:25:20 +00:00
|
|
|
|
self.stdout.write(" OK 🖼")
|
|
|
|
|
|
|
|
|
|
self.stdout.write("🧑🎨 ⎨ I’m all done! ✧ Enjoy ✧")
|