From b1780ca83b786eea64e8bc15b89284509485d0e4 Mon Sep 17 00:00:00 2001 From: citrons Date: Sun, 20 Jun 2021 06:42:51 +0000 Subject: refactoring and UI improvement --- apioforum/__init__.py | 6 +++++- apioforum/auth.py | 5 ++--- apioforum/forum.py | 29 +++++++++++++++---------- apioforum/templates/base.html | 3 ++- apioforum/templates/common.html | 2 +- apioforum/templates/config_thread.html | 26 +++++++++++----------- apioforum/templates/delete_post.html | 4 +--- apioforum/templates/search_results.html | 4 +--- apioforum/templates/user_settings.html | 6 ------ apioforum/templates/view_thread.html | 4 +--- apioforum/templates/view_user.html | 6 ++---- apioforum/thread.py | 38 +++++++++++++++------------------ apioforum/user.py | 12 +++-------- 13 files changed, 65 insertions(+), 80 deletions(-) diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 54d18c3..9d49f36 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -46,7 +46,11 @@ def create_app(): if len(request.query_string) > 0: p += "?" + request.query_string.decode("utf-8") return dict(path_for_next=p) - + + from .mdrender import render + @app.template_filter('md') + def md_render(s): + return render(s) app.add_url_rule("/",endpoint="index") diff --git a/apioforum/auth.py b/apioforum/auth.py index dae7b03..8a34700 100644 --- a/apioforum/auth.py +++ b/apioforum/auth.py @@ -5,7 +5,6 @@ from flask import ( from werkzeug.security import check_password_hash, generate_password_hash from .db import get_db import functools -import datetime bp = Blueprint("auth", __name__, url_prefix="/auth") @@ -58,8 +57,8 @@ def register(): if err is None: db.execute( - "INSERT INTO users (username, password, joined) VALUES (?,?,?);", - (username,generate_password_hash(password),datetime.datetime.now()) + "INSERT INTO users (username, password, joined) VALUES (?,?,current_timestamp);", + (username,generate_password_hash(password)) ) db.commit() flash("successfully created account") diff --git a/apioforum/forum.py b/apioforum/forum.py index 30e29cb..defc5b1 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -9,6 +9,8 @@ from flask import ( from .db import get_db from .mdrender import render +from sqlite3 import OperationalError + bp = Blueprint("forum", __name__, url_prefix="/") @@ -71,15 +73,21 @@ def create_thread(): def search(): db = get_db() query = request.args["q"] - results = db.execute(""" - SELECT posts.id, highlight(posts_fts, 0, '', '') AS content, posts.thread, posts.author, posts.created, posts.edited, posts.updated, threads.title AS thread_title - FROM posts_fts - JOIN posts ON posts_fts.rowid = posts.id - JOIN threads ON threads.id = posts.thread - WHERE posts_fts MATCH ? - ORDER BY rank - LIMIT 50 - """, (query,)).fetchall() + try: + results = db.execute(""" + SELECT posts.id, highlight(posts_fts, 0, '', '') AS + content, posts.thread, posts.author, posts.created, posts.edited, + posts.updated, threads.title AS thread_title + FROM posts_fts + JOIN posts ON posts_fts.rowid = posts.id + JOIN threads ON threads.id = posts.thread + WHERE posts_fts MATCH ? + ORDER BY rank + LIMIT 50 + """, (query,)).fetchall() + except OperationalError: + flash('your search query was malformed.') + return redirect(url_for("forum.view_forum")) display_thread_id = [ True ] * len(results) last_thread = None @@ -87,5 +95,4 @@ def search(): if result["thread"] == last_thread: display_thread_id[ix] = False last_thread = result["thread"] - rendered_posts = [render(q['content']) for q in results] - return render_template("search_results.html", results=results, query=query, rendered_posts=rendered_posts, display_thread_id=display_thread_id) + return render_template("search_results.html", results=results, query=query, display_thread_id=display_thread_id) diff --git a/apioforum/templates/base.html b/apioforum/templates/base.html index 573c9ce..637cc09 100644 --- a/apioforum/templates/base.html +++ b/apioforum/templates/base.html @@ -1,4 +1,5 @@ {# BASED? BASED ON WHAT? #} +{% from 'common.html' import disp_user with context %} @@ -20,7 +21,7 @@

home

{% if g.user %} -

{{g.user}}

+

{{ disp_user(g.user) }}

{% if is_admin %}

admin

diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index 3db9974..28598e7 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -23,7 +23,7 @@
- {{ caller() }} + {{ post.content|md|safe }}
{% endmacro %} diff --git a/apioforum/templates/config_thread.html b/apioforum/templates/config_thread.html index 973fbf5..b26a73d 100644 --- a/apioforum/templates/config_thread.html +++ b/apioforum/templates/config_thread.html @@ -5,25 +5,23 @@
title -

if you want to change the title of this thread, make sure you check the "change title?" box.

- -
tags -

if you want to change the tags on this thread, make sure you check the "change tags?" box.

- -
-
    - {% for the_tag in avail_tags %} -
  • - - -
  • - {% endfor %} -
+{% if avail_tags %} +
    + {% for the_tag in avail_tags %} +
  • + + +
  • + {% endfor %} +
+{% else %} +

there are no available tags.

+{% endif %}

confirm changes?

diff --git a/apioforum/templates/delete_post.html b/apioforum/templates/delete_post.html index 6f99704..2f16598 100644 --- a/apioforum/templates/delete_post.html +++ b/apioforum/templates/delete_post.html @@ -5,9 +5,7 @@ {% endblock %} {% block content %} -{% call disp_post(post, False) %} -{{ rendered_content | safe }} -{% endcall %} +{{ disp_post(post, False) }}

confirm delete?

diff --git a/apioforum/templates/search_results.html b/apioforum/templates/search_results.html index 4d0be2f..fe016ab 100644 --- a/apioforum/templates/search_results.html +++ b/apioforum/templates/search_results.html @@ -16,9 +16,7 @@
{% endif %} - {% call disp_post(result, False) %} - {{ rendered_posts[loop.index0] | safe}} - {% endcall %} + {{ disp_post(result, False) }} {% endfor %} {% if results|length > 0 %} diff --git a/apioforum/templates/user_settings.html b/apioforum/templates/user_settings.html index ad93036..cac613a 100644 --- a/apioforum/templates/user_settings.html +++ b/apioforum/templates/user_settings.html @@ -4,9 +4,6 @@
change password -

if you want to change your password, make sure you check the "change password?" box.

- -

@@ -14,9 +11,6 @@
change bio -

if you want to change your bio, make sure you check the "change bio?" box.

- -
diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index abd6aaa..fb62880 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -21,9 +21,7 @@
{% for post in posts %} - {% call disp_post(post, True) %} - {{ rendered_posts[loop.index0] | safe}} - {% endcall %} + {{ disp_post(post, True) }} {% endfor %}
{% if g.user %} diff --git a/apioforum/templates/view_user.html b/apioforum/templates/view_user.html index f773978..612c2c0 100644 --- a/apioforum/templates/view_user.html +++ b/apioforum/templates/view_user.html @@ -12,7 +12,7 @@