From d4211837747e0fda14cb278c10aad9f7e05e076c Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 7 Jun 2021 23:39:39 +0000 Subject: edit post --- apioforum/templates/common.html | 4 ++-- apioforum/templates/edit_post.html | 5 ++--- apioforum/thread.py | 41 +++++++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index a547f73..0baa1f5 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -7,11 +7,11 @@ {% if buttons and post.author == g.user %} + href="{{url_for('thread.edit_post',post_id=post.id)}}"> edit + href="{{url_for('thread.delete_post',post_id=post.id)}}"> delete diff --git a/apioforum/templates/edit_post.html b/apioforum/templates/edit_post.html index 00673b8..d8eac58 100644 --- a/apioforum/templates/edit_post.html +++ b/apioforum/templates/edit_post.html @@ -6,8 +6,7 @@ {% block content %}
- +
+{% endblock %} diff --git a/apioforum/thread.py b/apioforum/thread.py index 61066a1..f0cdeaf 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -50,11 +50,38 @@ def create_post(thread_id): flash("post posted postfully") return redirect(url_for('thread.view_thread',thread_id=thread_id)) -@bp.route("//delete_post/", methods=["GET","POST"]) -def delete_post(thread_id, post_id): - return f"todo (t: {thread_id}, p: {post_id})" +@bp.route("/delete_post/", methods=["GET","POST"]) +def delete_post(post_id): + return f"todo (p: {post_id})" -@bp.route("//edit_post/",methods=["GET","POST"]) -def edit_post(thread_id, post_id): - if request.method == "GET": - return render_templace("edit_post.html") +@bp.route("/edit_post/",methods=["GET","POST"]) +def edit_post(post_id): + db = get_db() + post = db.execute("SELECT * FROM posts WHERE id = ?",(post_id,)).fetchone() + if post is None: + flash("that post doesn't exist") + # todo: index route + return redirect("/") + + if post['author'] != g.user: + flash("you can only edit posts that you created") + return redirect(url_for("thread.view_thread",thread_id=post['thread'])) + # note: i am writing this while i am very tired, so probably + # come back and test this properly later + if request.method == "POST": + err = None + newcontent = request.form['newcontent'] + if len(newcontent.strip()) == 0: + err="post contents can't be empty" + print(err) + if err is None: + print("a") + db.execute("UPDATE posts SET content = ? WHERE id = ?",(newcontent,post_id)) + db.commit() + flash("post edited editiously") + return redirect(url_for("thread.view_thread",thread_id=post['thread'])) + else: + flash(err) + return render_template("edit_post.html",post=post) + + -- cgit v1.2.3