From 282a2a1dd49d8c21402574e853ad231fe606dfbb Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 24 May 2021 00:26:43 +0000 Subject: create posts and threads --- apioforum/forum.py | 33 ++++++++++++++++++++++++++++++++- apioforum/templates/create_thread.html | 14 ++++++++++++++ apioforum/templates/view_forum.html | 1 + apioforum/templates/view_thread.html | 6 +++++- apioforum/thread.py | 33 +++++++++++++++++++++++++++++++-- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 apioforum/templates/create_thread.html diff --git a/apioforum/forum.py b/apioforum/forum.py index c09b0e8..261da15 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -2,7 +2,8 @@ # currently there is only ever one forum however from flask import ( - Blueprint, render_template + Blueprint, render_template, request, + g, redirect, url_for ) from .db import get_db @@ -13,3 +14,33 @@ def view_forum(): db = get_db() threads = db.execute("SELECT * FROM threads ORDER BY updated DESC LIMIT 10;").fetchall() return render_template("view_forum.html",threads=threads) + +@bp.route("/create_thread",methods=("GET","POST")) +def create_thread(): + db = get_db() + if request.method == "POST": + title = request.form['title'] + content = request.form['content'] + err = None + if g.user is None: + err = "you need to be logged in to create a thread" + elif len(title.strip()) == 0 or len(content.strip()) == 0: + err = "title and content can't be empty" + + if err is None: + cur = db.cursor() + cur.execute( + "INSERT INTO threads (title,creator,created,updated) VALUES (?,?,current_timestamp,current_timestamp);", + (title,g.user) + ) + thread_id = cur.lastrowid + cur.execute( + "INSERT INTO posts (thread,created,author,content) VALUES (?,current_timestamp,?,?);", + (thread_id,g.user,content) + ) + db.commit() + return redirect(url_for('thread.view_thread',thread_id=thread_id)) + + + return render_template("create_thread.html") + diff --git a/apioforum/templates/create_thread.html b/apioforum/templates/create_thread.html new file mode 100644 index 0000000..b8d204c --- /dev/null +++ b/apioforum/templates/create_thread.html @@ -0,0 +1,14 @@ +{% extends 'base.html' %} +{% block header %} +

{% block title %}create thread{% endblock %}

+{% endblock %} + +{% block content %} +
+ + + + + +
+{% endblock %} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 2140c7a..13e35a8 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -2,6 +2,7 @@ {% block header %}

{% block title %}apio forum george etc{%endblock%}

{%endblock%} {%block content%}

the

+create thread diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index ebbcd3b..718df4e 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -9,7 +9,7 @@
{% for post in posts %}
{{post.author}} {{post.created}}
-
{{post.content}}
+
{{rendered_posts[loop.index0] | safe}}
{% else %}
there weren't
any posts
@@ -17,6 +17,10 @@
{% if g.user %}

loggedi n as {{ g.user }}

+ + + + {% else %}

not logged in

{% endif %} diff --git a/apioforum/thread.py b/apioforum/thread.py index 2276e84..638ff7f 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -1,8 +1,11 @@ # view posts in thread from flask import ( - Blueprint, render_template, abort + Blueprint, render_template, abort, request, g, redirect, + url_for, flash ) +from markdown import markdown +from markupsafe import escape from .db import get_db bp = Blueprint("thread", __name__, url_prefix="/thread") @@ -18,5 +21,31 @@ def view_thread(thread_id): "SELECT * FROM posts WHERE thread = ? ORDER BY created ASC;", (thread_id,) ).fetchall() - return render_template("view_thread.html",posts=posts,thread=thread) + rendered_posts = [markdown(escape(q['content'])) for q in posts] + return render_template("view_thread.html",posts=posts,thread=thread,thread_id=thread_id,rendered_posts=rendered_posts) +@bp.route("//create_post", methods=("POST",)) +def create_post(thread_id): + if g.user is None: + flash("you need to log in before you can post") + return redirect(url_for('thread.view_thread',thread_id=thread_id)) + else: + db = get_db() + content = request.form['content'] + thread = db.execute("SELECT * FROM threads WHERE id = ?;",(thread_id,)).fetchone() + if len(content.strip()) == 0: + flash("you cannot post an empty message") + elif not thread: + flash("that thread does not exist") + else: + db.execute( + "INSERT INTO posts (thread,author,content,created) VALUES (?,?,?,current_timestamp);", + (thread_id,g.user,content) + ) + db.execute( + "UPDATE threads SET updated = current_timestamp WHERE id = ?;", + (thread_id,) + ) + db.commit() + flash("post posted postfully") + return redirect(url_for('thread.view_thread',thread_id=thread_id)) -- cgit v1.2.3
id