Add generate_preview_images command

This commit is contained in:
Joachim 2021-05-26 13:52:10 +02:00
parent b47edc5f0d
commit 65de40a95a
4 changed files with 55 additions and 3 deletions

View file

@ -0,0 +1,48 @@
""" Generate preview images """
import sys
from django.core.management.base import BaseCommand
from bookwyrm import activitystreams, models, settings, preview_images
def generate_preview_images():
"""generate preview images"""
print(" | Hello! I will be generating preview images for your instance.")
print("🧑‍🎨 ⎨ This might take quite long if your instance has a lot of books and users.")
print(" | ✧ Thank you for your patience ✧")
# Site
sys.stdout.write(" → Site preview image: ")
preview_images.generate_site_preview_image_task()
sys.stdout.write(" OK 🖼\n")
# Users
users = models.User.objects.filter(
local=True,
is_active=True,
)
sys.stdout.write(" → User preview images ({}): ".format(len(users)))
for user in users:
preview_images.generate_user_preview_image_task(user.id)
sys.stdout.write(".")
sys.stdout.write(" OK 🖼\n")
# Books
books = models.Book.objects.select_subclasses().filter()
sys.stdout.write(" → Book preview images ({}): ".format(len(books)))
for book in books:
preview_images.generate_edition_preview_image_task(book.id)
sys.stdout.write(".")
sys.stdout.write(" OK 🖼\n")
print("🧑‍🎨 ⎨ Im all done! ✧ Enjoy ✧")
class Command(BaseCommand):
help = "Generate preview images"
# pylint: disable=no-self-use,unused-argument
def handle(self, *args, **options):
"""run feed builder"""
generate_preview_images()

View file

@ -344,6 +344,7 @@ def generate_edition_preview_image_task(book_id):
save_and_cleanup(image, instance=book)
@app.task
def generate_user_preview_image_task(user_id):
"""generate preview_image for a book"""

View file

@ -41,10 +41,10 @@ DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# Specify RGB tuple or RGB hex strings,
# or "use_dominant_color_light" / "use_dominant_color_dark"
PREVIEW_BG_COLOR = "use_dominant_color_dark"
PREVIEW_BG_COLOR = "use_dominant_color_light"
PREVIEW_IMG_WIDTH = 1200
PREVIEW_IMG_HEIGHT = 630
PREVIEW_TEXT_COLOR = "#FFF"
PREVIEW_TEXT_COLOR = "#363636" # Change to "#FFF" if you use "use_dominant_color_dark"
PREVIEW_DEFAULT_COVER_COLOR = "#002549"
# Quick-start development settings - unsuitable for production

5
bw-dev
View file

@ -107,7 +107,10 @@ case "$CMD" in
populate_streams)
runweb python manage.py populate_streams
;;
generate_preview_images)
runweb python manage.py generate_preview_images
;;
*)
echo "Unrecognised command. Try: build, clean, up, initdb, resetdb, makemigrations, migrate, bash, shell, dbshell, restart_celery, test, pytest, test_report, black, populate_feeds"
echo "Unrecognised command. Try: build, clean, up, initdb, resetdb, makemigrations, migrate, bash, shell, dbshell, restart_celery, test, pytest, test_report, black, populate_feeds, generate_preview_images"
;;
esac