import datetime import streamlit as st from config import DEFAULT_ICON from shared_page import common_page_config from data_storage import add_new_user, is_admin, DB_PATH, drop_tables, migrate_players_from_week def admin_add_new_user(): add_new_user(st.session_state["admin_new_user_email"], st.session_state["admin_new_user_name"]) def admin_add_new_user_form(): if st.button("Add new user"): with st.form("Add new user"): st.header("Admin add new user") st.write("Adds new user to sqlite db") st.text_input("Email Address", key="admin_new_user_email") st.text_input("Name", key="admin_new_user_name") st.form_submit_button("Submit", on_click=admin_add_new_user) def database_backup_form(): if st.button("DB Backup"): with st.container(): st.header("Utility for backing up and restoring db file") with open(DB_PATH, "rb") as f: file_name = f"backup-{datetime.datetime.now().strftime('%y%m%d%H%M')}.db" st.download_button("Download db", f, file_name=file_name) def drop_db_if_confirmed(): confirmed = st.session_state.get("delete_confirmation") == "delete" if confirmed: drop_tables() st.warning("Database tables have been dropped") else: st.warning("Must confirm deletion by typing 'delete'") def drop_db_form(): if st.button("Reset DB"): with st.form("Reset DB"): st.header("Reset DB") st.text_input("Enter 'delete' to confirm deletion", key="delete_confirmation") st.form_submit_button("Submit", on_click=drop_db_if_confirmed) def migrate_players_week(): with st.container(): st.header("Utility for migrating players to the next week") week_migrate_from = st.selectbox("week to migrate from", options=[1, 2, 3]) st.text_input("Enter week to confirm", key="week_migrate_from") if st.button("Migrate Week"): if st.session_state.get("week_migrate_from") == str(week_migrate_from): migrate_players_from_week(week_migrate_from) st.warning("Week migrated") else: st.warning("Must confirm migration by entering matching week") def get_page(): page_title = "Admin" st.set_page_config(page_title=page_title, page_icon=DEFAULT_ICON, layout="wide") common_page_config() if not is_admin(st.session_state.get("logged_in_user")): st.write("Not authorized") st.stop() st.title(page_title) database_backup_form() admin_add_new_user_form() drop_db_form() migrate_players_week() if __name__ == "__main__": get_page()