diff options
author | ubq323 <ubq323> | 2021-06-07 23:39:39 +0000 |
---|---|---|
committer | ubq323 <ubq323> | 2021-06-07 23:39:39 +0000 |
commit | d4211837747e0fda14cb278c10aad9f7e05e076c (patch) | |
tree | 545af927661035a405e6ed9adc8bba1afe990fb3 | |
parent | 5fd96e6fc69a1ab488630cf4e693efcb7683c4b8 (diff) |
edit post
-rw-r--r-- | apioforum/templates/common.html | 4 | ||||
-rw-r--r-- | apioforum/templates/edit_post.html | 5 | ||||
-rw-r--r-- | 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 %} <span class="post-heading-b"> <a class="actionbutton" - href="{{url_for('thread.edit_post',post_id=post.id,thread_id=thread_id)}}"> + href="{{url_for('thread.edit_post',post_id=post.id)}}"> edit </a> <a class="actionbutton" - href="{{url_for('thread.delete_post',post_id=post.id,thread_id=thread_id)}}"> + href="{{url_for('thread.delete_post',post_id=post.id)}}"> delete </a> </span> 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 %} <form method="post"> -<textarea name="newcontent"> - the -</textarea> +<textarea name="newcontent">{{post.content}}</textarea> <input type="submit" value="go"> </form> +{% 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("/<int:thread_id>/delete_post/<int:post_id>", methods=["GET","POST"]) -def delete_post(thread_id, post_id): - return f"todo (t: {thread_id}, p: {post_id})" +@bp.route("/delete_post/<int:post_id>", methods=["GET","POST"]) +def delete_post(post_id): + return f"todo (p: {post_id})" -@bp.route("/<int:thread_id>/edit_post/<int:post_id>",methods=["GET","POST"]) -def edit_post(thread_id, post_id): - if request.method == "GET": - return render_templace("edit_post.html") +@bp.route("/edit_post/<int:post_id>",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) + + |