From 924c4c547f7780513940f1e99c58c29dd2307844 Mon Sep 17 00:00:00 2001 From: Lili Date: Sun, 14 Dec 2025 19:22:57 +0100 Subject: [PATCH] show polls on dashboard --- tlapbot/poll_handler.py | 3 ++- tlapbot/redeems/polls.py | 37 ++++++++++++++++++++++++++++++++ tlapbot/templates/dashboard.html | 27 ++++++++++++++++++++++- tlapbot/tlapbot_dashboard.py | 3 +++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/tlapbot/poll_handler.py b/tlapbot/poll_handler.py index d91e04e..2d83bc8 100644 --- a/tlapbot/poll_handler.py +++ b/tlapbot/poll_handler.py @@ -28,6 +28,7 @@ def handle_poll_vote(message: str, user_id: str) -> None: poll_points = int(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.") + return if (vote_in_poll(db, poll_name, option, poll_points) and use_points(db, user_id, poll_points)): - send_chat(f"{poll_points} donated to {option} in poll {poll_name}") \ No newline at end of file + send_chat(f"{poll_points} points donated to {option} in poll {poll_name}") \ No newline at end of file diff --git a/tlapbot/redeems/polls.py b/tlapbot/redeems/polls.py index 7af29d4..328958a 100644 --- a/tlapbot/redeems/polls.py +++ b/tlapbot/redeems/polls.py @@ -2,6 +2,43 @@ from flask import current_app 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: """Returns None only if error was logged.""" try: diff --git a/tlapbot/templates/dashboard.html b/tlapbot/templates/dashboard.html index 5e7ef2e..b342e0a 100644 --- a/tlapbot/templates/dashboard.html +++ b/tlapbot/templates/dashboard.html @@ -23,7 +23,7 @@

Redeems Dashboard

- {% if (username and users ) %} + {% if (username and users) %} @@ -47,6 +47,31 @@ {% endif %} {% if not passive %} + {% if polls %} + +
+ + + + + + + + {% for poll_name, poll_dict in polls.items() %} + {% for option in poll_dict.keys() %} + {% set max_vote = max_poll_votes(poll_dict) %} + + + + + + + {% endfor %} + {% endfor %} + +
Active pollsPoints
{{ poll_name }} {{ option }} {{ poll_dict[option] }}
+ + {% endif %} {% if counters %} diff --git a/tlapbot/tlapbot_dashboard.py b/tlapbot/tlapbot_dashboard.py index 021ea81..48b683e 100644 --- a/tlapbot/tlapbot_dashboard.py +++ b/tlapbot/tlapbot_dashboard.py @@ -3,6 +3,7 @@ from tlapbot.db import get_db from tlapbot.redeems.redeems import all_active_redeems, pretty_redeem_queue from tlapbot.redeems.counters import all_active_counters 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 datetime import timezone @@ -22,7 +23,9 @@ def dashboard() -> str: queue=pretty_redeem_queue(db), counters=all_active_counters(db), milestones=all_active_milestones(db), + polls=all_active_polls(db), redeems=all_active_redeems(), + max_poll_votes=max_poll_votes, prefix=current_app.config['PREFIX'], passive=current_app.config['PASSIVE'], username=username,