174 lines
6.0 KiB
Python
174 lines
6.0 KiB
Python
from contextlib import closing
|
|
import streamlit as st
|
|
from auth import get_fullname_for_user, get_role_for_user
|
|
from app_db.app_db import get_conn, get_list
|
|
# import sqlite3
|
|
|
|
|
|
def build_sidebar():
|
|
|
|
if st.session_state.get("authentication_status") != True:
|
|
return
|
|
|
|
authenticator = st.session_state.get("authenticator")
|
|
username = st.session_state.get("username")
|
|
|
|
if not authenticator or not username:
|
|
return
|
|
|
|
role = get_role_for_user(username)
|
|
fullname = get_fullname_for_user(username)
|
|
|
|
if role == "admin":
|
|
sql = """
|
|
select
|
|
g.group_id,
|
|
g.group_text,
|
|
d.dash_id,
|
|
d.dash_text,
|
|
d.page_file,
|
|
d.dash_type
|
|
from
|
|
groups g
|
|
left join dashboards d
|
|
on g.group_id = d.group_id
|
|
where
|
|
g.active = 1
|
|
and d.active = 1
|
|
"""
|
|
else:
|
|
sql = """
|
|
SELECT
|
|
d.group_id,
|
|
g.group_text,
|
|
p.dash_id,
|
|
d.dash_text,
|
|
d.page_file,
|
|
d.dash_type
|
|
FROM
|
|
users u
|
|
left join permissions p
|
|
on u.role_id = p.role_id
|
|
left join dashboards d
|
|
on p.dash_id = d.dash_id
|
|
left join groups g
|
|
on d.group_id = g.group_id
|
|
where
|
|
u.active = 1
|
|
and g.active = 1
|
|
and d.active = 1
|
|
and p.active = 1
|
|
and u.username = ?
|
|
order by
|
|
g.order_no,
|
|
d.order_no
|
|
"""
|
|
params = (username,) if "?" in sql else None
|
|
df = get_list(sql, params)
|
|
|
|
with st.sidebar:
|
|
st.logo("app/images/GMN_Logo_neu_rgb.png", size="small")
|
|
st.write(f"**{fullname}** ({role})")
|
|
|
|
col1, col2 = st.columns(2)
|
|
with col1:
|
|
if st.button("Logout", use_container_width=True):
|
|
authenticator.logout("Logout", "unrendered")
|
|
st.rerun()
|
|
with col2:
|
|
if st.button("Home", use_container_width=True):
|
|
st.switch_page("pages/home.py")
|
|
st.divider()
|
|
st.markdown("## Menü")
|
|
|
|
# for group_text, df_group in df.groupby("group_text"):
|
|
# with st.expander(group_text, expanded=False):
|
|
# for _, row in df_group.iterrows():
|
|
# dash_type = row.get("dash_type")
|
|
# page_file = row.get("page_file")
|
|
# label = row.get("dash_text", "")
|
|
# print(dash_type, page_file, label)
|
|
# # 1) echte Streamlit-Page
|
|
# if dash_type == "page" and isinstance(page_file, str) and page_file.strip():
|
|
# st.page_link(
|
|
# page_file, # z.B. "pages/umsatz.py"
|
|
# label=label,
|
|
# )
|
|
|
|
# # 2) externer Link (oder interner HTTP-Link)
|
|
# elif dash_type == "url" and isinstance(page_file, str) and page_file.strip():
|
|
# st.markdown(
|
|
# f"[{label}]({page_file})",
|
|
# unsafe_allow_html=False,
|
|
# )
|
|
|
|
# # 3) Platzhalter / noch nicht implementiert
|
|
# else:
|
|
# st.write(f"▫️ {label} (in Vorbereitung)")
|
|
|
|
|
|
# --- Suchfeld ---
|
|
query = st.text_input("Menü-Suche", "", placeholder="z.B. Umsatz, Kosten, User ...")
|
|
query = query.strip()
|
|
|
|
# Aktive Seite ermitteln (für Expander-Status / Highlight)
|
|
current_page = st.session_state.get("_page_path")
|
|
|
|
# --- DF filtern, falls Suchbegriff gesetzt ---
|
|
if query:
|
|
mask = (
|
|
df["dash_text"].str.contains(query, case=False, na=False)
|
|
| df["group_text"].str.contains(query, case=False, na=False)
|
|
)
|
|
df_view = df[mask].copy()
|
|
else:
|
|
df_view = df
|
|
|
|
if df_view.empty:
|
|
st.info("Keine Einträge zum Suchbegriff gefunden.")
|
|
return
|
|
|
|
# --- Gruppiert durchlaufen ---
|
|
for group_text, df_group in df_view.groupby("group_text"):
|
|
# Expander offen, wenn:
|
|
# - aktuelle Seite in dieser Gruppe liegt
|
|
group_open = any(
|
|
(row["page_file"] == current_page)
|
|
for _, row in df_group.iterrows()
|
|
)
|
|
|
|
with st.expander(group_text, expanded=(group_open or bool(query))):
|
|
for _, row in df_group.iterrows():
|
|
dash_type = row.get("dash_type")
|
|
page_file = row.get("page_file")
|
|
label = row.get("dash_text", "")
|
|
|
|
# Streamlit-Page
|
|
if dash_type == "page" and isinstance(page_file, str) and page_file.strip():
|
|
st.page_link(page_file, label=label)
|
|
|
|
# Externer Link
|
|
elif dash_type == "url" and isinstance(page_file, str) and page_file.strip():
|
|
st.markdown(f"[{label}]({page_file})")
|
|
|
|
# Platzhalter / Sonstiges
|
|
else:
|
|
st.write(f"▫️ {label}")
|
|
|
|
# Damit die leere Sidebar komplett verschwindet nach dem Logout
|
|
def hide_sidebar_if_logged_out():
|
|
if st.session_state.get("authentication_status") != True:
|
|
st.markdown("""
|
|
<style>
|
|
/* komplette Sidebar + Toggle ausblenden */
|
|
[data-testid="stSidebar"] {display: none;}
|
|
[data-testid="stSidebarNav"] {display: none;}
|
|
[data-testid="collapsedControl"] {display: none;}
|
|
|
|
/* Content wieder ganz nach links */
|
|
[data-testid="stAppViewContainer"] {
|
|
margin-left: 0;
|
|
}
|
|
</style>
|
|
""", unsafe_allow_html=True)
|