From 5e4f73d057dfa26ef374837ad5ec50e105281871 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Wed, 16 Oct 2024 13:19:33 +0100 Subject: [PATCH] Complete basic forum functionality --- kanken_online/forum.py | 33 ++++++++++++++++++++++-- kanken_online/static/lang/en.json | 7 ++++- kanken_online/static/lang/ja.json | 7 ++++- kanken_online/templates/base.html | 1 + kanken_online/templates/forum/index.html | 16 +++++++++++- kanken_online/templates/forum/new.html | 15 +++++++++++ 6 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 kanken_online/templates/forum/new.html diff --git a/kanken_online/forum.py b/kanken_online/forum.py index 4fae4d3..5a43f81 100644 --- a/kanken_online/forum.py +++ b/kanken_online/forum.py @@ -1,8 +1,37 @@ -from flask import Blueprint, render_template +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(): - return render_template("forum/index.html") + # 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")) \ No newline at end of file diff --git a/kanken_online/static/lang/en.json b/kanken_online/static/lang/en.json index 3b657e5..ea1c937 100644 --- a/kanken_online/static/lang/en.json +++ b/kanken_online/static/lang/en.json @@ -36,5 +36,10 @@ "kanji_tsv_download": "Download kanji TSV", "kotoba_tsv_download": "Download kotoba TSV", "ankipkg_download": "Download Anki package", -"database_download": "Download database" +"database_download": "Download database", +"new_post": "New post", +"submit_post": "Submit post", +"title": "Title", +"post_body": "Post body", +"success_new_post": "Successfully created new post" } \ No newline at end of file diff --git a/kanken_online/static/lang/ja.json b/kanken_online/static/lang/ja.json index 3f00449..bce93ff 100644 --- a/kanken_online/static/lang/ja.json +++ b/kanken_online/static/lang/ja.json @@ -36,5 +36,10 @@ "kanji_tsv_download": "漢字TSVをダウンロード", "kotoba_tsv_download": "言葉TSVをダウンロード", "ankipkg_download": "APKGをダウンロード", -"database_download": "データーベースをダウンロード" +"database_download": "データーベースをダウンロード", +"new_post": "新しい投稿", +"submit_post": "投稿する", +"title": "話題", +"post_body": "投稿内容", +"success_new_post": "無事に新しい投稿を作りました" } \ No newline at end of file diff --git a/kanken_online/templates/base.html b/kanken_online/templates/base.html index 6fbf042..6a9ff0d 100644 --- a/kanken_online/templates/base.html +++ b/kanken_online/templates/base.html @@ -27,6 +27,7 @@
  • {{ localize("about") }}
  • {{ localize("indices") }}
  • {{ localize("search") }}
  • +
  • {{ localize("forum") }}
  • {{ localize("data") }}
  • diff --git a/kanken_online/templates/forum/index.html b/kanken_online/templates/forum/index.html index 8e33168..9523ba6 100644 --- a/kanken_online/templates/forum/index.html +++ b/kanken_online/templates/forum/index.html @@ -5,5 +5,19 @@ {% endblock %} {% block content %} - ... +
    + +
    +
    +
    + {% for post in posts %} +
    +

    {{ post.title }}

    + +

    {{ post.body }}

    +
    + +
    + {% endfor %} +
    {% endblock %} \ No newline at end of file diff --git a/kanken_online/templates/forum/new.html b/kanken_online/templates/forum/new.html new file mode 100644 index 0000000..f8c3a70 --- /dev/null +++ b/kanken_online/templates/forum/new.html @@ -0,0 +1,15 @@ +{% extends 'base.html' %} + +{% block header %} +

    {% block title %}{{ localize("new_post") }}{% endblock %}

    +{% endblock %} + +{% block content %} +
    +
    + + + +
    +
    +{% endblock %} \ No newline at end of file