File size: 2,714 Bytes
f9247be
a3c0746
 
 
 
 
e11a10a
a3c0746
 
 
 
 
 
 
3894ea2
 
 
 
 
 
 
a3c0746
 
f9247be
3894ea2
 
 
 
 
 
f9247be
 
0b5b559
 
 
 
 
 
 
 
 
 
3894ea2
 
 
 
 
0b5b559
 
e11a10a
 
 
 
 
 
 
 
 
 
 
 
 
a3c0746
 
 
 
 
 
 
 
 
 
f9247be
a3c0746
0b5b559
e11a10a
a3c0746
 
 
 
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
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()