diff options
author | ubq323 <ubq323> | 2021-07-18 14:38:07 +0000 |
---|---|---|
committer | ubq323 <ubq323> | 2021-07-18 14:38:07 +0000 |
commit | 153ac274490c08375d58fa83042406c6ec52f968 (patch) | |
tree | 26e878a1c7df08b1a68c7ab02bd2c85550c8a3b4 | |
parent | 8d2e7d0f6562f42730b8bc62576f10e7d06e5117 (diff) |
vote meter on view thread page
-rw-r--r-- | apioforum/forum.py | 2 | ||||
-rw-r--r-- | apioforum/templates/common.html | 37 | ||||
-rw-r--r-- | apioforum/templates/view_forum.html | 39 | ||||
-rw-r--r-- | apioforum/templates/view_thread.html | 3 | ||||
-rw-r--r-- | 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) -%} <span class="tag" style="color: {{the_tag.text_colour}}; background-color: {{the_tag.bg_colour}}">{{the_tag.name}}</span> {%- endmacro %} + +{% macro vote_meter(poll) %} + {% set total_votes = poll.total_votes %} + {% set n = namespace() %} + {% set n.runningtotal = 0 %} + <svg width="100%" height="15px" xmlns="http://www.w3.org/2000/svg"> + {% if total_votes == 0 %} + <text text-anchor="middle" dominant-baseline="middle" x="11%" y="55%" fill="black" style="font-size:15px">no votes</text> + {% 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 %} + <rect y="0" height="100%" x="{{n.runningtotal}}%" width="{{percentage}}%" stroke="black" fill="{{colour}}" /> + <text text-anchor="middle" dominant-baseline="middle" y="55%" fill="black" style="font-size:15px" x="{{n.runningtotal+(percentage/2)}}%"> + {{opt_text}}: {{opt_count}} + </text> + {% set n.runningtotal = n.runningtotal + percentage %} + {% endif %} + {% endfor %} + {% endif %} + <desc> + poll: {{poll.title}} + {% for opt in poll.options %} + option "{{opt.text}}": {{opt.num or 0}} votes + {% endfor %} + total votes: {{total_votes}} + </desc> + </svg> +{% 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 %}<h1>{% block title %}apioforum{%endblock%}</h1>{%endblock%} {%block content%} <p>welcome to the apioforum</p> @@ -44,43 +44,8 @@ </div> {% 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 %} - <div class="thread-vote-summary"> - <svg width="100%" height="15px" xmlns="http://www.w3.org/2000/svg"> - {% if total_votes == 0 %} - <text text-anchor="middle" dominant-baseline="middle" x="11%" y="55%" fill="black" style="font-size:15px">no votes</text> - {% 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 %} - <rect y="0" height="100%" x="{{n.runningtotal}}%" width="{{percentage}}%" stroke="black" fill="{{colour}}" /> - <text text-anchor="middle" dominant-baseline="middle" y="55%" fill="black" style="font-size:15px" x="{{n.runningtotal+(percentage/2)}}%"> - {{opt_text}}: {{opt_count}} - </text> - {% set n.runningtotal = n.runningtotal + percentage %} - {% endif %} - {% endfor %} - {% endif %} - <desc> - poll: {{poll.title}} - {% for opt in poll.options %} - option "{{opt.text}}": {{opt.num or 0}} votes - {% endfor %} - total votes: {{total_votes}} - </desc> - </svg> + {{ vote_meter(thread_polls[thread.id]) }} </div> {% endif %} </div> 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 %} <h1>{%block title %}{{thread.title}}{% endblock %}</h1> @@ -12,6 +12,7 @@ <li>#{{opt.option_idx}} - {{opt.text}} - {{opt.num or 0}}</li> {%endfor%} </ul> +{{ vote_meter(poll) }} {% endif %} <div class="thread-top-bar"> <span class="thread-top-bar-a"> 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 |