show polls on dashboard
This commit is contained in:
parent
d7fdc1e183
commit
924c4c547f
@ -28,6 +28,7 @@ def handle_poll_vote(message: str, user_id: str) -> None:
|
|||||||
poll_points = int(poll_points)
|
poll_points = int(poll_points)
|
||||||
if user_points < poll_points:
|
if user_points < poll_points:
|
||||||
send_chat(f"Can't vote in {poll_name} poll, you're voting with more points than you have.")
|
send_chat(f"Can't vote in {poll_name} poll, you're voting with more points than you have.")
|
||||||
|
return
|
||||||
if (vote_in_poll(db, poll_name, option, poll_points) and
|
if (vote_in_poll(db, poll_name, option, poll_points) and
|
||||||
use_points(db, user_id, poll_points)):
|
use_points(db, user_id, poll_points)):
|
||||||
send_chat(f"{poll_points} donated to {option} in poll {poll_name}")
|
send_chat(f"{poll_points} points donated to {option} in poll {poll_name}")
|
||||||
@ -2,6 +2,43 @@ from flask import current_app
|
|||||||
from sqlite3 import Error, Connection
|
from sqlite3 import Error, Connection
|
||||||
|
|
||||||
|
|
||||||
|
def get_poll_votes(db: Connection, poll_name: str) -> dict[str, int] | None:
|
||||||
|
"""Get all poll votes and point values for a given `poll_name`"""
|
||||||
|
try:
|
||||||
|
options = {}
|
||||||
|
cursor = db.execute(
|
||||||
|
"SELECT option, points from polls WHERE poll_name = ?",
|
||||||
|
(poll_name,)
|
||||||
|
)
|
||||||
|
for row in cursor:
|
||||||
|
options[row[0]] = row[1]
|
||||||
|
return options
|
||||||
|
except Error as e:
|
||||||
|
current_app.logger.error(f"Error occurred getting poll votes: {e.args[0]}")
|
||||||
|
current_app.logger.error(f"For poll {poll_name}")
|
||||||
|
|
||||||
|
|
||||||
|
def all_active_polls(db: Connection) -> dict[str, dict[str, int]] | None:
|
||||||
|
"""Returns a dict where the keys are poll names, values is a dict of options and points.
|
||||||
|
Returns None if polls aren't enabled."""
|
||||||
|
if not current_app.config['POLLS_ENABLED']:
|
||||||
|
return None
|
||||||
|
polls = current_app.config['POLLS']
|
||||||
|
poll_votes = {}
|
||||||
|
for poll_name in polls.keys():
|
||||||
|
poll_votes[poll_name] = get_poll_votes(db, poll_name)
|
||||||
|
return poll_votes
|
||||||
|
|
||||||
|
|
||||||
|
def max_poll_votes(poll_dict: dict[str, int]) -> int:
|
||||||
|
"""Returns the maximum number of poll votes/points. Input is the output of `get_poll_votes`"""
|
||||||
|
highest_points = 0
|
||||||
|
for _, points in poll_dict.items():
|
||||||
|
if points > highest_points:
|
||||||
|
highest_points = points
|
||||||
|
return highest_points
|
||||||
|
|
||||||
|
|
||||||
def poll_option_exists(db: Connection, poll_name: str, option: str) -> bool | None:
|
def poll_option_exists(db: Connection, poll_name: str, option: str) -> bool | None:
|
||||||
"""Returns None only if error was logged."""
|
"""Returns None only if error was logged."""
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
<h3>Redeems Dashboard</h3>
|
<h3>Redeems Dashboard</h3>
|
||||||
{% if (username and users ) %}
|
{% if (username and users) %}
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@ -47,6 +47,31 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if not passive %}
|
{% if not passive %}
|
||||||
|
{% if polls %}
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th colspan="2">Active polls</th>
|
||||||
|
<th colspan="2">Points</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for poll_name, poll_dict in polls.items() %}
|
||||||
|
{% for option in poll_dict.keys() %}
|
||||||
|
{% set max_vote = max_poll_votes(poll_dict) %}
|
||||||
|
<tr>
|
||||||
|
<td> {{ poll_name }} </td>
|
||||||
|
<td> {{ option }} </td>
|
||||||
|
<td> {{ poll_dict[option] }} </td>
|
||||||
|
<td> <progress id="file" max={{ max_vote }} value={{ poll_dict[option] }}></progress></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
{% endif %}
|
||||||
{% if counters %}
|
{% if counters %}
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
|
|||||||
@ -3,6 +3,7 @@ from tlapbot.db import get_db
|
|||||||
from tlapbot.redeems.redeems import all_active_redeems, pretty_redeem_queue
|
from tlapbot.redeems.redeems import all_active_redeems, pretty_redeem_queue
|
||||||
from tlapbot.redeems.counters import all_active_counters
|
from tlapbot.redeems.counters import all_active_counters
|
||||||
from tlapbot.redeems.milestones import all_active_milestones
|
from tlapbot.redeems.milestones import all_active_milestones
|
||||||
|
from tlapbot.redeems.polls import all_active_polls, max_poll_votes
|
||||||
from tlapbot.owncast_helpers import read_all_users_with_username
|
from tlapbot.owncast_helpers import read_all_users_with_username
|
||||||
from datetime import timezone
|
from datetime import timezone
|
||||||
|
|
||||||
@ -22,7 +23,9 @@ def dashboard() -> str:
|
|||||||
queue=pretty_redeem_queue(db),
|
queue=pretty_redeem_queue(db),
|
||||||
counters=all_active_counters(db),
|
counters=all_active_counters(db),
|
||||||
milestones=all_active_milestones(db),
|
milestones=all_active_milestones(db),
|
||||||
|
polls=all_active_polls(db),
|
||||||
redeems=all_active_redeems(),
|
redeems=all_active_redeems(),
|
||||||
|
max_poll_votes=max_poll_votes,
|
||||||
prefix=current_app.config['PREFIX'],
|
prefix=current_app.config['PREFIX'],
|
||||||
passive=current_app.config['PASSIVE'],
|
passive=current_app.config['PASSIVE'],
|
||||||
username=username,
|
username=username,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user