Jon Solow
commited on
Commit
·
f9247be
1
Parent(s):
a3c0746
Allow database download
Browse files- src/data_storage.py +30 -0
- src/pages/99_Admin.py +11 -1
src/data_storage.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import sqlite3
|
3 |
|
4 |
DATA_DIR = "/data"
|
@@ -15,6 +16,7 @@ def initialize_data_storage():
|
|
15 |
cur = con.cursor()
|
16 |
cur.execute("CREATE TABLE IF NOT EXISTS user_rosters( user_id INTEGER, position_id TEXT, player_id TEXT)")
|
17 |
cur.execute("CREATE TABLE IF NOT EXISTS users( user_id INTEGER PRIMARY KEY ASC, email TEXT, name TEXT)")
|
|
|
18 |
|
19 |
|
20 |
def update_selection(user_id: str, position_id: str, player_id: str):
|
@@ -74,3 +76,31 @@ def get_user_id_if_email_exists(email: str) -> int | None:
|
|
74 |
def is_admin(user_id: int):
|
75 |
# Replace with db data field later
|
76 |
return user_id == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
from secrets import token_urlsafe
|
3 |
import sqlite3
|
4 |
|
5 |
DATA_DIR = "/data"
|
|
|
16 |
cur = con.cursor()
|
17 |
cur.execute("CREATE TABLE IF NOT EXISTS user_rosters( user_id INTEGER, position_id TEXT, player_id TEXT)")
|
18 |
cur.execute("CREATE TABLE IF NOT EXISTS users( user_id INTEGER PRIMARY KEY ASC, email TEXT, name TEXT)")
|
19 |
+
cur.execute("CREATE TABLE IF NOT EXISTS tokens( user_id INTEGER PRIMARY KEY, token TEXT)")
|
20 |
|
21 |
|
22 |
def update_selection(user_id: str, position_id: str, player_id: str):
|
|
|
76 |
def is_admin(user_id: int):
|
77 |
# Replace with db data field later
|
78 |
return user_id == 1
|
79 |
+
|
80 |
+
|
81 |
+
def login_by_token(token: str):
|
82 |
+
# returns true if logged in successfully
|
83 |
+
with get_db_connection() as con:
|
84 |
+
cur = con.cursor()
|
85 |
+
query_result = cur.execute(f"select user_id from users where token = '{token}'").fetchone()
|
86 |
+
if query_result:
|
87 |
+
user_id = query_result[0]
|
88 |
+
else:
|
89 |
+
user_id = None
|
90 |
+
return user_id
|
91 |
+
|
92 |
+
|
93 |
+
def create_new_token_for_user(user_id: int, existing_user: bool = False):
|
94 |
+
# returns true if logged in successfully
|
95 |
+
token = token_urlsafe(32)
|
96 |
+
if existing_user:
|
97 |
+
sql_cmd = "REPLACE"
|
98 |
+
else:
|
99 |
+
sql_cmd = "INSERT"
|
100 |
+
with get_db_connection() as con:
|
101 |
+
cur = con.cursor()
|
102 |
+
cur.execute(
|
103 |
+
f"""{sql_cmd} INTO tokens (user_id, token )
|
104 |
+
VALUES({user_id}, '{token}')
|
105 |
+
"""
|
106 |
+
)
|
src/pages/99_Admin.py
CHANGED
@@ -1,9 +1,10 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
from config import DEFAULT_ICON
|
4 |
from shared_page import common_page_config
|
5 |
|
6 |
-
from data_storage import add_new_user, is_admin
|
7 |
|
8 |
|
9 |
def admin_add_new_user():
|
@@ -19,6 +20,14 @@ def admin_add_new_user_form():
|
|
19 |
st.form_submit_button("Submit", on_click=admin_add_new_user)
|
20 |
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def get_page():
|
23 |
page_title = "Admin"
|
24 |
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
@@ -29,6 +38,7 @@ def get_page():
|
|
29 |
|
30 |
st.title(page_title)
|
31 |
|
|
|
32 |
admin_add_new_user_form()
|
33 |
|
34 |
|
|
|
1 |
+
import datetime
|
2 |
import streamlit as st
|
3 |
|
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
|
8 |
|
9 |
|
10 |
def admin_add_new_user():
|
|
|
20 |
st.form_submit_button("Submit", on_click=admin_add_new_user)
|
21 |
|
22 |
|
23 |
+
def database_backup_form():
|
24 |
+
with st.container():
|
25 |
+
st.header("Utility for backing up and restoring db file")
|
26 |
+
with open(DB_PATH, "rb") as f:
|
27 |
+
file_name = f"backup-{datetime.datetime.now().strftime('%y%m%d%H%M')}.db"
|
28 |
+
st.download_button("Download db", f, file_name=file_name)
|
29 |
+
|
30 |
+
|
31 |
def get_page():
|
32 |
page_title = "Admin"
|
33 |
st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide")
|
|
|
38 |
|
39 |
st.title(page_title)
|
40 |
|
41 |
+
database_backup_form()
|
42 |
admin_add_new_user_form()
|
43 |
|
44 |
|