mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-07-08 06:35:49 +00:00
29 lines
795 B
Python
29 lines
795 B
Python
''' template filters '''
|
|
from django import template
|
|
|
|
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
|
|
|
|
@register.filter(name='stars')
|
|
def stars(number):
|
|
''' turn integers into stars '''
|
|
try:
|
|
number = int(number)
|
|
except TypeError:
|
|
number = 0
|
|
return ('★' * number) + '☆' * (5 - number)
|
|
|
|
@register.filter(name='description')
|
|
def description_format(description):
|
|
''' handle the various OL description formats '''
|
|
if isinstance(description, dict) and 'value' in description:
|
|
description = description['value']
|
|
if '----------' in description:
|
|
description = description.split('----------')[0]
|
|
|
|
return description.strip()
|