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