From a2c82ca4f96420234460ea5fdea5df37078117d7 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 17 Jul 2021 23:27:37 +0000 Subject: vote meters idk --- apioforum/__init__.py | 3 +++ apioforum/db.py | 4 ++++ apioforum/forum.py | 32 +++++++++++++++++++++++++++-- apioforum/templates/view_forum.html | 40 +++++++++++++++++++++++++++++++++++++ setup.py | 1 + 5 files changed, 78 insertions(+), 2 deletions(-) diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 9d49f36..6c9eaa8 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -40,6 +40,9 @@ def create_app(): from .fuzzy import fuzzy app.jinja_env.filters['fuzzy']=fuzzy + from .util import gen_colour + app.jinja_env.filters['gen_colour']=gen_colour + @app.context_processor def path_for_next(): p = request.path diff --git a/apioforum/db.py b/apioforum/db.py index 25bda94..ba1f6a3 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -115,6 +115,10 @@ ALTER TABLE posts ADD COLUMN vote INTEGER REFERENCES votes(id); CREATE VIEW vote_counts AS SELECT poll, option_idx, count(*) AS num FROM votes WHERE current GROUP BY option_idx,poll; """, +""" +CREATE VIEW total_vote_counts AS + SELECT poll, count(*) AS total_votes FROM votes WHERE current GROUP BY poll; +""", ] diff --git a/apioforum/forum.py b/apioforum/forum.py index fa6fcc8..56811e0 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -19,7 +19,7 @@ def view_forum(): db = get_db() threads = db.execute( """SELECT threads.id, threads.title, threads.creator, threads.created, - threads.updated, count(posts.id) as num_replies, max(posts.id), posts.author as last_user + threads.updated, threads.poll, count(posts.id) as num_replies, max(posts.id), posts.author as last_user FROM threads INNER JOIN posts ON posts.thread = threads.id GROUP BY threads.id @@ -27,6 +27,7 @@ def view_forum(): """).fetchall() thread_tags = {} preview_post = {} + thread_polls = {} #todo: somehow optimise this for thread in threads: thread_tags[thread['id']] = db.execute( @@ -39,10 +40,37 @@ def view_forum(): """SELECT * FROM posts WHERE thread = ? ORDER BY created DESC; """,(thread['id'],)).fetchone() + + if thread['poll'] is not None: + # todo: make this not be duplicated from thread.py + poll_row= db.execute(""" + SELECT polls.*,total_vote_counts.total_votes FROM polls + LEFT OUTER JOIN total_vote_counts ON polls.id = total_vote_counts.poll + WHERE polls.id = ?; + """,(thread['poll'],)).fetchone() + options = db.execute(""" + SELECT poll_options.*, vote_counts.num + FROM poll_options + LEFT OUTER JOIN vote_counts ON poll_options.poll = vote_counts.poll + AND poll_options.option_idx = vote_counts.option_idx + WHERE poll_options.poll = ? + ORDER BY option_idx asc; + """,(poll_row['id'],)).fetchall() + + poll = {} + poll.update(poll_row) + poll['options'] = options + poll['total_votes']=poll['total_votes'] or 0 + thread_polls[thread['id']]=poll + + + + return render_template("view_forum.html", threads=threads, thread_tags=thread_tags, - preview_post=preview_post + preview_post=preview_post, + thread_polls=thread_polls ) @bp.route("/create_thread",methods=("GET","POST")) diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index b27088d..017d9f0 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -43,6 +43,46 @@ {% endif %} + {% if thread_polls[thread.id] %} + {% set poll = thread_polls[thread.id] %} + {% set total_votes = poll.total_votes %} + {% set n = namespace() %} + {% set n.runningtotal = 0 %} + +
+ + {% if total_votes == 0 %} + no votes + {% else %} + {% for opt in poll.options %} + {% set opt_count = opt.num or 0 %} + {% set colour = (loop.count|string + opt.text)|gen_colour %} + {% if opt_count != 0 %} + {% set percentage = 100*(opt_count/total_votes) %} + {# todo: do this in css somehow #} + {% if opt.text|length > 10 %} + {% set opt_text = opt.text[:7] + "..." %} + {% else %} + {% set opt_text = opt.text %} + {% endif %} + + + {{opt_text}}: {{opt_count}} + + {% set n.runningtotal = n.runningtotal + percentage %} + {% endif %} + {% endfor %} + {% endif %} + + poll: {{poll.title}} + {% for opt in poll.options %} + option "{{opt.text}}": {{opt.num}} votes + {% endfor %} + total votes: {{total_votes}} + + +
+ {% endif %} {%endfor%} diff --git a/setup.py b/setup.py index 089f3a4..15992ed 100644 --- a/setup.py +++ b/setup.py @@ -10,5 +10,6 @@ setup( 'markdown', 'bleach', 'pymdown-extensions', + 'hsluv', ], ) -- cgit v1.2.3