aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--apioforum/db.py28
-rw-r--r--apioforum/forum.py4
-rw-r--r--apioforum/templates/common.html4
-rw-r--r--apioforum/templates/view_post.html8
-rw-r--r--apioforum/thread.py48
5 files changed, 53 insertions, 39 deletions
diff --git a/apioforum/db.py b/apioforum/db.py
index ad34af3..e09b8d2 100644
--- a/apioforum/db.py
+++ b/apioforum/db.py
@@ -7,7 +7,7 @@ def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
- detect_types=sqlite3.PARSE_DECLTYPES
+ detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
)
g.db.row_factory = sqlite3.Row
g.db.execute("PRAGMA foreign_keys = ON;")
@@ -263,29 +263,32 @@ ALTER TABLE threads ADD COLUMN locked INT NOT NULL DEFAULT 0;
""",
"""
ALTER TABLE threads ADD COLUMN type TEXT NOT NULL DEFAULT "thread";
+
ALTER TABLE posts ADD COLUMN wiki INT NOT NULL DEFAULT 0;
-ALTER TABLE posts ADD COLUMN updated_by_others INT NOT NULL DEFAULT 0;
-CREATE TABLE revisions (
+ALTER TABLE posts ADD COLUMN et_al INT NOT NULL DEFAULT 0;
+ALTER TABLE posts ADD COLUMN rev_index INT NOT NULL DEFAULT 0;
+UPDATE posts SET rev_index = edited;
+ALTER TABLE posts ADD COLUMN editor TEXT REFERENCES users(username);
+ALTER TABLE posts ADD COLUMN comment TEXT;
+
+CREATE TABLE past_revisions (
post NOT NULL REFERENCES posts(id),
- id INTEGER PRIMARY KEY,
- author NOT NULL REFERENCES users(username),
+ rev_index INTEGER NOT NULL,
+ editor NOT NULL REFERENCES users(username),
content TEXT NOT NULL,
time TIMESTAMP NOT NULL,
- comment TEXT
+ comment TEXT,
+ PRIMARY KEY (post, rev_index)
);
+
CREATE TABLE wiki_pages (
path TEXT NOT NULL,
forum NOT NULL REFERENCES forums(id),
thread REFERENCES threads(id),
- type TEXT NOT NULL DEFAULT "page",
PRIMARY KEY (path, forum)
);
ALTER TABLE role_config ADD COLUMN p_edit_wiki INT NOT NULL DEFAULT 1
""",
-"""
-INSERT INTO revisions (post, author, content, time)
- SELECT id, author, content, coalesce(updated,created) FROM posts;
-""",
]
@@ -293,10 +296,11 @@ def init_db():
db = get_db()
version = db.execute("PRAGMA user_version;").fetchone()[0]
for i in range(version, len(migrations)):
+ click.echo(f"migrating {i}...")
+ click.echo(migrations[i])
db.executescript(migrations[i])
db.execute(f"PRAGMA user_version = {i+1}")
db.commit()
- click.echo(f"migration {i}")
@click.command("migrate")
@with_appcontext
diff --git a/apioforum/forum.py b/apioforum/forum.py
index 0de45ee..55ea770 100644
--- a/apioforum/forum.py
+++ b/apioforum/forum.py
@@ -328,10 +328,6 @@ def create_thread(forum):
"INSERT INTO posts (thread,created,author,content) VALUES (?,current_timestamp,?,?);",
(thread_id,g.user,content)
)
- db.execute("""
- INSERT INTO revisions (post,author,content,time,comment)
- VALUES (?,?,?,current_timestamp,?);
- """, (cur.lastrowid,g.user,content,"(created)"))
if thread_type == "wiki page":
db.execute("UPDATE posts SET wiki = 1 WHERE id = ?;", (cur.lastrowid,))
db.execute("""
diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html
index 6ef34f2..7c07f16 100644
--- a/apioforum/templates/common.html
+++ b/apioforum/templates/common.html
@@ -24,7 +24,7 @@
{%- if role != "other" %}
<span class="user-role"> ({{ role }})</span>
{%- endif -%}
- {%- if post.updated_by_others == 1 -%}
+ {%- if post.et_al -%}
, <i>et al.</i>
{%- endif %}
@@ -55,7 +55,7 @@
</a>
{% endif %}
- <a class="post-anchor-link" href="{{post_jump(post.id)}}">#{{post.id}}</a>
+ <a class="post-anchor-link" href="{{post_jump(post.id)}}">#{{post.id}}</a>/{{post.rev_index}}
</span>
</div>
<div class="post-content md">
diff --git a/apioforum/templates/view_post.html b/apioforum/templates/view_post.html
index 6c3fe73..5155a67 100644
--- a/apioforum/templates/view_post.html
+++ b/apioforum/templates/view_post.html
@@ -25,18 +25,18 @@
<table class="history">
{% for rev in history %}
<tr>
- <td>{{disp_user(rev.author)}}
+ <td>{{disp_user(rev.editor)}}
<td>
- {% if rev.id == revision.id or (loop.index == 1 and not revision) %}
+ {% if rev.rev_index == revision.rev_index or (loop.index == 1 and not revision) %}
<b>{{ts(rev.time)}} ago</b>
{% else %}
<a href="{{url_for('thread.view_post',
- post_id=post.id,revision=rev.id if loop.index!=1 else None,
+ post_id=post.id,revision=rev.rev_index if loop.index!=1 else None,
_anchor='history')}}">
{{ts(rev.time)}} ago
</a>
{% endif %}
- <td>{{rev.comment or "(no comment)"}}</td>
+ {% if rev.comment %}<td>{{rev.comment}}</td>{%endif%}
{% endfor %}
</table>
{% endif %}
diff --git a/apioforum/thread.py b/apioforum/thread.py
index 485fe01..26e89e9 100644
--- a/apioforum/thread.py
+++ b/apioforum/thread.py
@@ -284,10 +284,6 @@ def create_post(thread_id):
VALUES (?,?,?,current_timestamp,?);
""",(thread_id,g.user,content,vote_id))
post_id = cur.lastrowid
- cur.execute("""
- INSERT INTO revisions (post,author,content,time,comment)
- VALUES (?,?,?,current_timestamp,?);
- """, (post_id,g.user,content,"(created)"))
cur.execute(
"UPDATE threads SET updated = current_timestamp WHERE id = ?;",
(thread_id,)
@@ -381,14 +377,25 @@ def edit_post(post_id):
if len(newcontent.strip()) == 0:
err="post contents can't be empty"
if err is None:
- db.execute(
- "UPDATE posts SET content = ?, edited = 1, updated = current_timestamp WHERE id = ?",(newcontent,post_id))
+ params = dict(post_id=post_id, content=newcontent, user=g.user, comment=comment)
+ db.execute("""
+ INSERT INTO past_revisions (post, rev_index, editor, content, time, comment)
+ SELECT id, rev_index, coalesce(editor,author), content,
+ coalesce(updated,created), comment
+ FROM posts WHERE id = :post_id;
+ """,params)
db.execute("""
- INSERT INTO revisions (post, author, content, time, comment)
- VALUES (?,?,?,current_timestamp,?);
- """, (post_id,g.user,newcontent,comment))
- if g.user != post['author']:
- db.execute("UPDATE posts SET updated_by_others = 1 WHERE id = ?",(post_id,))
+ UPDATE posts SET
+ content = :content,
+ edited = 1,
+ updated = current_timestamp,
+ editor = :user,
+ comment = :comment,
+ rev_index = rev_index + 1,
+ et_al = et_al or author<>:user
+ WHERE
+ id = :post_id;
+ """,params)
db.commit()
flash("post edited editiously")
return redirect(jump_url)
@@ -415,18 +422,18 @@ def view_post(post_id):
rev = None
if 'revision' in request.args:
- rev_id = None
+ rev_idx = None
try:
- rev_id = int(request.args['revision'])
+ rev_idx = int(request.args['revision'])
except ValueError:
abort(404)
if not can_view_history:
abort(403)
- rev = db.execute("SELECT * FROM revisions WHERE id = ?;",(rev_id,)).fetchone()
+ rev = db.execute(
+ "SELECT * FROM past_revisions WHERE post=? and rev_index=?;",
+ (post_id,rev_idx)).fetchone()
if not rev:
abort(404)
- if rev['post'] != post_id:
- abort(404)
post = dict(post)
post['content'] = rev['content']
post['updated'] = rev['time']
@@ -434,8 +441,15 @@ def view_post(post_id):
history = None
if can_view_history:
history = db.execute("""
- SELECT author, time, comment, id FROM revisions WHERE post = ? ORDER BY id DESC;
+ SELECT editor, time, comment, rev_index FROM past_revisions WHERE post = ? ORDER BY rev_index DESC;
""",(post_id,)).fetchall()
+ history.insert(0, db.execute(
+ """SELECT
+ coalesce(editor,author) as editor,
+ coalesce(updated, created) as "time [timestamp]",
+ comment, NULL as rev_index
+ FROM posts WHERE id=?""",(post_id,)).fetchone())
+
return render_template("view_post.html",post=post,revision=rev,history=history)