From 8481d0c07475aee7b9ad4a6aa899bcc9af437e64 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 15 Jun 2021 18:24:48 +0000 Subject: fetch tags from database --- apioforum/forum.py | 14 ++++++++++++-- apioforum/templates/common.html | 4 ++-- apioforum/templates/view_forum.html | 6 +++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/apioforum/forum.py b/apioforum/forum.py index 81674a8..98c71f7 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -5,11 +5,13 @@ from flask import ( Blueprint, render_template, request, g, redirect, url_for, flash ) + from .db import get_db from .mdrender import render bp = Blueprint("forum", __name__, url_prefix="/") + @bp.route("/") def view_forum(): db = get_db() @@ -21,7 +23,15 @@ def view_forum(): GROUP BY threads.id ORDER BY threads.updated DESC; """).fetchall() - return render_template("view_forum.html",threads=threads) + thread_tags = {} + #todo: somehow optimise this + for thread in threads: + thread_tags[thread['id']] = db.execute( + """SELECT tags.* FROM tags + INNER JOIN thread_tags ON thread_tags.tag = tags.id + WHERE thread_tags.thread = ?; + """,(thread['id'],)).fetchall() + return render_template("view_forum.html",threads=threads,thread_tags=thread_tags) @bp.route("/create_thread",methods=("GET","POST")) def create_thread(): @@ -77,4 +87,4 @@ def search(): 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) \ No newline at end of file + return render_template("search_results.html", results=results, query=query, rendered_posts=rendered_posts, display_thread_id=display_thread_id) diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index 042f8bd..2e59b2c 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -27,6 +27,6 @@ {%- endmacro %} -{% macro tag(name, fg, bg) -%} -{{name}} +{% macro tag(the_tag) -%} +{{the_tag.name}} {%- endmacro %} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index ee0e496..6958c20 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -37,7 +37,11 @@ {%for thread in threads%}
-
{{tag("the","black","yellow")}} {{tag("bees","yellow","black")}} {{tag("excellent","black","lightsteelblue")}}
+
+ {% for the_tag in thread_tags[thread.id] %} + {{tag(the_tag)}} + {% endfor %} +
{{thread.creator}}
{{ts(thread.created)}}
{{ts(thread.updated)}}
-- cgit v1.2.3