Add dashboard management - edit function
This commit is contained in:
Binary file not shown.
@@ -5,7 +5,7 @@ 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_roles, get_id
|
||||
from ui.selectboxes import get_groups, get_id
|
||||
import bcrypt
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ def sidebar():
|
||||
if st.button("Home", use_container_width=True, icon=":material/home:"):
|
||||
st.switch_page("pages/home.py")
|
||||
|
||||
user()
|
||||
dashborad()
|
||||
|
||||
|
||||
@st.dialog("Dashboard anlegen")
|
||||
@@ -84,90 +84,70 @@ def dialog_delete_dashboard(id):
|
||||
|
||||
|
||||
@st.dialog("Dashboard bearbeiten")
|
||||
def dialog_edit_dashboard(id):
|
||||
if id == None:
|
||||
st.write("kein Benutzer ausgewählt")
|
||||
def dialog_edit_dashboard(dash_id):
|
||||
if dash_id == None:
|
||||
st.write("kein Dashboard ausgewählt")
|
||||
else:
|
||||
sql = """
|
||||
select
|
||||
u.id,
|
||||
u.username as user, -- ACHTUNG: nicht mit username arbeiten, da Überschneidung in sessionstate!!
|
||||
u.firstname,
|
||||
u.lastname,
|
||||
u.email,
|
||||
u.role_id || ' | ' || r.role_text as role,
|
||||
r.role_text,
|
||||
u.new_pwd,
|
||||
u.active
|
||||
from
|
||||
users u
|
||||
left join roles r
|
||||
on u.role_id = r.role_id
|
||||
where u.id = ?
|
||||
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,(id,))
|
||||
df = get_list(sql,(dash_id,))
|
||||
|
||||
|
||||
# df = get_list("select username from users where id = ?",(id,))
|
||||
|
||||
# st.session_state.orig_user_data = df
|
||||
|
||||
|
||||
df_roles = get_roles()
|
||||
roles = df_roles["text"].tolist()
|
||||
role = df.iloc[0]["role"]
|
||||
# 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 = roles.index(role)
|
||||
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_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:
|
||||
is_active = st.checkbox(label="Aktiv", value=df.iloc[0]["active"])
|
||||
txt_firstname = st.text_input(label="Vorname", value=df.iloc[0]["firstname"])
|
||||
txt_lastname = st.text_input(label="Nachname", value=df.iloc[0]["lastname"])
|
||||
txt_email = st.text_input(label="Email", value=df.iloc[0]["email"])
|
||||
txt_pwd = st.text_input(label="Passwort", placeholder="Neues Passwort eingeben", type="password")
|
||||
new_pwd = st.checkbox(label="Neues Passwort", value=df.iloc[0]["new_pwd"])
|
||||
cmb_role = st.selectbox(label="Rolle", options=roles, placeholder="Rolle auswählen", index=idx)
|
||||
|
||||
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"):
|
||||
pw_hash = bcrypt.hashpw(txt_pwd.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
||||
|
||||
if txt_pwd and txt_pwd.strip():
|
||||
sql = """
|
||||
update users set
|
||||
username = ?,
|
||||
active = ?,
|
||||
firstname = ?,
|
||||
lastname = ?,
|
||||
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)
|
||||
# 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)
|
||||
|
||||
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"✅ Benutzer '{txt_username}' erfolgreich geändert"
|
||||
st.session_state.save_msg = f"✅ Dashboard '{txt_dash_text}' erfolgreich geändert"
|
||||
else:
|
||||
st.session_state.save_msg = "❌ Fehler beim Speichern"
|
||||
st.rerun()
|
||||
@@ -176,62 +156,58 @@ def dialog_edit_dashboard(id):
|
||||
|
||||
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
|
||||
|
||||
# tab_user, tab_role, tab_permission = st.tabs(["Benutzer", "Rollen", "Berechtigungen"])
|
||||
|
||||
#--------------------------------------------------------------------------------------------------
|
||||
# Dashboard-Verwaltung
|
||||
#--------------------------------------------------------------------------------------------------
|
||||
|
||||
# with tab_user:
|
||||
|
||||
|
||||
|
||||
df = get_list("""
|
||||
select
|
||||
u.id,
|
||||
u.username,
|
||||
u.firstname,
|
||||
u.lastname,
|
||||
u.role_id || ' | ' || r.role_text as role,
|
||||
u.new_pwd,
|
||||
u.active
|
||||
from
|
||||
users u
|
||||
left join roles r
|
||||
on u.role_id = r.role_id
|
||||
""")
|
||||
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_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:
|
||||
txt_search = st.text_input(label="Suche", label_visibility="hidden", placeholder="Benutzer, Vorname, ...", icon=":material/search:")
|
||||
with col_find_dashboard:
|
||||
txt_search = st.text_input(label="Suche", label_visibility="hidden", placeholder="Dashboard, Beschreibung ...", icon=":material/search:")
|
||||
|
||||
with col_create_user:
|
||||
if st.button(label="Benutzer anlegen", use_container_width=True, icon=":material/person_add:"):
|
||||
dialog_create_user()
|
||||
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_user:
|
||||
if st.button(label="Benutzer bearbeiten", use_container_width=True, icon=":material/person_edit:"):
|
||||
if not st.session_state.selected_user_id is None:
|
||||
dialog_edit_user(st.session_state.selected_user_id)
|
||||
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_user:
|
||||
if st.button(label="Benutzer löschen", use_container_width=True, icon=":material/person_remove:"):
|
||||
if not st.session_state.selected_user_id is None:
|
||||
dialog_delete_user(st.session_state.selected_user_id)
|
||||
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:
|
||||
@@ -244,21 +220,21 @@ def dashborad():
|
||||
if txt_search.strip():
|
||||
txt_search_norm = txt_search.strip().lower()
|
||||
mask = (
|
||||
df["username"].fillna("").str.lower().str.contains(txt_search_norm)
|
||||
| df["firstname"].fillna("").str.lower().str.contains(txt_search_norm)
|
||||
| df["lastname"].fillna("").str.lower().str.contains(txt_search_norm)
|
||||
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="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
|
||||
if rows:
|
||||
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:
|
||||
st.session_state.selected_user_id = None
|
||||
st.session_state.selected_dash_id = None
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,19 @@ def get_roles():
|
||||
|
||||
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):
|
||||
id = int(id_text.split("|")[0])
|
||||
if not id:
|
||||
|
||||
Reference in New Issue
Block a user