KankenOnline/kanken_online/forum.py
2024-10-16 13:19:56 +01:00

37 lines
1.1 KiB
Python

import datetime
from flask import Blueprint, flash, redirect, render_template, request, session, url_for
from kanken_online.auth import login_required
from kanken_online.database import get_database
blueprint = Blueprint("forum", __name__, url_prefix="/forum")
@blueprint.route("/")
def index():
# Get forum posts
db = get_database()
posts = db.execute("SELECT post.*, user.username FROM post, user WHERE user.id = post.author_id ORDER BY post.created DESC LIMIT 50").fetchall()
# Pass to the template to render
return render_template("forum/index.html", posts=posts)
@blueprint.route("/new", methods=["GET", "POST"])
@login_required
def new_post():
if request.method == "GET":
return render_template("forum/new.html")
else:
title = request.form["title"]
body = request.form["body"]
author_id = session["user_id"]
created = datetime.datetime.now()
db = get_database()
db.execute("INSERT INTO post VALUES (NULL, ?, ?, ?, ?)",
(author_id, created, title, body))
db.commit()
flash("success_new_post")
return redirect(url_for("forum.index"))