2021-03-08 16:49:10 +00:00
|
|
|
""" template filters """
|
2020-11-02 19:46:23 +00:00
|
|
|
from uuid import uuid4
|
2020-11-05 19:40:03 +00:00
|
|
|
|
2021-04-22 18:41:58 +00:00
|
|
|
from django import template, utils
|
2021-01-31 18:34:25 +00:00
|
|
|
from django.db.models import Avg
|
2020-01-29 23:10:32 +00:00
|
|
|
|
2021-01-31 18:34:25 +00:00
|
|
|
from bookwyrm import models, views
|
2021-01-12 22:02:38 +00:00
|
|
|
from bookwyrm.views.status import to_markdown
|
2020-02-21 23:39:25 +00:00
|
|
|
|
|
|
|
|
2020-01-29 23:10:32 +00:00
|
|
|
register = template.Library()
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
@register.filter(name="dict_key")
|
2020-01-29 23:10:32 +00:00
|
|
|
def dict_key(d, k):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""Returns the given key from a dictionary."""
|
2020-01-29 23:10:32 +00:00
|
|
|
return d.get(k) or 0
|
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="rating")
|
2020-04-03 19:43:49 +00:00
|
|
|
def get_rating(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get the overall rating of a book"""
|
2021-02-24 19:35:19 +00:00
|
|
|
queryset = views.helpers.privacy_filter(
|
2021-03-08 16:49:10 +00:00
|
|
|
user, models.Review.objects.filter(book=book)
|
|
|
|
)
|
|
|
|
return queryset.aggregate(Avg("rating"))["rating__avg"]
|
2021-01-31 18:34:25 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="user_rating")
|
2021-01-31 18:34:25 +00:00
|
|
|
def get_user_rating(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get a user's rating of a book"""
|
2021-03-08 16:49:10 +00:00
|
|
|
rating = (
|
|
|
|
models.Review.objects.filter(
|
|
|
|
user=user,
|
|
|
|
book=book,
|
|
|
|
rating__isnull=False,
|
|
|
|
)
|
|
|
|
.order_by("-published_date")
|
|
|
|
.first()
|
|
|
|
)
|
2020-04-03 19:43:49 +00:00
|
|
|
if rating:
|
|
|
|
return rating.rating
|
|
|
|
return 0
|
2020-01-29 23:32:43 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="username")
|
2020-02-22 00:03:05 +00:00
|
|
|
def get_user_identifier(user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""use localname for local users, username for remote"""
|
2020-02-22 00:03:05 +00:00
|
|
|
return user.localname if user.localname else user.username
|
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="notification_count")
|
2020-03-07 22:50:29 +00:00
|
|
|
def get_notification_count(user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""how many UNREAD notifications are there"""
|
2020-03-07 22:50:29 +00:00
|
|
|
return user.notification_set.filter(read=False).count()
|
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="replies")
|
2020-03-10 00:03:59 +00:00
|
|
|
def get_replies(status):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get all direct replies to a status"""
|
2021-03-08 16:49:10 +00:00
|
|
|
# TODO: this limit could cause problems
|
2021-04-19 22:06:33 +00:00
|
|
|
return models.Status.objects.filter(
|
|
|
|
reply_parent=status,
|
|
|
|
deleted=False,
|
|
|
|
).select_subclasses()[:10]
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.filter(name="parent")
|
2020-03-10 00:03:59 +00:00
|
|
|
def get_parent(status):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get the reply parent for a status"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return (
|
|
|
|
models.Status.objects.filter(id=status.reply_parent_id)
|
|
|
|
.select_subclasses()
|
|
|
|
.get()
|
|
|
|
)
|
2020-03-10 00:03:59 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="liked")
|
2020-03-15 22:25:43 +00:00
|
|
|
def get_user_liked(user, status):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""did the given user fav a status?"""
|
2020-03-15 22:25:43 +00:00
|
|
|
try:
|
|
|
|
models.Favorite.objects.get(user=user, status=status)
|
|
|
|
return True
|
|
|
|
except models.Favorite.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="boosted")
|
2020-04-01 22:15:21 +00:00
|
|
|
def get_user_boosted(user, status):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""did the given user fav a status?"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return user.id in status.boosters.all().values_list("user", flat=True)
|
2020-04-01 22:15:21 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="follow_request_exists")
|
2020-03-29 23:18:04 +00:00
|
|
|
def follow_request_exists(user, requester):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""see if there is a pending follow request for a user"""
|
2020-03-29 23:18:04 +00:00
|
|
|
try:
|
|
|
|
models.UserFollowRequest.objects.filter(
|
|
|
|
user_subject=requester,
|
|
|
|
user_object=user,
|
|
|
|
).get()
|
|
|
|
return True
|
|
|
|
except models.UserFollowRequest.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="boosted_status")
|
2020-04-01 21:55:32 +00:00
|
|
|
def get_boosted(boost):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""load a boosted status. have to do this or it wont get foregin keys"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return (
|
|
|
|
models.Status.objects.select_subclasses()
|
|
|
|
.filter(id=boost.boosted_status.id)
|
|
|
|
.get()
|
|
|
|
)
|
2020-04-01 21:55:32 +00:00
|
|
|
|
2020-03-29 23:18:04 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="book_description")
|
2020-10-31 21:18:56 +00:00
|
|
|
def get_book_description(book):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""use the work's text if the book doesn't have it"""
|
2020-10-31 21:18:56 +00:00
|
|
|
return book.description or book.parent_work.description
|
|
|
|
|
2020-12-13 02:25:04 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="uuid")
|
2020-11-02 19:46:23 +00:00
|
|
|
def get_uuid(identifier):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""for avoiding clashing ids when there are many forms"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return "%s%s" % (identifier, uuid4())
|
2020-11-02 19:46:23 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="to_markdown")
|
2020-12-20 20:24:17 +00:00
|
|
|
def get_markdown(content):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""convert markdown to html"""
|
2020-12-20 20:24:17 +00:00
|
|
|
if content:
|
|
|
|
return to_markdown(content)
|
|
|
|
return None
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
@register.filter(name="mentions")
|
2020-12-31 00:33:04 +00:00
|
|
|
def get_mentions(status, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""people to @ in a reply: the parent and all mentions"""
|
2020-12-31 00:33:04 +00:00
|
|
|
mentions = set([status.user] + list(status.mention_users.all()))
|
2021-03-08 16:49:10 +00:00
|
|
|
return (
|
|
|
|
" ".join("@" + get_user_identifier(m) for m in mentions if not m == user) + " "
|
|
|
|
)
|
2020-12-31 00:33:04 +00:00
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
@register.filter(name="status_preview_name")
|
2021-01-06 23:53:09 +00:00
|
|
|
def get_status_preview_name(obj):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""text snippet with book context for a status"""
|
2021-01-06 23:53:09 +00:00
|
|
|
name = obj.__class__.__name__.lower()
|
2021-03-08 16:49:10 +00:00
|
|
|
if name == "review":
|
|
|
|
return "%s of <em>%s</em>" % (name, obj.book.title)
|
|
|
|
if name == "comment":
|
|
|
|
return "%s on <em>%s</em>" % (name, obj.book.title)
|
|
|
|
if name == "quotation":
|
|
|
|
return "%s from <em>%s</em>" % (name, obj.book.title)
|
2021-01-06 23:53:09 +00:00
|
|
|
return name
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
|
|
|
@register.filter(name="next_shelf")
|
2021-01-30 19:43:40 +00:00
|
|
|
def get_next_shelf(current_shelf):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""shelf you'd use to update reading progress"""
|
2021-03-08 16:49:10 +00:00
|
|
|
if current_shelf == "to-read":
|
|
|
|
return "reading"
|
|
|
|
if current_shelf == "reading":
|
|
|
|
return "read"
|
|
|
|
if current_shelf == "read":
|
|
|
|
return "read"
|
|
|
|
return "to-read"
|
|
|
|
|
2021-04-23 22:01:35 +00:00
|
|
|
|
2021-04-23 21:58:48 +00:00
|
|
|
@register.filter(name="title")
|
|
|
|
def get_title(book):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""display the subtitle if the title is short"""
|
2021-04-23 22:29:55 +00:00
|
|
|
if not book:
|
|
|
|
return ""
|
2021-04-23 21:58:48 +00:00
|
|
|
title = book.title
|
|
|
|
if len(title) < 6 and book.subtitle:
|
|
|
|
title = "{:s}: {:s}".format(title, book.subtitle)
|
|
|
|
return title
|
|
|
|
|
2021-01-30 19:43:40 +00:00
|
|
|
|
2021-01-06 23:53:09 +00:00
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def related_status(notification):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""for notifications"""
|
2021-01-06 23:53:09 +00:00
|
|
|
if not notification.related_status:
|
|
|
|
return None
|
2021-03-08 16:49:10 +00:00
|
|
|
if hasattr(notification.related_status, "quotation"):
|
2021-01-06 23:53:09 +00:00
|
|
|
return notification.related_status.quotation
|
2021-03-08 16:49:10 +00:00
|
|
|
if hasattr(notification.related_status, "review"):
|
2021-01-06 23:53:09 +00:00
|
|
|
return notification.related_status.review
|
2021-03-08 16:49:10 +00:00
|
|
|
if hasattr(notification.related_status, "comment"):
|
2021-01-06 23:53:09 +00:00
|
|
|
return notification.related_status.comment
|
|
|
|
return notification.related_status
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
|
2020-02-21 23:39:25 +00:00
|
|
|
@register.simple_tag(takes_context=True)
|
2020-11-06 00:48:15 +00:00
|
|
|
def active_shelf(context, book):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""check what shelf a user has a book on, if any"""
|
2020-03-27 16:37:11 +00:00
|
|
|
shelf = models.ShelfBook.objects.filter(
|
2021-03-08 16:49:10 +00:00
|
|
|
shelf__user=context["request"].user, book__in=book.parent_work.editions.all()
|
2020-03-27 16:37:11 +00:00
|
|
|
).first()
|
2021-03-08 16:49:10 +00:00
|
|
|
return shelf if shelf else {"book": book}
|
2020-02-21 23:39:25 +00:00
|
|
|
|
|
|
|
|
2020-04-15 12:06:23 +00:00
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def latest_read_through(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the most recent read activity"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return (
|
|
|
|
models.ReadThrough.objects.filter(user=user, book=book)
|
|
|
|
.order_by("-start_date")
|
|
|
|
.first()
|
|
|
|
)
|
2020-11-06 00:48:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def active_read_through(book, user):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""the most recent read activity"""
|
2021-03-08 16:49:10 +00:00
|
|
|
return (
|
|
|
|
models.ReadThrough.objects.filter(
|
|
|
|
user=user, book=book, finish_date__isnull=True
|
|
|
|
)
|
|
|
|
.order_by("-start_date")
|
|
|
|
.first()
|
|
|
|
)
|
2021-01-30 19:43:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def comparison_bool(str1, str2):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""idk why I need to write a tag for this, it reutrns a bool"""
|
2021-01-30 19:43:40 +00:00
|
|
|
return str1 == str2
|
2021-04-22 18:41:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def get_lang():
|
2021-04-26 16:15:42 +00:00
|
|
|
"""get current language, strip to the first two letters"""
|
2021-04-22 18:41:58 +00:00
|
|
|
language = utils.translation.get_language()
|
2021-04-22 18:48:06 +00:00
|
|
|
return language[0 : language.find("-")]
|