diff options
author | ubq323 <ubq323> | 2021-06-26 01:23:58 +0000 |
---|---|---|
committer | ubq323 <ubq323> | 2021-06-26 01:23:58 +0000 |
commit | cd8b3aa2c5abd414068c8f9f5edbebab807153eb (patch) | |
tree | 94030d3e8d85981f9495cebca962bfe9398e2acd | |
parent | a1660cad33f03828e2c74d8f083ad6490ea0da7f (diff) |
display vote counts (badly)
-rw-r--r-- | apioforum/db.py | 5 | ||||
-rw-r--r-- | apioforum/templates/view_thread.html | 2 | ||||
-rw-r--r-- | apioforum/thread.py | 9 |
3 files changed, 13 insertions, 3 deletions
diff --git a/apioforum/db.py b/apioforum/db.py index 1faa167..67989df 100644 --- a/apioforum/db.py +++ b/apioforum/db.py @@ -109,7 +109,10 @@ CREATE TABLE votes ( ); 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; +""", ] diff --git a/apioforum/templates/view_thread.html b/apioforum/templates/view_thread.html index 513ee59..622cf06 100644 --- a/apioforum/templates/view_thread.html +++ b/apioforum/templates/view_thread.html @@ -9,7 +9,7 @@ <p>{{poll.title}}</p> <ul> {%for opt in poll.options%} - <li>#{{opt.option_idx}} - {{opt.text}}</li> + <li>#{{opt.option_idx}} - {{opt.text}} - {{opt.num or 0}}</li> {%endfor%} </ul> {% endif %} diff --git a/apioforum/thread.py b/apioforum/thread.py index 7d068c4..ec50b3b 100644 --- a/apioforum/thread.py +++ b/apioforum/thread.py @@ -29,9 +29,16 @@ def view_thread(thread_id): WHERE thread_tags.thread = ? ORDER BY tags.id""",(thread_id,)).fetchall() poll = None + votes = None if thread['poll'] is not None: poll_row = db.execute("SELECT * FROM polls where id = ?",(thread['poll'],)).fetchone() - options = db.execute("SELECT * FROM poll_options WHERE poll = ?",(poll_row['id'],)).fetchall() + 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 = ?; + """,(poll_row['id'],)).fetchall() poll = {} poll.update(poll_row) poll['options'] = options |