From 76da1c3e61b9ef4b2422b9e9ea7642cb86882227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Wed, 2 Jul 2025 16:21:55 +0300 Subject: [PATCH] add posts route --- app/__init__.py | 2 + app/routes/posts.py | 79 ++++++++++++++++++++++++++++++-- app/templates/common/macros.html | 2 +- app/templates/posts/edit.html | 18 ++++++++ 4 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 app/templates/posts/edit.html diff --git a/app/__init__.py b/app/__init__.py index d167a80..aed1421 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -77,12 +77,14 @@ def create_app(): from app.routes.users import bp as users_bp from app.routes.mod import bp as mod_bp from app.routes.api import bp as api_bp + from app.routes.posts import bp as posts_bp app.register_blueprint(app_bp) app.register_blueprint(topics_bp) app.register_blueprint(threads_bp) app.register_blueprint(users_bp) app.register_blueprint(mod_bp) app.register_blueprint(api_bp) + app.register_blueprint(posts_bp) app.config['SESSION_COOKIE_SECURE'] = True diff --git a/app/routes/posts.py b/app/routes/posts.py index 4677a21..afd9e8c 100644 --- a/app/routes/posts.py +++ b/app/routes/posts.py @@ -1,9 +1,14 @@ -from flask import Blueprint, redirect, url_for +from flask import ( + Blueprint, redirect, url_for, flash, render_template, request + ) +from .users import login_required, get_active_user from ..lib.babycode import babycode_to_html +from ..constants import InfoboxKind from ..db import db -from ..models import Posts, PostHistory +from ..models import Posts, PostHistory, Threads + +bp = Blueprint("posts", __name__, url_prefix = "/post") -bp = Blueprint("posts", __name__, url_prefix = "/posts") def create_post(thread_id, user_id, content, markup_language="babycode"): with db.transaction(): @@ -24,3 +29,71 @@ def create_post(thread_id, user_id, content, markup_language="babycode"): post.update({"current_revision_id": revision.id}) return post + +def update_post(post_id, new_content, markup_language='babycode'): + with db.transaction(): + post = Posts.find({'id': post_id}) + new_revision = PostHistory.create({ + 'post_id': post.id, + 'content': babycode_to_html(new_content), + 'is_initial_revision': False, + 'original_markup': new_content, + 'markup_language': markup_language, + }) + + post.update({'current_revision_id': new_revision.id}) + + +@bp.post("//delete") +@login_required +def delete(post_id): + post = Posts.find({'id': post_id}) + thread = Threads.find({'id': post.thread_id}) + user = get_active_user() + + if user.is_mod() or post.user_id == user.id: + post.delete() + flash("Post deleted.", InfoboxKind.INFO) + + return redirect(url_for('threads.thread', slug=thread.slug)) + + +@bp.get("//edit") +@login_required +def edit(post_id): + user = get_active_user() + q = f"{Posts.FULL_POSTS_QUERY} WHERE posts.id = ?" + editing_post = db.fetch_one(q, post_id) + if not editing_post: + return redirect(url_for('topics.all_topics')) + if editing_post['user_id'] != user.id: + return redirect(url_for('topics.all_topics')) + + thread = Threads.find({'id': editing_post['thread_id']}) + + thread_predicate = f'{Posts.FULL_POSTS_QUERY} WHERE posts.thread_id = ?' + + context_prev_q = f'{thread_predicate} AND posts.created_at < ? ORDER BY posts.created_at DESC LIMIT 2' + context_next_q = f'{thread_predicate} AND posts.created_at > ? ORDER BY posts.created_at ASC LIMIT 2' + prev_context = db.query(context_prev_q, thread.id, editing_post['created_at']) + next_context = db.query(context_next_q, thread.id, editing_post['created_at']) + + return render_template('posts/edit.html', + editing_post = editing_post, + thread = thread, + prev_context = prev_context, + next_context = next_context, + ) + + +@bp.post("//edit") +@login_required +def edit_form(post_id): + user = get_active_user() + post = Posts.find({'id': post_id}) + if post.user_id != user.id: + return redirect(url_for('topics.all_topics')) + + update_post(post.id, request.form.get('new_content', default='')) + thread = Threads.find({'id': post.thread_id}) + return redirect(url_for('threads.thread', slug=thread.slug, after=post.id, _anchor=f'post-{post.id}')) diff --git a/app/templates/common/macros.html b/app/templates/common/macros.html index 675d255..6ac0775 100644 --- a/app/templates/common/macros.html +++ b/app/templates/common/macros.html @@ -123,7 +123,7 @@ {% set show_edit = (active_user.id | string) == (post['user_id'] | string) and (not post['thread_is_locked'] or active_user.is_mod()) and not no_reply %} {% endif %} {% if show_edit %} - Edit + Edit {% endif %} {% set show_reply = true %} diff --git a/app/templates/posts/edit.html b/app/templates/posts/edit.html new file mode 100644 index 0000000..2949fe0 --- /dev/null +++ b/app/templates/posts/edit.html @@ -0,0 +1,18 @@ +{% from 'common/macros.html' import full_post %} +{% extends 'base.html' %} +{% block title %}editing a post{% endblock %} +{% block content %} +{% for post in prev_context | reverse %} + {{ full_post(post=post, no_reply=true, active_user=active_user) }} +{% endfor %} + + ↑↑↑Context↑↑↑ + +{{ full_post(post=editing_post, editing=true, no_reply=true, active_user=active_user) }} + + ↓↓↓Context↓↓↓ + +{% for post in next_context %} + {{ full_post(post=post, no_reply=true, active_user=active_user) }} +{% endfor %} +{% endblock %}