Make alt text a model property

This commit is contained in:
Mouse Reeve 2020-12-17 12:30:49 -08:00
parent 2799ed68e3
commit 39dc0501a5
6 changed files with 43 additions and 46 deletions

View file

@ -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):

View file

@ -1,13 +1,13 @@
{% load bookwyrm_tags %}
<div class="cover-container is-{{ size }}">
{% if book.cover %}
<img class="book-cover" src="/images/{{ book.cover }}" alt="{% include 'snippets/cover_alt.html' with book=book %}">
<img class="book-cover" src="/images/{{ book.cover }}" alt="{{ book.alt_text }}">
{% else %}
<div class="no-cover book-cover">
<img class="book-cover" src="/static/images/no_cover.jpg" alt="No cover">
<div>
<p>{{ book.title }}</p>
<p>({{ book|edition_info }})</p>
<p>({{ book.edition_info }})</p>
</div>
</div>
{% endif %}

View file

@ -1,2 +0,0 @@
{% load bookwyrm_tags %}
'{{ book.title }}' Cover ({{ book|edition_info }})

View file

@ -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 '''

View file

@ -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)')

View file

@ -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')