From d72fd14c92e730fdb4b777e758433a5391576a41 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Fri, 7 Oct 2022 13:18:19 +0200 Subject: [PATCH] Basics to view threads --- .gitignore | 3 +++ Makefile | 14 +++++++++++ db/sqlite.py | 53 +++++++++++++++++++++++++++++++++++++++++ main.py | 48 +++++++++++++++++++++++++++++++++++++ schema.txt | 41 +++++++++++++++++++++++++++++++ templates/base.html | 13 ++++++++++ templates/index.html | 9 +++++++ templates/subforum.html | 10 ++++++++ templates/thread.html | 19 +++++++++++++++ test/all.sh | 22 +++++++++++++++++ test/init_db.txt | 15 ++++++++++++ 11 files changed, 247 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 db/sqlite.py create mode 100644 main.py create mode 100644 schema.txt create mode 100644 templates/base.html create mode 100644 templates/index.html create mode 100644 templates/subforum.html create mode 100644 templates/thread.html create mode 100755 test/all.sh create mode 100644 test/init_db.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..177f8e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/venv +__pycache__ +*.db diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..11e0a43 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +PYTHON = python3 +FLASK = flask +SQLITE = sqlite3 + +default: test + +test:: + test/all.sh + +venv: + $(PYTHON) -m venv $@ + +forum.db: + $(SQLITE) $@ < schema.txt diff --git a/db/sqlite.py b/db/sqlite.py new file mode 100644 index 0000000..f1ebaf6 --- /dev/null +++ b/db/sqlite.py @@ -0,0 +1,53 @@ +import sqlite3 + +class DB: + def __init__(self, conn): + self.conn = conn + pass + + def get_subforums(self): + return self._db().execute('select forum_id, name, description from subforums').fetchall() + + def get_subforum(self, subforum): + return self._db().execute('select name, description from subforums where forum_id = ?', (subforum,)).fetchone() + + def get_threads(self, subforum): + return self._db().execute('select thread_id, title from threads where forum_id = ?', (subforum,)).fetchall() + + def get_thread(self, thread): + db = self._db() + title, text, author = db.execute(''' + select title, text, name + from threads, users + where thread_id = ? and author_id = user_id + ''', + (thread,) + ).fetchone() + comments = db.execute(''' + select comment_id, parent_id, name, text + from comments, users + where thread_id = ? and author_id = user_id + ''', + (thread,) + ).fetchall() + return title, text, author, comments + + def get_comments(self, thread): + return self._db().execute(''' + select text + from comments + where thread_id = ? + ''', + (thread,) + ).fetchall() + + def get_comment_tree(self, comment): + db = self._db() + parent = db.execute('select text from comments where comment_id = ?', (comment,)).fetchall() + children = db.execute('select text from comments where parent_id = ?', (comment,)).fetchall() + print(parent, children) + return str(parent) + str(children) + return parent + + def _db(self): + return sqlite3.connect(self.conn) diff --git a/main.py b/main.py new file mode 100644 index 0000000..23d336f --- /dev/null +++ b/main.py @@ -0,0 +1,48 @@ +from flask import Flask, render_template +from db.sqlite import DB +import os + +app = Flask(__name__) +db = DB(os.getenv('DB')) +NAME = 'Agrepy' + +@app.route('/') +def index(): + return render_template('index.html', title = NAME, subforums = db.get_subforums()) + +@app.route('/forum//') +def subforum(forum_id): + title, description = db.get_subforum(forum_id) + threads = db.get_threads(forum_id) + return render_template('subforum.html', title = title, description = description, threads = threads) + +@app.route('/thread//') +def thread(thread_id): + title, text, author, comments = db.get_thread(thread_id) + comments = create_comment_tree(comments) + return render_template('thread.html', title = title, text = text, author = author, comments = comments) + +@app.route('/comment//') +def comment(comment_id): + #return str(db.get_comment_tree(comment_id)[0]) + return str(db.get_comment_tree(comment_id)) + + +class Comment: + def __init__(self, author, text): + self.author = author + self.text = text + self.children = [] + +def create_comment_tree(comments): + root = [] + comment_map = {} + for comment_id, parent_id, author, text in comments: + comment = Comment(author, text) + parent = comment_map.get(parent_id) + if parent is not None: + parent.children.append(comment) + else: + root.append(comment) + comment_map[comment_id] = comment + return root diff --git a/schema.txt b/schema.txt new file mode 100644 index 0000000..3e69eb4 --- /dev/null +++ b/schema.txt @@ -0,0 +1,41 @@ +create table users ( + user_id integer unique not null primary key autoincrement, + name varchar(32) unique not null, + password varchar(128) not null, + email varchar(254), + about text, + join_date integer, + role integer not null default 0 +); + +create table threads ( + thread_id integer unique not null primary key autoincrement, + author_id integer not null, + forum_id integer not null, + create_date integer not null, + modify_date integer not null, + update_date integer not null, + title varchar(64) not null, + text text not null, + score integer not null default 0, + dead boolean not null default false +); + +create table comments ( + comment_id integer unique not null primary key autoincrement, + thread_id integer not null, + author_id integer not null, + parent_id integer, + create_date integer not null, + modify_date integer not null, + text text not null, + score integer not null default 0, + dead boolean not null default false +); + +create table subforums ( + forum_id integer unique not null primary key autoincrement, + name varchar(64) not null, + description text, + allowed_roles_mask integer not null +); diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..bf64e8a --- /dev/null +++ b/templates/base.html @@ -0,0 +1,13 @@ + + + {{ title }} + + + +

{{ title }}

+
+ {% block content %}{% endblock %} +
+ diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..962e8bc --- /dev/null +++ b/templates/index.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} + +{% endblock %} diff --git a/templates/subforum.html b/templates/subforum.html new file mode 100644 index 0000000..a7432d1 --- /dev/null +++ b/templates/subforum.html @@ -0,0 +1,10 @@ +{% extends 'base.html' %} + +{% block content %} +

{{ description }}

+
    + {% for id, title in threads %} +
  • {{ title }}
  • + {% endfor %} +
+{% endblock %} diff --git a/templates/thread.html b/templates/thread.html new file mode 100644 index 0000000..de251f7 --- /dev/null +++ b/templates/thread.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} + +{% macro render_comment(comment) %} +
+

{{ comment.author }}

+

{{ comment.text }}

+ {% for c in comment.children %} + {{ render_comment(c) }} + {% endfor %} +
+{% endmacro %} + +{% block content %} +

{{ author }} - rjgoire

+

{{ text }}

+{% for c in comments %} +{{ render_comment(c) }} +{% endfor %} +{% endblock %} diff --git a/test/all.sh b/test/all.sh new file mode 100755 index 0000000..d05d465 --- /dev/null +++ b/test/all.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +SQLITE=sqlite3 +FLASK=flask + +set -e +set -x + +tmp=$(mktemp -d) +trap 'rm -rf $tmp' EXIT +base=$(dirname "$0") + +db=$tmp/forum.db + +. $base/../venv/bin/activate + +# initialize db +$SQLITE $db < $base/../schema.txt +$SQLITE $db < $base/init_db.txt +cd $base/.. + +DB=$db $FLASK --app main --debug run diff --git a/test/init_db.txt b/test/init_db.txt new file mode 100644 index 0000000..e5c9f64 --- /dev/null +++ b/test/init_db.txt @@ -0,0 +1,15 @@ +insert into users (name, password, email) values ("Foo", "supasecret", "foo@bar.baz"); +insert into users (name, password, email) values ("Bar", "rgjieogir", "bar@foo.baz"); +insert into users (name, password) values ("bazzers", "reogke"); + +insert into subforums (name, description, allowed_roles_mask) + values ("Earth", "The totality of all space and time; all that is, has been, and will be.", 1); + +insert into threads (author_id, forum_id, create_date, modify_date, update_date, title, text) + values (1, 1, 0, 0, 0, "Hello, world!", + 'In its most general sense, the term "world" refers to the totality of entities, to the whole of reality or to everything that is.'); + +insert into comments (author_id, thread_id, create_date, modify_date, text) + values (2, 1, 0, 0, "Hi!"); +insert into comments (author_id, thread_id, create_date, modify_date, text, parent_id) + values (3, 1, 0, 0, "Greetings.", 1);