252 lines
8.4 KiB
Python
252 lines
8.4 KiB
Python
import streamlit as st
|
|
from auth_runtime import require_login
|
|
# from ui.sidebar import build_sidebar
|
|
from auth import create_user
|
|
from pathlib import Path
|
|
from tools.load_css import load_css
|
|
from app_db.app_db import get_list, send_cmd
|
|
from ui.selectboxes import get_groups, get_id
|
|
import bcrypt
|
|
|
|
|
|
DASH_NAME = Path(__file__).stem # Hier muss die dash_id aus der DB stehen -> wird gegen die session_state geprüft (User-Berechtigung)
|
|
|
|
load_css()
|
|
|
|
st.set_page_config(page_title="Co-App Benutzer", page_icon=":material/person:", layout="wide", initial_sidebar_state="collapsed")
|
|
|
|
authenticator = require_login()
|
|
st.session_state["authenticator"] = authenticator
|
|
|
|
|
|
|
|
|
|
def sidebar():
|
|
|
|
fullname = st.session_state.get("fullname")
|
|
role_text = st.session_state.get("role_text")
|
|
|
|
with st.sidebar:
|
|
|
|
st.logo("app/images/GMN_Logo_neu_rgb.png", size="small")
|
|
st.markdown(f"**{fullname}** ({role_text})")
|
|
|
|
col1, col2 = st.columns([2,2])
|
|
with col1:
|
|
authenticator.logout("Logout")
|
|
with col2:
|
|
if st.button("Home", use_container_width=True, icon=":material/home:"):
|
|
st.switch_page("pages/home.py")
|
|
|
|
dashborad()
|
|
|
|
|
|
@st.dialog("Dashboard anlegen")
|
|
def dialog_create_dashboard():
|
|
txt_username = st.text_input("Benutzername")
|
|
txt_firstname = st.text_input("Vorname")
|
|
txt_lastname = st.text_input("Nachname")
|
|
txt_email = st.text_input("Email")
|
|
txt_pwd = st.text_input("Kennwort", type="password")
|
|
cmb_role = st.selectbox("Rolle", get_roles(), index=None)
|
|
if st.button("Save"):
|
|
if create_user(
|
|
username=txt_username,
|
|
firstname=txt_firstname,
|
|
lastname=txt_lastname,
|
|
email=txt_email,
|
|
role_id=get_id(cmb_role),
|
|
password=txt_pwd
|
|
):
|
|
st.session_state.save_msg = f"✅ Benutzer '{txt_username}' erfolgreich gespeichert"
|
|
else:
|
|
st.session_state.save_msg = "❌ Fehler beim Speichern"
|
|
st.rerun()
|
|
|
|
|
|
@st.dialog("Dashboard löschen")
|
|
def dialog_delete_dashboard(id):
|
|
if id == None:
|
|
st.write("kein Benutzer ausgewählt")
|
|
else:
|
|
df = get_list("select username from users where id = ?",(id,))
|
|
username = df.iloc[0]["username"]
|
|
st.write(f"Der Benutzer {username} wird gelöscht! Sind Sie sicher?")
|
|
if st.button("Löschen"):
|
|
if username != "admin":
|
|
if send_cmd("delete from users where id = ?",(id,)):
|
|
st.session_state.delete_msg = f"✅ Benutzer '{username}' erfolgreich gelöscht!"
|
|
else:
|
|
st.session_state.delete_msg = f"❌ Benutzer '{username}' konnte nicht gelöscht werden!"
|
|
else:
|
|
st.session_state.delete_msg = f"❌ Benutzer '{username}' darf nicht gelöscht werden!"
|
|
st.rerun()
|
|
|
|
|
|
@st.dialog("Dashboard bearbeiten")
|
|
def dialog_edit_dashboard(dash_id):
|
|
if dash_id == None:
|
|
st.write("kein Dashboard ausgewählt")
|
|
else:
|
|
sql = """
|
|
select
|
|
d.dash_id,
|
|
d.dash_text,
|
|
d.dash_description,
|
|
d.group_id || ' | ' || g.group_text as "group",
|
|
d.page_file,
|
|
d.dash_type,
|
|
d.active,
|
|
d.order_no
|
|
from
|
|
dashboards d
|
|
left join groups g
|
|
on d.group_id = g.group_id
|
|
where
|
|
d.dash_id = ?
|
|
"""
|
|
df = get_list(sql,(dash_id,))
|
|
|
|
# Index für Gruppe aus DB für selectbox ermitteln
|
|
df_groups = get_groups()
|
|
groups = df_groups["group"].tolist()
|
|
group = df.iloc[0]["group"]
|
|
try:
|
|
idx = groups.index(group)
|
|
except:
|
|
idx = None
|
|
|
|
# Index für Dash-Typ aus DB für selectbox ermitteln
|
|
dash_types = ["page", "url"]
|
|
dash_type_index = dash_types.index(df.iloc[0]["dash_type"])
|
|
|
|
col1, col2 = st.columns([2,1],vertical_alignment="center")
|
|
with col1:
|
|
txt_dash_text = st.text_input(label="Dashboard", value=df.iloc[0]["dash_text"])
|
|
with col2:
|
|
is_active = st.checkbox(label="Aktiv", value=df.iloc[0]["active"])
|
|
txt_dash_description = st.text_area(label="Beschreibung", value=df.iloc[0]["dash_description"])
|
|
cmb_dash_type = st.selectbox(label="Typ", options=dash_types, index=dash_type_index)
|
|
txt_page_file = st.text_input(label="Page", value=df.iloc[0]["page_file"])
|
|
cmb_group = st.selectbox(label="Gruppe", options=groups, placeholder="Gruppe auswählen", index=idx)
|
|
txt_order_no = st.text_input(label="Order-Nr",value=df.iloc[0]["order_no"])
|
|
|
|
if st.button("Save"):
|
|
|
|
sql = """
|
|
update dashboards set
|
|
dash_text = ?,
|
|
dash_description = ?,
|
|
group_id = ?,
|
|
page_file = ?,
|
|
dash_type = ?,
|
|
active = ?,
|
|
order_no = ?
|
|
where dash_id = ?
|
|
"""
|
|
params = (txt_dash_text, txt_dash_description, get_id(cmb_group), txt_page_file, cmb_dash_type, is_active, txt_order_no, dash_id)
|
|
|
|
if send_cmd(sql, params):
|
|
st.session_state.save_msg = f"✅ Dashboard '{txt_dash_text}' erfolgreich geändert"
|
|
else:
|
|
st.session_state.save_msg = "❌ Fehler beim Speichern"
|
|
st.rerun()
|
|
|
|
|
|
|
|
def dashborad():
|
|
|
|
if "selected_dash_id" not in st.session_state:
|
|
st.session_state.selected_dash_id = None
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
# Dashboard-Verwaltung
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
|
|
df = get_list("""
|
|
select
|
|
d.dash_id,
|
|
d.dash_text,
|
|
d.dash_description,
|
|
d.group_id || ' | ' || g.group_text as "group",
|
|
d.page_file,
|
|
d.dash_type,
|
|
d.active,
|
|
d.order_no
|
|
from
|
|
dashboards d
|
|
left join groups g
|
|
on d.group_id = g.group_id
|
|
""")
|
|
|
|
|
|
|
|
col_find_dashboard, col_create_dashboard, col_edit_dashboard, col_delete_dashboard = st.columns([3,2,2,2], vertical_alignment="bottom")
|
|
|
|
with col_find_dashboard:
|
|
txt_search = st.text_input(label="Suche", label_visibility="hidden", placeholder="Dashboard, Beschreibung ...", icon=":material/search:")
|
|
|
|
with col_create_dashboard:
|
|
if st.button(label="Dashboard anlegen", use_container_width=True, icon=":material/add:"):
|
|
dialog_create_dashboard()
|
|
if "save_msg" in st.session_state:
|
|
st.toast(st.session_state.save_msg)
|
|
del st.session_state.save_msg
|
|
|
|
with col_edit_dashboard:
|
|
if st.button(label="Dashboard bearbeiten", use_container_width=True, icon=":material/edit:"):
|
|
if not st.session_state.selected_dash_id is None:
|
|
dialog_edit_dashboard(st.session_state.selected_dash_id)
|
|
else:
|
|
st.toast("❌ Bitte erst eine Zeile auswählen")
|
|
if "save_msg" in st.session_state:
|
|
st.toast(st.session_state.save_msg)
|
|
del st.session_state.save_msg
|
|
|
|
with col_delete_dashboard:
|
|
if st.button(label="Dashboard löschen", use_container_width=True, icon=":material/delete:"):
|
|
if not st.session_state.selected_dash_id is None:
|
|
dialog_delete_dashboard(st.session_state.selected_dash_id)
|
|
else:
|
|
st.toast("❌ Bitte erst eine Zeile auswählen")
|
|
if "delete_msg" in st.session_state:
|
|
st.toast(st.session_state.delete_msg)
|
|
del st.session_state.delete_msg
|
|
|
|
|
|
|
|
|
|
if txt_search.strip():
|
|
txt_search_norm = txt_search.strip().lower()
|
|
mask = (
|
|
df["dash_text"].fillna("").str.lower().str.contains(txt_search_norm)
|
|
| df["dash_description"].fillna("").str.lower().str.contains(txt_search_norm)
|
|
| df["group"].fillna("").str.lower().str.contains(txt_search_norm)
|
|
)
|
|
df_view = df.loc[mask].copy()
|
|
else:
|
|
df_view = df.copy()
|
|
|
|
event = st.dataframe(df_view,key="dash_data", on_select="rerun", selection_mode="single-row")
|
|
rows = event.selection.rows
|
|
if rows:
|
|
row_idx = rows[0]
|
|
st.session_state.selected_dash_id = int(df_view.iloc[row_idx]["dash_id"])
|
|
else:
|
|
st.session_state.selected_dash_id = None
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sidebar()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|