mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 11:01:12 +00:00
Use global switch to enable/disable preview images
This commit is contained in:
parent
878cc5cb17
commit
f206f61e9a
9 changed files with 99 additions and 63 deletions
|
@ -52,3 +52,15 @@ EMAIL_USE_SSL=false
|
|||
|
||||
# Set this to true when initializing certbot for domain, false when not
|
||||
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
|
||||
|
|
|
@ -52,3 +52,15 @@ EMAIL_USE_SSL=false
|
|||
|
||||
# Set this to true when initializing certbot for domain, false when not
|
||||
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
|
||||
|
|
|
@ -1,18 +1,20 @@
|
|||
""" customize the info available in context for rendering templates """
|
||||
from bookwyrm import models
|
||||
from bookwyrm.settings import STATIC_URL, STATIC_PATH, MEDIA_URL, MEDIA_PATH
|
||||
from bookwyrm import models, settings
|
||||
|
||||
|
||||
def site_settings(request): # pylint: disable=unused-argument
|
||||
"""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 {
|
||||
"site": models.SiteSettings.objects.get(),
|
||||
"active_announcements": models.Announcement.active_announcements(),
|
||||
"static_url": STATIC_URL,
|
||||
"media_url": MEDIA_URL,
|
||||
"static_path": STATIC_PATH,
|
||||
"media_path": MEDIA_PATH,
|
||||
"static_url": settings.STATIC_URL,
|
||||
"media_url": settings.MEDIA_URL,
|
||||
"static_path": settings.STATIC_PATH,
|
||||
"media_path": settings.MEDIA_PATH,
|
||||
"preview_images_enabled": settings.ENABLE_PREVIEW_IMAGES,
|
||||
"request_protocol": request_protocol,
|
||||
}
|
||||
|
|
|
@ -13,9 +13,9 @@ from django.core.files.uploadedfile import InMemoryUploadedFile
|
|||
from django.db.models import Avg
|
||||
|
||||
from bookwyrm import models, settings
|
||||
from bookwyrm.settings import DOMAIN, STATIC_ROOT
|
||||
from bookwyrm.tasks import app
|
||||
|
||||
|
||||
IMG_WIDTH = settings.PREVIEW_IMG_WIDTH
|
||||
IMG_HEIGHT = settings.PREVIEW_IMG_HEIGHT
|
||||
BG_COLOR = settings.PREVIEW_BG_COLOR
|
||||
|
@ -27,7 +27,7 @@ margin = math.floor(IMG_HEIGHT / 10)
|
|||
gutter = math.floor(margin / 2)
|
||||
inner_img_height = math.floor(IMG_HEIGHT * 0.8)
|
||||
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):
|
||||
|
@ -103,7 +103,7 @@ def generate_instance_layer(content_width):
|
|||
if site.logo_small:
|
||||
logo_img = Image.open(site.logo_small)
|
||||
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)
|
||||
|
||||
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):
|
||||
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(
|
||||
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_margin = 10
|
||||
|
@ -307,61 +307,64 @@ def save_and_cleanup(image, instance=None):
|
|||
@app.task
|
||||
def generate_site_preview_image_task():
|
||||
"""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:
|
||||
logo = site.logo
|
||||
else:
|
||||
logo = os.path.join(STATIC_ROOT, "images/logo.png")
|
||||
if site.logo:
|
||||
logo = site.logo
|
||||
else:
|
||||
logo = os.path.join(settings.STATIC_ROOT, "images/logo.png")
|
||||
|
||||
texts = {
|
||||
"text_zero": DOMAIN,
|
||||
"text_one": site.name,
|
||||
"text_three": site.instance_tagline,
|
||||
}
|
||||
texts = {
|
||||
"text_zero": settings.DOMAIN,
|
||||
"text_one": site.name,
|
||||
"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
|
||||
def generate_edition_preview_image_task(book_id):
|
||||
"""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(
|
||||
privacy="public",
|
||||
deleted=False,
|
||||
book__in=[book_id],
|
||||
).aggregate(Avg("rating"))["rating__avg"]
|
||||
rating = models.Review.objects.filter(
|
||||
privacy="public",
|
||||
deleted=False,
|
||||
book__in=[book_id],
|
||||
).aggregate(Avg("rating"))["rating__avg"]
|
||||
|
||||
texts = {
|
||||
"text_one": book.title,
|
||||
"text_two": book.subtitle,
|
||||
"text_three": book.author_text,
|
||||
}
|
||||
texts = {
|
||||
"text_one": book.title,
|
||||
"text_two": book.subtitle,
|
||||
"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
|
||||
def generate_user_preview_image_task(user_id):
|
||||
"""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 = {
|
||||
"text_one": user.display_name,
|
||||
"text_three": "@{}@{}".format(user.localname, DOMAIN),
|
||||
}
|
||||
texts = {
|
||||
"text_one": user.display_name,
|
||||
"text_three": "@{}@{}".format(user.localname, settings.DOMAIN),
|
||||
}
|
||||
|
||||
if user.avatar:
|
||||
avatar = user.avatar
|
||||
else:
|
||||
avatar = os.path.join(STATIC_ROOT, "images/default_avi.jpg")
|
||||
if user.avatar:
|
||||
avatar = user.avatar
|
||||
else:
|
||||
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)
|
||||
|
|
|
@ -38,14 +38,12 @@ LOCALE_PATHS = [
|
|||
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
||||
|
||||
# Preview image
|
||||
|
||||
# Specify RGB tuple or RGB hex strings,
|
||||
# or "use_dominant_color_light" / "use_dominant_color_dark"
|
||||
PREVIEW_BG_COLOR = "use_dominant_color_light"
|
||||
PREVIEW_IMG_WIDTH = 1200
|
||||
PREVIEW_IMG_HEIGHT = 630
|
||||
PREVIEW_TEXT_COLOR = "#363636" # Change to "#FFF" if you use "use_dominant_color_dark"
|
||||
PREVIEW_DEFAULT_COVER_COLOR = "#002549"
|
||||
ENABLE_PREVIEW_IMAGES = env.bool("ENABLE_PREVIEW_IMAGES", False)
|
||||
PREVIEW_BG_COLOR = env.str("PREVIEW_BG_COLOR", "use_dominant_color_light")
|
||||
PREVIEW_TEXT_COLOR = env.str("PREVIEW_TEXT_COLOR", "#363636")
|
||||
PREVIEW_IMG_WIDTH = env.int("PREVIEW_IMG_WIDTH", 1200)
|
||||
PREVIEW_IMG_HEIGHT = env.int("PREVIEW_IMG_HEIGHT", 630)
|
||||
PREVIEW_DEFAULT_COVER_COLOR = env.str("PREVIEW_DEFAULT_COVER_COLOR", "#002549")
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
{% block title %}{{ book|book_title }}{% endblock %}
|
||||
|
||||
{% block opengraph_images %}
|
||||
<meta name="twitter:image" content="{{ request_protocol }}{{ media_path }}{{ book.preview_image }}">
|
||||
<meta name="og:image" content="{{ request_protocol }}{{ media_path }}{{ book.preview_image }}">
|
||||
{% include 'snippets/opengraph_images.html' with image=book.preview_image %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
<meta name="og:description" content="{{ site.instance_tagline }}">
|
||||
|
||||
{% block opengraph_images %}
|
||||
<meta name="twitter:image" content="{{ request.protocol }}{{ media_path }}{{ site.preview_image }}">
|
||||
<meta name="og:image" content="{{ request.protocol }}{{ media_path }}{{ site.preview_image }}">
|
||||
{% include 'snippets/opengraph_images.html' %}
|
||||
{% endblock %}
|
||||
<meta name="twitter:image:alt" content="BookWyrm Logo">
|
||||
</head>
|
||||
|
|
12
bookwyrm/templates/snippets/opengraph_images.html
Normal file
12
bookwyrm/templates/snippets/opengraph_images.html
Normal 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 %}
|
|
@ -8,8 +8,7 @@
|
|||
{% block title %}{{ user.display_name }}{% endblock %}
|
||||
|
||||
{% block opengraph_images %}
|
||||
<meta name="twitter:image" content="{{ request_protocol }}{{ media_path }}{{ user.preview_image }}">
|
||||
<meta name="og:image" content="{{ request_protocol }}{{ media_path }}{{ user.preview_image }}">
|
||||
{% include 'snippets/opengraph_images.html' with image=user.preview_image %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
|
Loading…
Reference in a new issue