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 From 589dae7625f7f98ee3d546887792db3dfc90b464 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 18 Jul 2021 13:59:06 +0000 Subject: fix spacing on vote meter --- apioforum/static/style.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apioforum/static/style.css b/apioforum/static/style.css index 6178dd9..5263568 100644 --- a/apioforum/static/style.css +++ b/apioforum/static/style.css @@ -137,6 +137,11 @@ nav .links { display: flex; } .thread-preview-post { font-style: italic; } .thread-preview-ts { font-weight: bold; } +.thread-vote-summary { + margin-top: 4px; + margin-bottom: -8px; +} + /* wide screens */ @media all and (min-width: 600px) { .thread-listing-title { font-size: larger; } -- cgit v1.2.3 From 8d2e7d0f6562f42730b8bc62576f10e7d06e5117 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 18 Jul 2021 14:15:18 +0000 Subject: minor change things --- apioforum/__init__.py | 3 +++ apioforum/templates/view_forum.html | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/apioforum/__init__.py b/apioforum/__init__.py index 6c9eaa8..364bad3 100644 --- a/apioforum/__init__.py +++ b/apioforum/__init__.py @@ -17,6 +17,9 @@ def create_app(): except OSError: pass + app.jinja_env.trim_blocks = True + app.jinja_env.lstrip_blocks = True + from . import db db.init_app(app) from . import permissions diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 017d9f0..2e49a41 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -76,7 +76,7 @@ poll: {{poll.title}} {% for opt in poll.options %} - option "{{opt.text}}": {{opt.num}} votes + option "{{opt.text}}": {{opt.num or 0}} votes {% endfor %} total votes: {{total_votes}} -- cgit v1.2.3 From 153ac274490c08375d58fa83042406c6ec52f968 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 18 Jul 2021 14:38:07 +0000 Subject: vote meter on view thread page --- apioforum/forum.py | 2 +- apioforum/templates/common.html | 37 ++++++++++++++++++++++++++++++++++ apioforum/templates/view_forum.html | 39 ++---------------------------------- apioforum/templates/view_thread.html | 3 ++- apioforum/thread.py | 6 +++++- 5 files changed, 47 insertions(+), 40 deletions(-) diff --git a/apioforum/forum.py b/apioforum/forum.py index 56811e0..1da6d27 100644 --- a/apioforum/forum.py +++ b/apioforum/forum.py @@ -45,7 +45,7 @@ def view_forum(): # 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 + LEFT OUTER JOIN total_vote_counts ON polls.id = total_vote_counts.poll WHERE polls.id = ?; """,(thread['poll'],)).fetchone() options = db.execute(""" diff --git a/apioforum/templates/common.html b/apioforum/templates/common.html index 3321085..c8afd9e 100644 --- a/apioforum/templates/common.html +++ b/apioforum/templates/common.html @@ -49,3 +49,40 @@ {% macro tag(the_tag) -%} {{the_tag.name}} {%- endmacro %} + +{% macro vote_meter(poll) %} + {% 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 or 0}} votes + {% endfor %} + total votes: {{total_votes}} + + +{% endmacro %} diff --git a/apioforum/templates/view_forum.html b/apioforum/templates/view_forum.html index 2e49a41..e8d4f8a 100644 --- a/apioforum/templates/view_forum.html +++ b/apioforum/templates/view_forum.html @@ -1,5 +1,5 @@ {% extends 'base.html' %} -{% from 'common.html' import ts, tag, disp_user, post_url %} +{% from 'common.html' import ts, tag, disp_user, post_url, vote_meter %} {% block header %}

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

{%endblock%} {%block content%}

welcome to the apioforum

@@ -44,43 +44,8 @@ {% 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 or 0}} votes - {% endfor %} - total votes: {{total_votes}} - - + {{ vote_meter(thread_polls[thread.id]) }}
{% endif %} diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index 7bf253d..aec9103 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -1,4 +1,4 @@ -{% from 'common.html' import disp_post,tag %} +{% from 'common.html' import disp_post,tag,vote_meter %} {% extends 'base.html' %} {% block header %}

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

@@ -12,6 +12,7 @@
  • #{{opt.option_idx}} - {{opt.text}} - {{opt.num or 0}}
  • {%endfor%} +{{ vote_meter(poll) }} {% endif %}
    diff --git a/apioforum/thread.py b/apioforum/thread.py index daf0b85..f8e8036 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -33,7 +33,11 @@ def view_thread(thread_id): poll = None votes = None if thread['poll'] is not None: - poll_row = db.execute("SELECT * FROM polls where id = ?",(thread['poll'],)).fetchone() + 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 -- cgit v1.2.3 From 2a0a21ae101ce54080bdedd323a6ca6ddf36d35f Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 18 Jul 2021 14:43:00 +0000 Subject: fix displaying of poll options --- apioforum/templates/view_thread.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index aec9103..429621a 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -7,11 +7,11 @@ {%block content%} {% if poll %}

    {{poll.title}}

    -
      +
        {%for opt in poll.options%} -
      1. #{{opt.option_idx}} - {{opt.text}} - {{opt.num or 0}}
      2. +
      3. {{opt.text}}: {{opt.num or 0}} votes
      4. {%endfor%} -
    + {{ vote_meter(poll) }} {% endif %}
    -- cgit v1.2.3 From 2d92be0e223a8b54749d0b2d304a78b16b92000d Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 18 Jul 2021 14:51:55 +0000 Subject: shouldn't count retractions as part of the total vote count --- apioforum/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apioforum/db.py b/apioforum/db.py index ba1f6a3..f3693a9 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -117,7 +117,7 @@ CREATE VIEW vote_counts AS """, """ CREATE VIEW total_vote_counts AS - SELECT poll, count(*) AS total_votes FROM votes WHERE current GROUP BY poll; + SELECT poll, count(*) AS total_votes FROM votes WHERE current AND NOT is_retraction GROUP BY poll; """, ] -- cgit v1.2.3