From b96db0b905584ba7e345096942eb9a56c4df8265 Mon Sep 17 00:00:00 2001 From: David Hoppenbrouwers Date: Fri, 7 Oct 2022 15:20:17 +0200 Subject: [PATCH] Add thread creation & deletion --- db/sqlite.py | 47 +++++++++++++++++++++--- main.py | 53 +++++++++++++++++++++++++--- schema.txt | 12 +++---- templates/confirm_delete_thread.html | 11 ++++++ templates/new_thread.html | 9 +++++ templates/subforum.html | 1 + templates/thread.html | 7 ++++ test/init_db.txt | 6 ++-- 8 files changed, 129 insertions(+), 17 deletions(-) create mode 100644 templates/confirm_delete_thread.html create mode 100644 templates/new_thread.html diff --git a/db/sqlite.py b/db/sqlite.py index f096ae6..7bbba3a 100644 --- a/db/sqlite.py +++ b/db/sqlite.py @@ -16,8 +16,8 @@ class DB: def get_thread(self, thread): db = self._db() - title, text, author = db.execute(''' - select title, text, name + title, text, author, author_id = db.execute(''' + select title, text, name, author_id from threads, users where thread_id = ? and author_id = user_id ''', @@ -30,7 +30,16 @@ class DB: ''', (thread,) ).fetchall() - return title, text, author, comments + return title, text, author, author_id, comments + + def get_thread_title(self, thread_id): + return self._db().execute(''' + select title + from threads + where thread_id = ? + ''', + (thread_id,) + ).fetchone() def get_comments(self, thread): return self._db().execute(''' @@ -77,7 +86,6 @@ class DB: ).fetchone() def set_user_private_info(self, user_id, about): - print('BROH', about) db = self._db() db.execute(''' update users @@ -88,5 +96,36 @@ class DB: ) db.commit() + def add_thread(self, author_id, forum_id, title, text, time): + db = self._db() + c = db.cursor() + c.execute(''' + insert into threads (author_id, forum_id, title, text, + create_time, modify_time, update_time) + values (?, ?, ?, ?, ?, ?, ?) + ''', + (author_id, forum_id, title, text, time, time, time) + ) + rowid = c.lastrowid + db.commit() + return db.execute(''' + select thread_id + from threads + where rowid = ? + ''', + (rowid,) + ).fetchone() + + def delete_thread(self, thread_id): + db = self._db() + db.execute(''' + delete + from threads + where thread_id = ? + ''', + (thread_id,) + ) + db.commit() + def _db(self): return sqlite3.connect(self.conn) diff --git a/main.py b/main.py index 78e61cf..477b640 100644 --- a/main.py +++ b/main.py @@ -19,13 +19,27 @@ def index(): 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) + return render_template( + 'subforum.html', + title = title, + forum_id = forum_id, + description = description, + threads = threads, + ) @app.route('/thread//') def thread(thread_id): - title, text, author, comments = db.get_thread(thread_id) + user_id = session.get('user_id') + title, text, author, author_id, comments = db.get_thread(thread_id) comments = create_comment_tree(comments) - return render_template('thread.html', title = title, text = text, author = author, comments = comments) + return render_template( + 'thread.html', + title = title, + text = text, + author = author, + comments = comments, + manage = author_id == user_id, + ) @app.route('/comment//') def comment(comment_id): @@ -74,7 +88,7 @@ def user_edit(): about = about ) -@app.route('/user/') +@app.route('/user//') def user_info(user_id): name, about = db.get_user_public_info(user_id) return render_template( @@ -84,6 +98,37 @@ def user_info(user_id): about = about ) +@app.route('/forum//new/', methods = ['GET', 'POST']) +def new_thread(forum_id): + user_id = session.get('user_id') + if user_id is None: + return redirect(url_for('login')) + + if request.method == 'POST': + id, = db.add_thread(user_id, forum_id, request.form['title'], request.form['text'], time.time_ns()) + flash('Created thread', 'success') + return redirect(url_for('thread', thread_id = id)) + + return render_template( + 'new_thread.html', + title = 'Create new thread', + ) + +@app.route('/thread//confirm_delete/') +def confirm_delete_thread(thread_id): + title, = db.get_thread_title(thread_id) + return render_template( + 'confirm_delete_thread.html', + title = 'Delete thread', + thread_title = title, + ) + +@app.route('/thread//delete/', methods = ['POST']) +def delete_thread(thread_id): + db.delete_thread(thread_id) + flash('Thread has been deleted', 'success') + return redirect(url_for('index')) + class Comment: def __init__(self, author, text): self.author = author diff --git a/schema.txt b/schema.txt index 8639a9f..d0a47fa 100644 --- a/schema.txt +++ b/schema.txt @@ -4,7 +4,7 @@ create table users ( password varchar(128) not null, email varchar(254), about text not null default '', - join_date integer, + join_time integer, role integer not null default 0 ); @@ -12,9 +12,9 @@ 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, + create_time integer not null, + modify_time integer not null, + update_time integer not null, title varchar(64) not null, text text not null, score integer not null default 0, @@ -26,8 +26,8 @@ create table comments ( thread_id integer not null, author_id integer not null, parent_id integer, - create_date integer not null, - modify_date integer not null, + create_time integer not null, + modify_time integer not null, text text not null, score integer not null default 0, dead boolean not null default false diff --git a/templates/confirm_delete_thread.html b/templates/confirm_delete_thread.html new file mode 100644 index 0000000..fc78442 --- /dev/null +++ b/templates/confirm_delete_thread.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} + +{% block content %} +

Are you sure you want to delete "{{ thread_title }}"?

+
+ +
+
+ +
+{% endblock %} diff --git a/templates/new_thread.html b/templates/new_thread.html new file mode 100644 index 0000000..71dab75 --- /dev/null +++ b/templates/new_thread.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} +
+ + + +
+{% endblock %} diff --git a/templates/subforum.html b/templates/subforum.html index a7432d1..353b5b7 100644 --- a/templates/subforum.html +++ b/templates/subforum.html @@ -2,6 +2,7 @@ {% block content %}

{{ description }}

+Create thread
    {% for id, title in threads %}
  • {{ title }}
  • diff --git a/templates/thread.html b/templates/thread.html index de251f7..a3d2d5e 100644 --- a/templates/thread.html +++ b/templates/thread.html @@ -11,6 +11,13 @@ {% endmacro %} {% block content %} +{% if manage %} +
    +
    + +
    +
    +{% endif %}

    {{ author }} - rjgoire

    {{ text }}

    {% for c in comments %} diff --git a/test/init_db.txt b/test/init_db.txt index dcefd30..5a78ff8 100644 --- a/test/init_db.txt +++ b/test/init_db.txt @@ -19,11 +19,11 @@ insert into users (name, password) values ( 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) +insert into threads (author_id, forum_id, create_time, modify_time, update_time, 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) +insert into comments (author_id, thread_id, create_time, modify_time, text) values (2, 1, 0, 0, "Hi!"); -insert into comments (author_id, thread_id, create_date, modify_date, text, parent_id) +insert into comments (author_id, thread_id, create_time, modify_time, text, parent_id) values (3, 1, 0, 0, "Greetings.", 1);