Allow editing threads

This commit is contained in:
David Hoppenbrouwers
2022-10-08 13:34:09 +02:00
parent 6299a9e1fb
commit 47f92b2d83
4 changed files with 70 additions and 0 deletions

View File

@@ -67,6 +67,15 @@ class DB:
(thread_id,) (thread_id,)
).fetchone() ).fetchone()
def get_thread_title_text(self, thread_id):
return self._db().execute('''
select title, text
from threads
where thread_id = ?
''',
(thread_id,)
).fetchone()
def get_recent_threads(self, limit): def get_recent_threads(self, limit):
return self._db().execute(''' return self._db().execute('''
select thread_id, title, modify_date select thread_id, title, modify_date
@@ -244,5 +253,20 @@ class DB:
return True return True
return False return False
def modify_thread(self, thread_id, user_id, title, text, time):
db = self._db()
c = db.cursor()
c.execute('''
update threads
set title = ?, text = ?, modify_time = ?
where thread_id = ? and author_id = ?
''',
(title, text, time, thread_id, user_id)
)
if c.rowcount > 0:
db.commit()
return True
return False
def _db(self): def _db(self):
return sqlite3.connect(self.conn) return sqlite3.connect(self.conn)

28
main.py
View File

@@ -201,6 +201,34 @@ def delete_comment(comment_id):
# TODO return 403, maybe? # TODO return 403, maybe?
return redirect(url_for('index')) return redirect(url_for('index'))
@app.route('/thread/<int:thread_id>/edit', methods = ['GET', 'POST'])
def edit_thread(thread_id):
user_id = session.get('user_id')
if user_id is None:
return redirect(url_for('login'))
if request.method == 'POST':
if db.modify_thread(
thread_id,
user_id,
request.form['title'],
request.form['text'].replace('\r', ''),
time.time_ns(),
):
flash('Thread has been edited', 'success')
else:
flash('Thread could not be edited', 'error')
return redirect(url_for('thread', thread_id = thread_id))
title, text = db.get_thread_title_text(thread_id)
return render_template(
'edit_thread.html',
title = 'Edit thread',
thread_title = title,
text = text,
)
class Comment: class Comment:
def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id): def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id):

View File

@@ -22,6 +22,7 @@
<p><sub> <p><sub>
{{- author(name, ctime, mtime) -}} {{- author(name, ctime, mtime) -}}
{%- if author_id == session.get('user_id') -%} {%- if author_id == session.get('user_id') -%}
<a href="{{ url_for('edit_thread', thread_id = thread_id) }}"> edit</a>
{%- endif -%} {%- endif -%}
{%- if author_id == session.get('user_id') -%} {%- if author_id == session.get('user_id') -%}
<a href="{{ url_for('confirm_delete_thread', thread_id = thread_id) }}"> delete</a> <a href="{{ url_for('confirm_delete_thread', thread_id = thread_id) }}"> delete</a>

View File

@@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% block content %}
<form method="post">
<table class=form>
<tr>
<td>Title</td>
<td><input type="text" name="title" value="{{ thread_title }}"></td>
</tr>
<tr>
<td>Text</td>
<td><textarea name="text">{{ text }}</textarea></td>
</tr>
</table>
<p><input type="submit" value="Post"></p>
</form>
{% endblock %}