import streamlit as st
import sqlite3
from passlib.hash import pbkdf2_sha256
# Option to use database integration. Toggle to True to use SQLite database.
USE_DATABASE = False
# =============================================================================
# Database Integration / Dummy Store Setup
# =============================================================================
if USE_DATABASE:
# Function to get a database connection with SQLite.
def get_db_connection():
conn = sqlite3.connect("users.db", check_same_thread=False)
conn.execute(
"CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password TEXT)"
)
return conn
@st.experimental_singleton
def get_connection():
return get_db_connection()
def add_user(username, password_hash):
conn = get_connection()
try:
with conn:
conn.execute(
"INSERT INTO users (username, password) VALUES (?, ?)",
(username, password_hash),
)
return True
except sqlite3.IntegrityError:
return False
def get_user(username):
conn = get_connection()
cur = conn.execute("SELECT username, password FROM users WHERE username = ?", (username,))
return cur.fetchone()
def update_user_password(username, new_password_hash):
conn = get_connection()
with conn:
conn.execute(
"UPDATE users SET password = ? WHERE username = ?",
(new_password_hash, username),
)
return True
else:
# Dummy in-memory store.
users_store = {}
def add_user(username, password_hash):
if username in users_store:
return False
users_store[username] = password_hash
return True
def get_user(username):
if username in users_store:
return (username, users_store[username])
return None
def update_user_password(username, new_password_hash):
if username in users_store:
users_store[username] = new_password_hash
return True
return False
# =============================================================================
# Authentication Functions
# =============================================================================
def hash_password(password):
# Hashing the password using pbkdf2_sha256.
return pbkdf2_sha256.hash(password)
def verify_password(password, password_hash):
# Verifies a plaintext password against its hash.
return pbkdf2_sha256.verify(password, password_hash)
def login_user(username, password):
user = get_user(username)
if user and verify_password(password, user[1]):
return True, ""
return False, "Invalid username or password."
def signup_user(username, password):
password_hash = hash_password(password)
if add_user(username, password_hash):
return True, ""
return False, "Username already exists. Please choose a different username."
def reset_user_password(username, new_password):
user = get_user(username)
if user:
new_hash = hash_password(new_password)
update_user_password(username, new_hash)
return True, ""
return False, "Username not found."
# =============================================================================
# Modular Pages for the Dashboard
# =============================================================================
def treatment_planning_page():
st.header("Treatment Planning")
st.write("This section will help with planning dental treatments.")
# Additional treatment planning functionality goes here
def inventory_page():
st.header("Inventory")
st.write("Manage and view your inventory here.")
# Additional inventory management functionality
def scheduling_page():
st.header("Scheduling")
st.write("Schedule appointments and manage calendars here.")
# Additional scheduling functionality
def lab_records_page():
st.header("Lab Records")
st.write("Access and manage dental lab records here.")
# Additional lab records management functionality
def dashboard_page():
st.title("Dental Clinic Dashboard")
# Sidebar Navigation
menu = ["Treatment Planning", "Inventory", "Scheduling", "Lab Records", "Logout"]
choice = st.sidebar.radio("Navigation", menu)
if choice == "Treatment Planning":
treatment_planning_page()
elif choice == "Inventory":
inventory_page()
elif choice == "Scheduling":
scheduling_page()
elif choice == "Lab Records":
lab_records_page()
elif choice == "Logout":
st.session_state.authenticated = False
st.experimental_rerun()
# =============================================================================
# Landing Page with Introduction and Authentication Tabs
# =============================================================================
def landing_page():
st.title("Welcome to Our Dental Clinic")
# Introduction Section
st.markdown("## About Our Clinic")
st.write(
"""
Our clinic offers comprehensive dental services including routine check-ups,
cosmetic dentistry, orthodontics, and emergency dental care. We are committed to
providing high-quality patient care with a focus on compassion and excellence.
"""
)
# Authentication Section as Tabs
tabs = st.tabs(["Login", "Sign Up", "Reset Password"])
with tabs[0]:
st.subheader("Login")
login_username = st.text_input("Username", key="login_username")
login_password = st.text_input("Password", type="password", key="login_password")
if st.button("Log In", key="login_button"):
if not login_username or not login_password:
st.error("Please enter both username and password.")
else:
success, msg = login_user(login_username, login_password)
if success:
st.session_state.authenticated = True
st.session_state.username = login_username
st.success("Logged in successfully!")
st.experimental_rerun()
else:
st.error(msg)
with tabs[1]:
st.subheader("Sign Up")
signup_username = st.text_input("Username", key="signup_username")
signup_password = st.text_input("Password", type="password", key="signup_password")
signup_confirm = st.text_input("Confirm Password", type="password", key="signup_confirm")
if st.button("Sign Up", key="signup_button"):
if not signup_username or not signup_password or not signup_confirm:
st.error("Please fill in all fields.")
elif signup_password != signup_confirm:
st.error("Passwords do not match.")
else:
success, msg = signup_user(signup_username, signup_password)
if success:
st.success("Account created! You can now log in.")
else:
st.error(msg)
with tabs[2]:
st.subheader("Reset Password")
reset_username = st.text_input("Username", key="reset_username")
new_password = st.text_input("New Password", type="password", key="new_password")
new_password_confirm = st.text_input("Confirm New Password", type="password", key="new_password_confirm")
if st.button("Reset Password", key="reset_button"):
if not reset_username or not new_password or not new_password_confirm:
st.error("Please fill in all fields.")
elif new_password != new_password_confirm:
st.error("Passwords do not match.")
else:
success, msg = reset_user_password(reset_username, new_password)
if success:
st.success("Password reset successfully! You can now log in with your new password.")
else:
st.error(msg)
# =============================================================================
# Main Application Control
# =============================================================================
def main():
# Initialize session state for authentication if not set
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if st.session_state.authenticated:
dashboard_page()
else:
landing_page()
if __name__ == "__main__":
main()