Jon Solow
commited on
Commit
·
560823b
1
Parent(s):
75deac9
Replace rosters stored in google with sqlite3 local file
Browse files- src/data_storage.py +37 -0
- src/login.py +0 -19
- src/pages/10_Set_Your_Lineup.py +6 -6
src/data_storage.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sqlite3
|
3 |
+
|
4 |
+
DATA_DIR = "/data"
|
5 |
+
|
6 |
+
DB_PATH = os.path.join(DATA_DIR, "data.db")
|
7 |
+
|
8 |
+
|
9 |
+
def get_db_connection():
|
10 |
+
return sqlite3.connect(DB_PATH)
|
11 |
+
|
12 |
+
|
13 |
+
def initialize_data_storage():
|
14 |
+
with get_db_connection() as con:
|
15 |
+
cur = con.cursor()
|
16 |
+
cur.execute("CREATE TABLE IF NOT EXISTS user_rosters( user_id INTEGER, position_id TEXT, player_id TEXT)")
|
17 |
+
|
18 |
+
|
19 |
+
def update_selection(user_id: str, position_id: str, player_id: str):
|
20 |
+
with get_db_connection() as con:
|
21 |
+
cur = con.cursor()
|
22 |
+
cur.execute(
|
23 |
+
f"""REPLACE INTO user_rosters (user_id, position_id, player_id )
|
24 |
+
VALUES({user_id}, '{position_id}', '{player_id}')
|
25 |
+
"""
|
26 |
+
)
|
27 |
+
|
28 |
+
|
29 |
+
def get_user_team(user_id):
|
30 |
+
with get_db_connection() as con:
|
31 |
+
cur = con.cursor()
|
32 |
+
cur = get_db_connection().cursor()
|
33 |
+
team = cur.execute(f"select * from user_rosters where user_id = {user_id}").fetchall()
|
34 |
+
if team:
|
35 |
+
return {x[1]: x[2] for x in team}
|
36 |
+
else:
|
37 |
+
return {}
|
src/login.py
CHANGED
@@ -135,25 +135,6 @@ def check_password():
|
|
135 |
return False
|
136 |
|
137 |
|
138 |
-
def get_user_team():
|
139 |
-
user_id = st.session_state.get("logged_in_user")
|
140 |
-
if not user_id:
|
141 |
-
return {}
|
142 |
-
df_team = conn.read(
|
143 |
-
worksheet=f"user-{user_id}-roster",
|
144 |
-
ttl=1,
|
145 |
-
)
|
146 |
-
return df_team.loc[0].to_dict()
|
147 |
-
|
148 |
-
|
149 |
-
def save_user_team(team_selections):
|
150 |
-
user_id = st.session_state.get("logged_in_user")
|
151 |
-
conn.update(
|
152 |
-
worksheet=f"user-{user_id}-roster",
|
153 |
-
data=pd.DataFrame(team_selections, index=[0]),
|
154 |
-
)
|
155 |
-
|
156 |
-
|
157 |
def login_by_token(token: str):
|
158 |
# returns true if logged in successfully
|
159 |
df = conn.read(
|
|
|
135 |
return False
|
136 |
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
def login_by_token(token: str):
|
139 |
# returns true if logged in successfully
|
140 |
df = conn.read(
|
src/pages/10_Set_Your_Lineup.py
CHANGED
@@ -10,7 +10,8 @@ from domain.playoffs import PLAYOFF_WEEK_TO_NAME, CURRENT_PLAYOFF_WEEK, ROSTER_W
|
|
10 |
from domain.teams import SCHEDULE_NAME_TO_PFR_NAME_MAP, PLAYOFFS_TEAMS
|
11 |
from queries.nflverse.github_data import get_weekly_rosters
|
12 |
from queries.pfr.league_schedule import get_season_time_map
|
13 |
-
from login import check_password
|
|
|
14 |
|
15 |
|
16 |
@dataclass
|
@@ -159,15 +160,14 @@ def position_cell(
|
|
159 |
display_player(selected_player)
|
160 |
return
|
161 |
else:
|
162 |
-
update_and_save_selection(pos_label, selected_player_from_box.gsis_id
|
163 |
display_player(selected_player_from_box)
|
164 |
else:
|
165 |
display_player(selected_player)
|
166 |
|
167 |
|
168 |
-
def update_and_save_selection(pos_label: str, selection_id: str
|
169 |
-
|
170 |
-
save_user_team(existing_selection_map)
|
171 |
|
172 |
|
173 |
def get_page():
|
@@ -182,7 +182,7 @@ def get_page():
|
|
182 |
|
183 |
if st.button("Refresh Data"):
|
184 |
st.rerun()
|
185 |
-
existing_selections = get_user_team()
|
186 |
|
187 |
player_options = load_options()
|
188 |
|
|
|
10 |
from domain.teams import SCHEDULE_NAME_TO_PFR_NAME_MAP, PLAYOFFS_TEAMS
|
11 |
from queries.nflverse.github_data import get_weekly_rosters
|
12 |
from queries.pfr.league_schedule import get_season_time_map
|
13 |
+
from login import check_password
|
14 |
+
from data_storage import update_selection, get_user_team
|
15 |
|
16 |
|
17 |
@dataclass
|
|
|
160 |
display_player(selected_player)
|
161 |
return
|
162 |
else:
|
163 |
+
update_and_save_selection(pos_label, selected_player_from_box.gsis_id),
|
164 |
display_player(selected_player_from_box)
|
165 |
else:
|
166 |
display_player(selected_player)
|
167 |
|
168 |
|
169 |
+
def update_and_save_selection(pos_label: str, selection_id: str):
|
170 |
+
update_selection(st.session_state["logged_in_user"], pos_label, selection_id)
|
|
|
171 |
|
172 |
|
173 |
def get_page():
|
|
|
182 |
|
183 |
if st.button("Refresh Data"):
|
184 |
st.rerun()
|
185 |
+
existing_selections = get_user_team(st.session_state["logged_in_user"])
|
186 |
|
187 |
player_options = load_options()
|
188 |
|