From 4f9524a4c1dcaa9df2770f75f2bd6b603147e1f7 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 9 May 2024 20:25:01 -0400 Subject: [PATCH] Slight database change, add util functions for adding points to users. --- database_setup/database.sql | 4 +- database_setup/sql_data_classes.py | 2 + utils.py | 77 +++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/database_setup/database.sql b/database_setup/database.sql index 2278fba..10b7061 100644 --- a/database_setup/database.sql +++ b/database_setup/database.sql @@ -42,8 +42,8 @@ CREATE TABLE Presences ( CREATE TABLE ParticipationPoints ( Cycle varchar(30) not null, - MemberId varchar(32) not null, + UserId varchar(32) not null, DiscordMember boolean not null, Points int unsigned DEFAULT 0, - PRIMARY KEY (Cycle, MemberId) + PRIMARY KEY (Cycle, UserId) ); \ No newline at end of file diff --git a/database_setup/sql_data_classes.py b/database_setup/sql_data_classes.py index cd6406e..9fa382a 100644 --- a/database_setup/sql_data_classes.py +++ b/database_setup/sql_data_classes.py @@ -1,6 +1,7 @@ """ Simple file to set up the sqlalchemy connection to database. """ + from sqlalchemy import create_engine from sqlalchemy.ext.automap import automap_base @@ -16,3 +17,4 @@ CouncilMembers = Base.classes.CouncilMembers Votes = Base.classes.Votes UserBirthday = Base.classes.UserBirthday Presences = Base.classes.Presences +ParticipationPoints = Base.classes.ParticipationPoints diff --git a/utils.py b/utils.py index 657f2cd..0126e9c 100644 --- a/utils.py +++ b/utils.py @@ -9,7 +9,12 @@ from sqlalchemy import select from sqlalchemy.orm import Session from config import DakinyConfig -from database_setup.sql_data_classes import CouncilMembers, Votes, engine +from database_setup.sql_data_classes import ( + CouncilMembers, + ParticipationPoints, + Votes, + engine, +) NOMINEE_SPLITTER = "========================================\n\n" @@ -314,3 +319,73 @@ def decorate_dm_nomination(user_id: int, speech: str) -> str: msg += speech return msg + + +def record_event_participation(config: DakinyConfig, users: list[str], is_member: bool): + """ + Adds participation points to all users listed. Checks if they don't exist in order to make a new entry, otherwise + adds points to the existing value. + + Args: + config (DakinyConfig): Dakiny config for getting points + users (list[int]): users that participated and need points. + is_member (bool): whether the users are a discord member or not + """ + + cycle = get_current_cycle(config) + participation_point_value = config.params.event_participate_points + with Session(engine) as session: + for user in users: + stmt = ( + select(ParticipationPoints) + .where(ParticipationPoints.UserId == user) + .where(ParticipationPoints.Cycle == cycle) + ) + + points = session.scalar(stmt) + if points is None: + new_points = ParticipationPoints( + Cycle=cycle, + UserId=user, + DiscordMember=is_member, + Points=participation_point_value, + ) + session.add(new_points) + else: + points.Points += participation_point_value + + session.commit() + + +def record_event_host(config: DakinyConfig, user: str, is_member: bool): + """ + Adds host points to the given user. Checks if they don't exist in order to make a new entry, otherwise adds points + to the existing value. + + Args: + config (DakinyConfig): Dakiny config for getting points + user (str): User that hosted. + is_member (bool): whether the user is a discord member or not + """ + cycle = get_current_cycle(config) + host_point_value = config.params.event_host_points + with Session(engine) as session: + stmt = ( + select(ParticipationPoints) + .where(ParticipationPoints.UserId == user) + .where(ParticipationPoints.Cycle == cycle) + ) + + points = session.scalar(stmt) + if points is None: + new_points = ParticipationPoints( + Cycle=cycle, + UserId=user, + DiscordMember=is_member, + Points=host_point_value, + ) + session.add(new_points) + else: + points.Points += host_point_value + + session.commit()