From 7219926bfd51b51e9dad7012ba1dc3fe76604989 Mon Sep 17 00:00:00 2001 From: Hendrik L Date: Mon, 8 Jul 2024 15:19:49 +0200 Subject: [PATCH] update routes after post_edit template cleanup --- app/community/routes.py | 10 +- app/community/util.py | 14 +- app/post/routes.py | 352 +++------------------------------------- 3 files changed, 39 insertions(+), 337 deletions(-) diff --git a/app/community/routes.py b/app/community/routes.py index d39bfa34..0866da08 100644 --- a/app/community/routes.py +++ b/app/community/routes.py @@ -531,7 +531,7 @@ def add_discussion_post(actor): if not can_create_post(current_user, community): abort(401) 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.last_active = g.site.last_active = utcnow() db.session.commit() @@ -597,7 +597,7 @@ def add_image_post(actor): if not can_create_post(current_user, community): abort(401) 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.last_active = g.site.last_active = utcnow() db.session.commit() @@ -677,7 +677,7 @@ def add_link_post(actor): if not can_create_post(current_user, community): abort(401) 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.last_active = g.site.last_active = utcnow() db.session.commit() @@ -757,7 +757,7 @@ def add_video_post(actor): if not can_create_post(current_user, community): abort(401) 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.last_active = g.site.last_active = utcnow() db.session.commit() @@ -837,7 +837,7 @@ def add_poll_post(actor): if not can_create_post(current_user, community): abort(401) 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() community.post_count += 1 community.last_active = g.site.last_active = utcnow() diff --git a/app/community/util.py b/app/community/util.py index 2fbad9a6..6b8782ce 100644 --- a/app/community/util.py +++ b/app/community/util.py @@ -242,7 +242,7 @@ def actor_to_community(actor) -> 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.sticky = form.sticky.data post.nsfw = form.nsfw.data @@ -253,9 +253,9 @@ def save_post(form, post: Post, type: str): post.title = form.title.data post.body = form.body.data 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 - elif type == 'link': + elif type == POST_TYPE_LINK: 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.type = POST_TYPE_LINK @@ -294,7 +294,7 @@ def save_post(form, post: Post, type: str): post.image = file db.session.add(file) - elif type == 'image': + elif 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 uploaded_file = request.files['image_file'] @@ -351,7 +351,7 @@ def save_post(form, post: Post, type: str): db.session.add(file) db.session.commit() post.image_id = file.id - elif type == 'video': + elif type == POST_TYPE_VIDEO: form.video_url.data = form.video_url.data.strip() url_changed = post.id is None or form.video_url.data != post.url 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 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_html = markdown_to_html(post.body) post.type = POST_TYPE_POLL @@ -414,7 +414,7 @@ def save_post(form, post: Post, type: str): 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 - 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" WHERE post_id = :post_id'), {'post_id': post.id}) for i in range(1, 10): diff --git a/app/post/routes.py b/app/post/routes.py index 3aaf5e7a..dda6f30a 100644 --- a/app/post/routes.py +++ b/app/post/routes.py @@ -891,91 +891,25 @@ def post_reply_options(post_id: int, comment_id: int): ) -@bp.route('/post//edit', methods=['GET']) +@bp.route('/post//edit', methods=['GET', 'POST']) @login_required def post_edit(post_id: int): post = Post.query.get_or_404(post_id) 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: - return redirect(url_for('post.post_edit_link_post', post_id=post_id)) + form = CreateLinkForm() 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: - return redirect(url_for('post.post_edit_video_post', post_id=post_id)) + form = CreateVideoForm() 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: abort(404) - - -@bp.route('/post//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//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 mods = post.community.moderators() @@ -1000,10 +934,9 @@ def post_edit_image_post(post_id: int): form.language_id.choices = languages_for_form() if form.validate_on_submit(): - save_post(form, post, 'image') + save_post(form, post, post.type) post.community.last_active = utcnow() post.edited_at = utcnow() - db.session.commit() if post.url != old_url: if post.cross_posts is not None: @@ -1025,11 +958,11 @@ def post_edit_image_post(post_id: int): else: post.cross_posts.append(ncp.id) - db.session.commit() + db.session.commit() flash(_('Your changes have been saved.'), 'success') - # federate edit + # federate edit if not post.community.local_only: federate_post_update(post) federate_post_edit_to_user_followers(post) @@ -1038,259 +971,28 @@ def post_edit_image_post(post_id: int): else: form.title.data = post.title form.body.data = post.body - form.image_alt_text.data = post.image.alt_text 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) + if post.type == POST_TYPE_LINK: + form.link_url.data = post.url + elif post.type == POST_TYPE_IMAGE: + form.image_alt_text.data = post.image.alt_text + elif post.type == POST_TYPE_VIDEO: + form.video_url.data = post.url + elif post.type == POST_TYPE_POLL: + 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 - -@bp.route('/post//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//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//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()): form.sticky.render_kw = {'disabled': True} return render_template('post/post_edit.html', title=_('Edit post'), form=form, post=post,