From 2f947e6d0da0d0f9ed7b2d38f59805f4f665f66f Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:15:43 +1300 Subject: [PATCH] alt text on images --- app/activitypub/util.py | 4 +++- app/community/forms.py | 1 + app/community/util.py | 4 +++- app/post/routes.py | 1 + app/templates/community/add_post.html | 2 ++ app/templates/post/post_edit.html | 17 +++++++++++++++++ 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/activitypub/util.py b/app/activitypub/util.py index eadaa228..36ee3006 100644 --- a/app/activitypub/util.py +++ b/app/activitypub/util.py @@ -154,7 +154,9 @@ def post_to_activity(post: Post, community: Community): if post.type == POST_TYPE_LINK and post.url is not None: activity_data["object"]["object"]["attachment"] = {"href": post.url, "type": "Link"} if post.image_id is not None: - activity_data["object"]["object"]["image"] = {"href": post.image.source_url, "type": "Image"} + activity_data["object"]["object"]["image"] = {"href": post.image.view_url(), "type": "Image"} + if post.image.alt_text: + activity_data["object"]["object"]["image"]['altText'] = post.image.alt_text return activity_data diff --git a/app/community/forms.py b/app/community/forms.py index d40b4977..f3b7f8e3 100644 --- a/app/community/forms.py +++ b/app/community/forms.py @@ -50,6 +50,7 @@ class CreatePostForm(FlaskForm): render_kw={'placeholder': 'Text (optional)'}) link_url = StringField(_l('URL'), render_kw={'placeholder': 'https://...'}) image_title = StringField(_l('Title'), validators={Optional(), Length(min=3, max=255)}) + image_alt_text = StringField(_l('Alt text'), validators={Optional(), Length(min=3, max=255)}) image_body = TextAreaField(_l('Body'), validators={Optional(), Length(min=3, max=5000)}, render_kw={'placeholder': 'Text (optional)'}) image_file = FileField(_('Image')) diff --git a/app/community/util.py b/app/community/util.py index 5a927381..17f107a6 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -214,6 +214,8 @@ def save_post(form, post: Post): post.body = form.image_body.data post.body_html = markdown_to_html(post.body) post.type = POST_TYPE_IMAGE + alt_text = form.image_alt_text.data if form.image_alt_text.data else form.image_title.data + post.image.alt_text = alt_text uploaded_file = request.files['image_file'] if uploaded_file and uploaded_file.filename != '': if post.image_id: @@ -258,7 +260,7 @@ def save_post(form, post: Post): thumbnail_width = img.width thumbnail_height = img.height - file = File(file_path=final_place, file_name=new_filename + file_ext, alt_text=form.image_title.data, + file = File(file_path=final_place, file_name=new_filename + file_ext, alt_text=alt_text, width=img_width, height=img_height, thumbnail_width=thumbnail_width, thumbnail_height=thumbnail_height, thumbnail_path=final_place_thumbnail, source_url=final_place.replace('app/static/', f"https://{current_app.config['SERVER_NAME']}/static/")) diff --git a/app/post/routes.py b/app/post/routes.py index 87bd3421..8c584952 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -655,6 +655,7 @@ def post_edit(post_id: int): form.post_type.data = 'image' form.image_title.data = post.title form.image_body.data = post.body + form.image_alt_text.data = post.image.alt_text form.notify_author.data = post.notify_author return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, images_disabled=images_disabled, markdown_editor=True, diff --git a/app/templates/community/add_post.html b/app/templates/community/add_post.html index eed8070c..b550cd53 100644 --- a/app/templates/community/add_post.html +++ b/app/templates/community/add_post.html @@ -57,6 +57,8 @@