2020-01-29 23:10:32 +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()
|
|
|
|
|
|
|
|
@register.filter(name='dict_key')
|
|
|
|
def dict_key(d, k):
|
2020-12-13 03:34:33 +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
|
|
|
|
2020-04-03 19:43:49 +00:00
|
|
|
@register.filter(name='rating')
|
|
|
|
def get_rating(book, user):
|
2021-01-31 18:34:25 +00:00
|
|
|
''' get the overall rating of a book '''
|
2021-02-24 19:35:19 +00:00
|
|
|
queryset = views.helpers.privacy_filter(
|
|
|
|
user, models.Review.objects.filter(book=book))
|
2021-01-31 18:34:25 +00:00
|
|
|
return queryset.aggregate(Avg('rating'))['rating__avg']
|
|
|
|
|
|
|
|
|
|
|
|
@register.filter(name='user_rating')
|
|
|
|
def get_user_rating(book, user):
|
2020-04-03 19:43:49 +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()
|
|
|
|
if rating:
|
|
|
|
return rating.rating
|
|
|
|
return 0
|
2020-01-29 23:32:43 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-02-22 00:03:05 +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
|
|
|
|
|
|
|
|
|
2020-03-07 22:50:29 +00:00
|
|
|
@register.filter(name='notification_count')
|
|
|
|
def get_notification_count(user):
|
|
|
|
''' how many UNREAD notifications are there '''
|
|
|
|
return user.notification_set.filter(read=False).count()
|
|
|
|
|
|
|
|
|
2020-03-10 00:03:59 +00:00
|
|
|
@register.filter(name='replies')
|
|
|
|
def get_replies(status):
|
2020-03-27 16:26:27 +00:00
|
|
|
''' get all direct replies to a status '''
|
|
|
|
#TODO: this limit could cause problems
|
|
|
|
return models.Status.objects.filter(
|
2020-10-08 19:40:47 +00:00
|
|
|
reply_parent=status,
|
|
|
|
deleted=False,
|
2020-03-27 16:26:27 +00:00
|
|
|
).select_subclasses().all()[:10]
|
2020-03-10 00:03:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register.filter(name='parent')
|
|
|
|
def get_parent(status):
|
2020-03-27 16:26:27 +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
|
|
|
|
|
|
|
|
2020-03-15 22:25:43 +00:00
|
|
|
@register.filter(name='liked')
|
|
|
|
def get_user_liked(user, status):
|
2020-03-27 16:26:27 +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
|
|
|
|
|
|
|
|
|
2020-04-01 22:15:21 +00:00
|
|
|
@register.filter(name='boosted')
|
|
|
|
def get_user_boosted(user, status):
|
|
|
|
''' did the given user fav a status? '''
|
|
|
|
return user.id in status.boosters.all().values_list('user', flat=True)
|
|
|
|
|
|
|
|
|
2020-03-29 23:18:04 +00:00
|
|
|
@register.filter(name='follow_request_exists')
|
|
|
|
def follow_request_exists(user, requester):
|
|
|
|
''' see if there is a pending follow request for a user '''
|
|
|
|
try:
|
|
|
|
models.UserFollowRequest.objects.filter(
|
|
|
|
user_subject=requester,
|
|
|
|
user_object=user,
|
|
|
|
).get()
|
|
|
|
return True
|
|
|
|
except models.UserFollowRequest.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-04-01 21:55:32 +00:00
|
|
|
@register.filter(name='boosted_status')
|
|
|
|
def get_boosted(boost):
|
|
|
|
''' 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-03-29 23:18:04 +00:00
|
|
|
|
2020-10-31 21:18:56 +00:00
|
|
|
@register.filter(name='book_description')
|
|
|
|
def get_book_description(book):
|
|
|
|
''' use the work's text if the book doesn't have it '''
|
|
|
|
return book.description or book.parent_work.description
|
|
|
|
|
2020-12-13 02:25:04 +00:00
|
|
|
|
2020-11-02 19:46:23 +00:00
|
|
|
@register.filter(name='uuid')
|
|
|
|
def get_uuid(identifier):
|
2020-11-05 18:48:28 +00:00
|
|
|
''' for avoiding clashing ids when there are many forms '''
|
2020-11-02 19:46:23 +00:00
|
|
|
return '%s%s' % (identifier, uuid4())
|
|
|
|
|
|
|
|
|
2020-12-31 00:33:04 +00:00
|
|
|
@register.filter(name='post_date')
|
2020-11-05 19:40:03 +00:00
|
|
|
def time_since(date):
|
|
|
|
''' concise time ago function '''
|
|
|
|
if not isinstance(date, datetime):
|
|
|
|
return ''
|
|
|
|
now = timezone.now()
|
|
|
|
delta = now - date
|
|
|
|
|
|
|
|
if date < (now - relativedelta(weeks=1)):
|
2020-12-16 17:15:26 +00:00
|
|
|
formatter = '%b %-d'
|
2020-12-13 04:02:14 +00:00
|
|
|
if date.year != now.year:
|
2020-12-16 17:15:26 +00:00
|
|
|
formatter += ' %Y'
|
|
|
|
return date.strftime(formatter)
|
2020-11-05 19:40:03 +00:00
|
|
|
delta = relativedelta(now, date)
|
|
|
|
if delta.days:
|
|
|
|
return '%dd' % delta.days
|
|
|
|
if delta.hours:
|
|
|
|
return '%dh' % delta.hours
|
|
|
|
if delta.minutes:
|
|
|
|
return '%dm' % delta.minutes
|
|
|
|
return '%ds' % delta.seconds
|
|
|
|
|
|
|
|
|
2020-12-31 00:33:04 +00:00
|
|
|
@register.filter(name='to_markdown')
|
2020-12-20 20:24:17 +00:00
|
|
|
def get_markdown(content):
|
|
|
|
''' convert markdown to html '''
|
|
|
|
if content:
|
|
|
|
return to_markdown(content)
|
|
|
|
return None
|
|
|
|
|
2020-12-31 00:33:04 +00:00
|
|
|
@register.filter(name='mentions')
|
|
|
|
def get_mentions(status, user):
|
2021-01-20 00:16:22 +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()))
|
|
|
|
return ' '.join(
|
2021-01-20 00:16:22 +00:00
|
|
|
'@' + get_user_identifier(m) for m in mentions if not m == user) + ' '
|
2020-12-31 00:33:04 +00:00
|
|
|
|
2021-01-06 23:53:09 +00:00
|
|
|
@register.filter(name='status_preview_name')
|
|
|
|
def get_status_preview_name(obj):
|
2021-01-12 04:21:32 +00:00
|
|
|
''' text snippet with book context for a status '''
|
2021-01-06 23:53:09 +00:00
|
|
|
name = obj.__class__.__name__.lower()
|
|
|
|
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)
|
|
|
|
return name
|
|
|
|
|
2021-01-30 19:43:40 +00:00
|
|
|
@register.filter(name='next_shelf')
|
|
|
|
def get_next_shelf(current_shelf):
|
2021-01-30 21:11:43 +00:00
|
|
|
''' shelf you'd use to update reading progress '''
|
2021-01-30 19:43:40 +00:00
|
|
|
if current_shelf == 'to-read':
|
|
|
|
return 'reading'
|
|
|
|
if current_shelf == 'reading':
|
|
|
|
return 'read'
|
2021-01-30 21:11:43 +00:00
|
|
|
if current_shelf == 'read':
|
|
|
|
return 'read'
|
2021-01-30 19:43:40 +00:00
|
|
|
return 'to-read'
|
|
|
|
|
2021-01-06 23:53:09 +00:00
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def related_status(notification):
|
|
|
|
''' for notifications '''
|
|
|
|
if not notification.related_status:
|
|
|
|
return None
|
|
|
|
if hasattr(notification.related_status, 'quotation'):
|
|
|
|
return notification.related_status.quotation
|
|
|
|
if hasattr(notification.related_status, 'review'):
|
|
|
|
return notification.related_status.review
|
|
|
|
if hasattr(notification.related_status, 'comment'):
|
|
|
|
return notification.related_status.comment
|
|
|
|
return notification.related_status
|
|
|
|
|
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):
|
2020-02-21 23:39:25 +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(
|
|
|
|
shelf__user=context['request'].user,
|
2020-12-16 17:15:26 +00:00
|
|
|
book__in=book.parent_work.editions.all()
|
2020-03-27 16:37:11 +00:00
|
|
|
).first()
|
2020-12-16 17:15:26 +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):
|
2020-09-21 17:25:26 +00:00
|
|
|
''' the most recent read activity '''
|
2020-04-15 12:06:23 +00:00
|
|
|
return models.ReadThrough.objects.filter(
|
|
|
|
user=user,
|
2020-11-06 00:48:15 +00:00
|
|
|
book=book
|
|
|
|
).order_by('-start_date').first()
|
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=False)
|
|
|
|
def active_read_through(book, user):
|
|
|
|
''' 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-01-30 21:11:43 +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
|