diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py
index 8d441e8b..a6b2774d 100644
--- a/bookwyrm/models/book.py
+++ b/bookwyrm/models/book.py
@@ -52,12 +52,29 @@ 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)
objects = InheritanceManager()
+ @property
+ def edition_info(self):
+ ''' properties of this edition, as a string '''
+ items = [
+ 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)
+
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/models/fields.py b/bookwyrm/models/fields.py
index 082ac53b..b3011e00 100644
--- a/bookwyrm/models/fields.py
+++ b/bookwyrm/models/fields.py
@@ -305,18 +305,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 '''
@@ -326,9 +330,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 %}
-
+
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 %}
{{ book.title }}
-({{ book|edition_info }})
+({{ book.edition_info }})