|
from secrets import token_urlsafe |
|
import streamlit as st |
|
|
|
from supabase import Client |
|
|
|
|
|
USERS_TABLE = "npcs_users" |
|
USER_ROSTERS_TABLE = "npcs_user_rosters" |
|
TOKENS_TABLE = "npcs_tokens" |
|
|
|
|
|
def update_selection(user_id: str | int, position_id: str, player_id: str, supabase_client: Client): |
|
existing_record = ( |
|
supabase_client.table(USER_ROSTERS_TABLE) |
|
.select("*") |
|
.match({"user_id": user_id, "position_id": position_id}) |
|
.execute() |
|
.data |
|
) |
|
if existing_record: |
|
( |
|
supabase_client.table(USER_ROSTERS_TABLE) |
|
.update({"user_id": user_id, "position_id": position_id, "player_id": player_id}) |
|
.match({"user_id": user_id, "position_id": position_id}) |
|
.execute() |
|
) |
|
else: |
|
( |
|
supabase_client.table(USER_ROSTERS_TABLE) |
|
.insert({"user_id": user_id, "position_id": position_id, "player_id": player_id}) |
|
.execute() |
|
) |
|
|
|
|
|
def get_user_team(user_id, supabase_client: Client): |
|
team = ( |
|
supabase_client.table(USER_ROSTERS_TABLE) |
|
.select("position_id", "player_id") |
|
.eq("user_id", user_id) |
|
.execute() |
|
.data |
|
) |
|
if team: |
|
return {x["position_id"]: x["player_id"] for x in team} |
|
else: |
|
return {} |
|
|
|
|
|
def add_new_user(email: str, name: str, supabase_client: Client): |
|
(supabase_client.table(USERS_TABLE).insert({"email": email.lower(), "name": name}).execute()) |
|
|
|
|
|
def get_user(user_id: int, supabase_client: Client): |
|
user_data = ( |
|
supabase_client.table("npcs_users").select("user_id", "email", "name").eq("user_id", user_id).execute().data |
|
) |
|
if not user_data: |
|
return {} |
|
return user_data[0] |
|
|
|
|
|
def get_user_id_if_email_exists(email: str, supabase_client: Client) -> int | None: |
|
query_result = supabase_client.table(USERS_TABLE).select("user_id").eq("email", email.lower()).execute().data |
|
if query_result: |
|
user_id = query_result[0]["user_id"] |
|
else: |
|
user_id = None |
|
return user_id |
|
|
|
|
|
def is_admin(user_id: int | None, supabase_client: Client): |
|
if user_id is None: |
|
return False |
|
query_result = supabase_client.table(USERS_TABLE).select("admin").eq("user_id", user_id).execute().data |
|
if query_result: |
|
return query_result[0]["admin"] is True |
|
return False |
|
|
|
|
|
def login_by_token(token: str, supabase_client: Client): |
|
|
|
query_result = supabase_client.table(TOKENS_TABLE).select("user_id").eq("token", token).execute().data |
|
if query_result: |
|
user_id = query_result[0]["user_id"] |
|
st.session_state["logged_in_user"] = user_id |
|
else: |
|
user_id = None |
|
return user_id |
|
|
|
|
|
def create_new_token_for_user(user_id: int, supabase_client: Client, existing_user: bool = False): |
|
|
|
token = token_urlsafe(32) |
|
supabase_client.table(TOKENS_TABLE).upsert({"user_id": user_id, "token": token}).execute() |
|
return token |
|
|
|
|
|
def get_all_users(supabase_client: Client, columns_included: list[str] = ["user_id", "name", "email"]): |
|
all_users = supabase_client.table(USERS_TABLE).select(*columns_included).execute().data |
|
return all_users |
|
|
|
|
|
def get_all_rosters(supabase_client: Client) -> list[dict[str, int | str]]: |
|
all_rosters = supabase_client.table(USER_ROSTERS_TABLE).select("user_id", "position_id", "player_id").execute().data |
|
return all_rosters |
|
|
|
|
|
def get_all_rosters_week(week: int, supabase_client: Client) -> list[dict[str, int | str]]: |
|
week_rosters = ( |
|
supabase_client.table(USER_ROSTERS_TABLE) |
|
.select("user_id", "position_id", "player_id") |
|
.like("position_id", f"{week}%") |
|
.execute() |
|
.data |
|
) |
|
return week_rosters |
|
|
|
|
|
def migrate_players_from_week(migrate_from_week: int, supabase_client: Client): |
|
""" |
|
Migrate players from the week = migrate_from_week to the week = migrate_from_week + 1 |
|
""" |
|
rosters = get_all_rosters_week(migrate_from_week, supabase_client) |
|
for roster_slot_map in rosters: |
|
position_id = str(roster_slot_map["position_id"]) |
|
player_id = str(roster_slot_map["player_id"]) |
|
user_id = int(roster_slot_map["user_id"]) |
|
new_position_id = f"""{migrate_from_week + 1}-{position_id.split("-", 1)[1]}""" |
|
update_selection(user_id, new_position_id, player_id, supabase_client) |
|
|