Py.Cafe

noordentist/

dental-clinic-management

Dental Clinic Management

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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()