Jacksonnavigator7 commited on
Commit
a5ba942
Β·
verified Β·
1 Parent(s): 1f94c41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -38
app.py CHANGED
@@ -3,37 +3,37 @@ import sqlite3
3
  import datetime
4
  import os
5
 
6
- # Create or connect to the database
 
 
7
  def init_db():
8
- conn = sqlite3.connect("attendance.db")
9
- c = conn.cursor()
10
-
11
- # Create tables if they don't exist
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
-
20
- c.execute('''CREATE TABLE IF NOT EXISTS attendance (
21
- id INTEGER PRIMARY KEY AUTOINCREMENT,
22
- username TEXT,
23
- date TEXT,
24
- time TEXT,
25
- session TEXT
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
- 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,24 +42,30 @@ def register_user(username, password, role):
42
 
43
  def login_user(username, password):
44
  try:
45
- c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
46
- return c.fetchone()
47
- except Exception as e:
 
 
48
  return None
49
 
50
  def set_device(username, device_id):
51
  try:
52
- c.execute("UPDATE users SET device_id=? WHERE username=?", (device_id, username))
53
- conn.commit()
 
 
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
- c.execute("SELECT device_id FROM users WHERE username=?", (username,))
61
- result = c.fetchone()
62
- return result and result[0] == device_id
 
 
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
- c.execute("INSERT INTO attendance (username, date, time, session) VALUES (?, ?, ?, ?)", (username, date, time, session))
72
- conn.commit()
 
 
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
- c.execute("SELECT * FROM attendance ORDER BY date DESC, time DESC")
81
- records = c.fetchall()
 
 
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])