Add thread creation & deletion

This commit is contained in:
David Hoppenbrouwers
2022-10-07 15:20:17 +02:00
parent 6f3b4de047
commit b96db0b905
8 changed files with 129 additions and 17 deletions

View File

@@ -16,8 +16,8 @@ class DB:
def get_thread(self, thread): def get_thread(self, thread):
db = self._db() db = self._db()
title, text, author = db.execute(''' title, text, author, author_id = db.execute('''
select title, text, name select title, text, name, author_id
from threads, users from threads, users
where thread_id = ? and author_id = user_id where thread_id = ? and author_id = user_id
''', ''',
@@ -30,7 +30,16 @@ class DB:
''', ''',
(thread,) (thread,)
).fetchall() ).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): def get_comments(self, thread):
return self._db().execute(''' return self._db().execute('''
@@ -77,7 +86,6 @@ class DB:
).fetchone() ).fetchone()
def set_user_private_info(self, user_id, about): def set_user_private_info(self, user_id, about):
print('BROH', about)
db = self._db() db = self._db()
db.execute(''' db.execute('''
update users update users
@@ -88,5 +96,36 @@ class DB:
) )
db.commit() 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): def _db(self):
return sqlite3.connect(self.conn) return sqlite3.connect(self.conn)

53
main.py
View File

@@ -19,13 +19,27 @@ def index():
def subforum(forum_id): def subforum(forum_id):
title, description = db.get_subforum(forum_id) title, description = db.get_subforum(forum_id)
threads = db.get_threads(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/<int:thread_id>/') @app.route('/thread/<int:thread_id>/')
def thread(thread_id): 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) 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/<int:comment_id>/') @app.route('/comment/<int:comment_id>/')
def comment(comment_id): def comment(comment_id):
@@ -74,7 +88,7 @@ def user_edit():
about = about about = about
) )
@app.route('/user/<int:user_id>') @app.route('/user/<int:user_id>/')
def user_info(user_id): def user_info(user_id):
name, about = db.get_user_public_info(user_id) name, about = db.get_user_public_info(user_id)
return render_template( return render_template(
@@ -84,6 +98,37 @@ def user_info(user_id):
about = about about = about
) )
@app.route('/forum/<int:forum_id>/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/<int:thread_id>/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/<int:thread_id>/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: class Comment:
def __init__(self, author, text): def __init__(self, author, text):
self.author = author self.author = author

View File

@@ -4,7 +4,7 @@ create table users (
password varchar(128) not null, password varchar(128) not null,
email varchar(254), email varchar(254),
about text not null default '', about text not null default '',
join_date integer, join_time integer,
role integer not null default 0 role integer not null default 0
); );
@@ -12,9 +12,9 @@ create table threads (
thread_id integer unique not null primary key autoincrement, thread_id integer unique not null primary key autoincrement,
author_id integer not null, author_id integer not null,
forum_id integer not null, forum_id integer not null,
create_date integer not null, create_time integer not null,
modify_date integer not null, modify_time integer not null,
update_date integer not null, update_time integer not null,
title varchar(64) not null, title varchar(64) not null,
text text not null, text text not null,
score integer not null default 0, score integer not null default 0,
@@ -26,8 +26,8 @@ create table comments (
thread_id integer not null, thread_id integer not null,
author_id integer not null, author_id integer not null,
parent_id integer, parent_id integer,
create_date integer not null, create_time integer not null,
modify_date integer not null, modify_time integer not null,
text text not null, text text not null,
score integer not null default 0, score integer not null default 0,
dead boolean not null default false dead boolean not null default false

View File

@@ -0,0 +1,11 @@
{% extends 'base.html' %}
{% block content %}
<p>Are you sure you want to delete "{{ thread_title }}"?</p>
<form method="post" action="../delete">
<input type="submit" value="Yes">
</form>
<form method="get" action="..">
<input type="submit" value="No">
</form>
{% endblock %}

View File

@@ -0,0 +1,9 @@
{% extends 'base.html' %}
{% block content %}
<form method="post">
<input type="text" name="title">
<textarea name="text"></textarea>
<input type="submit" value="Post">
</form>
{% endblock %}

View File

@@ -2,6 +2,7 @@
{% block content %} {% block content %}
<p>{{ description }}</p> <p>{{ description }}</p>
<a href="{{ url_for('new_thread', forum_id = forum_id) }}">Create thread</a>
<ul> <ul>
{% for id, title in threads %} {% for id, title in threads %}
<li><a href="{{ url_for('thread', thread_id = id) }}">{{ title }}</a></li> <li><a href="{{ url_for('thread', thread_id = id) }}">{{ title }}</a></li>

View File

@@ -11,6 +11,13 @@
{% endmacro %} {% endmacro %}
{% block content %} {% block content %}
{% if manage %}
<div>
<form method="get" action="confirm_delete/">
<input type="submit" value="Delete">
</form>
</div>
{% endif %}
<p>{{ author }} - rjgoire</p> <p>{{ author }} - rjgoire</p>
<p>{{ text }}</p> <p>{{ text }}</p>
{% for c in comments %} {% for c in comments %}

View File

@@ -19,11 +19,11 @@ insert into users (name, password) values (
insert into subforums (name, description, allowed_roles_mask) 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); 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!", 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.'); '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!"); 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); values (3, 1, 0, 0, "Greetings.", 1);