Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,37 +3,37 @@ import sqlite3
|
|
| 3 |
import datetime
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
def init_db():
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
conn.commit()
|
| 28 |
-
return conn, c
|
| 29 |
-
|
| 30 |
-
conn, c = init_db()
|
| 31 |
|
| 32 |
# Auth functions
|
| 33 |
def register_user(username, password, role):
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
return "β
User registered successfully."
|
| 38 |
except sqlite3.IntegrityError:
|
| 39 |
return "β Username already exists."
|
|
@@ -42,24 +42,30 @@ def register_user(username, password, role):
|
|
| 42 |
|
| 43 |
def login_user(username, password):
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
return None
|
| 49 |
|
| 50 |
def set_device(username, device_id):
|
| 51 |
try:
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
return "Device registered."
|
| 55 |
except Exception as e:
|
| 56 |
return f"β Device registration error: {str(e)}"
|
| 57 |
|
| 58 |
def check_device(username, device_id):
|
| 59 |
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
except:
|
| 64 |
return False
|
| 65 |
|
|
@@ -68,8 +74,10 @@ def mark_attendance(username, session):
|
|
| 68 |
try:
|
| 69 |
date = datetime.date.today().isoformat()
|
| 70 |
time = datetime.datetime.now().strftime("%H:%M:%S")
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
return f"β
Attendance marked at {time} for session: {session}"
|
| 74 |
except Exception as e:
|
| 75 |
return f"β Attendance error: {str(e)}"
|
|
@@ -77,8 +85,10 @@ def mark_attendance(username, session):
|
|
| 77 |
# View attendance dashboard
|
| 78 |
def view_attendance():
|
| 79 |
try:
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
if not records:
|
| 83 |
return "No attendance records yet."
|
| 84 |
return "\n".join([f"{r[1]} | {r[2]} | {r[3]} | {r[4]}" for r in records])
|
|
|
|
| 3 |
import datetime
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
DB_PATH = "attendance.db"
|
| 7 |
+
|
| 8 |
+
# Create database if not exists
|
| 9 |
def init_db():
|
| 10 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 11 |
+
c = conn.cursor()
|
| 12 |
+
c.execute('''CREATE TABLE IF NOT EXISTS users (
|
| 13 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
+
username TEXT UNIQUE,
|
| 15 |
+
password TEXT,
|
| 16 |
+
role TEXT,
|
| 17 |
+
device_id TEXT
|
| 18 |
+
)''')
|
| 19 |
+
c.execute('''CREATE TABLE IF NOT EXISTS attendance (
|
| 20 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 21 |
+
username TEXT,
|
| 22 |
+
date TEXT,
|
| 23 |
+
time TEXT,
|
| 24 |
+
session TEXT
|
| 25 |
+
)''')
|
| 26 |
+
conn.commit()
|
| 27 |
+
|
| 28 |
+
init_db()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Auth functions
|
| 31 |
def register_user(username, password, role):
|
| 32 |
try:
|
| 33 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 34 |
+
c = conn.cursor()
|
| 35 |
+
c.execute("INSERT INTO users (username, password, role) VALUES (?, ?, ?)", (username, password, role))
|
| 36 |
+
conn.commit()
|
| 37 |
return "β
User registered successfully."
|
| 38 |
except sqlite3.IntegrityError:
|
| 39 |
return "β Username already exists."
|
|
|
|
| 42 |
|
| 43 |
def login_user(username, password):
|
| 44 |
try:
|
| 45 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 46 |
+
c = conn.cursor()
|
| 47 |
+
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
|
| 48 |
+
return c.fetchone()
|
| 49 |
+
except Exception:
|
| 50 |
return None
|
| 51 |
|
| 52 |
def set_device(username, device_id):
|
| 53 |
try:
|
| 54 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 55 |
+
c = conn.cursor()
|
| 56 |
+
c.execute("UPDATE users SET device_id=? WHERE username=?", (device_id, username))
|
| 57 |
+
conn.commit()
|
| 58 |
return "Device registered."
|
| 59 |
except Exception as e:
|
| 60 |
return f"β Device registration error: {str(e)}"
|
| 61 |
|
| 62 |
def check_device(username, device_id):
|
| 63 |
try:
|
| 64 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 65 |
+
c = conn.cursor()
|
| 66 |
+
c.execute("SELECT device_id FROM users WHERE username=?", (username,))
|
| 67 |
+
result = c.fetchone()
|
| 68 |
+
return result and result[0] == device_id
|
| 69 |
except:
|
| 70 |
return False
|
| 71 |
|
|
|
|
| 74 |
try:
|
| 75 |
date = datetime.date.today().isoformat()
|
| 76 |
time = datetime.datetime.now().strftime("%H:%M:%S")
|
| 77 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 78 |
+
c = conn.cursor()
|
| 79 |
+
c.execute("INSERT INTO attendance (username, date, time, session) VALUES (?, ?, ?, ?)", (username, date, time, session))
|
| 80 |
+
conn.commit()
|
| 81 |
return f"β
Attendance marked at {time} for session: {session}"
|
| 82 |
except Exception as e:
|
| 83 |
return f"β Attendance error: {str(e)}"
|
|
|
|
| 85 |
# View attendance dashboard
|
| 86 |
def view_attendance():
|
| 87 |
try:
|
| 88 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 89 |
+
c = conn.cursor()
|
| 90 |
+
c.execute("SELECT * FROM attendance ORDER BY date DESC, time DESC")
|
| 91 |
+
records = c.fetchall()
|
| 92 |
if not records:
|
| 93 |
return "No attendance records yet."
|
| 94 |
return "\n".join([f"{r[1]} | {r[2]} | {r[3]} | {r[4]}" for r in records])
|