diff options
-rw-r--r-- | apioforum/db.py | 27 | ||||
-rw-r--r-- | apioforum/forum.py | 26 | ||||
-rw-r--r-- | apioforum/static/style.css | 24 | ||||
-rw-r--r-- | apioforum/templates/base.html | 2 | ||||
-rw-r--r-- | apioforum/templates/create_thread.html | 2 | ||||
-rw-r--r-- | apioforum/templates/view_forum.html | 7 | ||||
-rw-r--r-- | apioforum/templates/view_thread.html | 8 |
7 files changed, 78 insertions, 18 deletions
diff --git a/apioforum/db.py b/apioforum/db.py index e5159db..76aacc2 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -84,6 +84,33 @@ CREATE TABLE thread_tags ( ALTER TABLE users ADD COLUMN bio TEXT; ALTER TABLE users ADD COLUMN joined TIMESTAMP; """, +""" +CREATE TABLE forums ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + parent INTEGER REFERENCES forums(id), + description TEXT +); +INSERT INTO forums (name,parent,description) values ('root',null,'the default root forum'); + +PRAGMA foreign_keys = off; +BEGIN TRANSACTION; +CREATE TABLE threads_new ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL, + creator TEXT NOT NULL, + created TIMESTAMP NOT NULL, + updated TIMESTAMP NOT NULL, + forum NOT NULL REFERENCES forums(id) +); +INSERT INTO threads_new (id,title,creator,created,updated,forum) + SELECT id,title,creator,created,updated,1 FROM threads; +DROP TABLE threads; +ALTER TABLE threads_new RENAME TO threads; +COMMIT; +PRAGMA foreign_keys = on; +""", + ] def init_db(): diff --git a/apioforum/forum.py b/apioforum/forum.py index fa6fcc8..09d3166 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -11,20 +11,27 @@ from .mdrender import render from sqlite3 import OperationalError -bp = Blueprint("forum", __name__, url_prefix="/") +from sqlite3 import OperationalError +bp = Blueprint("forum", __name__, url_prefix="/") @bp.route("/") -def view_forum(): +def not_actual_index(): + return redirect("/1") + +@bp.route("/<int:forum_id>") +def view_forum(forum_id): db = get_db() + forum = db.execute("SELECT * FROM forums WHERE id = ?",(forum_id,)).fetchone() threads = db.execute( """SELECT threads.id, threads.title, threads.creator, threads.created, threads.updated, count(posts.id) as num_replies, max(posts.id), posts.author as last_user FROM threads INNER JOIN posts ON posts.thread = threads.id + WHERE threads.forum = ? GROUP BY threads.id ORDER BY threads.updated DESC; - """).fetchall() + """,(forum_id,)).fetchall() thread_tags = {} preview_post = {} #todo: somehow optimise this @@ -40,14 +47,19 @@ def view_forum(): ORDER BY created DESC; """,(thread['id'],)).fetchone() return render_template("view_forum.html", + forum=forum, threads=threads, thread_tags=thread_tags, preview_post=preview_post ) -@bp.route("/create_thread",methods=("GET","POST")) -def create_thread(): +@bp.route("/<int:forum_id>/create_thread",methods=("GET","POST")) +def create_thread(forum_id): db = get_db() + forum = db.execute("SELECT * FROM forums WHERE id = ?",(forum_id,)).fetchone() + if forum is None: + flash("that forum doesn't exist") + return redirect(url_for('index')) if g.user is None: flash("you need to be logged in to create a thread") @@ -63,8 +75,8 @@ def create_thread(): if err is None: cur = db.cursor() cur.execute( - "INSERT INTO threads (title,creator,created,updated) VALUES (?,?,current_timestamp,current_timestamp);", - (title,g.user) + "INSERT INTO threads (title,creator,created,updated,forum) VALUES (?,?,current_timestamp,current_timestamp,?);", + (title,g.user,forum_id) ) thread_id = cur.lastrowid cur.execute( diff --git a/apioforum/static/style.css b/apioforum/static/style.css index da580bf..b456e26 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -79,11 +79,11 @@ dt { font-weight: bold } img { max-width: 100% } -nav { float: right; padding: 5px; margin: 2px; border: 1px solid black; display:flex; align-items: center; flex-wrap: wrap } -nav p { margin-left: 15px; margin-top: 0; margin-bottom: 0; margin-right: 10px; padding: 0 } -nav p:first-of-type { margin-left:0.5em } -nav a { color: blue; text-decoration: none } -nav .links { display: flex; } +nav#navbar { float: right; padding: 5px; margin: 2px; border: 1px solid black; display:flex; align-items: center; flex-wrap: wrap } +nav#navbar p { margin-left: 15px; margin-top: 0; margin-bottom: 0; margin-right: 10px; padding: 0 } +nav#navbar p:first-of-type { margin-left:0.5em } +nav#navbar a { color: blue; text-decoration: none } +nav#navbar .links { display: flex; } /* todo: make the navbar less bad */ .flashmsg { border: 1px solid black; background-color: yellow; max-width: max-content; padding: 5px; clear: both;} @@ -208,3 +208,17 @@ blockquote { padding: 1px 3px; border: 1px solid black; } + +.breadcrumbs { + list-style: none; +} + +.breadcrumbs li { + display: inline; +} + +.breadcrumbs li+li::before { + content: "/\00a0"; + padding: 8px; +} + diff --git a/apioforum/templates/base.html b/apioforum/templates/base.html index 637cc09..f462df2 100644 --- a/apioforum/templates/base.html +++ b/apioforum/templates/base.html @@ -9,7 +9,7 @@ <link rel="icon" href="//gh0.pw/favicon.ico"> </head> <body> - <nav> + <nav id="navbar"> <p style="font-family: monospace;"><b>apio</b><i>forum</i>™</p> <form class="search-form" action="/search"> <input type="search" placeholder="query" name="q"> diff --git a/apioforum/templates/create_thread.html b/apioforum/templates/create_thread.html index 9de7312..04b4f42 100644 --- a/apioforum/templates/create_thread.html +++ b/apioforum/templates/create_thread.html @@ -4,7 +4,7 @@ {% endblock %} {% block content %} -<form action="{{url_for('forum.create_thread')}}", method="POST"> +<form method="POST"> <label for="title">thread title</label> <input name="title" id="title"> <br> diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index d1489c8..f83503b 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -1,12 +1,11 @@ {% extends 'base.html' %} {% from 'common.html' import ts, tag, disp_user, post_url %} -{% block header %}<h1>{% block title %}apioforum{%endblock%}</h1>{%endblock%} +{% block header %}<h1>{% block title %}{{forum.name}}{%endblock%}</h1>{%endblock%} {%block nmcontent%} <main class="widemain"> -<p>welcome to the apioforum</p> -<p>forum rules: do not be a bad person. do not do bad things.</p> +{{forum.description|md|safe}} {% if g.user %} -<p><a class="actionbutton" href="{{url_for('forum.create_thread')}}">create new thread</a></p> +<p><a class="actionbutton" href="{{url_for('forum.create_thread',forum_id=forum.id)}}">create new thread</a></p> {% else %} <p>please log in to create a new thread</p> {% endif %} diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index fb62880..ded2d52 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -2,6 +2,14 @@ {% extends 'base.html' %} {% block header %} <h1>{%block title %}{{thread.title}}{% endblock %}</h1> +<nav aria-label="Breadcrumb"> +<ol class="breadcrumbs"> + <li><a href="{{url_for('index')}}">apioforum</a></li> + <li><a href="#">the</a></li> + <li>the</li> +</ol> +</nav> + {% endblock %} {%block content%} |