show polls on dashboard
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user