diff options
| author | rebecca <ubq323@ubq323.website> | 2026-11-14 20:05:16 +0000 |
|---|---|---|
| committer | rebecca <ubq323@ubq323.website> | 2026-11-14 20:05:16 +0000 |
| commit | 4925836a08656aa2493ddfecbefd160e6a7f95fd (patch) | |
| tree | 7853355ac3c0efb310df6232ba60161e2fbf5ec7 | |
| parent | 0fe047d4495328b61c6c46838e94d56cc09b5a1a (diff) | |
| parent | a61dea3eb69675cc4720b6575bbbde3d3fbe1aef (diff) | |
Merge branch 'wiki' into trunk
| -rw-r--r-- | apioforum/__init__.py | 8 | ||||
| -rw-r--r-- | apioforum/db.py | 47 | ||||
| -rw-r--r-- | apioforum/forum.py | 54 | ||||
| -rw-r--r-- | apioforum/mdrender.py | 15 | ||||
| -rw-r--r-- | apioforum/permissions.py | 1 | ||||
| -rw-r--r-- | apioforum/roles.py | 1 | ||||
| -rw-r--r-- | apioforum/static/style.css | 19 | ||||
| -rw-r--r-- | apioforum/templates/base.html | 2 | ||||
| -rw-r--r-- | apioforum/templates/common.html | 54 | ||||
| -rw-r--r-- | apioforum/templates/config_thread.html | 21 | ||||
| -rw-r--r-- | apioforum/templates/edit_permissions.html | 2 | ||||
| -rw-r--r-- | apioforum/templates/edit_post.html | 8 | ||||
| -rw-r--r-- | apioforum/templates/view_forum.html | 5 | ||||
| -rw-r--r-- | apioforum/templates/view_post.html | 50 | ||||
| -rw-r--r-- | apioforum/templates/view_thread.html | 88 | ||||
| -rw-r--r-- | apioforum/templates/view_user.html | 6 | ||||
| -rw-r--r-- | apioforum/thread.py | 117 | ||||
| -rw-r--r-- | apioforum/wiki.py | 2 |
18 files changed, 400 insertions, 100 deletions
diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 06595c9..673bb0b 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -58,18 +58,20 @@ def create_app(): app.jinja_env.globals.update(forum_path=forum.forum_path) app.jinja_env.globals.update(post_jump=thread.post_jump) + from .permissions import is_admin from .roles import has_permission, is_bureaucrat, get_user_role from .thread import thread_info app.jinja_env.globals.update( has_permission=has_permission, thread_info=thread_info, is_bureaucrat=is_bureaucrat, - get_user_role=get_user_role) + get_user_role=get_user_role, + is_admin=is_admin) from .mdrender import render @app.template_filter('md') - def md_render(s): - return render(s) + def md_render(s, forum_id): + return render(s, forum_id) @app.errorhandler(404) def not_found(e): diff --git a/apioforum/db.py b/apioforum/db.py index 61f1bc3..6ec65b6 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;") @@ -261,21 +261,48 @@ ALTER TABLE users ADD COLUMN banreason TEXT; """ ALTER TABLE threads ADD COLUMN locked INT NOT NULL DEFAULT 0; """, -"", "", # put wiki stuff here when merging +""" +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 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), + rev_index INTEGER NOT NULL, + editor NOT NULL REFERENCES users(username), + content TEXT NOT NULL, + time TIMESTAMP NOT NULL, + 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), + PRIMARY KEY (path, forum) +); +ALTER TABLE role_config ADD COLUMN p_edit_wiki INT NOT NULL DEFAULT 1 +""", """ ALTER TABLE polls ADD COLUMN closed INT NOT NULL DEFAULT 0; """, ] - 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 @@ -284,7 +311,19 @@ def migrate_command(): init_db() click.echo("ok") +@click.command("create_admin") +@click.argument("username") +@with_appcontext +def create_admin_command(username): + db = get_db() + from werkzeug.security import generate_password_hash + db.execute( + "INSERT INTO users (username, password, admin) VALUES (?,?,1);", + (username, generate_password_hash("changeme"))) + db.commit() + def init_app(app): app.teardown_appcontext(close_db) app.cli.add_command(migrate_command) + app.cli.add_command(create_admin_command) diff --git a/apioforum/forum.py b/apioforum/forum.py index a955ed5..55ea770 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -12,6 +12,7 @@ from .mdrender import render from .roles import get_forum_roles,has_permission,is_bureaucrat,get_user_role, permissions as role_permissions from .permissions import is_admin from . import read +from . import wiki from sqlite3 import OperationalError import datetime import math @@ -280,11 +281,21 @@ def create_thread(forum): if forum is None: flash("that forum doesn't exist") return redirect(url_for('index')) + + if "type" in request.args: + thread_type = request.args["type"] + else: + thread_type = "thread" + thread_type = thread_type.replace('_', " ") + if thread_type != "thread" and thread_type != "wiki page": + abort(404) if g.user is None: - flash("you need to be logged in to create a thread") + flash("you need to be logged in to create a " + thread_type) return redirect(url_for('index')) - + + title = request.args['title'] if 'title' in request.args else "" + content = request.args['content'] if 'content' in request.args else "" if request.method == "POST": title = request.form['title'] content = request.form['content'] @@ -294,19 +305,35 @@ def create_thread(forum): elif 'lock' in request.form and not has_permission(forum['id'], g.user, "p_manage_threads"): err = "you do not have permission to lock threads" + wiki_path = wiki.normalize_page_name(title) + if thread_type == "wiki page": + existing = db.execute(""" + SELECT * FROM wiki_pages WHERE path = ? AND forum = ?; + """,(wiki_path,forum['id'])).fetchone() + if existing: + err = "there's already a page with that name" + if not has_permission(forum['id'], g.user, "p_edit_wiki"): + err = "you do not have permission to create wiki pages" + if err is None: lock = int(request.form.get('lock','0')) cur = db.cursor() cur.execute( - """INSERT INTO threads (title,creator,created,updated,forum,locked) - VALUES (?,?,current_timestamp,current_timestamp,?,?);""", - (title,g.user,forum['id'],lock) + """INSERT INTO threads (title,creator,created,updated,forum,locked,type) + VALUES (?,?,current_timestamp,current_timestamp,?,?,?);""", + (title,g.user,forum['id'],lock,thread_type) ) thread_id = cur.lastrowid cur.execute( "INSERT INTO posts (thread,created,author,content) VALUES (?,current_timestamp,?,?);", (thread_id,g.user,content) ) + if thread_type == "wiki page": + db.execute("UPDATE posts SET wiki = 1 WHERE id = ?;", (cur.lastrowid,)) + db.execute(""" + INSERT INTO wiki_pages (path, forum, thread) + VALUES (?,?,?) + """, (wiki_path,forum['id'],thread_id)) thread = {'id': thread_id, 'forum': forum['id']} apply_selected_tags(thread, request.form) db.commit() @@ -321,8 +348,25 @@ def create_thread(forum): return render_template("config_thread.html", forum=forum, + thread_type=thread_type, + title=title, + content=content, avail_tags=get_avail_tags(forum['id'])) +from .thread import do_view_thread + +@bp.route("/<int:forum_id>/wiki/<path>") +def view_wiki_page(forum_id, path): + db = get_db() + if path != wiki.normalize_page_name(path): + redirect(url_for('thread.view_thread_wiki', forum_id, path)) + wiki_page = db.execute(""" + SELECT * FROM wiki_pages WHERE forum = ? AND path = ?; + """,(forum_id,path)).fetchone() + if not wiki_page: + abort(404) + return do_view_thread(wiki_page['thread']) + @forum_route("roles",methods=("GET","POST")) @requires_bureaucrat def edit_roles(forum): diff --git a/apioforum/mdrender.py b/apioforum/mdrender.py index 2d58f5c..8d23be7 100644 --- a/apioforum/mdrender.py +++ b/apioforum/mdrender.py @@ -1,6 +1,8 @@ import bleach from bleach._vendor.html5lib.filters.base import Filter from .csscolors import csscolors +from .wiki import normalize_page_name +from flask import url_for, g allowed_tags = [ 'p', @@ -86,7 +88,16 @@ cleaner = bleach.sanitizer.Cleaner( css_sanitizer=IFrameCSS(), filters=[IFrameSandboxFilter]) +def wikilink_url(label, base, end): + if g.forum_id is None: + return "#" + else: + normalized = normalize_page_name(label) + return url_for('forum.view_wiki_page', forum_id=g.forum_id, path=normalized) + + import markdown +from markdown.extensions.wikilinks import WikiLinkExtension md = markdown.Markdown(extensions=[ 'pymdownx.tilde', 'pymdownx.caret', @@ -94,9 +105,11 @@ md = markdown.Markdown(extensions=[ 'tables', 'pymdownx.details', 'pymdownx.fancylists', + WikiLinkExtension(build_url=wikilink_url), ]) -def render(text): +def render(text, forum_id): + g.forum_id = forum_id text = md.reset().convert(text) text = cleaner.clean(text) return text diff --git a/apioforum/permissions.py b/apioforum/permissions.py index f705d08..60cba6f 100644 --- a/apioforum/permissions.py +++ b/apioforum/permissions.py @@ -38,6 +38,5 @@ def make_admin(username): def init_app(app): app.cli.add_command(make_admin) - app.context_processor(lambda: dict(is_admin=is_admin())) diff --git a/apioforum/roles.py b/apioforum/roles.py index 42ce21c..e2428af 100644 --- a/apioforum/roles.py +++ b/apioforum/roles.py @@ -10,6 +10,7 @@ permissions = [ "p_view_threads", "p_vote", "p_create_polls", + "p_edit_wiki", "p_approve", "p_create_subforum", "p_view_forum" diff --git a/apioforum/static/style.css b/apioforum/static/style.css index a5c2357..24e740e 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -57,6 +57,7 @@ summary { user-select: none; } .post-heading-b { float: right; margin-right: 0.5em } .post-footer { margin-left: 0.2em; font-style: italic; } +.wiki-footer-comment { font-style: normal; } .post-anchor-link { color: var(--username-colour); } @@ -81,6 +82,11 @@ div.deleted-post { color: hsl(0,0%,80%); } +.preview-post { + overflow: hidden; + max-height: 303px; +} + @media (prefers-color-scheme: dark) { .deleted-post > .post-heading > * { color: hsl(0,0%,45%); @@ -372,6 +378,10 @@ a.thread-preview-post { box-sizing: border-box; border:1px solid var(--border-colour); margin-top: 5px; + &[readonly] { + height: 10em; + min-height: initial; + } } #polloptions { @@ -527,3 +537,12 @@ sitelen { src: url("nasin-nanpa.otf") format("opentype"); } +.wiki-body { + margin-bottom: 1em; + .post { + border: none; + background: none; + } +} + + diff --git a/apioforum/templates/base.html b/apioforum/templates/base.html index 753e78c..f66395d 100644 --- a/apioforum/templates/base.html +++ b/apioforum/templates/base.html @@ -26,7 +26,7 @@ {% if g.user %} <p>{{ disp_user(g.user) }}</p> - {% if is_admin %} + {% if is_admin() %} <p><a href="{{url_for('admin.admin_page')}}">admin</a></p> {% endif %} diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index 20bc00b..e9e310b 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -2,7 +2,7 @@ <a href="{{url_for('user.view_user',username=username)}}" class="username">{{username}}</a> {%- endmacro %} -{% macro disp_post(post, buttons=False, forum=None, footer=None) %} +{% macro disp_post(post, buttons=False, forum=None, footer=None, preview=False) %} {% if not forum %} {% if post.forum %} {% set forum = post.forum %} @@ -15,43 +15,55 @@ {% elif post.deleted %} {{ hidden_post("this post never existed.", post) }} {% else %} -<div class="post {% if post.deleted %}deleted-post{% endif %}" id="post_{{post.id}}"> +<div class="post {% if post.deleted %}deleted-post{% endif %} {% if preview %}preview-post{% endif %}" id="post_{{post.id}}"> <div class="post-heading"> <span class="post-heading-a"> {{disp_user(post.author)}} - {% set role = get_user_role(forum, post.author) %} - {% if role != "other" %} - <span class="user-role"> - ({{ role }}) - </span> - {% endif %} + {%- set role = get_user_role(forum, post.author) -%} + {%- if role != "other" %} + <span class="user-role"> ({{ role }})</span> + {%- endif -%} + {%- if post.et_al -%} + , <i>et al.</i> + {%- endif %} {{ts(post.created)}} - {% if post.edited %} + {% if not post.wiki and post.edited %} (edited {{ts(post.updated)}}) {% endif %} </span> <span class="post-heading-b"> {% if buttons %} - {% if post.author == g.user %} + {% if post.author == g.user or ( + post.wiki == 1 and + has_permission(forum, g.user, "p_edit_wiki") + and not thread_info(post.thread).locked == 1) + %} <a class="actionbutton" href="{{url_for('thread.edit_post',post_id=post.id)}}">edit</a> {% endif %} - {% if post.author == g.user or (forum and has_permission(forum, g.user, "p_delete_posts")) %} + {% if post.wiki != 1 and (post.author == g.user or (forum and has_permission(forum, g.user, "p_delete_posts"))) %} <a class="actionbutton" href="{{url_for('thread.delete_post',post_id=post.id)}}">delete</a> {% endif %} - <a class="actionbutton" - href="{{url_for('thread.view_post',post_id=post.id)}}">src</a> + <a class="actionbutton" href="{{url_for('thread.view_post',post_id=post.id)}}">src</a> + {% if post.wiki == 1 or is_admin() or g.user == post.author %} + <a class="actionbutton" href="{{url_for('thread.view_post',post_id=post.id,_anchor='history')}}">history</a> + {% endif %} + </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"> - {{ post.content|md|safe }} + {% if not preview %} + {{ post.content|md(forum)|safe }} + {% else %} + {{ post.content[:2500]|md(forum)|safe }} + {% endif %} </div> {% if footer %} <div class="post-footer"> @@ -75,7 +87,7 @@ </span> </div> <div class="post-content md"> - {{ message|md|safe }} + {{ message|md(None)|safe }} </div> </div> {% endmacro %} @@ -173,12 +185,12 @@ </svg> {% endmacro %} -{% macro pagination_nav(page,max_pageno,view,bottom=False) %} +{% macro pagination_nav(page,max_pageno,view,bottom=False,fragment="") %} <nav aria-label="pagination" class="pages{% if not bottom %} pages-top{% endif %}"> <div class="links"> {% if page > 1 %} - <a href="{{url_for(view,**kwargs)}}" aria-label="first page"><< first</a> - <a href="{{url_for(view,page=page-1,**kwargs)}}" aria-label="previous page">< prev</a> + <a href="{{url_for(view,**kwargs)}}#{{fragment}}" aria-label="first page"><< first</a> + <a href="{{url_for(view,page=page-1,**kwargs)}}#{{fragment}}" aria-label="previous page">< prev</a> {% else %} <a aria-label="first page"><< first</a> <a aria-label="previous page">< prev</a> @@ -187,8 +199,8 @@ <div class="pageno"><span id="the-word-page">page </span>{{page}} of {{max_pageno}}</div> <div class="links"> {% if page < max_pageno %} - <a href="{{url_for(view,page=page+1,**kwargs)}}" aria-label="next page">next ></a> - <a href="{{url_for(view,page=max_pageno,**kwargs)}}" aria-label="last page">last >></a> + <a href="{{url_for(view,page=page+1,**kwargs)}}#{{fragment}}" aria-label="next page">next ></a> + <a href="{{url_for(view,page=max_pageno,**kwargs)}}#{{fragment}}" aria-label="last page">last >></a> {% else %} <a aria-label="next page">next ></a> <a aria-label="last page">last >></a> diff --git a/apioforum/templates/config_thread.html b/apioforum/templates/config_thread.html index 3e84f86..bb1e605 100644 --- a/apioforum/templates/config_thread.html +++ b/apioforum/templates/config_thread.html @@ -1,24 +1,27 @@ {% extends 'base.html' %} {% from 'common.html' import tag %} {% block header %} + <h1> {% block title %} - {% if thread %}configure thread '{{thread.title}}' - {% else %}create new thread{% endif %} + {% set type = thread_type or thread.type %} + {% if thread %}configure {{type}} '{{thread.title}}' + {% else %}create new {{type}}{% endif %} {% endblock %} </h1> {% endblock %} {% block content %} +{% set type = thread_type or thread.type %} <form method="post"> <fieldset> -<legend>thread</legend> -<label for="title">thread title</label> -<input type="text" id="title" name="title" {%if thread%}value="{{thread.title}}"{%endif%}> +<legend>{{type}}</legend> +<label for="title">{{type}} title</label> +<input type="text" id="title" name="title" {%if thread%}value="{{thread.title}}"{%elif title%}value="{{title}}"{%endif%}> {% if not thread %} <br> - <label for="content">thread content</label> - <textarea name="content" id="content" class="new-post-box" placeholder="thread content here"></textarea> + <label for="content">{{type}} content</label> + <textarea name="content" id="content" class="new-post-box" placeholder="welcome to my extraordinary {{type}}.">{{content}}</textarea> {% endif %} </fieldset> @@ -54,7 +57,7 @@ <input type="submit" value="confirm"> <a href="{{url_for('thread.view_thread',thread_id=thread.id)}}">cancel</a> {% else %} - <input type="submit" value="create thread"> + <input type="submit" value="create {{type}}"> <a href="{{url_for('forum.view_forum',forum_id=forum.id)}}">cancel</a> {% endif %} </form> @@ -76,7 +79,7 @@ </form> {% else %} <h2>poll operations</h2> -<p>there is already a poll attached to this thread.</p> +<p>there is already a poll attached to this {{type}}.</p> {% if poll.closed %} <p>it has been closed already, and may not be reopened.</p> {% else %} diff --git a/apioforum/templates/edit_permissions.html b/apioforum/templates/edit_permissions.html index 59c9093..3fadfe7 100644 --- a/apioforum/templates/edit_permissions.html +++ b/apioforum/templates/edit_permissions.html @@ -45,6 +45,8 @@ "allow users with the role to add a poll to a thread")}} {{perm("p_vote","vote", "allow users with the role to vote in polls")}} + {{perm("p_edit_wiki","edit wiki", + "create and edit the content of wiki pages")}} {{perm("p_create_subforum","create subforæ", "allow users with the role to create subforæ in this forum. " + "they will automatically become a bureaucrat in this subforum.")}} diff --git a/apioforum/templates/edit_post.html b/apioforum/templates/edit_post.html index 275f97f..573fd4e 100644 --- a/apioforum/templates/edit_post.html +++ b/apioforum/templates/edit_post.html @@ -1,12 +1,18 @@ {% from 'common.html' import disp_post %} {% extends 'base.html' %} {% block header %} -<h1>{%block title%}editing post{%endblock%}</h1> +<h1>{%block title%}editing {% if post.wiki == 1 %}wiki{% endif %} post{%endblock%}</h1> {% endblock %} {% block content %} <form method="post"> <textarea class="new-post-box" name="newcontent">{{post.content}}</textarea> +{% if post.wiki == 1 %} +<p> +<label for="comment">comment</label> +<input type="text" id="comment" name="comment" placeholder="fixed spelling issues"> +</p> +{% endif %} <p>confirm edit?</p> <input type="submit" value="confirm"> <a href="{{post_jump(post.id)}}">cancel</a> diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index a67d846..b2afc03 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -8,7 +8,7 @@ {%endblock%} {%block content%} -{{forum.description|md|safe}} +{{forum.description|md(forum.id)|safe}} <hr/> <div class="forum-info"> @@ -46,6 +46,9 @@ <p> {% if has_permission(forum.id, g.user, "p_create_threads") %} <a class="actionbutton" href="{{url_for('forum.create_thread',forum_id=forum.id)}}">create new thread</a> + {% if has_permission(forum.id, g.user, "p_edit_wiki") %} + <a class="actionbutton" href="{{url_for('forum.create_thread',forum_id=forum.id)}}?type=wiki_page">create new wiki page</a> + {% endif %} {% elif has_permission(forum.id, g.user, "p_create_threads", login_required=False) %} please log in to create a new thread {% else %} diff --git a/apioforum/templates/view_post.html b/apioforum/templates/view_post.html index fcaf29b..df27040 100644 --- a/apioforum/templates/view_post.html +++ b/apioforum/templates/view_post.html @@ -1,12 +1,50 @@ -{% from 'common.html' import disp_post %} +{% from 'common.html' import disp_post, ts %} {% extends 'base.html' %} {% block header %} -<h1>{%block title%}viewing post{%endblock%}</h1> +<h1> + {%block title%} + {% if not revision %} + viewing post #{{post.id}} + {% else %} + viewing revision #{{revision.rev_index}} of #{{post.id}} + {% endif %} + {%endblock%} +</h1> {% endblock %} {% block content %} -{{disp_post(post,False)}} -<p>post source:</p> -<textarea readonly class="new-post-box" name="newcontent">{{post.content}}</textarea> -<a href="{{post_jump(post.id)}}">i am satisfied</a> + +{{disp_post(post)}} + +<details><summary>view post source</summary> +<textarea readonly class="view-post-source new-post-box" name="newcontent">{{post.content}}</textarea> +</details> + +{% if history %} +<h2 id="history">revision history</h2> +<table class="history"> +{% for rev in history %} +{% set is_current = (not revision and loop.index==1) or (rev.rev_index == revision.rev_index) %} + + <tr> + <td> + {% if is_current %} + #{{post.id}}/{{rev.rev_index}} + {% else %} + <a href="{{url_for('thread.view_post', + post_id=post.id, revision=rev.rev_index if loop.index!=1 else None, + _anchor=None)}}"> + #{{post.id}}/{{rev.rev_index}}</a> + {% endif %} + </td> + <td>{{disp_user(rev.editor)}} + <td>{{ts(rev.time)}}</td> + {% if rev.comment %}<td>{{rev.comment}}</td>{%endif%} +{% endfor %} +</table> +{% endif %} + +<p><a href="{{post_jump(post.id)}}">i am satisfied</a></p> + + {% endblock %} diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index 8c7843a..7e35db2 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -1,4 +1,4 @@ -{% from 'common.html' import disp_post,tag_in_forum,thread_breadcrumb,vote_meter,pagination_nav %} +{% from 'common.html' import ts,disp_post,tag_in_forum,thread_breadcrumb,vote_meter,pagination_nav %} {% extends 'base.html' %} {% block header %} <h1>{%block title %}{{thread.title}}{% endblock %} <span class="thing-id">#{{thread.id}}</span></h1> @@ -18,10 +18,10 @@ <div class="thread-top-bar"> <span class="thread-top-bar-a"> {% if g.user == thread.creator or has_permission(thread.forum, g.user, "p_manage_threads") %} - <a class="actionbutton" href="{{url_for('thread.config_thread',thread_id=thread.id)}}">configure thread</a> + <a class="actionbutton" href="{{url_for('thread.config_thread',thread_id=thread.id)}}">configure {{thread.type}}</a> {% endif %} {% if has_permission(thread.forum, g.user, "p_delete_posts") %} - <a class="actionbutton" href="{{url_for('thread.delete_thread',thread_id=thread.id)}}">delete thread</a> + <a class="actionbutton" href="{{url_for('thread.delete_thread',thread_id=thread.id)}}">delete {{thread.type}}</a> {% endif %} </span> @@ -32,46 +32,69 @@ </span> </div> -{{ pagination_nav(page,max_pageno,'thread.view_thread',thread_id=thread.id) }} -<div class="posts"> - {% for post in posts %} - {% if votes[post.id] %} +{% macro show_post(post,wiki_body=False) %} + {% if votes[post.id] %} - {% set vote = votes[post.id] %} - {% set option_idx = vote.option_idx %} - - {# this is bad but it's going to get refactored anyway #} - {% set footer %} - {% if vote.is_retraction %} - {% if not post.deleted %} - {{post.author}} retracted their vote - {% else %} - this post retracted a vote - {% endif %} + {% set vote = votes[post.id] %} + {% set option_idx = vote.option_idx %} + + {# this is bad but it's going to get refactored anyway #} + {% set footer %} + {% if vote.is_retraction %} + {% if not post.deleted %} + {{post.author}} retracted their vote + {% else %} + this post retracted a vote + {% endif %} + {% else %} + {% set option = poll.options[option_idx-1] %} + {% if vote.current %} + {{post.author}} votes for {{option_idx}}: {{option.text}} {% else %} - {% set option = poll.options[option_idx-1] %} - {% if vote.current %} - {{post.author}} votes for {{option_idx}}: {{option.text}} + {% if not post.deleted %} + {{post.author}} voted for {{option_idx}}: {{option.text}}, but later changed their vote {% else %} - {% if not post.deleted %} - {{post.author}} voted for {{option_idx}}: {{option.text}}, but later changed their vote - {% else %} - this post presented a vote that was later changed - {% endif %} + this post presented a vote that was later changed {% endif %} {% endif %} + {% endif %} + + {% endset %} - {% endset %} + {{ disp_post(post, forum=thread.forum, buttons=True, footer=footer) }} - {{ disp_post(post, forum=thread.forum, buttons=True, footer=footer) }} - - {% else %} - {{ disp_post(post, forum=thread.forum, buttons=True) }} + {% elif wiki_body and post.wiki and post.edited %} + {% set footer %} + {{disp_user(post.editor)}} {{ts(post.updated)}}: + <span class="wiki-footer-comment">{{post.comment}}</span> + {% endset %} + + {{ disp_post(post, forum=thread.forum, buttons=True, footer=footer) }} + + {% else %} + {{ disp_post(post, forum=thread.forum, buttons=True) }} + {% endif %} +{% endmacro %} + +{% if wiki_body %} +<div class="wiki-body"> + {{show_post(wiki_body,True)}} +</div> +{% endif %} + +<div id="replies"> +{% if not wiki_body or page != 1 or posts|length > 1 %} + {{ pagination_nav(page,max_pageno,'thread.view_thread',thread_id=thread.id,fragment="replies") }} +{% endif %} +<div class="posts"> + {% for post in posts %} + {% if post != wiki_body %} + {{show_post(post)}} {% endif %} {% endfor %} </div> {% if page != max_pageno %} - {{ pagination_nav(page,max_pageno,'thread.view_thread',thread_id=thread.id,bottom=True) }} + {{ pagination_nav(page,max_pageno,'thread.view_thread',thread_id=thread.id,bottom=True,fragment="replies") }} {% elif g.user and has_permission(thread.forum, g.user, "p_reply_threads") %} {% if thread.locked and not has_permission(thread.forum, g.user, "p_manage_threads") %} <p>this thread is locked, and may not receive further replies</p> @@ -123,4 +146,5 @@ {% else %} <p>please log in to reply to this thread</p> {% endif %} +</div> {% endblock %} diff --git a/apioforum/templates/view_user.html b/apioforum/templates/view_user.html index 2235b5e..fa0d194 100644 --- a/apioforum/templates/view_user.html +++ b/apioforum/templates/view_user.html @@ -11,13 +11,13 @@ {% endif %} {% if user.banned %} <span><b>BANNED:</b> {{ user.banreason or "(no reason given)" }}</span> - {%elif is_admin%} + {%elif is_admin()%} <a class="actionbutton" href="{{url_for('user.ban_user',username=user.username)}}">ban user</a> {% endif %} </div> <div class="user_info"> <div class="user_bio_quote"> - <div class="user_bio">{{(user.bio or "hail GEORGE")|md|safe}}</div> + <div class="user_bio">{{(user.bio or "hail GEORGE")|md(None)|safe}}</div> <p class="user_bio_attribution">— {{user.username|e}}</p> </div> <dl> @@ -34,7 +34,7 @@ {{ pagination_nav(page,max_pageno,'user.view_user', username=user.username) }} <div class="user_posts"> {% for post in posts %} - {{ disp_post(post, False) }} + {{ disp_post(post, False, preview = True) }} {% endfor %} </div> {{ pagination_nav(page,max_pageno,'user.view_user', username=user.username,bottom=True) }} diff --git a/apioforum/thread.py b/apioforum/thread.py index d57e500..4e3b326 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -8,8 +8,10 @@ from flask import ( ) from .db import get_db from .roles import has_permission +from .permissions import is_admin from . import webhooks from . import forum +from . import wiki from .forum import get_avail_tags bp = Blueprint("thread", __name__, url_prefix="/thread") @@ -48,9 +50,7 @@ def thread_info(thread_id): db = get_db() return db.execute("SELECT * FROM threads WHERE id = ?;",(thread_id,)).fetchone() -@bp.route("/<int:thread_id>") -@bp.route("/<int:thread_id>/page/<int:page>") -def view_thread(thread_id,page=1): +def do_view_thread(thread_id,page=1): db = get_db() thread = thread_info(thread_id) if thread is None: @@ -116,6 +116,14 @@ def view_thread(thread_id,page=1): db.execute("DELETE FROM read WHERE user = ? AND thread = ?;", (g.user, thread_id)) db.execute("INSERT INTO read (user,thread,time) VALUES (?,?,current_timestamp);", (g.user, thread_id)) db.commit() + + wiki_body = None + if thread['type'] == "wiki page": + wiki_body = db.execute(""" + SELECT * FROM posts + WHERE posts.thread = ? + ORDER BY created ASC; + """,(thread_id,)).fetchone() return render_template( "view_thread.html", @@ -125,10 +133,20 @@ def view_thread(thread_id,page=1): poll=poll, votes=votes, has_voted=has_voted, + wiki_body=wiki_body, page=page, max_pageno=max_pageno, ) +@bp.route("/<int:thread_id>") +@bp.route("/<int:thread_id>/page/<int:page>") +def view_thread(thread_id, page=1): + db = get_db() + wiki_page = db.execute("SELECT * FROM wiki_pages WHERE thread = ?;",(thread_id,)).fetchone() + if page == 1 and wiki_page: + return redirect(url_for('forum.view_wiki_page', forum_id=wiki_page['forum'], path=wiki_page['path'])) + return do_view_thread(thread_id, page) + def register_vote(thread,pollval): if pollval is None or pollval == 'dontvote': return @@ -322,11 +340,15 @@ def create_post(thread_id): def delete_post(post_id): db = get_db() post = db.execute("SELECT * FROM posts WHERE id = ?",(post_id,)).fetchone() + thread = thread_info(post['thread']) if post is None: flash("that post doesn't exist") return redirect("/") jump_url = post_jump(post_id) + if post['wiki'] == 1: + flash("cannot delete wiki post") + return redirect(jump_url) if post['author'] != g.user and not has_permission(thread['forum'], g.user, "p_delete_posts"): flash("you do not have permission to do that") return redirect(jump_url) @@ -370,23 +392,50 @@ def delete_thread(thread_id): def edit_post(post_id): db = get_db() post = db.execute("SELECT * FROM posts WHERE id = ?",(post_id,)).fetchone() + thread = thread_info(post['thread']) if post is None: flash("that post doesn't exist") return redirect(url_for('index')) jump_url = post_jump(post_id) if post['author'] != g.user: - flash("you can only edit posts that you created") - return redirect(jump_url) + if post['wiki'] != 1: + flash("you can only edit posts that you created") + return redirect(jump_url) + elif thread['locked'] == 1: + flash("you cannot edit this post because the page is locked") + return redirect(jump_url) + if not has_permission(thread['forum'], g.user, "p_edit_wiki"): + flash("you do not have permission to do that") + return redirect(jump_url) if request.method == "POST": err = None newcontent = request.form['newcontent'] + comment = request.form['comment'] if 'comment' in request.form else "" + 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(""" + 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) @@ -409,8 +458,40 @@ def view_post(post_id): if not has_permission(post['f_id'], g.user, "p_view_threads", False): abort(403) - return render_template("view_post.html",post=post) - + can_view_history = post['wiki'] or is_admin() or g.user == post['author'] + + rev = None + if 'revision' in request.args: + rev_index = None + try: + rev_index = int(request.args['revision']) + except ValueError: + abort(404) + if not can_view_history: + abort(403) + rev = db.execute( + "SELECT * FROM past_revisions WHERE post=? and rev_index=?;", + (post_id,rev_index)).fetchone() + if not rev: + abort(404) + post = dict(post) + post['content'] = rev['content'] + post['updated'] = rev['time'] + post['rev_index'] = rev['rev_index'] + + history = None + if can_view_history: + history = db.execute(""" + 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, rev_index + FROM posts WHERE id=?""",(post_id,)).fetchone()) + + return render_template("view_post.html",post=post,revision=rev,history=history) def apply_selected_tags(thread, form): db = get_db() @@ -460,9 +541,21 @@ def config_thread(thread_id): if len(title.strip()) == 0: err.append("title can't be empty") else: - db.execute("update threads set title = ? where id = ?;",(title,thread_id)) - flash("title updated titledly") - db.commit() + ok = True + wiki_path = wiki.normalize_page_name(title) + if thread['type'] == "wiki page": + existing = db.execute(""" + SELECT * FROM wiki_pages WHERE path = ? AND forum = ?; + """,(wiki_path,thread['forum'])).fetchone() + if existing and existing['thread'] != thread_id: + err.append("there's already a page with that name") + ok = False + if ok: + if thread['type'] == "wiki page": + db.execute("UPDATE wiki_pages SET path = ? WHERE thread = ?;",(wiki_path,thread_id)) + db.execute("update threads set title = ? where id = ?;",(title,thread_id)) + flash("title updated titledly") + db.commit() if apply_selected_tags(thread, request.form): db.commit() diff --git a/apioforum/wiki.py b/apioforum/wiki.py new file mode 100644 index 0000000..9cd2b8e --- /dev/null +++ b/apioforum/wiki.py @@ -0,0 +1,2 @@ +def normalize_page_name(name): + return name.lower().replace(" ", "_").replace("/", "_") |
