Allow editing threads
This commit is contained in:
24
db/sqlite.py
24
db/sqlite.py
@@ -67,6 +67,15 @@ class DB:
|
||||
(thread_id,)
|
||||
).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):
|
||||
return self._db().execute('''
|
||||
select thread_id, title, modify_date
|
||||
@@ -244,5 +253,20 @@ class DB:
|
||||
return True
|
||||
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):
|
||||
return sqlite3.connect(self.conn)
|
||||
|
||||
28
main.py
28
main.py
@@ -201,6 +201,34 @@ def delete_comment(comment_id):
|
||||
# TODO return 403, maybe?
|
||||
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:
|
||||
def __init__(self, id, author_id, author, text, create_time, modify_time, parent_id):
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
<p><sub>
|
||||
{{- author(name, ctime, mtime) -}}
|
||||
{%- if author_id == session.get('user_id') -%}
|
||||
<a href="{{ url_for('edit_thread', thread_id = thread_id) }}"> edit</a>
|
||||
{%- endif -%}
|
||||
{%- if author_id == session.get('user_id') -%}
|
||||
<a href="{{ url_for('confirm_delete_thread', thread_id = thread_id) }}"> delete</a>
|
||||
|
||||
17
templates/edit_thread.html
Normal file
17
templates/edit_thread.html
Normal 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 %}
|
||||
Reference in New Issue
Block a user