Use global switch to enable/disable preview images

This commit is contained in:
Joachim 2021-05-28 17:00:07 +02:00
parent 878cc5cb17
commit f206f61e9a
9 changed files with 99 additions and 63 deletions

View file

@ -52,3 +52,15 @@ EMAIL_USE_SSL=false
# Set this to true when initializing certbot for domain, false when not # Set this to true when initializing certbot for domain, false when not
CERTBOT_INIT=false CERTBOT_INIT=false
# Preview image generation can be computing and storage intensive
# ENABLE_PREVIEW_IMAGES=True
# Specify RGB tuple or RGB hex strings,
# or use_dominant_color_light / use_dominant_color_dark
PREVIEW_BG_COLOR=use_dominant_color_light
# Change to #FFF if you use use_dominant_color_dark
PREVIEW_TEXT_COLOR=#363636
PREVIEW_IMG_WIDTH=1200
PREVIEW_IMG_HEIGHT=630
PREVIEW_DEFAULT_COVER_COLOR=#002549

View file

@ -52,3 +52,15 @@ EMAIL_USE_SSL=false
# Set this to true when initializing certbot for domain, false when not # Set this to true when initializing certbot for domain, false when not
CERTBOT_INIT=false CERTBOT_INIT=false
# Preview image generation can be computing and storage intensive
# ENABLE_PREVIEW_IMAGES=True
# Specify RGB tuple or RGB hex strings,
# or use_dominant_color_light / use_dominant_color_dark
PREVIEW_BG_COLOR=use_dominant_color_light
# Change to #FFF if you use use_dominant_color_dark
PREVIEW_TEXT_COLOR=#363636
PREVIEW_IMG_WIDTH=1200
PREVIEW_IMG_HEIGHT=630
PREVIEW_DEFAULT_COVER_COLOR=#002549

View file

@ -1,18 +1,20 @@
""" customize the info available in context for rendering templates """ """ customize the info available in context for rendering templates """
from bookwyrm import models from bookwyrm import models, settings
from bookwyrm.settings import STATIC_URL, STATIC_PATH, MEDIA_URL, MEDIA_PATH
def site_settings(request): # pylint: disable=unused-argument def site_settings(request): # pylint: disable=unused-argument
"""include the custom info about the site""" """include the custom info about the site"""
request_protocol = "https://" if request.is_secure() else "http://" request_protocol = "https://"
if not request.is_secure():
request_protocol = "http://"
return { return {
"site": models.SiteSettings.objects.get(), "site": models.SiteSettings.objects.get(),
"active_announcements": models.Announcement.active_announcements(), "active_announcements": models.Announcement.active_announcements(),
"static_url": STATIC_URL, "static_url": settings.STATIC_URL,
"media_url": MEDIA_URL, "media_url": settings.MEDIA_URL,
"static_path": STATIC_PATH, "static_path": settings.STATIC_PATH,
"media_path": MEDIA_PATH, "media_path": settings.MEDIA_PATH,
"preview_images_enabled": settings.ENABLE_PREVIEW_IMAGES,
"request_protocol": request_protocol, "request_protocol": request_protocol,
} }

View file

@ -13,9 +13,9 @@ from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db.models import Avg from django.db.models import Avg
from bookwyrm import models, settings from bookwyrm import models, settings
from bookwyrm.settings import DOMAIN, STATIC_ROOT
from bookwyrm.tasks import app from bookwyrm.tasks import app
IMG_WIDTH = settings.PREVIEW_IMG_WIDTH IMG_WIDTH = settings.PREVIEW_IMG_WIDTH
IMG_HEIGHT = settings.PREVIEW_IMG_HEIGHT IMG_HEIGHT = settings.PREVIEW_IMG_HEIGHT
BG_COLOR = settings.PREVIEW_BG_COLOR BG_COLOR = settings.PREVIEW_BG_COLOR
@ -27,7 +27,7 @@ margin = math.floor(IMG_HEIGHT / 10)
gutter = math.floor(margin / 2) gutter = math.floor(margin / 2)
inner_img_height = math.floor(IMG_HEIGHT * 0.8) inner_img_height = math.floor(IMG_HEIGHT * 0.8)
inner_img_width = math.floor(inner_img_height * 0.7) inner_img_width = math.floor(inner_img_height * 0.7)
font_dir = os.path.join(STATIC_ROOT, "fonts/public_sans") font_dir = os.path.join(settings.STATIC_ROOT, "fonts/public_sans")
def get_font(font_name, size=28): def get_font(font_name, size=28):
@ -103,7 +103,7 @@ def generate_instance_layer(content_width):
if site.logo_small: if site.logo_small:
logo_img = Image.open(site.logo_small) logo_img = Image.open(site.logo_small)
else: else:
static_path = os.path.join(STATIC_ROOT, "images/logo-small.png") static_path = os.path.join(settings.STATIC_ROOT, "images/logo-small.png")
logo_img = Image.open(static_path) logo_img = Image.open(static_path)
instance_layer = Image.new("RGBA", (content_width, 62), color=TRANSPARENT_COLOR) instance_layer = Image.new("RGBA", (content_width, 62), color=TRANSPARENT_COLOR)
@ -126,11 +126,11 @@ def generate_instance_layer(content_width):
def generate_rating_layer(rating, content_width): def generate_rating_layer(rating, content_width):
icon_star_full = Image.open(os.path.join(STATIC_ROOT, "images/icons/star-full.png")) icon_star_full = Image.open(os.path.join(settings.STATIC_ROOT, "images/icons/star-full.png"))
icon_star_empty = Image.open( icon_star_empty = Image.open(
os.path.join(STATIC_ROOT, "images/icons/star-empty.png") os.path.join(settings.STATIC_ROOT, "images/icons/star-empty.png")
) )
icon_star_half = Image.open(os.path.join(STATIC_ROOT, "images/icons/star-half.png")) icon_star_half = Image.open(os.path.join(settings.STATIC_ROOT, "images/icons/star-half.png"))
icon_size = 64 icon_size = 64
icon_margin = 10 icon_margin = 10
@ -307,61 +307,64 @@ def save_and_cleanup(image, instance=None):
@app.task @app.task
def generate_site_preview_image_task(): def generate_site_preview_image_task():
"""generate preview_image for the website""" """generate preview_image for the website"""
site = models.SiteSettings.objects.get() if settings.ENABLE_PREVIEW_IMAGES == True:
site = models.SiteSettings.objects.get()
if site.logo: if site.logo:
logo = site.logo logo = site.logo
else: else:
logo = os.path.join(STATIC_ROOT, "images/logo.png") logo = os.path.join(settings.STATIC_ROOT, "images/logo.png")
texts = { texts = {
"text_zero": DOMAIN, "text_zero": settings.DOMAIN,
"text_one": site.name, "text_one": site.name,
"text_three": site.instance_tagline, "text_three": site.instance_tagline,
} }
image = generate_preview_image(texts=texts, picture=logo, show_instance_layer=False) image = generate_preview_image(texts=texts, picture=logo, show_instance_layer=False)
save_and_cleanup(image, instance=site) save_and_cleanup(image, instance=site)
@app.task @app.task
def generate_edition_preview_image_task(book_id): def generate_edition_preview_image_task(book_id):
"""generate preview_image for a book""" """generate preview_image for a book"""
book = models.Book.objects.select_subclasses().get(id=book_id) if settings.ENABLE_PREVIEW_IMAGES == True:
book = models.Book.objects.select_subclasses().get(id=book_id)
rating = models.Review.objects.filter( rating = models.Review.objects.filter(
privacy="public", privacy="public",
deleted=False, deleted=False,
book__in=[book_id], book__in=[book_id],
).aggregate(Avg("rating"))["rating__avg"] ).aggregate(Avg("rating"))["rating__avg"]
texts = { texts = {
"text_one": book.title, "text_one": book.title,
"text_two": book.subtitle, "text_two": book.subtitle,
"text_three": book.author_text, "text_three": book.author_text,
} }
image = generate_preview_image(texts=texts, picture=book.cover, rating=rating) image = generate_preview_image(texts=texts, picture=book.cover, rating=rating)
save_and_cleanup(image, instance=book) save_and_cleanup(image, instance=book)
@app.task @app.task
def generate_user_preview_image_task(user_id): def generate_user_preview_image_task(user_id):
"""generate preview_image for a book""" """generate preview_image for a book"""
user = models.User.objects.get(id=user_id) if settings.ENABLE_PREVIEW_IMAGES == True:
user = models.User.objects.get(id=user_id)
texts = { texts = {
"text_one": user.display_name, "text_one": user.display_name,
"text_three": "@{}@{}".format(user.localname, DOMAIN), "text_three": "@{}@{}".format(user.localname, settings.DOMAIN),
} }
if user.avatar: if user.avatar:
avatar = user.avatar avatar = user.avatar
else: else:
avatar = os.path.join(STATIC_ROOT, "images/default_avi.jpg") avatar = os.path.join(settings.STATIC_ROOT, "images/default_avi.jpg")
image = generate_preview_image(texts=texts, picture=avatar) image = generate_preview_image(texts=texts, picture=avatar)
save_and_cleanup(image, instance=user) save_and_cleanup(image, instance=user)

View file

@ -38,14 +38,12 @@ LOCALE_PATHS = [
DEFAULT_AUTO_FIELD = "django.db.models.AutoField" DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# Preview image # Preview image
ENABLE_PREVIEW_IMAGES = env.bool("ENABLE_PREVIEW_IMAGES", False)
# Specify RGB tuple or RGB hex strings, PREVIEW_BG_COLOR = env.str("PREVIEW_BG_COLOR", "use_dominant_color_light")
# or "use_dominant_color_light" / "use_dominant_color_dark" PREVIEW_TEXT_COLOR = env.str("PREVIEW_TEXT_COLOR", "#363636")
PREVIEW_BG_COLOR = "use_dominant_color_light" PREVIEW_IMG_WIDTH = env.int("PREVIEW_IMG_WIDTH", 1200)
PREVIEW_IMG_WIDTH = 1200 PREVIEW_IMG_HEIGHT = env.int("PREVIEW_IMG_HEIGHT", 630)
PREVIEW_IMG_HEIGHT = 630 PREVIEW_DEFAULT_COVER_COLOR = env.str("PREVIEW_DEFAULT_COVER_COLOR", "#002549")
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 # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

View file

@ -4,8 +4,7 @@
{% block title %}{{ book|book_title }}{% endblock %} {% block title %}{{ book|book_title }}{% endblock %}
{% block opengraph_images %} {% block opengraph_images %}
<meta name="twitter:image" content="{{ request_protocol }}{{ media_path }}{{ book.preview_image }}"> {% include 'snippets/opengraph_images.html' with image=book.preview_image %}
<meta name="og:image" content="{{ request_protocol }}{{ media_path }}{{ book.preview_image }}">
{% endblock %} {% endblock %}
{% block content %} {% block content %}

View file

@ -17,8 +17,7 @@
<meta name="og:description" content="{{ site.instance_tagline }}"> <meta name="og:description" content="{{ site.instance_tagline }}">
{% block opengraph_images %} {% block opengraph_images %}
<meta name="twitter:image" content="{{ request.protocol }}{{ media_path }}{{ site.preview_image }}"> {% include 'snippets/opengraph_images.html' %}
<meta name="og:image" content="{{ request.protocol }}{{ media_path }}{{ site.preview_image }}">
{% endblock %} {% endblock %}
<meta name="twitter:image:alt" content="BookWyrm Logo"> <meta name="twitter:image:alt" content="BookWyrm Logo">
</head> </head>

View file

@ -0,0 +1,12 @@
{% if preview_images_enabled is True %}
{% if image %}
<meta name="twitter:image" content="{{ request.protocol }}{{ media_path }}{{ image }}">
<meta name="og:image" content="{{ request.protocol }}{{ media_path }}{{ image }}">
{% else %}
<meta name="twitter:image" content="{{ request.protocol }}{{ media_path }}{{ site.preview_image }}">
<meta name="og:image" content="{{ request.protocol }}{{ media_path }}{{ site.preview_image }}">
{% endif %}
{% else %}
<meta name="twitter:image" content="{{ request.protocol }}{% if site.logo %}{{ media_path }}{{ site.logo }}{% else %}{{ static_path }}/images/logo.png{% endif %}">
<meta name="og:image" content="{{ request.protocol }}{% if site.logo %}{{ media_path }}{{ site.logo }}{% else %}{{ static_path }}/images/logo.png{% endif %}">
{% endif %}

View file

@ -8,8 +8,7 @@
{% block title %}{{ user.display_name }}{% endblock %} {% block title %}{{ user.display_name }}{% endblock %}
{% block opengraph_images %} {% block opengraph_images %}
<meta name="twitter:image" content="{{ request_protocol }}{{ media_path }}{{ user.preview_image }}"> {% include 'snippets/opengraph_images.html' with image=user.preview_image %}
<meta name="og:image" content="{{ request_protocol }}{{ media_path }}{{ user.preview_image }}">
{% endblock %} {% endblock %}
{% block content %} {% block content %}