Complete basic forum functionality

This commit is contained in:
Kiril Kovachev 2024-10-16 13:19:33 +01:00
parent 2f8ab9ac96
commit 5e4f73d057
6 changed files with 74 additions and 5 deletions

View File

@ -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"))

View File

@ -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"
}

View File

@ -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": "無事に新しい投稿を作りました"
}

View File

@ -27,6 +27,7 @@
<li><a href="{{ url_for('about_page') }}">{{ localize("about") }}</a></li>
<li><a href="{{ url_for('indices.indices_page') }}">{{ localize("indices") }}</a></li>
<li><a href="{{ url_for('search.search_page') }}">{{ localize("search") }}</a></li>
<li><a href="{{ url_for('forum.index') }}">{{ localize("forum") }}</a></li>
<li><a href="{{ url_for('data_page') }}">{{ localize("data") }}</a></li>
</ul>
</nav>

View File

@ -5,5 +5,19 @@
{% endblock %}
{% block content %}
...
<form action="{{ url_for('forum.new_post') }}">
<input id="new-post-button" type="submit" value="{{ localize('new_post') }}">
</form>
<hr>
<div id="forum-container">
{% for post in posts %}
<div class="post">
<h2 class="post-title">{{ post.title }}</h2>
<span class="post-author">{{ post.username }}</span>
<p>{{ post.body }} </p>
<br>
<span class="post-timestamp">{{ post.created }}</span>
</div>
{% endfor %}
</div>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}{{ localize("new_post") }}{% endblock %}</h1>
{% endblock %}
{% block content %}
<div id="new-post-area">
<form id="post" method="post">
<input type="text" name="title" id="post-title" placeholder="{{localize('title')}}" required>
<textarea id="post-body" name="body" placeholder="{{localize('post_body')}}" required></textarea>
<button type="submit"> {{ localize("submit_post") }}</button>
</form>
</div>
{% endblock %}