From 39dc0501a5a1abff0a1b2ff67aa4f3b1c734c745 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 17 Dec 2020 12:30:49 -0800 Subject: [PATCH 1/3] 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') From 530198adea8c136d3506e59b879b0d38ae66952f Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 17 Dec 2020 12:46:05 -0800 Subject: [PATCH 2/3] Serialize alt text of images --- bookwyrm/models/book.py | 6 ++++-- bookwyrm/models/fields.py | 22 ++++++++++++++++++---- bookwyrm/models/status.py | 6 +++--- bookwyrm/models/user.py | 8 +++++++- bookwyrm/templates/snippets/avatar.html | 2 +- bookwyrm/tests/models/test_book_model.py | 2 +- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index c28f0a23..b20cb6ef 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -52,7 +52,8 @@ class Book(ActivitypubMixin, BookWyrmModel): authors = fields.ManyToManyField('Author') # preformatted authorship string for search and easier display author_text = models.CharField(max_length=255, blank=True, null=True) - cover = fields.ImageField(upload_to='covers/', blank=True, null=True) + cover = fields.ImageField( + upload_to='covers/', blank=True, null=True, alt_field='alt_text') first_published_date = fields.DateTimeField(blank=True, null=True) published_date = fields.DateTimeField(blank=True, null=True) @@ -62,13 +63,14 @@ class Book(ActivitypubMixin, BookWyrmModel): def edition_info(self): ''' properties of this edition, as a string ''' items = [ - self.physical_format if isinstance(self, models.Edition) else None, + self.physical_format, 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) + @property def alt_text(self): ''' image alt test ''' return '%s cover (%s)' % (self.title, self.edition_info) diff --git a/bookwyrm/models/fields.py b/bookwyrm/models/fields.py index 52933715..e2ccc1ba 100644 --- a/bookwyrm/models/fields.py +++ b/bookwyrm/models/fields.py @@ -295,18 +295,22 @@ class TagField(ManyToManyField): return items -def image_serializer(value): +def image_serializer(value, alt): ''' helper for serializing images ''' if value and hasattr(value, 'url'): url = value.url else: return None url = 'https://%s%s' % (DOMAIN, url) - return activitypub.Image(url=url) + return activitypub.Image(url=url, name=alt) class ImageField(ActivitypubFieldMixin, models.ImageField): ''' activitypub-aware image field ''' + def __init__(self, *args, alt_field=None, **kwargs): + self.alt_field = alt_field + super().__init__(*args, **kwargs) + # pylint: disable=arguments-differ def set_field_from_activity(self, instance, data, save=True): ''' helper function for assinging a value to the field ''' @@ -316,9 +320,19 @@ class ImageField(ActivitypubFieldMixin, models.ImageField): return getattr(instance, self.name).save(*formatted, save=save) + def set_activity_from_field(self, activity, instance): + value = getattr(instance, self.name) + if value is None: + return + alt_text = getattr(instance, self.alt_field) + formatted = self.field_to_activity(value, alt_text) - def field_to_activity(self, value): - return image_serializer(value) + key = self.get_activitypub_field() + activity[key] = formatted + + + def field_to_activity(self, value, alt=None): + return image_serializer(value, alt) def field_from_activity(self, value): diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 49fbad55..b358554c 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -86,11 +86,11 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel): activity['name'] = self.pure_name activity['type'] = self.pure_type activity['attachment'] = [ - image_serializer(b.cover) for b in self.mention_books.all() \ - if b.cover] + image_serializer(b.cover, b.alt_text) \ + for b in self.mention_books.all()[:4] if b.cover] if hasattr(self, 'book'): activity['attachment'].append( - image_serializer(self.book.cover) + image_serializer(self.book.cover, self.book.alt_text) ) return activity diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 9d66eb5c..d5db9949 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -53,7 +53,8 @@ class User(OrderedCollectionPageMixin, AbstractUser): # name is your display name, which you can change at will name = fields.CharField(max_length=100, default='') avatar = fields.ImageField( - upload_to='avatars/', blank=True, null=True, activitypub_field='icon') + upload_to='avatars/', blank=True, null=True, + activitypub_field='icon', alt_field='alt_text') followers = fields.ManyToManyField( 'self', link_only=True, @@ -90,6 +91,11 @@ class User(OrderedCollectionPageMixin, AbstractUser): last_active_date = models.DateTimeField(auto_now=True) manually_approves_followers = fields.BooleanField(default=False) + @property + def alt_text(self): + ''' alt text with username ''' + return 'avatar for %s' % (self.localname or self.username) + @property def display_name(self): ''' show the cleanest version of the user's name possible ''' diff --git a/bookwyrm/templates/snippets/avatar.html b/bookwyrm/templates/snippets/avatar.html index da4b5fd2..ca49075c 100644 --- a/bookwyrm/templates/snippets/avatar.html +++ b/bookwyrm/templates/snippets/avatar.html @@ -1,3 +1,3 @@ {% load bookwyrm_tags %} -avatar for {{ user|username }} +{{ user.alt_text }} diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index 76c9829e..e8b31084 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -62,7 +62,7 @@ class Book(TestCase): def test_get_edition_info(self): ''' text slug about an edition ''' - book = models.Book.object.create(title='Test Edition') + book = models.Book.objects.create(title='Test Edition') self.assertEqual(book.edition_info, '') book.physical_format = 'worm' From 60239c867a04e770fd7b8f542773a617e149dc87 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 17 Dec 2020 13:01:08 -0800 Subject: [PATCH 3/3] Updates unit tests --- bookwyrm/tests/models/test_book_model.py | 2 +- bookwyrm/tests/models/test_fields.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index e8b31084..a52133ea 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -62,7 +62,7 @@ class Book(TestCase): def test_get_edition_info(self): ''' text slug about an edition ''' - book = models.Book.objects.create(title='Test Edition') + book = models.Edition.objects.create(title='Test Edition') self.assertEqual(book.edition_info, '') book.physical_format = 'worm' diff --git a/bookwyrm/tests/models/test_fields.py b/bookwyrm/tests/models/test_fields.py index 81b1d528..10c674d9 100644 --- a/bookwyrm/tests/models/test_fields.py +++ b/bookwyrm/tests/models/test_fields.py @@ -393,17 +393,19 @@ class ActivitypubFields(TestCase): ContentFile(output.getvalue()) ) - output = fields.image_serializer(user.avatar) + output = fields.image_serializer(user.avatar, alt='alt text') self.assertIsNotNone( re.match( r'.*\.jpg', output.url, ) ) + self.assertEqual(output.name, 'alt text') self.assertEqual(output.type, 'Image') instance = fields.ImageField() + output = fields.image_serializer(user.avatar, alt=None) self.assertEqual(instance.field_to_activity(user.avatar), output) responses.add(