2020-01-29 23:10:32 +00:00
|
|
|
''' template filters '''
|
|
|
|
from django import template
|
|
|
|
|
2020-02-21 23:39:25 +00:00
|
|
|
from fedireads import models
|
|
|
|
|
|
|
|
|
2020-01-29 23:10:32 +00:00
|
|
|
register = template.Library()
|
|
|
|
|
|
|
|
@register.filter(name='dict_key')
|
|
|
|
def dict_key(d, k):
|
|
|
|
'''Returns the given key from a dictionary.'''
|
|
|
|
return d.get(k) or 0
|
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-01-29 23:10:32 +00:00
|
|
|
@register.filter(name='stars')
|
|
|
|
def stars(number):
|
|
|
|
''' turn integers into stars '''
|
2020-01-29 23:32:43 +00:00
|
|
|
try:
|
|
|
|
number = int(number)
|
2020-02-15 20:35:54 +00:00
|
|
|
except (ValueError, TypeError):
|
2020-01-29 23:32:43 +00:00
|
|
|
number = 0
|
2020-01-29 23:10:32 +00:00
|
|
|
return ('★' * number) + '☆' * (5 - number)
|
2020-01-29 23:32:43 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-01-29 23:32:43 +00:00
|
|
|
@register.filter(name='description')
|
|
|
|
def description_format(description):
|
|
|
|
''' handle the various OL description formats '''
|
2020-03-07 06:56:44 +00:00
|
|
|
if not description:
|
|
|
|
return ''
|
|
|
|
|
2020-01-29 23:32:43 +00:00
|
|
|
if '----------' in description:
|
|
|
|
description = description.split('----------')[0]
|
|
|
|
|
|
|
|
return description.strip()
|
2020-02-11 06:40:20 +00:00
|
|
|
|
2020-03-07 06:56:44 +00:00
|
|
|
|
2020-02-11 06:40:20 +00:00
|
|
|
@register.filter(name='author_bio')
|
|
|
|
def bio_format(bio):
|
|
|
|
''' clean up OL author bios '''
|
2020-02-20 03:03:50 +00:00
|
|
|
if isinstance(bio, dict) and 'value' in bio:
|
|
|
|
bio = bio['value']
|
2020-02-11 06:40:20 +00:00
|
|
|
bio = bio.split('\n')
|
|
|
|
return bio[0].strip()
|
2020-02-21 23:39:25 +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(
|
|
|
|
reply_parent=status
|
|
|
|
).select_subclasses().all()[:10]
|
2020-03-10 00:03:59 +00:00
|
|
|
|
|
|
|
|
2020-03-10 00:27:54 +00:00
|
|
|
@register.filter(name='reply_count')
|
|
|
|
def get_reply_count(status):
|
2020-03-27 16:26:27 +00:00
|
|
|
''' how many replies does a status have? '''
|
2020-03-10 00:27:54 +00:00
|
|
|
return models.Status.objects.filter(reply_parent=status).count()
|
|
|
|
|
|
|
|
|
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-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-02-21 23:39:25 +00:00
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
|
def shelve_button_identifier(context, book):
|
|
|
|
''' check what shelf a user has a book on, if any '''
|
2020-03-27 16:37:11 +00:00
|
|
|
#TODO: books can be on multiple shelves, handle that better
|
|
|
|
shelf = models.ShelfBook.objects.filter(
|
|
|
|
shelf__user=context['request'].user,
|
|
|
|
book=book
|
|
|
|
).first()
|
|
|
|
if not shelf:
|
2020-02-21 23:39:25 +00:00
|
|
|
return 'to-read'
|
2020-03-27 16:37:11 +00:00
|
|
|
|
2020-02-21 23:39:25 +00:00
|
|
|
identifier = shelf.shelf.identifier
|
|
|
|
if identifier == 'to-read':
|
|
|
|
return 'reading'
|
|
|
|
elif identifier == 'reading':
|
|
|
|
return 'read'
|
|
|
|
return 'to-read'
|
|
|
|
|
|
|
|
|
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
|
def shelve_button_text(context, book):
|
|
|
|
''' check what shelf a user has a book on, if any '''
|
2020-03-27 16:37:11 +00:00
|
|
|
#TODO: books can be on multiple shelves
|
|
|
|
shelf = models.ShelfBook.objects.filter(
|
|
|
|
shelf__user=context['request'].user,
|
|
|
|
book=book
|
|
|
|
).first()
|
|
|
|
if not shelf:
|
2020-02-21 23:39:25 +00:00
|
|
|
return 'Want to read'
|
2020-03-27 16:37:11 +00:00
|
|
|
|
2020-02-21 23:39:25 +00:00
|
|
|
identifier = shelf.shelf.identifier
|
2020-02-22 00:26:52 +00:00
|
|
|
if identifier == 'to-read':
|
|
|
|
return 'Start reading'
|
2020-02-21 23:39:25 +00:00
|
|
|
elif identifier == 'reading':
|
|
|
|
return 'I\'m done!'
|
|
|
|
return 'Want to read'
|
2020-02-22 00:03:05 +00:00
|
|
|
|
2020-02-23 22:26:03 +00:00
|
|
|
|
|
|
|
@register.simple_tag(takes_context=True)
|
|
|
|
def current_shelf(context, book):
|
|
|
|
''' check what shelf a user has a book on, if any '''
|
|
|
|
try:
|
|
|
|
shelf = models.ShelfBook.objects.get(
|
|
|
|
shelf__user=context['user'],
|
|
|
|
book=book
|
|
|
|
)
|
|
|
|
except models.ShelfBook.DoesNotExist:
|
|
|
|
return None
|
|
|
|
return shelf.name
|
|
|
|
|