From 1b9ac4113239d2e963787ba5048a473394ee0a7c Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 15 Jun 2021 00:28:08 +0000 Subject: add stylings for tags, and change styles of view forum page to give us more space too --- apioforum/db.py | 12 ++++++++++++ apioforum/static/style.css | 14 ++++++++++++-- apioforum/templates/base.html | 2 ++ apioforum/templates/common.html | 4 ++++ apioforum/templates/view_forum.html | 10 ++++++++-- 5 files changed, 38 insertions(+), 4 deletions(-) diff --git a/apioforum/db.py b/apioforum/db.py index c24aa0e..1d7bd2b 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -65,7 +65,19 @@ CREATE TRIGGER posts_au AFTER UPDATE ON posts BEGIN INSERT INTO posts_fts(posts_fts, rowid, content) VALUES('delete', old.id, old.content); INSERT INTO posts_fts(rowid, content) VALUES (new.id, new.content); END; +""", """ +CREATE TABLE tags ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + text_colour TEXT NOT NULL, + bg_colour TEXT NOT NULL +); +CREATE TABLE thread_tags ( + thread INTEGER NOT NULL REFERENCES threads(id), + tag INTEGER NOT NULL REFERENCES tags(id) +); +""", ] def init_db(): diff --git a/apioforum/static/style.css b/apioforum/static/style.css index df8ce7e..0672548 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -69,7 +69,7 @@ nav .links { display: flex; } .threadlisting { display: contents } .threadlistings { display: grid; - grid-template-columns: 2fr repeat(4,1fr) 0.4fr; + grid-template-columns: 2fr repeat(5,1fr) 0.4fr; } .threadlisting-part { @@ -81,13 +81,14 @@ nav .links { display: flex; } border-bottom: 1px solid black; } + } /* small screens */ @media not all and (min-width: 800px) { .threadlisting { display: grid; - grid-template-columns: repeat(5,1fr); + grid-template-columns: repeat(6,1fr); margin-bottom: 5px; } .threadlisting-part-title { @@ -141,6 +142,9 @@ main { max-width: 60ch; margin: auto; } +main.widemain { + max-width: 120ch; +} blockquote { margin-left: 10px; @@ -151,3 +155,9 @@ blockquote { .search-form { display: inline-block; } + +.tag { + font-size: .75rem; + padding: 1px 3px; + border: 1px solid black; +} diff --git a/apioforum/templates/base.html b/apioforum/templates/base.html index ba96c91..1802ee5 100644 --- a/apioforum/templates/base.html +++ b/apioforum/templates/base.html @@ -49,9 +49,11 @@
{{ msg }}
{% endfor %} + {% block nmcontent %}
{%block content %}{% endblock %}
+ {% endblock %} diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index 33aee0b..042f8bd 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -26,3 +26,7 @@ {% macro ts(dt) -%} {%- endmacro %} + +{% macro tag(name, fg, bg) -%} +{{name}} +{%- endmacro %} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index a4a1ee1..ee0e496 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -1,7 +1,8 @@ {% extends 'base.html' %} -{% from 'common.html' import ts %} +{% from 'common.html' import ts, tag %} {% block header %}

{% block title %}apioforum{%endblock%}

{%endblock%} -{%block content%} +{%block nmcontent%} +

welcome to the apioforum

forum rules: do not be a bad person. do not do bad things.

{% if g.user %} @@ -14,6 +15,9 @@
name
+
+ tags +
creator
@@ -33,6 +37,7 @@ {%for thread in threads%}
+
{{tag("the","black","yellow")}} {{tag("bees","yellow","black")}} {{tag("excellent","black","lightsteelblue")}}
{{thread.creator}}
{{ts(thread.created)}}
{{ts(thread.updated)}}
@@ -41,4 +46,5 @@
{%endfor%} +
{%endblock%} -- cgit v1.2.3 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 From f2fb2736afac2326a8ed16d702a9a6a2d10101d0 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 15 Jun 2021 19:12:30 +0000 Subject: display tags on thread page --- apioforum/static/style.css | 14 ++++++++++++++ apioforum/templates/view_thread.html | 21 ++++++++++++++++----- apioforum/thread.py | 6 +++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index 0672548..66f7faa 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -30,6 +30,20 @@ body { font-family: sans-serif } .post-anchor-link { color: hsl(0,0%,25%); } +.thread-top-bar { + margin-bottom: 4px; +} + +.thread-top-bar-b { + float: right; + margin-right: -2px; +} + +.thread-top-bar-b .tag { + font-size: .9rem; +} + + .un-col-1 { color: hsl(0, 100%, 30%) } .un-col-2 { color: hsl(22.5, 100%, 30%) } .un-col-3 { color: hsl(45.0, 100%, 30%) } diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index eaaf581..abd6aaa 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -1,13 +1,24 @@ -{% from 'common.html' import disp_post %} +{% from 'common.html' import disp_post,tag %} {% extends 'base.html' %} {% block header %}

{%block title %}{{thread.title}}{% endblock %}

{% endblock %} {%block content%} -{% if g.user == thread.creator %} -configure thread -{% endif %} +
+ + {% if g.user == thread.creator %} + configure thread + {% endif %} + +   + + {% for the_tag in tags %} + {{ tag(the_tag) }} + {% endfor %} + +
+
{% for post in posts %} {% call disp_post(post, True) %} @@ -16,7 +27,7 @@ {% endfor %}
{% if g.user %} -
+
diff --git a/apioforum/thread.py b/apioforum/thread.py index 12ced72..e881a69 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -23,8 +23,12 @@ def view_thread(thread_id): "SELECT * FROM posts WHERE thread = ? ORDER BY created ASC;", (thread_id,) ).fetchall() + tags = db.execute( + """SELECT tags.* FROM tags + INNER JOIN thread_tags ON thread_tags.tag = tags.id + WHERE thread_tags.thread = ?""",(thread_id,)).fetchall() rendered_posts = [render(q['content']) for q in posts] - return render_template("view_thread.html",posts=posts,thread=thread,thread_id=thread_id,rendered_posts=rendered_posts) + return render_template("view_thread.html",posts=posts,thread=thread,rendered_posts=rendered_posts,tags=tags) @bp.route("//create_post", methods=("POST",)) def create_post(thread_id): -- cgit v1.2.3 From 2ed3c6588c6463e28a1f7e58a396f2cb590327cd Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 15 Jun 2021 23:01:06 +0000 Subject: allow changing tags on the configure thread page --- apioforum/templates/config_thread.html | 21 +++++++++++++++++++-- apioforum/thread.py | 17 ++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/apioforum/templates/config_thread.html b/apioforum/templates/config_thread.html index b0dd5f0..973fbf5 100644 --- a/apioforum/templates/config_thread.html +++ b/apioforum/templates/config_thread.html @@ -1,13 +1,30 @@ {% extends 'base.html' %} +{% from 'common.html' import tag %} {% block header %}

{% block title %}configure thread '{{thread.title}}'{% endblock %}

{% endblock %} {% block content %}
+
+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 %} +
+

confirm changes?

cancel diff --git a/apioforum/thread.py b/apioforum/thread.py index e881a69..fd196c5 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -113,6 +113,8 @@ def edit_post(post_id): def config_thread(thread_id): db = get_db() thread = db.execute("select * from threads where id = ?",(thread_id,)).fetchone() + thread_tags = [r['tag'] for r in db.execute("select tag from thread_tags where thread = ?",(thread_id,)).fetchall()] + avail_tags = db.execute("select * from tags order by id").fetchall() err = None if g.user is None: err = "you need to be logged in to do that" @@ -133,6 +135,19 @@ def config_thread(thread_id): db.execute("update threads set title = ? where id = ?;",(title,thread_id)) flash("title updated successfully") db.commit() + if 'do_chtags' in request.form: + wanted_tags = [] + for tagid in range(1,len(avail_tags)+1): + current = tagid in thread_tags + wanted = f'tag_{tagid}' in request.form + print(tagid, current, wanted) + if wanted and not current: + db.execute("insert into thread_tags (thread, tag) values (?,?)",(thread_id,tagid)) + flash(f"added tag {tagid}") + elif current and not wanted: + db.execute("delete from thread_tags where thread = ? and tag = ?",(thread_id,tagid)) + flash(f"removed tag {tagid}") + db.commit() if len(err) > 0: for e in err: @@ -141,5 +156,5 @@ def config_thread(thread_id): return redirect(url_for("thread.view_thread",thread_id=thread_id)) - return render_template("config_thread.html", thread=thread) + return render_template("config_thread.html", thread=thread,thread_tags=thread_tags,avail_tags=avail_tags) -- cgit v1.2.3 From 73d601135ccd40878c4ec14036737a6702478deb Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 15 Jun 2021 23:24:25 +0000 Subject: change tag change messages slightly --- apioforum/thread.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apioforum/thread.py b/apioforum/thread.py index fd196c5..30b5d84 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -136,6 +136,7 @@ def config_thread(thread_id): flash("title updated successfully") db.commit() if 'do_chtags' in request.form: + changed = False wanted_tags = [] for tagid in range(1,len(avail_tags)+1): current = tagid in thread_tags @@ -143,11 +144,13 @@ def config_thread(thread_id): print(tagid, current, wanted) if wanted and not current: db.execute("insert into thread_tags (thread, tag) values (?,?)",(thread_id,tagid)) - flash(f"added tag {tagid}") + changed = True elif current and not wanted: db.execute("delete from thread_tags where thread = ? and tag = ?",(thread_id,tagid)) - flash(f"removed tag {tagid}") - db.commit() + changed = True + if changed: + db.commit() + flash("tags updated successfully") if len(err) > 0: for e in err: -- cgit v1.2.3 From 6096acce8b922af98e6846a687fcfd19cf0370cc Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 15 Jun 2021 23:34:36 +0000 Subject: make tags less annoying to look at on small screens (i think) --- apioforum/static/style.css | 6 +++++- apioforum/templates/view_forum.html | 16 ++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index 66f7faa..935bde1 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -95,6 +95,8 @@ nav .links { display: flex; } border-bottom: 1px solid black; } + .only-small { display: none !important } + } @@ -102,7 +104,7 @@ nav .links { display: flex; } @media not all and (min-width: 800px) { .threadlisting { display: grid; - grid-template-columns: repeat(6,1fr); + grid-template-columns: repeat(5,1fr); margin-bottom: 5px; } .threadlisting-part-title { @@ -116,6 +118,8 @@ nav .links { display: flex; } border-right: 1px solid black; border-bottom: 1px solid black; } + + .only-big { display: none !important } } diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 6958c20..22f24c1 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -13,9 +13,9 @@
- name + name & tags
-
+
tags
@@ -36,8 +36,16 @@
{%for thread in threads%}
- -
+
{{thread.title}} + {% if thread_tags[thread.id]|length > 0 %} + + {% for the_tag in thread_tags[thread.id] %} + {{tag(the_tag)}} + {% endfor %} + + {%endif%} +
+
{% for the_tag in thread_tags[thread.id] %} {{tag(the_tag)}} {% endfor %} -- cgit v1.2.3