Spaces:
Sleeping
Sleeping
Update database.py
Browse files- database.py +33 -1
database.py
CHANGED
|
@@ -44,4 +44,36 @@ def get_user_role(email):
|
|
| 44 |
cursor.execute("SELECT role FROM users WHERE email=?", (email,))
|
| 45 |
row = cursor.fetchone()
|
| 46 |
conn.close()
|
| 47 |
-
return row[0] if row else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
cursor.execute("SELECT role FROM users WHERE email=?", (email,))
|
| 45 |
row = cursor.fetchone()
|
| 46 |
conn.close()
|
| 47 |
+
return row[0] if row else None
|
| 48 |
+
|
| 49 |
+
def get_sessions(class_id):
|
| 50 |
+
conn = get_conn()
|
| 51 |
+
cursor = conn.cursor()
|
| 52 |
+
cursor.execute("SELECT id, date, is_open FROM sessions WHERE class_id=? ORDER BY date DESC", (class_id,))
|
| 53 |
+
rows = cursor.fetchall()
|
| 54 |
+
conn.close()
|
| 55 |
+
return rows
|
| 56 |
+
|
| 57 |
+
def toggle_session(class_id, is_open):
|
| 58 |
+
from datetime import datetime
|
| 59 |
+
date = datetime.now().strftime("%Y-%m-%d %H:%M")
|
| 60 |
+
conn = get_conn()
|
| 61 |
+
cursor = conn.cursor()
|
| 62 |
+
cursor.execute("INSERT INTO sessions (class_id, date, is_open) VALUES (?, ?, ?)",
|
| 63 |
+
(class_id, date, int(is_open)))
|
| 64 |
+
conn.commit()
|
| 65 |
+
conn.close()
|
| 66 |
+
return f"Session {'opened' if is_open else 'closed'} for {class_id} on {date}"
|
| 67 |
+
|
| 68 |
+
def add_student(name, email, password, class_id):
|
| 69 |
+
conn = get_conn()
|
| 70 |
+
cursor = conn.cursor()
|
| 71 |
+
try:
|
| 72 |
+
cursor.execute("INSERT INTO users (name, email, password, role, class_id) VALUES (?, ?, ?, 'student', ?)",
|
| 73 |
+
(name, email, password, class_id))
|
| 74 |
+
conn.commit()
|
| 75 |
+
except sqlite3.IntegrityError:
|
| 76 |
+
return "Email already exists."
|
| 77 |
+
finally:
|
| 78 |
+
conn.close()
|
| 79 |
+
return "Student added successfully."
|