diff options
Diffstat (limited to 'apioforum/forum.py')
-rw-r--r-- | apioforum/forum.py | 33 |
1 files changed, 32 insertions, 1 deletions
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") + |