2021-05-11 21:41:28 +00:00
|
|
|
""" template filters for really common utilities """
|
2021-06-02 16:17:53 +00:00
|
|
|
import os
|
2021-11-01 04:48:52 +00:00
|
|
|
import re
|
2021-05-11 21:41:28 +00:00
|
|
|
from uuid import uuid4
|
2023-09-18 11:21:04 +00:00
|
|
|
from urllib.parse import urlparse
|
2021-05-11 21:41:28 +00:00
|
|
|
from django import template
|
2021-11-01 08:50:49 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2021-06-09 16:51:20 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-08-03 15:45:52 +00:00
|
|
|
from django.templatetags.static import static
|
2021-05-11 21:41:28 +00:00
|
|
|
|
2023-09-18 11:21:04 +00:00
|
|
|
from bookwyrm.models import User
|
2021-05-11 21:41:28 +00:00
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
|
|
|
|
@register.filter(name="uuid")
|
|
|
|
def get_uuid(identifier):
|
|
|
|
"""for avoiding clashing ids when there are many forms"""
|
2021-09-18 18:32:00 +00:00
|
|
|
return f"{identifier}{uuid4()}"
|
2021-05-11 21:41:28 +00:00
|
|
|
|
|
|
|
|
2021-12-30 22:07:30 +00:00
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def join(*args):
|
2023-04-04 15:12:59 +00:00
|
|
|
"""concatenate an arbitrary set of values"""
|
2021-12-30 22:07:30 +00:00
|
|
|
return "_".join(str(a) for a in args)
|
|
|
|
|
|
|
|
|
2021-05-11 21:41:28 +00:00
|
|
|
@register.filter(name="username")
|
|
|
|
def get_user_identifier(user):
|
|
|
|
"""use localname for local users, username for remote"""
|
|
|
|
return user.localname if user.localname else user.username
|
|
|
|
|
|
|
|
|
2023-09-18 11:21:04 +00:00
|
|
|
@register.filter(name="user_from_remote_id")
|
|
|
|
def get_user_identifier_from_remote_id(remote_id):
|
|
|
|
"""get the local user id from their remote id"""
|
|
|
|
user = User.objects.get(remote_id=remote_id)
|
|
|
|
return user if user else None
|
|
|
|
|
|
|
|
|
2021-05-20 15:09:35 +00:00
|
|
|
@register.filter(name="book_title")
|
2021-06-09 17:03:15 +00:00
|
|
|
def get_title(book, too_short=5):
|
2021-05-11 21:41:28 +00:00
|
|
|
"""display the subtitle if the title is short"""
|
|
|
|
if not book:
|
|
|
|
return ""
|
|
|
|
title = book.title
|
2021-06-09 17:03:15 +00:00
|
|
|
if len(title) <= too_short and book.subtitle:
|
2021-06-09 16:51:20 +00:00
|
|
|
title = _("%(title)s: %(subtitle)s") % {
|
|
|
|
"title": title,
|
2021-06-09 16:54:05 +00:00
|
|
|
"subtitle": book.subtitle,
|
2021-06-09 16:51:20 +00:00
|
|
|
}
|
2021-05-11 21:41:28 +00:00
|
|
|
return title
|
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
2021-09-29 17:59:36 +00:00
|
|
|
def comparison_bool(str1, str2, reverse=False):
|
2021-09-09 22:36:58 +00:00
|
|
|
"""idk why I need to write a tag for this, it returns a bool"""
|
2021-09-29 17:59:36 +00:00
|
|
|
if reverse:
|
|
|
|
return str1 != str2
|
2021-09-09 23:32:21 +00:00
|
|
|
return str1 == str2
|
2021-06-02 16:17:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.filter(is_safe=True)
|
|
|
|
def truncatepath(value, arg):
|
2022-07-06 17:50:50 +00:00
|
|
|
"""Truncate a path by removing all directories except the first and truncating"""
|
2021-06-02 16:17:53 +00:00
|
|
|
path = os.path.normpath(value.name)
|
|
|
|
path_list = path.split(os.sep)
|
|
|
|
try:
|
|
|
|
length = int(arg)
|
|
|
|
except ValueError: # invalid literal for int()
|
|
|
|
return path_list[-1] # Fail silently.
|
2021-09-18 18:32:00 +00:00
|
|
|
return f"{path_list[0]}/…{path_list[-1][-length:]}"
|
2021-08-03 15:45:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
2021-08-18 08:27:17 +00:00
|
|
|
def get_book_cover_thumbnail(book, size="medium", ext="jpg"):
|
2021-09-02 16:12:56 +00:00
|
|
|
"""Returns a book thumbnail at the specified size and extension,
|
2021-09-02 20:29:05 +00:00
|
|
|
with fallback if needed"""
|
2021-08-20 16:06:09 +00:00
|
|
|
if size == "":
|
|
|
|
size = "medium"
|
2021-08-03 15:45:52 +00:00
|
|
|
try:
|
2021-09-18 18:32:00 +00:00
|
|
|
cover_thumbnail = getattr(book, f"cover_bw_book_{size}_{ext}")
|
2021-08-03 15:45:52 +00:00
|
|
|
return cover_thumbnail.url
|
|
|
|
except OSError:
|
|
|
|
return static("images/no_cover.jpg")
|
2021-10-31 23:26:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.filter(name="get_isni_bio")
|
|
|
|
def get_isni_bio(existing, author):
|
|
|
|
"""Returns the isni bio string if an existing author has an isni listed"""
|
2021-11-01 05:00:34 +00:00
|
|
|
auth_isni = re.sub(r"\D", "", str(author.isni))
|
2021-10-31 23:26:17 +00:00
|
|
|
if len(existing) == 0:
|
|
|
|
return ""
|
2021-11-01 00:34:32 +00:00
|
|
|
for value in existing:
|
2021-11-21 21:49:22 +00:00
|
|
|
if hasattr(value, "bio") and auth_isni == re.sub(r"\D", "", str(value.isni)):
|
|
|
|
return mark_safe(f"Author of <em>{value.bio}</em>")
|
2021-11-01 00:39:37 +00:00
|
|
|
|
2021-11-01 00:34:32 +00:00
|
|
|
return ""
|
2021-11-01 04:48:52 +00:00
|
|
|
|
2021-11-01 05:00:34 +00:00
|
|
|
|
2021-11-01 09:04:25 +00:00
|
|
|
# pylint: disable=unused-argument
|
2021-11-01 08:50:49 +00:00
|
|
|
@register.filter(name="get_isni", needs_autoescape=True)
|
|
|
|
def get_isni(existing, author, autoescape=True):
|
|
|
|
"""Returns the isni ID if an existing author has an ISNI listing"""
|
|
|
|
auth_isni = re.sub(r"\D", "", str(author.isni))
|
|
|
|
if len(existing) == 0:
|
|
|
|
return ""
|
|
|
|
for value in existing:
|
2021-11-22 01:52:59 +00:00
|
|
|
if hasattr(value, "isni") and auth_isni == re.sub(r"\D", "", str(value.isni)):
|
2021-11-21 21:49:22 +00:00
|
|
|
isni = value.isni
|
2021-11-01 08:50:49 +00:00
|
|
|
return mark_safe(
|
|
|
|
f'<input type="text" name="isni-for-{author.id}" value="{isni}" hidden>'
|
|
|
|
)
|
|
|
|
return ""
|
2023-09-18 11:21:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def id_to_username(user_id):
|
2023-09-25 21:45:45 +00:00
|
|
|
"""given an arbitrary remote id, return the username"""
|
2023-09-25 12:05:43 +00:00
|
|
|
if user_id:
|
|
|
|
url = urlparse(user_id)
|
|
|
|
domain = url.netloc
|
|
|
|
parts = url.path.split("/")
|
|
|
|
name = parts[-1]
|
2023-09-25 21:45:45 +00:00
|
|
|
value = f"{name}@{domain}"
|
|
|
|
|
|
|
|
return value
|