Merge pull request 'Adjusting the post editing for images to display the existing image' (#297) from JollyDevelopment/pyfedi:jollydev/edit-image-posts-uses-existing-image into main

Reviewed-on: https://codeberg.org/rimu/pyfedi/pulls/297
This commit is contained in:
rimu 2024-08-18 01:55:52 +00:00
commit 0f4863afcf
5 changed files with 86 additions and 50 deletions

View file

@ -176,6 +176,16 @@ class CreateImageForm(CreatePostForm):
return True return True
class EditImageForm(CreatePostForm):
image_alt_text = StringField(_l('Alt text'), validators=[Optional(), Length(min=3, max=1500)])
def validate(self, extra_validators=None) -> bool:
if self.communities:
community = Community.query.get(self.communities.data)
if community.is_local() and g.site.allow_local_image_posts is False:
self.communities.errors.append(_l('Images cannot be posted to local communities.'))
return True
class CreatePollForm(CreatePostForm): class CreatePollForm(CreatePostForm):
mode = SelectField(_('Mode'), validators=[DataRequired()], choices=[('single', _l('Voters choose one option')), ('multiple', _l('Voters choose many options'))], render_kw={'class': 'form-select'}) mode = SelectField(_('Mode'), validators=[DataRequired()], choices=[('single', _l('Voters choose one option')), ('multiple', _l('Voters choose many options'))], render_kw={'class': 'form-select'})

View file

@ -614,8 +614,12 @@ def add_post(actor, type):
if community.posting_warning: if community.posting_warning:
flash(community.posting_warning) flash(community.posting_warning)
# empty post to pass since add_post.html extends edit_post.html
# and that one checks for a post.image_id for editing image posts
post = None
return render_template('community/add_post.html', title=_('Add post to community'), form=form, return render_template('community/add_post.html', title=_('Add post to community'), form=form,
post_type=post_type, community=community, post_type=post_type, community=community, post=post,
markdown_editor=current_user.markdown_editor, low_bandwidth=False, actor=actor, markdown_editor=current_user.markdown_editor, low_bandwidth=False, actor=actor,
moderating_communities=moderating_communities(current_user.get_id()), moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.id), joined_communities=joined_communities(current_user.id),

View file

@ -301,6 +301,10 @@ def save_post(form, post: Post, type: int):
elif type == POST_TYPE_IMAGE: elif type == POST_TYPE_IMAGE:
post.type = POST_TYPE_IMAGE post.type = POST_TYPE_IMAGE
alt_text = form.image_alt_text.data if form.image_alt_text.data else form.title.data alt_text = form.image_alt_text.data if form.image_alt_text.data else form.title.data
if post.image_id is not None:
# editing an existing image post, dont try an upload
pass
else:
uploaded_file = request.files['image_file'] uploaded_file = request.files['image_file']
if uploaded_file and uploaded_file.filename != '': if uploaded_file and uploaded_file.filename != '':
if post.image_id: if post.image_id:

View file

@ -14,7 +14,7 @@ from app.activitypub.util import notify_about_post_reply, inform_followers_of_po
from app.community.util import save_post, send_to_remote_instance from app.community.util import save_post, send_to_remote_instance
from app.inoculation import inoculation from app.inoculation import inoculation
from app.post.forms import NewReplyForm, ReportPostForm, MeaCulpaForm from app.post.forms import NewReplyForm, ReportPostForm, MeaCulpaForm
from app.community.forms import CreateLinkForm, CreateImageForm, CreateDiscussionForm, CreateVideoForm, CreatePollForm from app.community.forms import CreateLinkForm, CreateImageForm, CreateDiscussionForm, CreateVideoForm, CreatePollForm, EditImageForm
from app.post.util import post_replies, get_comment_branch, post_reply_count, tags_to_string, url_needs_archive, \ from app.post.util import post_replies, get_comment_branch, post_reply_count, tags_to_string, url_needs_archive, \
generate_archive_link, body_has_no_archive_link generate_archive_link, body_has_no_archive_link
from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_LINK, \ from app.constants import SUBSCRIPTION_MEMBER, SUBSCRIPTION_OWNER, SUBSCRIPTION_MODERATOR, POST_TYPE_LINK, \
@ -898,7 +898,7 @@ def post_edit(post_id: int):
elif post.type == POST_TYPE_LINK: elif post.type == POST_TYPE_LINK:
form = CreateLinkForm() form = CreateLinkForm()
elif post.type == POST_TYPE_IMAGE: elif post.type == POST_TYPE_IMAGE:
form = CreateImageForm() form = EditImageForm()
elif post.type == POST_TYPE_VIDEO: elif post.type == POST_TYPE_VIDEO:
form = CreateVideoForm() form = CreateVideoForm()
elif post.type == POST_TYPE_POLL: elif post.type == POST_TYPE_POLL:
@ -978,6 +978,7 @@ def post_edit(post_id: int):
if post.type == POST_TYPE_LINK: if post.type == POST_TYPE_LINK:
form.link_url.data = post.url form.link_url.data = post.url
elif post.type == POST_TYPE_IMAGE: elif post.type == POST_TYPE_IMAGE:
# existing_image = True
form.image_alt_text.data = post.image.alt_text form.image_alt_text.data = post.image.alt_text
elif post.type == POST_TYPE_VIDEO: elif post.type == POST_TYPE_VIDEO:
form.video_url.data = post.url form.video_url.data = post.url
@ -994,7 +995,7 @@ def post_edit(post_id: int):
if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()):
form.sticky.render_kw = {'disabled': True} form.sticky.render_kw = {'disabled': True}
return render_template('post/post_edit.html', title=_('Edit post'), form=form, return render_template('post/post_edit.html', title=_('Edit post'), form=form,
post_type=post.type, community=post.community, post_type=post.type, community=post.community, post=post,
markdown_editor=current_user.markdown_editor, mods=mod_list, markdown_editor=current_user.markdown_editor, mods=mod_list,
moderating_communities=moderating_communities(current_user.get_id()), moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()), joined_communities=joined_communities(current_user.get_id()),

View file

@ -17,7 +17,24 @@
{{ render_field(form.link_url) }} {{ render_field(form.link_url) }}
<div id="urlUsed"></div> <div id="urlUsed"></div>
{% elif post_type == POST_TYPE_IMAGE %} {% elif post_type == POST_TYPE_IMAGE %}
{% if post.image_id -%}
<div class="post_image">
{% if low_bandwidth -%}
<a href="{{ post.image.view_url(resize=True) }}" rel="nofollow ugc"><img src="{{ post.image.medium_url() }}"
alt="{{ post.image.alt_text if post.image.alt_text else post.title }}" fetchpriority="high" referrerpolicy="same-origin"
width="{{ post.image.width }}" height="{{ post.image.height }}" /></a>
{% else -%}
<a href="{{ post.image.view_url() }}" rel="nofollow ugc">
<img src="{{ post.image.view_url(resize=True) }}" lowsrc="{{ post.image.medium_url() }}"
sizes="(max-width: 512px) 100vw, 854px" srcset="{{ post.image.medium_url() }} 512w, {{ post.image.view_url(resize=True) }} 1024w"
alt="{{ post.image.alt_text if post.image.alt_text else post.title }}"
fetchpriority="high" referrerpolicy="same-origin" >
</a>
{% endif -%}
</div>
{% else %}
{{ render_field(form.image_file) }} {{ render_field(form.image_file) }}
{% endif %}
{{ render_field(form.image_alt_text) }} {{ render_field(form.image_alt_text) }}
<small class="field_hint">{{ _('Describe the image, to help visually impaired people.') }}</small> <small class="field_hint">{{ _('Describe the image, to help visually impaired people.') }}</small>
{% elif post_type == POST_TYPE_VIDEO %} {% elif post_type == POST_TYPE_VIDEO %}