File size: 4,414 Bytes
f9247be d3662ef 560823b 050fd9f 560823b a62c7e7 050fd9f 83e66a7 a62c7e7 83e66a7 a62c7e7 83e66a7 a62c7e7 83e66a7 560823b ea0c0c1 050fd9f a62c7e7 560823b a62c7e7 560823b 1f6b17c 050fd9f a62c7e7 1f6b17c 050fd9f 98ed27b a62c7e7 98ed27b d0acf6f 98ed27b 5c97774 050fd9f a62c7e7 5c97774 a3c0746 050fd9f 4b5313a 7e976d5 f9247be 050fd9f f9247be a62c7e7 f9247be 050fd9f f9247be 524e5b5 86b69b9 0b5b559 050fd9f a62c7e7 9db9dc4 9d8e6a4 050fd9f 83e66a7 9d8e6a4 e11a10a 050fd9f 83e66a7 a62c7e7 e11a10a 050fd9f e11a10a 050fd9f 83e66a7 e11a10a 050fd9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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):
# returns true if logged in successfully
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):
# returns true if logged in successfully
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)
|