update routes after post_edit template cleanup

This commit is contained in:
Hendrik L 2024-07-08 15:19:49 +02:00
parent a1fa74d3ba
commit 7219926bfd
3 changed files with 39 additions and 337 deletions

View file

@ -531,7 +531,7 @@ def add_discussion_post(actor):
if not can_create_post(current_user, community): if not can_create_post(current_user, community):
abort(401) abort(401)
post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1)
save_post(form, post, 'discussion') save_post(form, post, POST_TYPE_ARTICLE)
community.post_count += 1 community.post_count += 1
community.last_active = g.site.last_active = utcnow() community.last_active = g.site.last_active = utcnow()
db.session.commit() db.session.commit()
@ -597,7 +597,7 @@ def add_image_post(actor):
if not can_create_post(current_user, community): if not can_create_post(current_user, community):
abort(401) abort(401)
post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1)
save_post(form, post, 'image') save_post(form, post, POST_TYPE_IMAGE)
community.post_count += 1 community.post_count += 1
community.last_active = g.site.last_active = utcnow() community.last_active = g.site.last_active = utcnow()
db.session.commit() db.session.commit()
@ -677,7 +677,7 @@ def add_link_post(actor):
if not can_create_post(current_user, community): if not can_create_post(current_user, community):
abort(401) abort(401)
post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1)
save_post(form, post, 'link') save_post(form, post, POST_TYPE_LINK)
community.post_count += 1 community.post_count += 1
community.last_active = g.site.last_active = utcnow() community.last_active = g.site.last_active = utcnow()
db.session.commit() db.session.commit()
@ -757,7 +757,7 @@ def add_video_post(actor):
if not can_create_post(current_user, community): if not can_create_post(current_user, community):
abort(401) abort(401)
post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1)
save_post(form, post, 'video') save_post(form, post, POST_TYPE_VIDEO)
community.post_count += 1 community.post_count += 1
community.last_active = g.site.last_active = utcnow() community.last_active = g.site.last_active = utcnow()
db.session.commit() db.session.commit()
@ -837,7 +837,7 @@ def add_poll_post(actor):
if not can_create_post(current_user, community): if not can_create_post(current_user, community):
abort(401) abort(401)
post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1) post = Post(user_id=current_user.id, community_id=form.communities.data, instance_id=1)
save_post(form, post, 'poll') save_post(form, post, POST_TYPE_POLL)
poll = Poll.query.filter_by(post_id=post.id).first() poll = Poll.query.filter_by(post_id=post.id).first()
community.post_count += 1 community.post_count += 1
community.last_active = g.site.last_active = utcnow() community.last_active = g.site.last_active = utcnow()

View file

@ -242,7 +242,7 @@ def actor_to_community(actor) -> Community:
return community return community
def save_post(form, post: Post, type: str): def save_post(form, post: Post, type: int):
post.indexable = current_user.indexable post.indexable = current_user.indexable
post.sticky = form.sticky.data post.sticky = form.sticky.data
post.nsfw = form.nsfw.data post.nsfw = form.nsfw.data
@ -253,9 +253,9 @@ def save_post(form, post: Post, type: str):
post.title = form.title.data post.title = form.title.data
post.body = form.body.data post.body = form.body.data
post.body_html = markdown_to_html(post.body) post.body_html = markdown_to_html(post.body)
if type == '' or type == 'discussion': if not type or type == POST_TYPE_ARTICLE:
post.type = POST_TYPE_ARTICLE post.type = POST_TYPE_ARTICLE
elif type == 'link': elif type == POST_TYPE_LINK:
url_changed = post.id is None or form.link_url.data != post.url url_changed = post.id is None or form.link_url.data != post.url
post.url = remove_tracking_from_link(form.link_url.data.strip()) post.url = remove_tracking_from_link(form.link_url.data.strip())
post.type = POST_TYPE_LINK post.type = POST_TYPE_LINK
@ -294,7 +294,7 @@ def save_post(form, post: Post, type: str):
post.image = file post.image = file
db.session.add(file) db.session.add(file)
elif 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
uploaded_file = request.files['image_file'] uploaded_file = request.files['image_file']
@ -351,7 +351,7 @@ def save_post(form, post: Post, type: str):
db.session.add(file) db.session.add(file)
db.session.commit() db.session.commit()
post.image_id = file.id post.image_id = file.id
elif type == 'video': elif type == POST_TYPE_VIDEO:
form.video_url.data = form.video_url.data.strip() form.video_url.data = form.video_url.data.strip()
url_changed = post.id is None or form.video_url.data != post.url url_changed = post.id is None or form.video_url.data != post.url
post.url = remove_tracking_from_link(form.video_url.data.strip()) post.url = remove_tracking_from_link(form.video_url.data.strip())
@ -380,7 +380,7 @@ def save_post(form, post: Post, type: str):
post.image = file post.image = file
db.session.add(file) db.session.add(file)
elif type == 'poll': elif type == POST_TYPE_POLL:
post.body = form.title.data + '\n' + form.body.data if post.title not in form.body.data else form.body.data post.body = form.title.data + '\n' + form.body.data if post.title not in form.body.data else form.body.data
post.body_html = markdown_to_html(post.body) post.body_html = markdown_to_html(post.body)
post.type = POST_TYPE_POLL post.type = POST_TYPE_POLL
@ -414,7 +414,7 @@ def save_post(form, post: Post, type: str):
db.session.commit() db.session.commit()
# Save poll choices. NB this will delete all votes whenever a poll is edited. Partially because it's easier to code but also to stop malicious alterations to polls after people have already voted # Save poll choices. NB this will delete all votes whenever a poll is edited. Partially because it's easier to code but also to stop malicious alterations to polls after people have already voted
if type == 'poll': if type == POST_TYPE_POLL:
db.session.execute(text('DELETE FROM "poll_choice_vote" WHERE post_id = :post_id'), {'post_id': post.id}) db.session.execute(text('DELETE FROM "poll_choice_vote" WHERE post_id = :post_id'), {'post_id': post.id})
db.session.execute(text('DELETE FROM "poll_choice" WHERE post_id = :post_id'), {'post_id': post.id}) db.session.execute(text('DELETE FROM "poll_choice" WHERE post_id = :post_id'), {'post_id': post.id})
for i in range(1, 10): for i in range(1, 10):

View file

@ -891,91 +891,25 @@ def post_reply_options(post_id: int, comment_id: int):
) )
@bp.route('/post/<int:post_id>/edit', methods=['GET']) @bp.route('/post/<int:post_id>/edit', methods=['GET', 'POST'])
@login_required @login_required
def post_edit(post_id: int): def post_edit(post_id: int):
post = Post.query.get_or_404(post_id) post = Post.query.get_or_404(post_id)
if post.type == POST_TYPE_ARTICLE: if post.type == POST_TYPE_ARTICLE:
return redirect(url_for('post.post_edit_discussion_post', post_id=post_id)) form = CreateDiscussionForm()
elif post.type == POST_TYPE_LINK: elif post.type == POST_TYPE_LINK:
return redirect(url_for('post.post_edit_link_post', post_id=post_id)) form = CreateLinkForm()
elif post.type == POST_TYPE_IMAGE: elif post.type == POST_TYPE_IMAGE:
return redirect(url_for('post.post_edit_image_post', post_id=post_id)) form = CreateImageForm()
elif post.type == POST_TYPE_VIDEO: elif post.type == POST_TYPE_VIDEO:
return redirect(url_for('post.post_edit_video_post', post_id=post_id)) form = CreateVideoForm()
elif post.type == POST_TYPE_POLL: elif post.type == POST_TYPE_POLL:
return redirect(url_for('post.post_edit_poll_post', post_id=post_id)) form = CreatePollForm()
poll = Poll.query.filter_by(post_id=post_id).first()
del form.finish_in
else: else:
abort(404) abort(404)
@bp.route('/post/<int:post_id>/edit_discussion', methods=['GET', 'POST'])
@login_required
def post_edit_discussion_post(post_id: int):
post = Post.query.get_or_404(post_id)
form = CreateDiscussionForm()
del form.communities
mods = post.community.moderators()
if post.community.private_mods:
mod_list = []
else:
mod_user_ids = [mod.user_id for mod in mods]
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin():
if g.site.enable_nsfl is False:
form.nsfl.render_kw = {'disabled': True}
if post.community.nsfw:
form.nsfw.data = True
form.nsfw.render_kw = {'disabled': True}
if post.community.nsfl:
form.nsfl.data = True
form.nsfw.render_kw = {'disabled': True}
form.language_id.choices = languages_for_form()
if form.validate_on_submit():
save_post(form, post, 'discussion')
post.community.last_active = utcnow()
post.edited_at = utcnow()
db.session.commit()
flash(_('Your changes have been saved.'), 'success')
# federate edit
if not post.community.local_only:
federate_post_update(post)
federate_post_edit_to_user_followers(post)
return redirect(url_for('activitypub.post_ap', post_id=post.id))
else:
form.title.data = post.title
form.body.data = post.body
form.notify_author.data = post.notify_author
form.nsfw.data = post.nsfw
form.nsfl.data = post.nsfl
form.sticky.data = post.sticky
form.language_id.data = post.language_id
form.tags.data = tags_to_string(post)
if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()):
form.sticky.render_kw = {'disabled': True}
return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post,
markdown_editor=current_user.markdown_editor, mods=mod_list,
moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()),
menu_topics=menu_topics(), site=g.site,
inoculation=inoculation[randint(0, len(inoculation) - 1)]
)
else:
abort(401)
@bp.route('/post/<int:post_id>/edit_image', methods=['GET', 'POST'])
@login_required
def post_edit_image_post(post_id: int):
post = Post.query.get_or_404(post_id)
form = CreateImageForm()
del form.communities del form.communities
mods = post.community.moderators() mods = post.community.moderators()
@ -1000,10 +934,9 @@ def post_edit_image_post(post_id: int):
form.language_id.choices = languages_for_form() form.language_id.choices = languages_for_form()
if form.validate_on_submit(): if form.validate_on_submit():
save_post(form, post, 'image') save_post(form, post, post.type)
post.community.last_active = utcnow() post.community.last_active = utcnow()
post.edited_at = utcnow() post.edited_at = utcnow()
db.session.commit()
if post.url != old_url: if post.url != old_url:
if post.cross_posts is not None: if post.cross_posts is not None:
@ -1025,11 +958,11 @@ def post_edit_image_post(post_id: int):
else: else:
post.cross_posts.append(ncp.id) post.cross_posts.append(ncp.id)
db.session.commit() db.session.commit()
flash(_('Your changes have been saved.'), 'success') flash(_('Your changes have been saved.'), 'success')
# federate edit
# federate edit
if not post.community.local_only: if not post.community.local_only:
federate_post_update(post) federate_post_update(post)
federate_post_edit_to_user_followers(post) federate_post_edit_to_user_followers(post)
@ -1038,259 +971,28 @@ def post_edit_image_post(post_id: int):
else: else:
form.title.data = post.title form.title.data = post.title
form.body.data = post.body form.body.data = post.body
form.image_alt_text.data = post.image.alt_text
form.notify_author.data = post.notify_author form.notify_author.data = post.notify_author
form.nsfw.data = post.nsfw form.nsfw.data = post.nsfw
form.nsfl.data = post.nsfl form.nsfl.data = post.nsfl
form.sticky.data = post.sticky form.sticky.data = post.sticky
form.language_id.data = post.language_id form.language_id.data = post.language_id
form.tags.data = tags_to_string(post) form.tags.data = tags_to_string(post)
if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()): if post.type == POST_TYPE_LINK:
form.sticky.render_kw = {'disabled': True} form.link_url.data = post.url
return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post, elif post.type == POST_TYPE_IMAGE:
markdown_editor=current_user.markdown_editor, mods=mod_list, form.image_alt_text.data = post.image.alt_text
moderating_communities=moderating_communities(current_user.get_id()), elif post.type == POST_TYPE_VIDEO:
joined_communities=joined_communities(current_user.get_id()), form.video_url.data = post.url
menu_topics=menu_topics(), site=g.site, elif post.type == POST_TYPE_POLL:
inoculation=inoculation[randint(0, len(inoculation) - 1)] poll = Poll.query.filter_by(post_id=post.id).first()
) form.mode.data = poll.mode
else: form.local_only.data = poll.local_only
abort(401) i = 1
for choice in PollChoice.query.filter_by(post_id=post.id).order_by(PollChoice.sort_order).all():
form_field = getattr(form, f"choice_{i}")
form_field.data = choice.choice_text
i += 1
@bp.route('/post/<int:post_id>/edit_link', methods=['GET', 'POST'])
@login_required
def post_edit_link_post(post_id: int):
post = Post.query.get_or_404(post_id)
form = CreateLinkForm()
del form.communities
mods = post.community.moderators()
if post.community.private_mods:
mod_list = []
else:
mod_user_ids = [mod.user_id for mod in mods]
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin():
if g.site.enable_nsfl is False:
form.nsfl.render_kw = {'disabled': True}
if post.community.nsfw:
form.nsfw.data = True
form.nsfw.render_kw = {'disabled': True}
if post.community.nsfl:
form.nsfl.data = True
form.nsfw.render_kw = {'disabled': True}
old_url = post.url
form.language_id.choices = languages_for_form()
if form.validate_on_submit():
save_post(form, post, 'link')
post.community.last_active = utcnow()
post.edited_at = utcnow()
db.session.commit()
if post.url != old_url:
if post.cross_posts is not None:
old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all()
post.cross_posts.clear()
for ocp in old_cross_posts:
if ocp.cross_posts is not None:
ocp.cross_posts.remove(post.id)
new_cross_posts = Post.query.filter(Post.id != post.id, Post.url == post.url,
Post.posted_at > post.edited_at - timedelta(days=6)).all()
for ncp in new_cross_posts:
if ncp.cross_posts is None:
ncp.cross_posts = [post.id]
else:
ncp.cross_posts.append(post.id)
if post.cross_posts is None:
post.cross_posts = [ncp.id]
else:
post.cross_posts.append(ncp.id)
db.session.commit()
flash(_('Your changes have been saved.'), 'success')
# federate edit
if not post.community.local_only:
federate_post_update(post)
federate_post_edit_to_user_followers(post)
return redirect(url_for('activitypub.post_ap', post_id=post.id))
else:
form.title.data = post.title
form.body.data = post.body
form.link_url.data = post.url
form.notify_author.data = post.notify_author
form.nsfw.data = post.nsfw
form.nsfl.data = post.nsfl
form.sticky.data = post.sticky
form.language_id.data = post.language_id
form.tags.data = tags_to_string(post)
if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()):
form.sticky.render_kw = {'disabled': True}
return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post,
markdown_editor=current_user.markdown_editor, mods=mod_list,
moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()),
menu_topics=menu_topics(), site=g.site,
inoculation=inoculation[randint(0, len(inoculation) - 1)]
)
else:
abort(401)
@bp.route('/post/<int:post_id>/edit_video', methods=['GET', 'POST'])
@login_required
def post_edit_video_post(post_id: int):
post = Post.query.get_or_404(post_id)
form = CreateVideoForm()
del form.communities
mods = post.community.moderators()
if post.community.private_mods:
mod_list = []
else:
mod_user_ids = [mod.user_id for mod in mods]
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin():
if g.site.enable_nsfl is False:
form.nsfl.render_kw = {'disabled': True}
if post.community.nsfw:
form.nsfw.data = True
form.nsfw.render_kw = {'disabled': True}
if post.community.nsfl:
form.nsfl.data = True
form.nsfw.render_kw = {'disabled': True}
old_url = post.url
form.language_id.choices = languages_for_form()
if form.validate_on_submit():
save_post(form, post, 'video')
post.community.last_active = utcnow()
post.edited_at = utcnow()
db.session.commit()
if post.url != old_url:
if post.cross_posts is not None:
old_cross_posts = Post.query.filter(Post.id.in_(post.cross_posts)).all()
post.cross_posts.clear()
for ocp in old_cross_posts:
if ocp.cross_posts is not None:
ocp.cross_posts.remove(post.id)
new_cross_posts = Post.query.filter(Post.id != post.id, Post.url == post.url,
Post.posted_at > post.edited_at - timedelta(days=6)).all()
for ncp in new_cross_posts:
if ncp.cross_posts is None:
ncp.cross_posts = [post.id]
else:
ncp.cross_posts.append(post.id)
if post.cross_posts is None:
post.cross_posts = [ncp.id]
else:
post.cross_posts.append(ncp.id)
db.session.commit()
flash(_('Your changes have been saved.'), 'success')
# federate edit
if not post.community.local_only:
federate_post_update(post)
federate_post_edit_to_user_followers(post)
return redirect(url_for('activitypub.post_ap', post_id=post.id))
else:
form.title.data = post.title
form.body.data = post.body
form.video_url.data = post.url
form.notify_author.data = post.notify_author
form.nsfw.data = post.nsfw
form.nsfl.data = post.nsfl
form.sticky.data = post.sticky
form.language_id.data = post.language_id
form.tags.data = tags_to_string(post)
if not (post.community.is_moderator() or post.community.is_owner() or current_user.is_admin()):
form.sticky.render_kw = {'disabled': True}
return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post,
markdown_editor=current_user.markdown_editor, mods=mod_list,
moderating_communities=moderating_communities(current_user.get_id()),
joined_communities=joined_communities(current_user.get_id()),
menu_topics=menu_topics(), site=g.site,
inoculation=inoculation[randint(0, len(inoculation) - 1)]
)
else:
abort(401)
@bp.route('/post/<int:post_id>/edit_poll', methods=['GET', 'POST'])
@login_required
def post_edit_poll_post(post_id: int):
post = Post.query.get_or_404(post_id)
poll = Poll.query.filter_by(post_id=post_id).first()
form = CreatePollForm()
del form.communities
del form.finish_in
mods = post.community.moderators()
if post.community.private_mods:
mod_list = []
else:
mod_user_ids = [mod.user_id for mod in mods]
mod_list = User.query.filter(User.id.in_(mod_user_ids)).all()
if post.user_id == current_user.id or post.community.is_moderator() or current_user.is_admin():
if g.site.enable_nsfl is False:
form.nsfl.render_kw = {'disabled': True}
if post.community.nsfw:
form.nsfw.data = True
form.nsfw.render_kw = {'disabled': True}
if post.community.nsfl:
form.nsfl.data = True
form.nsfw.render_kw = {'disabled': True}
form.language_id.choices = languages_for_form()
if form.validate_on_submit():
save_post(form, post, 'poll')
post.community.last_active = utcnow()
post.edited_at = utcnow()
db.session.commit()
flash(_('Your changes have been saved.'), 'success')
# federate edit
if not post.community.local_only and not poll.local_only:
federate_post_update(post)
federate_post_edit_to_user_followers(post)
return redirect(url_for('activitypub.post_ap', post_id=post.id))
else:
form.title.data = post.title
form.body.data = post.body
form.notify_author.data = post.notify_author
form.nsfw.data = post.nsfw
form.nsfl.data = post.nsfl
form.sticky.data = post.sticky
form.language_id.data = post.language_id
poll = Poll.query.filter_by(post_id=post.id).first()
form.mode.data = poll.mode
form.local_only.data = poll.local_only
i = 1
for choice in PollChoice.query.filter_by(post_id=post.id).order_by(PollChoice.sort_order).all():
form_field = getattr(form, f"choice_{i}")
form_field.data = choice.choice_text
i += 1
form.tags.data = tags_to_string(post)
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, post=post, return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post,