Use trimmedwords_html instead of a custom filter

This commit is contained in:
Mouse Reeve 2020-12-12 19:34:33 -08:00
parent 1e08eeb4c2
commit b61a7cccff
3 changed files with 211 additions and 17 deletions

View file

@ -2,7 +2,7 @@
{% with 0|uuid as uuid %}
{% if full %}
{% with full|text_overflow as trimmed %}
{% with full|truncatewords_html:60 as trimmed %}
{% if trimmed != full %}
<div>
<input type="radio" name="show-hide-{{ uuid }}" id="show-{{ uuid }}" class="toggle-control" checked>

View file

@ -13,7 +13,7 @@ register = template.Library()
@register.filter(name='dict_key')
def dict_key(d, k):
'''Returns the given key from a dictionary.'''
''' Returns the given key from a dictionary. '''
return d.get(k) or 0
@ -117,21 +117,6 @@ def get_book_description(book):
return book.description or book.parent_work.description
@register.filter(name='text_overflow')
def text_overflow(text):
''' dont' let book descriptions run for ages '''
if not text:
return ''
char_max = 400
if text and len(text) < char_max:
return text
trimmed = text[:char_max]
# go back to the last space
trimmed = ' '.join(trimmed.split(' ')[:-1])
return trimmed + '...'
@register.filter(name='uuid')
def get_uuid(identifier):
''' for avoiding clashing ids when there are many forms '''

View file

@ -0,0 +1,209 @@
''' style fixes and lookups for templates '''
import re
from unittest.mock import patch
from dateutil.parser import parse
from django.test import TestCase
from django.utils import timezone
from bookwyrm import models
from bookwyrm.templatetags import bookwyrm_tags
class TemplateTags(TestCase):
''' lotta different things here '''
def setUp(self):
''' create some filler objects '''
self.user = models.User.objects.create_user(
'mouse', 'mouse@mouse.mouse', 'mouseword', local=True)
with patch('bookwyrm.models.user.set_remote_server.delay'):
self.remote_user = models.User.objects.create_user(
'rat', 'rat@rat.rat', 'ratword',
remote_id='http://example.com/rat', local=False)
self.book = models.Edition.objects.create(title='Test Book')
def test_dict_key(self):
''' just getting a value out of a dict '''
test_dict = {'a': 1, 'b': 3}
self.assertEqual(
bookwyrm_tags.dict_key(test_dict, 'a'), 1)
self.assertEqual(
bookwyrm_tags.dict_key(test_dict, 'c'), 0)
def test_get_rating(self):
''' get a user's most recent rating of a book '''
models.Review.objects.create(
user=self.user, book=self.book, rating=3)
self.assertEqual(
bookwyrm_tags.get_rating(self.book, self.user), 3)
def test_get_rating_doesnt_exist(self):
''' there is no rating available '''
self.assertEqual(
bookwyrm_tags.get_rating(self.book, self.user), 0)
def test_get_user_identifer_local(self):
''' fall back to the simplest uid available '''
self.assertNotEqual(self.user.username, self.user.localname)
self.assertEqual(
bookwyrm_tags.get_user_identifier(self.user), 'mouse')
def test_get_user_identifer_remote(self):
''' for a remote user, should be their full username '''
self.assertEqual(
bookwyrm_tags.get_user_identifier(self.remote_user),
'rat@example.com')
def test_get_notification_count(self):
''' just countin' '''
self.assertEqual(bookwyrm_tags.get_notification_count(self.user), 0)
models.Notification.objects.create(
user=self.user, notification_type='FOLLOW')
models.Notification.objects.create(
user=self.user, notification_type='FOLLOW')
models.Notification.objects.create(
user=self.remote_user, notification_type='FOLLOW')
self.assertEqual(bookwyrm_tags.get_notification_count(self.user), 2)
def test_get_replies(self):
''' direct replies to a status '''
parent = models.Review.objects.create(
user=self.user, book=self.book)
first_child = models.Status.objects.create(
reply_parent=parent, user=self.user)
second_child = models.Status.objects.create(
reply_parent=parent, user=self.user)
third_child = models.Status.objects.create(
reply_parent=parent, user=self.user, deleted=True)
replies = bookwyrm_tags.get_replies(parent)
self.assertEqual(len(replies), 2)
self.assertTrue(first_child in replies)
self.assertTrue(second_child in replies)
self.assertFalse(third_child in replies)
def test_get_parent(self):
''' get the reply parent of a status '''
parent = models.Review.objects.create(
user=self.user, book=self.book)
child = models.Status.objects.create(
reply_parent=parent, user=self.user)
result = bookwyrm_tags.get_parent(child)
self.assertEqual(result, parent)
self.assertIsInstance(result, models.Review)
def test_get_user_liked(self):
''' did a user like a status '''
status = models.Review.objects.create(
user=self.remote_user, book=self.book)
self.assertFalse(bookwyrm_tags.get_user_liked(self.user, status))
models.Favorite.objects.create(
user=self.user,
status=status
)
self.assertTrue(bookwyrm_tags.get_user_liked(self.user, status))
def test_get_user_boosted(self):
''' did a user boost a status '''
status = models.Review.objects.create(
user=self.remote_user, book=self.book)
self.assertFalse(bookwyrm_tags.get_user_boosted(self.user, status))
models.Boost.objects.create(
user=self.user,
boosted_status=status
)
self.assertTrue(bookwyrm_tags.get_user_boosted(self.user, status))
def test_follow_request_exists(self):
''' does a user want to follow '''
self.assertFalse(
bookwyrm_tags.follow_request_exists(self.user, self.remote_user))
models.UserFollowRequest.objects.create(
user_subject=self.user,
user_object=self.remote_user)
self.assertFalse(
bookwyrm_tags.follow_request_exists(self.user, self.remote_user))
self.assertTrue(
bookwyrm_tags.follow_request_exists(self.remote_user, self.user))
def test_get_boosted(self):
''' load a boosted status '''
status = models.Review.objects.create(
user=self.remote_user, book=self.book)
boost = models.Boost.objects.create(
user=self.user,
boosted_status=status
)
boosted = bookwyrm_tags.get_boosted(boost)
self.assertIsInstance(boosted, models.Review)
self.assertEqual(boosted, status)
def test_get_edition_info(self):
''' text slug about an edition '''
self.assertEqual(
bookwyrm_tags.get_edition_info(self.book), '')
self.book.physical_format = 'worm'
self.book.save()
self.assertEqual(
bookwyrm_tags.get_edition_info(self.book), 'worm')
self.book.languages = ['English']
self.book.save()
self.assertEqual(
bookwyrm_tags.get_edition_info(self.book), 'worm')
self.book.languages = ['Glorbish', 'English']
self.book.save()
self.assertEqual(
bookwyrm_tags.get_edition_info(self.book),
'worm, Glorbish language')
self.book.published_date = timezone.make_aware(parse('2020'))
self.book.save()
self.assertEqual(
bookwyrm_tags.get_edition_info(self.book),
'worm, Glorbish language, 2020')
def test_get_book_description(self):
''' grab it from the edition or the parent '''
work = models.Work.objects.create(title='Test Work')
self.book.parent_work = work
self.book.save()
self.assertIsNone(bookwyrm_tags.get_book_description(self.book))
work.description = 'hi'
work.save()
self.assertEqual(bookwyrm_tags.get_book_description(self.book), 'hi')
self.book.description = 'hello'
self.book.save()
self.assertEqual(bookwyrm_tags.get_book_description(self.book), 'hello')
def test_get_uuid(self):
''' uuid functionality '''
uuid = bookwyrm_tags.get_uuid('hi')
print(uuid)
self.assertTrue(re.match(r'hi[A-Za-z0-9\-]', uuid))