Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,9 +75,12 @@ def send_mail(to, subject, body):
|
|
| 75 |
|
| 76 |
def login(user, pwd):
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
row = staff_df[
|
| 79 |
-
(staff_df["staff_id"]==user) &
|
| 80 |
-
(staff_df["password"]==pwd)
|
| 81 |
]
|
| 82 |
|
| 83 |
if row.empty:
|
|
@@ -90,101 +93,131 @@ def login(user, pwd):
|
|
| 90 |
|
| 91 |
|
| 92 |
# =========================
|
| 93 |
-
# DATE
|
| 94 |
# =========================
|
| 95 |
|
| 96 |
-
def valid_date(
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
# =========================
|
| 105 |
-
# TIME
|
| 106 |
# =========================
|
| 107 |
|
| 108 |
-
def valid_time(
|
| 109 |
|
| 110 |
try:
|
| 111 |
-
f = datetime.strptime(
|
| 112 |
-
t = datetime.strptime(
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
|
|
|
| 118 |
return False
|
| 119 |
|
|
|
|
| 120 |
if f >= t:
|
| 121 |
return False
|
| 122 |
|
| 123 |
return True
|
|
|
|
| 124 |
except:
|
| 125 |
return False
|
| 126 |
|
| 127 |
|
| 128 |
# =========================
|
| 129 |
-
# SUBMIT
|
| 130 |
# =========================
|
| 131 |
|
| 132 |
-
def submit(staff_id,name,date,from_t,to_t,reason):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
if not all([staff_id,name,date,from_t,to_t,reason]):
|
| 135 |
-
return "β Fill All Fields"
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
if not valid_date(d):
|
| 140 |
-
return "β Apply within 2 days only"
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
-
# Same
|
| 147 |
cur.execute("""
|
| 148 |
SELECT COUNT(*) FROM permissions
|
| 149 |
WHERE staff_id=? AND date=?
|
| 150 |
-
""",(staff_id,date))
|
| 151 |
|
| 152 |
if cur.fetchone()[0] >= 1:
|
| 153 |
-
return "β Already
|
| 154 |
|
| 155 |
|
| 156 |
-
# Monthly
|
| 157 |
month = date[:7]
|
| 158 |
|
| 159 |
cur.execute("""
|
| 160 |
SELECT COUNT(*) FROM permissions
|
| 161 |
WHERE staff_id=? AND substr(date,1,7)=?
|
| 162 |
-
""",(staff_id,month))
|
| 163 |
|
| 164 |
if cur.fetchone()[0] >= 2:
|
| 165 |
-
return "β Monthly
|
| 166 |
|
| 167 |
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
cur.execute("""
|
| 171 |
INSERT INTO permissions
|
| 172 |
VALUES(NULL,?,?,?,?,?,?,?)
|
| 173 |
-
""",(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
conn.commit()
|
| 176 |
|
| 177 |
|
| 178 |
-
# Email
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
send_mail(
|
| 182 |
-
email,
|
| 183 |
-
"Permission Approved",
|
| 184 |
-
f"Your permission on {date} is submitted"
|
| 185 |
-
)
|
| 186 |
|
| 187 |
-
return "β
Applied Successfully"
|
| 188 |
|
| 189 |
|
| 190 |
# =========================
|
|
@@ -193,10 +226,10 @@ def submit(staff_id,name,date,from_t,to_t,reason):
|
|
| 193 |
|
| 194 |
def download_report():
|
| 195 |
|
| 196 |
-
df = pd.read_sql("SELECT * FROM permissions",conn)
|
| 197 |
|
| 198 |
file = "permission_report.xlsx"
|
| 199 |
-
df.to_excel(file,index=False)
|
| 200 |
|
| 201 |
return file
|
| 202 |
|
|
@@ -205,19 +238,26 @@ def download_report():
|
|
| 205 |
# RESET PASSWORD
|
| 206 |
# =========================
|
| 207 |
|
| 208 |
-
def reset_pwd(staff_id,newpwd):
|
| 209 |
|
| 210 |
global staff_df
|
| 211 |
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
if len(idx)==0:
|
| 215 |
-
return "β Invalid ID"
|
| 216 |
|
| 217 |
-
staff_df.loc[idx,"password"] = newpwd
|
| 218 |
-
staff_df.to_excel("staff_details.xlsx",index=False)
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
|
| 223 |
# =========================
|
|
@@ -232,11 +272,12 @@ with gr.Blocks() as app:
|
|
| 232 |
<style>
|
| 233 |
|
| 234 |
#head{
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
| 240 |
}
|
| 241 |
|
| 242 |
</style>
|
|
@@ -245,7 +286,7 @@ with gr.Blocks() as app:
|
|
| 245 |
|
| 246 |
with gr.Row(elem_id="head"):
|
| 247 |
|
| 248 |
-
gr.Image("logo.jpg",height=90)
|
| 249 |
|
| 250 |
gr.Markdown("""
|
| 251 |
## SRC, SASTRA
|
|
@@ -253,15 +294,16 @@ with gr.Blocks() as app:
|
|
| 253 |
""")
|
| 254 |
|
| 255 |
|
| 256 |
-
# LOGIN
|
| 257 |
-
|
|
|
|
| 258 |
|
| 259 |
uid = gr.Textbox(label="User ID")
|
| 260 |
-
pwd = gr.Textbox(label="Password",type="password")
|
| 261 |
|
| 262 |
login_btn = gr.Button("Login")
|
| 263 |
|
| 264 |
-
msg = gr.Textbox()
|
| 265 |
|
| 266 |
role = gr.State()
|
| 267 |
uname = gr.State()
|
|
@@ -269,33 +311,50 @@ with gr.Blocks() as app:
|
|
| 269 |
|
| 270 |
login_btn.click(
|
| 271 |
login,
|
| 272 |
-
[uid,pwd],
|
| 273 |
-
[msg,role,uname]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
)
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
| 279 |
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
|
| 285 |
-
apply_btn = gr.Button("Apply")
|
| 286 |
|
| 287 |
-
out = gr.Textbox()
|
| 288 |
|
| 289 |
|
| 290 |
apply_btn.click(
|
| 291 |
submit,
|
| 292 |
-
[uid,uname,date,ft,tt,reason],
|
| 293 |
out
|
| 294 |
)
|
| 295 |
|
| 296 |
|
| 297 |
-
# ADMIN PANEL
|
| 298 |
-
|
|
|
|
| 299 |
|
| 300 |
report_btn = gr.Button("Download Report")
|
| 301 |
|
|
@@ -309,20 +368,25 @@ with gr.Blocks() as app:
|
|
| 309 |
)
|
| 310 |
|
| 311 |
|
| 312 |
-
# RESET
|
| 313 |
-
|
|
|
|
| 314 |
|
| 315 |
rid = gr.Textbox(label="Staff ID")
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
|
| 318 |
reset_btn = gr.Button("Reset")
|
| 319 |
|
| 320 |
-
rmsg = gr.Textbox()
|
| 321 |
|
| 322 |
|
| 323 |
reset_btn.click(
|
| 324 |
reset_pwd,
|
| 325 |
-
[rid,np],
|
| 326 |
rmsg
|
| 327 |
)
|
| 328 |
|
|
|
|
| 75 |
|
| 76 |
def login(user, pwd):
|
| 77 |
|
| 78 |
+
if user == "" or pwd == "":
|
| 79 |
+
return "β Enter Username & Password", "", ""
|
| 80 |
+
|
| 81 |
row = staff_df[
|
| 82 |
+
(staff_df["staff_id"] == user) &
|
| 83 |
+
(staff_df["password"] == pwd)
|
| 84 |
]
|
| 85 |
|
| 86 |
if row.empty:
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
# =========================
|
| 96 |
+
# DATE VALIDATION
|
| 97 |
# =========================
|
| 98 |
|
| 99 |
+
def valid_date(date_str):
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
d = datetime.strptime(date_str, "%Y-%m-%d").date()
|
| 103 |
|
| 104 |
+
today = datetime.today().date()
|
| 105 |
+
max_day = today + timedelta(days=2)
|
| 106 |
|
| 107 |
+
if today <= d <= max_day:
|
| 108 |
+
return True
|
| 109 |
+
else:
|
| 110 |
+
return False
|
| 111 |
+
|
| 112 |
+
except:
|
| 113 |
+
return False
|
| 114 |
|
| 115 |
|
| 116 |
# =========================
|
| 117 |
+
# TIME VALIDATION
|
| 118 |
# =========================
|
| 119 |
|
| 120 |
+
def valid_time(from_t, to_t):
|
| 121 |
|
| 122 |
try:
|
| 123 |
+
f = datetime.strptime(from_t, "%I:%M %p").time()
|
| 124 |
+
t = datetime.strptime(to_t, "%I:%M %p").time()
|
| 125 |
|
| 126 |
+
min_time = datetime.strptime("08:45 AM", "%I:%M %p").time()
|
| 127 |
+
max_time = datetime.strptime("05:20 PM", "%I:%M %p").time()
|
| 128 |
|
| 129 |
+
# Range Check
|
| 130 |
+
if f < min_time or t > max_time:
|
| 131 |
return False
|
| 132 |
|
| 133 |
+
# Logical Check
|
| 134 |
if f >= t:
|
| 135 |
return False
|
| 136 |
|
| 137 |
return True
|
| 138 |
+
|
| 139 |
except:
|
| 140 |
return False
|
| 141 |
|
| 142 |
|
| 143 |
# =========================
|
| 144 |
+
# SUBMIT PERMISSION
|
| 145 |
# =========================
|
| 146 |
|
| 147 |
+
def submit(staff_id, name, date, from_t, to_t, reason):
|
| 148 |
+
|
| 149 |
+
# Empty check
|
| 150 |
+
if not all([staff_id, name, date, from_t, to_t, reason]):
|
| 151 |
+
return "β All fields are mandatory"
|
| 152 |
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Date validation
|
| 155 |
+
if not valid_date(date):
|
| 156 |
+
return "β Apply only within today and next 2 days"
|
| 157 |
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
# Time validation
|
| 160 |
+
if not valid_time(from_t, to_t):
|
| 161 |
+
return "β Time must be between 8:45 AM and 5:20 PM"
|
| 162 |
|
| 163 |
|
| 164 |
+
# Same day check
|
| 165 |
cur.execute("""
|
| 166 |
SELECT COUNT(*) FROM permissions
|
| 167 |
WHERE staff_id=? AND date=?
|
| 168 |
+
""", (staff_id, date))
|
| 169 |
|
| 170 |
if cur.fetchone()[0] >= 1:
|
| 171 |
+
return "β Already applied today"
|
| 172 |
|
| 173 |
|
| 174 |
+
# Monthly limit
|
| 175 |
month = date[:7]
|
| 176 |
|
| 177 |
cur.execute("""
|
| 178 |
SELECT COUNT(*) FROM permissions
|
| 179 |
WHERE staff_id=? AND substr(date,1,7)=?
|
| 180 |
+
""", (staff_id, month))
|
| 181 |
|
| 182 |
if cur.fetchone()[0] >= 2:
|
| 183 |
+
return "β Monthly limit (2 only)"
|
| 184 |
|
| 185 |
|
| 186 |
+
# Save
|
| 187 |
+
applied_on = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 188 |
|
| 189 |
cur.execute("""
|
| 190 |
INSERT INTO permissions
|
| 191 |
VALUES(NULL,?,?,?,?,?,?,?)
|
| 192 |
+
""", (
|
| 193 |
+
staff_id,
|
| 194 |
+
name,
|
| 195 |
+
date,
|
| 196 |
+
from_t,
|
| 197 |
+
to_t,
|
| 198 |
+
reason,
|
| 199 |
+
applied_on
|
| 200 |
+
))
|
| 201 |
|
| 202 |
conn.commit()
|
| 203 |
|
| 204 |
|
| 205 |
+
# Email alert
|
| 206 |
+
try:
|
| 207 |
+
email = staff_df[
|
| 208 |
+
staff_df["staff_id"] == staff_id
|
| 209 |
+
].iloc[0]["email"]
|
| 210 |
+
|
| 211 |
+
send_mail(
|
| 212 |
+
email,
|
| 213 |
+
"Permission Submitted",
|
| 214 |
+
f"Your permission on {date} has been submitted successfully."
|
| 215 |
+
)
|
| 216 |
+
except:
|
| 217 |
+
pass
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
return "β
Permission Applied Successfully"
|
| 221 |
|
| 222 |
|
| 223 |
# =========================
|
|
|
|
| 226 |
|
| 227 |
def download_report():
|
| 228 |
|
| 229 |
+
df = pd.read_sql("SELECT * FROM permissions", conn)
|
| 230 |
|
| 231 |
file = "permission_report.xlsx"
|
| 232 |
+
df.to_excel(file, index=False)
|
| 233 |
|
| 234 |
return file
|
| 235 |
|
|
|
|
| 238 |
# RESET PASSWORD
|
| 239 |
# =========================
|
| 240 |
|
| 241 |
+
def reset_pwd(staff_id, newpwd):
|
| 242 |
|
| 243 |
global staff_df
|
| 244 |
|
| 245 |
+
if staff_id == "" or newpwd == "":
|
| 246 |
+
return "β Enter ID and Password"
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
idx = staff_df[
|
| 250 |
+
staff_df["staff_id"] == staff_id
|
| 251 |
+
].index
|
| 252 |
|
| 253 |
+
if len(idx) == 0:
|
| 254 |
+
return "β Invalid Staff ID"
|
| 255 |
|
|
|
|
|
|
|
| 256 |
|
| 257 |
+
staff_df.loc[idx, "password"] = newpwd
|
| 258 |
+
staff_df.to_excel("staff_details.xlsx", index=False)
|
| 259 |
+
|
| 260 |
+
return "β
Password Updated"
|
| 261 |
|
| 262 |
|
| 263 |
# =========================
|
|
|
|
| 272 |
<style>
|
| 273 |
|
| 274 |
#head{
|
| 275 |
+
display:flex;
|
| 276 |
+
gap:20px;
|
| 277 |
+
align-items:center;
|
| 278 |
+
justify-content:center;
|
| 279 |
+
padding:15px;
|
| 280 |
+
border-bottom:2px solid #ccc;
|
| 281 |
}
|
| 282 |
|
| 283 |
</style>
|
|
|
|
| 286 |
|
| 287 |
with gr.Row(elem_id="head"):
|
| 288 |
|
| 289 |
+
gr.Image("logo.jpg", height=90)
|
| 290 |
|
| 291 |
gr.Markdown("""
|
| 292 |
## SRC, SASTRA
|
|
|
|
| 294 |
""")
|
| 295 |
|
| 296 |
|
| 297 |
+
# ================= LOGIN =================
|
| 298 |
+
|
| 299 |
+
gr.Markdown("## π Login")
|
| 300 |
|
| 301 |
uid = gr.Textbox(label="User ID")
|
| 302 |
+
pwd = gr.Textbox(label="Password", type="password")
|
| 303 |
|
| 304 |
login_btn = gr.Button("Login")
|
| 305 |
|
| 306 |
+
msg = gr.Textbox(interactive=False)
|
| 307 |
|
| 308 |
role = gr.State()
|
| 309 |
uname = gr.State()
|
|
|
|
| 311 |
|
| 312 |
login_btn.click(
|
| 313 |
login,
|
| 314 |
+
[uid, pwd],
|
| 315 |
+
[msg, role, uname]
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
# ================= STAFF PANEL =================
|
| 320 |
+
|
| 321 |
+
gr.Markdown("## π Staff Panel")
|
| 322 |
+
|
| 323 |
+
date = gr.Textbox(
|
| 324 |
+
label="Date (YYYY-MM-DD)",
|
| 325 |
+
placeholder="2026-02-15"
|
| 326 |
)
|
| 327 |
|
| 328 |
+
ft = gr.Textbox(
|
| 329 |
+
label="From Time (HH:MM AM/PM)",
|
| 330 |
+
placeholder="09:00 AM"
|
| 331 |
+
)
|
| 332 |
|
| 333 |
+
tt = gr.Textbox(
|
| 334 |
+
label="To Time (HH:MM AM/PM)",
|
| 335 |
+
placeholder="01:00 PM"
|
| 336 |
+
)
|
| 337 |
|
| 338 |
+
reason = gr.Textbox(
|
| 339 |
+
label="Reason",
|
| 340 |
+
lines=3
|
| 341 |
+
)
|
| 342 |
|
| 343 |
+
apply_btn = gr.Button("Apply Permission", variant="primary")
|
| 344 |
|
| 345 |
+
out = gr.Textbox(interactive=False)
|
| 346 |
|
| 347 |
|
| 348 |
apply_btn.click(
|
| 349 |
submit,
|
| 350 |
+
[uid, uname, date, ft, tt, reason],
|
| 351 |
out
|
| 352 |
)
|
| 353 |
|
| 354 |
|
| 355 |
+
# ================= ADMIN PANEL =================
|
| 356 |
+
|
| 357 |
+
gr.Markdown("## π Admin Panel")
|
| 358 |
|
| 359 |
report_btn = gr.Button("Download Report")
|
| 360 |
|
|
|
|
| 368 |
)
|
| 369 |
|
| 370 |
|
| 371 |
+
# ================= RESET =================
|
| 372 |
+
|
| 373 |
+
gr.Markdown("## π Reset Password")
|
| 374 |
|
| 375 |
rid = gr.Textbox(label="Staff ID")
|
| 376 |
+
|
| 377 |
+
np = gr.Textbox(
|
| 378 |
+
label="New Password",
|
| 379 |
+
type="password"
|
| 380 |
+
)
|
| 381 |
|
| 382 |
reset_btn = gr.Button("Reset")
|
| 383 |
|
| 384 |
+
rmsg = gr.Textbox(interactive=False)
|
| 385 |
|
| 386 |
|
| 387 |
reset_btn.click(
|
| 388 |
reset_pwd,
|
| 389 |
+
[rid, np],
|
| 390 |
rmsg
|
| 391 |
)
|
| 392 |
|