Slight database change, add util functions for adding points to users.

This commit is contained in:
2024-05-09 20:25:01 -04:00
parent 1495fb7ee6
commit 4f9524a4c1
3 changed files with 80 additions and 3 deletions
+2 -2
View File
@@ -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)
);
+2
View File
@@ -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
+76 -1
View File
@@ -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()