Add dashboard management - edit function

This commit is contained in:
hansi
2025-12-22 22:02:36 +01:00
parent 13385db5ef
commit 3f3ec5097d
3 changed files with 100 additions and 111 deletions

Binary file not shown.

View File

@@ -5,7 +5,7 @@ from auth import create_user
from pathlib import Path from pathlib import Path
from tools.load_css import load_css from tools.load_css import load_css
from app_db.app_db import get_list, send_cmd from app_db.app_db import get_list, send_cmd
from ui.selectboxes import get_roles, get_id from ui.selectboxes import get_groups, get_id
import bcrypt import bcrypt
@@ -38,7 +38,7 @@ def sidebar():
if st.button("Home", use_container_width=True, icon=":material/home:"): if st.button("Home", use_container_width=True, icon=":material/home:"):
st.switch_page("pages/home.py") st.switch_page("pages/home.py")
user() dashborad()
@st.dialog("Dashboard anlegen") @st.dialog("Dashboard anlegen")
@@ -84,90 +84,70 @@ def dialog_delete_dashboard(id):
@st.dialog("Dashboard bearbeiten") @st.dialog("Dashboard bearbeiten")
def dialog_edit_dashboard(id): def dialog_edit_dashboard(dash_id):
if id == None: if dash_id == None:
st.write("kein Benutzer ausgewählt") st.write("kein Dashboard ausgewählt")
else: else:
sql = """ sql = """
select select
u.id, d.dash_id,
u.username as user, -- ACHTUNG: nicht mit username arbeiten, da Überschneidung in sessionstate!! d.dash_text,
u.firstname, d.dash_description,
u.lastname, d.group_id || ' | ' || g.group_text as "group",
u.email, d.page_file,
u.role_id || ' | ' || r.role_text as role, d.dash_type,
r.role_text, d.active,
u.new_pwd, d.order_no
u.active
from from
users u dashboards d
left join roles r left join groups g
on u.role_id = r.role_id on d.group_id = g.group_id
where u.id = ? where
d.dash_id = ?
""" """
df = get_list(sql,(id,)) df = get_list(sql,(dash_id,))
# Index für Gruppe aus DB für selectbox ermitteln
# df = get_list("select username from users where id = ?",(id,)) df_groups = get_groups()
groups = df_groups["group"].tolist()
# st.session_state.orig_user_data = df group = df.iloc[0]["group"]
df_roles = get_roles()
roles = df_roles["text"].tolist()
role = df.iloc[0]["role"]
try: try:
idx = roles.index(role) idx = groups.index(group)
except: except:
idx = None 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") col1, col2 = st.columns([2,1],vertical_alignment="center")
with col1: with col1:
txt_username = st.text_input(label="Benutzername", value=df.iloc[0]["user"]) txt_dash_text = st.text_input(label="Dashboard", value=df.iloc[0]["dash_text"])
with col2: with col2:
is_active = st.checkbox(label="Aktiv", value=df.iloc[0]["active"]) is_active = st.checkbox(label="Aktiv", value=df.iloc[0]["active"])
txt_firstname = st.text_input(label="Vorname", value=df.iloc[0]["firstname"]) txt_dash_description = st.text_area(label="Beschreibung", value=df.iloc[0]["dash_description"])
txt_lastname = st.text_input(label="Nachname", value=df.iloc[0]["lastname"]) cmb_dash_type = st.selectbox(label="Typ", options=dash_types, index=dash_type_index)
txt_email = st.text_input(label="Email", value=df.iloc[0]["email"]) txt_page_file = st.text_input(label="Page", value=df.iloc[0]["page_file"])
txt_pwd = st.text_input(label="Passwort", placeholder="Neues Passwort eingeben", type="password") cmb_group = st.selectbox(label="Gruppe", options=groups, placeholder="Gruppe auswählen", index=idx)
new_pwd = st.checkbox(label="Neues Passwort", value=df.iloc[0]["new_pwd"]) txt_order_no = st.text_input(label="Order-Nr",value=df.iloc[0]["order_no"])
cmb_role = st.selectbox(label="Rolle", options=roles, placeholder="Rolle auswählen", index=idx)
if st.button("Save"): if st.button("Save"):
pw_hash = bcrypt.hashpw(txt_pwd.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
if txt_pwd and txt_pwd.strip():
sql = """ sql = """
update users set update dashboards set
username = ?, dash_text = ?,
dash_description = ?,
group_id = ?,
page_file = ?,
dash_type = ?,
active = ?, active = ?,
firstname = ?, order_no = ?
lastname = ?, where dash_id = ?
email = ?,
password_hash = ?,
new_pwd = ?,
role_id = ?
where id = ?
""" """
params = (txt_username, is_active, txt_firstname, txt_lastname, txt_email, pw_hash, new_pwd, get_id(cmb_role), 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)
# send_cmd(sql,(txt_username, txt_firstname, txt_lastname, txt_email, pw_hash, new_pwd, get_id(cmb_role), id))
else:
sql = """
update users set
username = ?,
active = ?,
firstname = ?,
lastname = ?,
email = ?,
new_pwd = ?,
role_id = ?
where id = ?
"""
params = (txt_username, is_active, txt_firstname, txt_lastname, txt_email, new_pwd, get_id(cmb_role), id)
# send_cmd(sql,(txt_username, txt_firstname, txt_lastname, txt_email, new_pwd, get_id(cmb_role), id))
print (params)
if send_cmd(sql, params): if send_cmd(sql, params):
st.session_state.save_msg = f"Benutzer '{txt_username}' erfolgreich geändert" st.session_state.save_msg = f"Dashboard '{txt_dash_text}' erfolgreich geändert"
else: else:
st.session_state.save_msg = "❌ Fehler beim Speichern" st.session_state.save_msg = "❌ Fehler beim Speichern"
st.rerun() st.rerun()
@@ -176,62 +156,58 @@ def dialog_edit_dashboard(id):
def dashborad(): def dashborad():
if "selected_dashid" not in st.session_state: if "selected_dash_id" not in st.session_state:
st.session_state.selected_dash_id = None st.session_state.selected_dash_id = None
# tab_user, tab_role, tab_permission = st.tabs(["Benutzer", "Rollen", "Berechtigungen"])
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# Dashboard-Verwaltung # Dashboard-Verwaltung
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# with tab_user:
df = get_list(""" df = get_list("""
select select
u.id, d.dash_id,
u.username, d.dash_text,
u.firstname, d.dash_description,
u.lastname, d.group_id || ' | ' || g.group_text as "group",
u.role_id || ' | ' || r.role_text as role, d.page_file,
u.new_pwd, d.dash_type,
u.active d.active,
d.order_no
from from
users u dashboards d
left join roles r left join groups g
on u.role_id = r.role_id on d.group_id = g.group_id
""") """)
col_find_dashboard col_create_dashboard, col_edit_dashboard, col_delete_user = st.columns([3,2,2,2], vertical_alignment="bottom") col_find_dashboard, col_create_dashboard, col_edit_dashboard, col_delete_dashboard = st.columns([3,2,2,2], vertical_alignment="bottom")
with col_find_user: with col_find_dashboard:
txt_search = st.text_input(label="Suche", label_visibility="hidden", placeholder="Benutzer, Vorname, ...", icon=":material/search:") txt_search = st.text_input(label="Suche", label_visibility="hidden", placeholder="Dashboard, Beschreibung ...", icon=":material/search:")
with col_create_user: with col_create_dashboard:
if st.button(label="Benutzer anlegen", use_container_width=True, icon=":material/person_add:"): if st.button(label="Dashboard anlegen", use_container_width=True, icon=":material/add:"):
dialog_create_user() dialog_create_dashboard()
if "save_msg" in st.session_state: if "save_msg" in st.session_state:
st.toast(st.session_state.save_msg) st.toast(st.session_state.save_msg)
del st.session_state.save_msg del st.session_state.save_msg
with col_edit_user: with col_edit_dashboard:
if st.button(label="Benutzer bearbeiten", use_container_width=True, icon=":material/person_edit:"): if st.button(label="Dashboard bearbeiten", use_container_width=True, icon=":material/edit:"):
if not st.session_state.selected_user_id is None: if not st.session_state.selected_dash_id is None:
dialog_edit_user(st.session_state.selected_user_id) dialog_edit_dashboard(st.session_state.selected_dash_id)
else: else:
st.toast("❌ Bitte erst eine Zeile auswählen") st.toast("❌ Bitte erst eine Zeile auswählen")
if "save_msg" in st.session_state: if "save_msg" in st.session_state:
st.toast(st.session_state.save_msg) st.toast(st.session_state.save_msg)
del st.session_state.save_msg del st.session_state.save_msg
with col_delete_user: with col_delete_dashboard:
if st.button(label="Benutzer löschen", use_container_width=True, icon=":material/person_remove:"): if st.button(label="Dashboard löschen", use_container_width=True, icon=":material/delete:"):
if not st.session_state.selected_user_id is None: if not st.session_state.selected_dash_id is None:
dialog_delete_user(st.session_state.selected_user_id) dialog_delete_dashboard(st.session_state.selected_dash_id)
else: else:
st.toast("❌ Bitte erst eine Zeile auswählen") st.toast("❌ Bitte erst eine Zeile auswählen")
if "delete_msg" in st.session_state: if "delete_msg" in st.session_state:
@@ -244,21 +220,21 @@ def dashborad():
if txt_search.strip(): if txt_search.strip():
txt_search_norm = txt_search.strip().lower() txt_search_norm = txt_search.strip().lower()
mask = ( mask = (
df["username"].fillna("").str.lower().str.contains(txt_search_norm) df["dash_text"].fillna("").str.lower().str.contains(txt_search_norm)
| df["firstname"].fillna("").str.lower().str.contains(txt_search_norm) | df["dash_description"].fillna("").str.lower().str.contains(txt_search_norm)
| df["lastname"].fillna("").str.lower().str.contains(txt_search_norm) | df["group"].fillna("").str.lower().str.contains(txt_search_norm)
) )
df_view = df.loc[mask].copy() df_view = df.loc[mask].copy()
else: else:
df_view = df.copy() df_view = df.copy()
event = st.dataframe(df_view,key="data", on_select="rerun", selection_mode="single-row") event = st.dataframe(df_view,key="dash_data", on_select="rerun", selection_mode="single-row")
rows = event.selection.rows rows = event.selection.rows
if rows: if rows:
row_idx = rows[0] row_idx = rows[0]
st.session_state.selected_user_id = int(df_view.iloc[row_idx]["id"]) st.session_state.selected_dash_id = int(df_view.iloc[row_idx]["dash_id"])
else: else:
st.session_state.selected_user_id = None st.session_state.selected_dash_id = None

View File

@@ -12,6 +12,19 @@ def get_roles():
return df return df
def get_groups():
sql = """
select
group_id || ' | ' || group_text as "group"
from
groups
"""
df = get_list(sql)
return df
def get_id(id_text: str): def get_id(id_text: str):
id = int(id_text.split("|")[0]) id = int(id_text.split("|")[0])
if not id: if not id: