From 39dc0501a5a1abff0a1b2ff67aa4f3b1c734c745 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 17 Dec 2020 12:30:49 -0800 Subject: [PATCH] Make alt text a model property --- bookwyrm/models/book.py | 15 +++++++++++ bookwyrm/templates/snippets/book_cover.html | 4 +-- bookwyrm/templates/snippets/cover_alt.html | 2 -- bookwyrm/templatetags/bookwyrm_tags.py | 14 ----------- bookwyrm/tests/models/test_book_model.py | 26 +++++++++++++++++++ bookwyrm/tests/test_templatetags.py | 28 --------------------- 6 files changed, 43 insertions(+), 46 deletions(-) delete mode 100644 bookwyrm/templates/snippets/cover_alt.html diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 3080a115..c28f0a23 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -58,6 +58,21 @@ class Book(ActivitypubMixin, BookWyrmModel): objects = InheritanceManager() + @property + def edition_info(self): + ''' properties of this edition, as a string ''' + items = [ + self.physical_format if isinstance(self, models.Edition) else None, + self.languages[0] + ' language' if self.languages and \ + self.languages[0] != 'English' else None, + str(self.published_date.year) if self.published_date else None, + ] + return ', '.join(i for i in items if i) + + def alt_text(self): + ''' image alt test ''' + return '%s cover (%s)' % (self.title, self.edition_info) + def save(self, *args, **kwargs): ''' can't be abstract for query reasons, but you shouldn't USE it ''' if not isinstance(self, Edition) and not isinstance(self, Work): diff --git a/bookwyrm/templates/snippets/book_cover.html b/bookwyrm/templates/snippets/book_cover.html index ceeef426..6d15b37f 100644 --- a/bookwyrm/templates/snippets/book_cover.html +++ b/bookwyrm/templates/snippets/book_cover.html @@ -1,13 +1,13 @@ {% load bookwyrm_tags %}
{% if book.cover %} - {% include 'snippets/cover_alt.html' with book=book %} +{{ book.alt_text }} {% else %}
No cover

{{ book.title }}

-

({{ book|edition_info }})

+

({{ book.edition_info }})

{% endif %} diff --git a/bookwyrm/templates/snippets/cover_alt.html b/bookwyrm/templates/snippets/cover_alt.html deleted file mode 100644 index 0cccc2e1..00000000 --- a/bookwyrm/templates/snippets/cover_alt.html +++ /dev/null @@ -1,2 +0,0 @@ -{% load bookwyrm_tags %} -'{{ book.title }}' Cover ({{ book|edition_info }}) diff --git a/bookwyrm/templatetags/bookwyrm_tags.py b/bookwyrm/templatetags/bookwyrm_tags.py index e6460d58..91934d48 100644 --- a/bookwyrm/templatetags/bookwyrm_tags.py +++ b/bookwyrm/templatetags/bookwyrm_tags.py @@ -97,20 +97,6 @@ def get_boosted(boost): ).get() -@register.filter(name='edition_info') -def get_edition_info(book): - ''' paperback, French language, 1982 ''' - if not book: - return '' - items = [ - book.physical_format if isinstance(book, models.Edition) else None, - book.languages[0] + ' language' if book.languages and \ - book.languages[0] != 'English' else None, - str(book.published_date.year) if book.published_date else None, - ] - return ', '.join(i for i in items if i) - - @register.filter(name='book_description') def get_book_description(book): ''' use the work's text if the book doesn't have it ''' diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index ff56b5d8..76c9829e 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -1,5 +1,7 @@ ''' testing models ''' +from dateutil.parser import parse from django.test import TestCase +from django.utils import timezone from bookwyrm import models, settings from bookwyrm.models.book import isbn_10_to_13, isbn_13_to_10 @@ -56,3 +58,27 @@ class Book(TestCase): isbn_13 = '978-1788-16167-1' isbn_10 = isbn_13_to_10(isbn_13) self.assertEqual(isbn_10, '178816167X') + + + def test_get_edition_info(self): + ''' text slug about an edition ''' + book = models.Book.object.create(title='Test Edition') + self.assertEqual(book.edition_info, '') + + book.physical_format = 'worm' + book.save() + self.assertEqual(book.edition_info, 'worm') + + book.languages = ['English'] + book.save() + self.assertEqual(book.edition_info, 'worm') + + book.languages = ['Glorbish', 'English'] + book.save() + self.assertEqual(book.edition_info, 'worm, Glorbish language') + + book.published_date = timezone.make_aware(parse('2020')) + book.save() + self.assertEqual(book.edition_info, 'worm, Glorbish language, 2020') + self.assertEqual( + book.alt_text, 'Test Edition cover (worm, Glorbish language, 2020)') diff --git a/bookwyrm/tests/test_templatetags.py b/bookwyrm/tests/test_templatetags.py index 67d28848..6956553e 100644 --- a/bookwyrm/tests/test_templatetags.py +++ b/bookwyrm/tests/test_templatetags.py @@ -158,34 +158,6 @@ class TemplateTags(TestCase): 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')