lanna_lalala;- commited on
Commit
ea04e9e
·
1 Parent(s): d836b37
Files changed (1) hide show
  1. utils/db.py +51 -14
utils/db.py CHANGED
@@ -1,22 +1,62 @@
1
  # utils/db.py (top of file)
 
2
  import os
3
  import json
4
  import certifi
5
- import mysql.connector
6
- from mysql.connector import Error
7
  from contextlib import contextmanager
8
- from datetime import date, timedelta
9
 
 
 
10
 
 
11
  DISABLE_DB = os.getenv("DISABLE_DB", "1") == "1"
12
 
13
- try:
14
- if DISABLE_DB:
15
- raise ImportError("DB disabled in frontend Space")
16
- import mysql.connector # provided by mysql-connector-python
17
- except Exception:
18
- # fall back; only error if a DB function is actually called
19
- mysql = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  # password hashing
@@ -172,10 +212,7 @@ def create_class(teacher_id:int, name:str):
172
  return {"class_id": cid, "code": code}
173
  raise RuntimeError("Could not generate unique class code")
174
 
175
- def list_classes_by_teacher(teacher_id:int):
176
- with cursor() as cur:
177
- cur.execute("SELECT * FROM v_class_stats WHERE teacher_id=%s", (teacher_id,))
178
- return cur.fetchall()
179
 
180
  def join_class_by_code(student_id:int, code:str):
181
  with cursor() as cur:
 
1
  # utils/db.py (top of file)
2
+ # utils/db.py (top of file)
3
  import os
4
  import json
5
  import certifi
 
 
6
  from contextlib import contextmanager
7
+ from datetime import date, timedelta
8
 
9
+ # password hashing
10
+ import bcrypt
11
 
12
+ # --- Feature flag: DB off by default in frontend Space ---
13
  DISABLE_DB = os.getenv("DISABLE_DB", "1") == "1"
14
 
15
+ # Import mysql connector only when DB is enabled
16
+ MYSQL_AVAILABLE = False
17
+ if not DISABLE_DB:
18
+ try:
19
+ import mysql.connector # provided by mysql-connector-python
20
+ from mysql.connector import Error # noqa: F401
21
+ MYSQL_AVAILABLE = True
22
+ except Exception:
23
+ MYSQL_AVAILABLE = False # will raise a friendly error if used
24
+
25
+ def _db_disabled_error():
26
+ raise RuntimeError(
27
+ "Database access is disabled in this frontend (DISABLE_DB=1). "
28
+ "Route calls through your backend Space instead."
29
+ )
30
+
31
+ def get_db_connection():
32
+ if DISABLE_DB or not MYSQL_AVAILABLE:
33
+ _db_disabled_error()
34
+ ssl_enabled = os.getenv("TIDB_ENABLE_SSL", "false").lower() == "true"
35
+ ssl_ca = certifi.where() if ssl_enabled else None
36
+ return mysql.connector.connect(
37
+ host=os.getenv("TIDB_HOST"),
38
+ port=int(os.getenv("TIDB_PORT", 4000)),
39
+ user=os.getenv("TIDB_USER"),
40
+ password=os.getenv("TIDB_PASSWORD"),
41
+ database=os.getenv("TIDB_DATABASE", "agenticfinance"),
42
+ ssl_ca=ssl_ca,
43
+ ssl_verify_cert=ssl_enabled,
44
+ autocommit=True,
45
+ )
46
+
47
+ @contextmanager
48
+ def cursor(dict_rows=True):
49
+ if DISABLE_DB or not MYSQL_AVAILABLE:
50
+ _db_disabled_error()
51
+ conn = get_db_connection()
52
+ try:
53
+ cur = conn.cursor(dictionary=dict_rows)
54
+ yield cur
55
+ conn.commit()
56
+ finally:
57
+ cur.close()
58
+ conn.close()
59
+
60
 
61
 
62
  # password hashing
 
212
  return {"class_id": cid, "code": code}
213
  raise RuntimeError("Could not generate unique class code")
214
 
215
+
 
 
 
216
 
217
  def join_class_by_code(student_id:int, code:str):
218
  with cursor() as cur: