aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorubq323 <ubq323>2021-05-23 20:23:54 +0000
committerubq323 <ubq323>2021-05-23 20:23:54 +0000
commit78bee4ec778947df35fa47e29282a60aec5a7acd (patch)
tree533cd1e3b9aad1e84aa0fe21dea8ecac177a2550
parenteb298f0c63eb6ac5547a6a7a0086e5e086ad5abc (diff)
threads etc
-rw-r--r--apioforum/__init__.py9
-rw-r--r--apioforum/auth.py7
-rw-r--r--apioforum/db.py3
-rw-r--r--apioforum/templates/base.html2
-rw-r--r--apioforum/templates/view_thread.html7
-rw-r--r--apioforum/thread.py11
6 files changed, 22 insertions, 17 deletions
diff --git a/apioforum/__init__.py b/apioforum/__init__.py
index df7185b..e4bea05 100644
--- a/apioforum/__init__.py
+++ b/apioforum/__init__.py
@@ -29,13 +29,4 @@ def create_app():
from . import thread
app.register_blueprint(thread.bp)
- @app.route("/")
- def main():
- return "the"
-
- @app.route("/b")
- def b():
- db=get_db()
- return str(db.execute("select 2 + 2;").fetchone()[0])
-
return app
diff --git a/apioforum/auth.py b/apioforum/auth.py
index fe03264..49808ea 100644
--- a/apioforum/auth.py
+++ b/apioforum/auth.py
@@ -75,9 +75,14 @@ def load_user():
if username is None:
g.user = None
else:
- g.user = get_db().execute(
+ row = get_db().execute(
"SELECT * FROM users WHERE username = ?;", (username,)
).fetchone()
+ if row is None:
+ g.user = None
+ else:
+ g.user = row['username']
+
def login_required(view):
@functools.wraps(view)
diff --git a/apioforum/db.py b/apioforum/db.py
index 31eb8ef..9db27db 100644
--- a/apioforum/db.py
+++ b/apioforum/db.py
@@ -37,8 +37,9 @@ CREATE TABLE posts (
content TEXT,
thread INTEGER NOT NULL REFERENCES threads(id),
author TEXT NOT NULL REFERENCES users(username),
- idx INTEGER NOT NULL
+ created TIMESTAMP NOT NULL
);
+
CREATE INDEX posts_thread_idx ON posts (thread);
""",
]
diff --git a/apioforum/templates/base.html b/apioforum/templates/base.html
index 6660686..28fd055 100644
--- a/apioforum/templates/base.html
+++ b/apioforum/templates/base.html
@@ -10,7 +10,7 @@
<h1>apioforum</h1>
<ul>
{% if g.user %}
- <li>{{ g.user['username'] }}</li>
+ <li>{{ g.user }}</li>
<li><a href="{{ url_for('auth.logout') }}">logout</a></li>
{% else %}
<li><a href="{{ url_for('auth.login') }}">login</a></li>
diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html
index 34faeea..ebbcd3b 100644
--- a/apioforum/templates/view_thread.html
+++ b/apioforum/templates/view_thread.html
@@ -8,11 +8,16 @@
<p>by {{thread.creator}}</p>
<dl>
{% for post in posts %}
-<dt>{{post.author}}</dt>
+<dt>{{post.author}} {{post.created}}</dt>
<dd>{{post.content}}</dd>
{% else %}
<dt>there weren't</dt>
<dd>any posts</dd>
{% endfor %}
</dl>
+{% if g.user %}
+<p>loggedi n as {{ g.user }}</p>
+{% else %}
+<p>not logged in</p>
+{% endif %}
{% endblock %}
diff --git a/apioforum/thread.py b/apioforum/thread.py
index 24b7766..2276e84 100644
--- a/apioforum/thread.py
+++ b/apioforum/thread.py
@@ -1,19 +1,22 @@
# view posts in thread
from flask import (
- Blueprint, render_template
+ Blueprint, render_template, abort
)
from .db import get_db
bp = Blueprint("thread", __name__, url_prefix="/thread")
-@bp.route("/view/<int:thread_id>")
+@bp.route("/<int:thread_id>")
def view_thread(thread_id):
db = get_db()
thread = db.execute("SELECT * FROM threads WHERE id = ?;",(thread_id,)).fetchone()
if thread is None:
abort(404)
else:
- posts = db.execute("SELECT * FROM posts WHERE thread = ? ORDER BY idx;",(thread_id,)).fetchall()
+ posts = db.execute(
+ "SELECT * FROM posts WHERE thread = ? ORDER BY created ASC;",
+ (thread_id,)
+ ).fetchall()
return render_template("view_thread.html",posts=posts,thread=thread)
-
+