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
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from dateutil.relativedelta import relativedelta
|
2020-01-29 23:10:32 +00:00
|
|
|
from django import template
|
2021-01-31 18:34:25 +00:00
|
|
|
from django.db.models import Avg
|
2020-11-05 19:40:03 +00:00
|
|
|
from django.utils import timezone
|
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-03-08 16:49:10 +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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" get a user's rating of a book """
|
|
|
|
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-03-08 16:49:10 +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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" get all direct replies to a status """
|
|
|
|
# TODO: this limit could cause problems
|
|
|
|
return (
|
|
|
|
models.Status.objects.filter(
|
|
|
|
reply_parent=status,
|
|
|
|
deleted=False,
|
|
|
|
)
|
|
|
|
.select_subclasses()
|
|
|
|
.all()[:10]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@register.filter(name="parent")
|
2020-03-10 00:03:59 +00:00
|
|
|
def get_parent(status):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" get the reply parent for a status """
|
|
|
|
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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" did the given user fav a status? """
|
|
|
|
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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" load a boosted status. have to do this or it wont get foregin keys """
|
|
|
|
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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" for avoiding clashing ids when there are many forms """
|
|
|
|
return "%s%s" % (identifier, uuid4())
|
2020-11-02 19:46:23 +00:00
|
|
|
|
|
|
|
|
2021-03-08 16:49:10 +00:00
|
|
|
@register.filter(name="post_date")
|
2020-11-05 19:40:03 +00:00
|
|
|
def time_since(date):
|
2021-03-08 16:49:10 +00:00
|
|
|
""" concise time ago function """
|
2020-11-05 19:40:03 +00:00
|
|
|
if not isinstance(date, datetime):
|
2021-03-08 16:49:10 +00:00
|
|
|
return ""
|
2020-11-05 19:40:03 +00:00
|
|
|
now = timezone.now()
|
|
|
|
delta = now - date
|
|
|
|
|
|
|
|
if date < (now - relativedelta(weeks=1)):
|
2021-03-08 16:49:10 +00:00
|
|
|
formatter = "%b %-d"
|
2020-12-13 04:02:14 +00:00
|
|
|
if date.year != now.year:
|
2021-03-08 16:49:10 +00:00
|
|
|
formatter += " %Y"
|
2020-12-16 17:15:26 +00:00
|
|
|
return date.strftime(formatter)
|
2020-11-05 19:40:03 +00:00
|
|
|
delta = relativedelta(now, date)
|
|
|
|
if delta.days:
|
2021-03-08 16:49:10 +00:00
|
|
|
return "%dd" % delta.days
|
2020-11-05 19:40:03 +00:00
|
|
|
if delta.hours:
|
2021-03-08 16:49:10 +00:00
|
|
|
return "%dh" % delta.hours
|
2020-11-05 19:40:03 +00:00
|
|
|
if delta.minutes:
|
2021-03-08 16:49:10 +00:00
|
|
|
return "%dm" % delta.minutes
|
|
|
|
return "%ds" % delta.seconds
|
2020-11-05 19:40:03 +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-03-08 16:49:10 +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-03-08 16:49:10 +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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" shelf you'd use to update reading progress """
|
|
|
|
if current_shelf == "to-read":
|
|
|
|
return "reading"
|
|
|
|
if current_shelf == "reading":
|
|
|
|
return "read"
|
|
|
|
if current_shelf == "read":
|
|
|
|
return "read"
|
|
|
|
return "to-read"
|
|
|
|
|
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-03-08 16:49:10 +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-03-08 16:49:10 +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-03-08 16:49:10 +00:00
|
|
|
""" the most recent read activity """
|
|
|
|
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-03-08 16:49:10 +00:00
|
|
|
""" the most recent read activity """
|
|
|
|
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-03-08 16:49:10 +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
|