Jon Solow commited on
Commit
e11a10a
1 Parent(s): 02ac7f2

Implement admin utility for migrating week

Browse files
Files changed (2) hide show
  1. src/data_storage.py +18 -1
  2. src/pages/99_Admin.py +15 -1
src/data_storage.py CHANGED
@@ -20,7 +20,7 @@ def initialize_data_storage():
20
  cur.execute("CREATE TABLE IF NOT EXISTS tokens( user_id INTEGER PRIMARY KEY, token TEXT)")
21
 
22
 
23
- def update_selection(user_id: str, position_id: str, player_id: str):
24
  with get_db_connection() as con:
25
  cur = con.cursor()
26
  cur.execute(
@@ -136,3 +136,20 @@ def get_all_rosters() -> list[tuple[int, str, str]]:
136
  cur = con.cursor()
137
  all_rosters = cur.execute("select * from user_rosters").fetchall()
138
  return all_rosters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  cur.execute("CREATE TABLE IF NOT EXISTS tokens( user_id INTEGER PRIMARY KEY, token TEXT)")
21
 
22
 
23
+ def update_selection(user_id: str | int, position_id: str, player_id: str):
24
  with get_db_connection() as con:
25
  cur = con.cursor()
26
  cur.execute(
 
136
  cur = con.cursor()
137
  all_rosters = cur.execute("select * from user_rosters").fetchall()
138
  return all_rosters
139
+
140
+
141
+ def get_all_rosters_week(week: int) -> list[tuple[int, str, str]]:
142
+ with get_db_connection() as con:
143
+ cur = con.cursor()
144
+ all_rosters = cur.execute(f"select * from user_rosters where position_id like '{week}%'").fetchall()
145
+ return all_rosters
146
+
147
+
148
+ def migrate_players_from_week(migrate_from_week: int):
149
+ """
150
+ Migrate players from the week = migrate_from_week to the week = migrate_from_week + 1
151
+ """
152
+ rosters = get_all_rosters_week(migrate_from_week)
153
+ for user_id, position_id, player_id in rosters:
154
+ new_position_id = f"""{migrate_from_week + 1}-{position_id.split("-", 1)[1]}"""
155
+ update_selection(user_id, new_position_id, player_id)
src/pages/99_Admin.py CHANGED
@@ -4,7 +4,7 @@ import streamlit as st
4
  from config import DEFAULT_ICON
5
  from shared_page import common_page_config
6
 
7
- from data_storage import add_new_user, is_admin, DB_PATH, drop_tables
8
 
9
 
10
  def admin_add_new_user():
@@ -47,6 +47,19 @@ def drop_db_form():
47
  st.form_submit_button("Submit", on_click=drop_db_if_confirmed)
48
 
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def get_page():
51
  page_title = "Admin"
52
  st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
@@ -60,6 +73,7 @@ def get_page():
60
  database_backup_form()
61
  admin_add_new_user_form()
62
  drop_db_form()
 
63
 
64
 
65
  if __name__ == "__main__":
 
4
  from config import DEFAULT_ICON
5
  from shared_page import common_page_config
6
 
7
+ from data_storage import add_new_user, is_admin, DB_PATH, drop_tables, migrate_players_from_week
8
 
9
 
10
  def admin_add_new_user():
 
47
  st.form_submit_button("Submit", on_click=drop_db_if_confirmed)
48
 
49
 
50
+ def migrate_players_week():
51
+ with st.container():
52
+ st.header("Utility for migrating players to the next week")
53
+ week_migrate_from = st.selectbox("week to migrate from", options=[1, 2, 3])
54
+ st.text_input("Enter week to confirm", key="week_migrate_from")
55
+ if st.button("Migrate Week"):
56
+ if st.session_state.get("week_migrate_from") == str(week_migrate_from):
57
+ migrate_players_from_week(week_migrate_from)
58
+ st.warning("Week migrated")
59
+ else:
60
+ st.warning("Must confirm migration by entering matching week")
61
+
62
+
63
  def get_page():
64
  page_title = "Admin"
65
  st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
 
73
  database_backup_form()
74
  admin_add_new_user_form()
75
  drop_db_form()
76
+ migrate_players_week()
77
 
78
 
79
  if __name__ == "__main__":