Serialize alt text of images
This commit is contained in:
parent
39dc0501a5
commit
530198adea
@ -52,7 +52,8 @@ class Book(ActivitypubMixin, BookWyrmModel):
|
|||||||
authors = fields.ManyToManyField('Author')
|
authors = fields.ManyToManyField('Author')
|
||||||
# preformatted authorship string for search and easier display
|
# preformatted authorship string for search and easier display
|
||||||
author_text = models.CharField(max_length=255, blank=True, null=True)
|
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)
|
first_published_date = fields.DateTimeField(blank=True, null=True)
|
||||||
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):
|
def edition_info(self):
|
||||||
''' properties of this edition, as a string '''
|
''' properties of this edition, as a string '''
|
||||||
items = [
|
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] + ' language' if self.languages and \
|
||||||
self.languages[0] != 'English' else None,
|
self.languages[0] != 'English' else None,
|
||||||
str(self.published_date.year) if self.published_date else None,
|
str(self.published_date.year) if self.published_date else None,
|
||||||
]
|
]
|
||||||
return ', '.join(i for i in items if i)
|
return ', '.join(i for i in items if i)
|
||||||
|
|
||||||
|
@property
|
||||||
def alt_text(self):
|
def alt_text(self):
|
||||||
''' image alt test '''
|
''' image alt test '''
|
||||||
return '%s cover (%s)' % (self.title, self.edition_info)
|
return '%s cover (%s)' % (self.title, self.edition_info)
|
||||||
|
@ -295,18 +295,22 @@ class TagField(ManyToManyField):
|
|||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
def image_serializer(value):
|
def image_serializer(value, alt):
|
||||||
''' helper for serializing images '''
|
''' helper for serializing images '''
|
||||||
if value and hasattr(value, 'url'):
|
if value and hasattr(value, 'url'):
|
||||||
url = value.url
|
url = value.url
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
url = 'https://%s%s' % (DOMAIN, url)
|
url = 'https://%s%s' % (DOMAIN, url)
|
||||||
return activitypub.Image(url=url)
|
return activitypub.Image(url=url, name=alt)
|
||||||
|
|
||||||
|
|
||||||
class ImageField(ActivitypubFieldMixin, models.ImageField):
|
class ImageField(ActivitypubFieldMixin, models.ImageField):
|
||||||
''' activitypub-aware image field '''
|
''' 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
|
# pylint: disable=arguments-differ
|
||||||
def set_field_from_activity(self, instance, data, save=True):
|
def set_field_from_activity(self, instance, data, save=True):
|
||||||
''' helper function for assinging a value to the field '''
|
''' helper function for assinging a value to the field '''
|
||||||
@ -316,9 +320,19 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
|
|||||||
return
|
return
|
||||||
getattr(instance, self.name).save(*formatted, save=save)
|
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):
|
key = self.get_activitypub_field()
|
||||||
return image_serializer(value)
|
activity[key] = formatted
|
||||||
|
|
||||||
|
|
||||||
|
def field_to_activity(self, value, alt=None):
|
||||||
|
return image_serializer(value, alt)
|
||||||
|
|
||||||
|
|
||||||
def field_from_activity(self, value):
|
def field_from_activity(self, value):
|
||||||
|
@ -86,11 +86,11 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
|||||||
activity['name'] = self.pure_name
|
activity['name'] = self.pure_name
|
||||||
activity['type'] = self.pure_type
|
activity['type'] = self.pure_type
|
||||||
activity['attachment'] = [
|
activity['attachment'] = [
|
||||||
image_serializer(b.cover) for b in self.mention_books.all() \
|
image_serializer(b.cover, b.alt_text) \
|
||||||
if b.cover]
|
for b in self.mention_books.all()[:4] if b.cover]
|
||||||
if hasattr(self, 'book'):
|
if hasattr(self, 'book'):
|
||||||
activity['attachment'].append(
|
activity['attachment'].append(
|
||||||
image_serializer(self.book.cover)
|
image_serializer(self.book.cover, self.book.alt_text)
|
||||||
)
|
)
|
||||||
return activity
|
return activity
|
||||||
|
|
||||||
|
@ -53,7 +53,8 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
|||||||
# name is your display name, which you can change at will
|
# name is your display name, which you can change at will
|
||||||
name = fields.CharField(max_length=100, default='')
|
name = fields.CharField(max_length=100, default='')
|
||||||
avatar = fields.ImageField(
|
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(
|
followers = fields.ManyToManyField(
|
||||||
'self',
|
'self',
|
||||||
link_only=True,
|
link_only=True,
|
||||||
@ -90,6 +91,11 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
|||||||
last_active_date = models.DateTimeField(auto_now=True)
|
last_active_date = models.DateTimeField(auto_now=True)
|
||||||
manually_approves_followers = fields.BooleanField(default=False)
|
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
|
@property
|
||||||
def display_name(self):
|
def display_name(self):
|
||||||
''' show the cleanest version of the user's name possible '''
|
''' show the cleanest version of the user's name possible '''
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
{% load bookwyrm_tags %}
|
{% load bookwyrm_tags %}
|
||||||
<img class="avatar image {% if large %}is-96x96{% else %}is-32x32{% endif %}" src="{% if user.avatar %}/images/{{ user.avatar }}{% else %}/static/images/default_avi.jpg{% endif %}" alt="avatar for {{ user|username }}">
|
<img class="avatar image {% if large %}is-96x96{% else %}is-32x32{% endif %}" src="{% if user.avatar %}/images/{{ user.avatar }}{% else %}/static/images/default_avi.jpg{% endif %}" alt="{{ user.alt_text }}">
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ class Book(TestCase):
|
|||||||
|
|
||||||
def test_get_edition_info(self):
|
def test_get_edition_info(self):
|
||||||
''' text slug about an edition '''
|
''' 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, '')
|
self.assertEqual(book.edition_info, '')
|
||||||
|
|
||||||
book.physical_format = 'worm'
|
book.physical_format = 'worm'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user