aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorubq323 <ubq323>2021-07-17 23:27:37 +0000
committerubq323 <ubq323>2021-07-17 23:27:37 +0000
commita2c82ca4f96420234460ea5fdea5df37078117d7 (patch)
treeb12848493c3630cb2aab445fc20542736b304ea9
parent2eae97d6a08da4b832ccc69ce66bd15009001737 (diff)
vote meters idk
-rw-r--r--apioforum/__init__.py3
-rw-r--r--apioforum/db.py4
-rw-r--r--apioforum/forum.py32
-rw-r--r--apioforum/templates/view_forum.html40
-rw-r--r--setup.py1
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 @@
</span>
</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}} votes
+ {% endfor %}
+ total votes: {{total_votes}}
+ </desc>
+ </svg>
+ </div>
+ {% endif %}
</div>
{%endfor%}
</div>
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',
],
)