forked from mirrors/bookwyrm
Add simple tag to provide fallback if original image doesn't exist
This commit is contained in:
parent
2370f60436
commit
ce4553b367
2 changed files with 73 additions and 24 deletions
|
@ -3,6 +3,7 @@
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
{% load static %}
|
{% load static %}
|
||||||
{% load imagekit %}
|
{% load imagekit %}
|
||||||
|
{% load utilities %}
|
||||||
|
|
||||||
{% if book.cover %}
|
{% if book.cover %}
|
||||||
<picture class="cover-container {{ cover_class }}">
|
<picture class="cover-container {{ cover_class }}">
|
||||||
|
@ -21,93 +22,130 @@
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/webp"
|
type="image/webp"
|
||||||
srcset="{{ book.cover_bw_book_xsmall_webp.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='xsmall' ext='webp' %}"
|
||||||
/>
|
/>
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/jpg"
|
type="image/jpg"
|
||||||
srcset="{{ book.cover_bw_book_xsmall_jpg.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='xsmall' ext='jpg' %}"
|
||||||
/>
|
/>
|
||||||
{% elif size_mobile == 'small' %}
|
{% elif size_mobile == 'small' %}
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/webp"
|
type="image/webp"
|
||||||
srcset="{{ book.cover_bw_book_small_webp.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='small' ext='webp' %}"
|
||||||
/>
|
/>
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/jpg"
|
type="image/jpg"
|
||||||
srcset="{{ book.cover_bw_book_small_jpg.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='small' ext='jpg' %}"
|
||||||
/>
|
/>
|
||||||
{% elif size_mobile == 'medium' %}
|
{% elif size_mobile == 'medium' %}
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/webp"
|
type="image/webp"
|
||||||
srcset="{{ book.cover_bw_book_medium_webp.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='medium' ext='webp' %}"
|
||||||
/>
|
/>
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/jpg"
|
type="image/jpg"
|
||||||
srcset="{{ book.cover_bw_book_medium_jpg.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='medium' ext='jpg' %}"
|
||||||
/>
|
/>
|
||||||
{% elif size_mobile == 'large' %}
|
{% elif size_mobile == 'large' %}
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/webp"
|
type="image/webp"
|
||||||
srcset="{{ book.cover_bw_book_large_webp.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='large' ext='webp' %}"
|
||||||
/>
|
/>
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/jpg"
|
type="image/jpg"
|
||||||
srcset="{{ book.cover_bw_book_large_jpg.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='large' ext='jpg' %}"
|
||||||
/>
|
/>
|
||||||
{% elif size_mobile == 'xlarge' %}
|
{% elif size_mobile == 'xlarge' %}
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/webp"
|
type="image/webp"
|
||||||
srcset="{{ book.cover_bw_book_xlarge_webp.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='xlarge' ext='webp' %}"
|
||||||
/>
|
/>
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/jpg"
|
type="image/jpg"
|
||||||
srcset="{{ book.cover_bw_book_xlarge_jpg.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='xlarge' ext='jpg' %}"
|
||||||
/>
|
/>
|
||||||
{% elif size_mobile == 'xxlarge' %}
|
{% elif size_mobile == 'xxlarge' %}
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/webp"
|
type="image/webp"
|
||||||
srcset="{{ book.cover_bw_book_xxlarge_webp.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='xxlarge' ext='webp' %}"
|
||||||
/>
|
/>
|
||||||
<source
|
<source
|
||||||
media="(max-width: 768px)"
|
media="(max-width: 768px)"
|
||||||
type="image/jpg"
|
type="image/jpg"
|
||||||
srcset="{{ book.cover_bw_book_xxlarge_jpg.url }}"
|
srcset="{% get_book_cover_thumbnail book=book size='xxlarge' ext='jpg' %}"
|
||||||
/>
|
/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if size == 'xsmall' %}
|
{% if size == 'xsmall' %}
|
||||||
<source type="image/webp" srcset="{{ book.cover_bw_book_xsmall_webp.url }}"/>
|
<source
|
||||||
<source type="image/jpg" srcset="{{ book.cover_bw_book_xsmall_jpg.url }}"/>
|
type="image/webp"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='xsmall' ext='webp' %}"
|
||||||
|
/>
|
||||||
|
<source
|
||||||
|
type="image/jpg"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='xsmall' ext='jpg' %}"
|
||||||
|
/>
|
||||||
{% elif size == 'small' %}
|
{% elif size == 'small' %}
|
||||||
<source type="image/webp" srcset="{{ book.cover_bw_book_small_webp.url }}"/>
|
<source
|
||||||
<source type="image/jpg" srcset="{{ book.cover_bw_book_small_jpg.url }}"/>
|
type="image/webp"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='small' ext='webp' %}"
|
||||||
|
/>
|
||||||
|
<source
|
||||||
|
type="image/jpg"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='small' ext='jpg' %}"
|
||||||
|
/>
|
||||||
{% elif size == 'medium' %}
|
{% elif size == 'medium' %}
|
||||||
<source type="image/webp" srcset="{{ book.cover_bw_book_medium_webp.url }}"/>
|
<source
|
||||||
<source type="image/jpg" srcset="{{ book.cover_bw_book_medium_jpg.url }}"/>
|
type="image/webp"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='medium' ext='webp' %}"
|
||||||
|
/>
|
||||||
|
<source
|
||||||
|
type="image/jpg"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='medium' ext='jpg' %}"
|
||||||
|
/>
|
||||||
{% elif size == 'large' %}
|
{% elif size == 'large' %}
|
||||||
<source type="image/webp" srcset="{{ book.cover_bw_book_large_webp.url }}"/>
|
<source
|
||||||
<source type="image/jpg" srcset="{{ book.cover_bw_book_large_jpg.url }}"/>
|
type="image/webp"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='large' ext='webp' %}"
|
||||||
|
/>
|
||||||
|
<source
|
||||||
|
type="image/jpg"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='large' ext='jpg' %}"
|
||||||
|
/>
|
||||||
{% elif size == 'xlarge' %}
|
{% elif size == 'xlarge' %}
|
||||||
<source type="image/webp" srcset="{{ book.cover_bw_book_xlarge_webp.url }}"/>
|
<source
|
||||||
<source type="image/jpg" srcset="{{ book.cover_bw_book_xlarge_jpg.url }}"/>
|
type="image/webp"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='xlarge' ext='webp' %}"
|
||||||
|
/>
|
||||||
|
<source
|
||||||
|
type="image/jpg"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='xlarge' ext='jpg' %}"
|
||||||
|
/>
|
||||||
{% elif size == 'xxlarge' %}
|
{% elif size == 'xxlarge' %}
|
||||||
<source type="image/webp" srcset="{{ book.cover_bw_book_xxlarge_webp.url }}"/>
|
<source
|
||||||
<source type="image/jpg" srcset="{{ book.cover_bw_book_xxlarge_jpg.url }}"/>
|
type="image/webp"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='xxlarge' ext='webp' %}"
|
||||||
|
/>
|
||||||
|
<source
|
||||||
|
type="image/jpg"
|
||||||
|
srcset="{% get_book_cover_thumbnail book=book size='xxlarge' ext='jpg' %}"
|
||||||
|
/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<img
|
<img
|
||||||
|
alt="{{ book.alt_text|default:'' }}"
|
||||||
class="book-cover"
|
class="book-cover"
|
||||||
itemprop="thumbnailUrl"
|
itemprop="thumbnailUrl"
|
||||||
src="{% if img_path is None %}{% get_media_prefix %}{% else %}{{ img_path }}{% endif %}{{ book.cover }}"
|
src="{% if img_path is None %}{% get_media_prefix %}{% else %}{{ img_path }}{% endif %}{{ book.cover }}"
|
||||||
|
|
|
@ -3,6 +3,7 @@ import os
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from django import template
|
from django import template
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from django.templatetags.static import static
|
||||||
|
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
@ -50,3 +51,13 @@ def truncatepath(value, arg):
|
||||||
except ValueError: # invalid literal for int()
|
except ValueError: # invalid literal for int()
|
||||||
return path_list[-1] # Fail silently.
|
return path_list[-1] # Fail silently.
|
||||||
return "%s/…%s" % (path_list[0], path_list[-1][-length:])
|
return "%s/…%s" % (path_list[0], path_list[-1][-length:])
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag(takes_context=False)
|
||||||
|
def get_book_cover_thumbnail(book, size, ext):
|
||||||
|
"""Returns a book thumbnail at the specified size and extension, with fallback if needed"""
|
||||||
|
try:
|
||||||
|
cover_thumbnail = getattr(book, "cover_bw_book_%s_%s" % (size, ext))
|
||||||
|
return cover_thumbnail.url
|
||||||
|
except OSError:
|
||||||
|
return static("images/no_cover.jpg")
|
||||||
|
|
Loading…
Reference in a new issue