Rationalize paths handling

This commit is contained in:
Joachim 2021-05-26 17:55:39 +02:00
parent e362c82495
commit e214d8b879

View file

@ -6,7 +6,6 @@ import textwrap
from colorthief import ColorThief from colorthief import ColorThief
from io import BytesIO from io import BytesIO
from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageColor from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageColor
from pathlib import Path
from uuid import uuid4 from uuid import uuid4
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
@ -14,7 +13,7 @@ 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 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
@ -28,17 +27,16 @@ 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)
path = Path(__file__).parent.absolute() font_dir = os.path.join(STATIC_ROOT, "fonts/public_sans")
font_dir = path.joinpath("static/fonts/public_sans")
def get_font(font_name, size=28): def get_font(font_name, size=28):
if font_name == "light": if font_name == "light":
font_path = "%s/PublicSans-Light.ttf" % font_dir font_path = os.path.join(font_dir, "PublicSans-Light.ttf")
if font_name == "regular": if font_name == "regular":
font_path = "%s/PublicSans-Regular.ttf" % font_dir font_path = os.path.join(font_dir, "PublicSans-Regular.ttf")
elif font_name == "bold": elif font_name == "bold":
font_path = "%s/PublicSans-Bold.ttf" % font_dir font_path = os.path.join(font_dir, "PublicSans-Bold.ttf")
try: try:
font = ImageFont.truetype(font_path, size) font = ImageFont.truetype(font_path, size)
@ -105,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 = path.joinpath("static/images/logo-small.png") static_path = os.path.join(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)
@ -128,9 +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(path.joinpath("static/images/icons/star-full.png")) icon_star_full = Image.open(os.path.join(STATIC_ROOT, "images/icons/star-full.png"))
icon_star_empty = Image.open(path.joinpath("static/images/icons/star-empty.png")) icon_star_empty = Image.open(
icon_star_half = Image.open(path.joinpath("static/images/icons/star-half.png")) os.path.join(STATIC_ROOT, "images/icons/star-empty.png")
)
icon_star_half = Image.open(os.path.join(STATIC_ROOT, "images/icons/star-half.png"))
icon_size = 64 icon_size = 64
icon_margin = 10 icon_margin = 10
@ -312,7 +312,7 @@ def generate_site_preview_image_task():
if site.logo: if site.logo:
logo = site.logo logo = site.logo
else: else:
logo = path.joinpath("static/images/logo.png") logo = os.path.join(STATIC_ROOT, "images/logo.png")
texts = { texts = {
"text_zero": DOMAIN, "text_zero": DOMAIN,
@ -360,7 +360,7 @@ def generate_user_preview_image_task(user_id):
if user.avatar: if user.avatar:
avatar = user.avatar avatar = user.avatar
else: else:
avatar = path.joinpath("static/images/default_avi.jpg") avatar = os.path.join(STATIC_ROOT, "images/default_avi.jpg")
image = generate_preview_image(texts=texts, picture=avatar) image = generate_preview_image(texts=texts, picture=avatar)