Alibrown commited on
Commit
5724eea
·
verified ·
1 Parent(s): c9d4ca5

Update fundaments/user_handler.py

Browse files
Files changed (1) hide show
  1. fundaments/user_handler.py +33 -5
fundaments/user_handler.py CHANGED
@@ -2,7 +2,7 @@
2
  # Copyright 2008-2025 - Volkan Kücükbudak
3
  # Apache License V. 2
4
  # Repo: https://github.com/VolkanSah/PyFundaments
5
- # user_handler.py
6
  # A Python module for handling user authentication and session management.
7
 
8
  import sqlite3
@@ -11,15 +11,40 @@ from datetime import datetime, timedelta
11
  from passlib.hash import pbkdf2_sha256
12
  import os
13
 
 
14
  class Database:
15
  """
16
- A simple placeholder class to simulate a database connection.
17
- In a real application, you would use a proper ORM like SQLAlchemy
18
- or a specific database driver.
19
  """
20
  def __init__(self, db_name="cms_database.db"):
21
- self.conn = sqlite3.connect(db_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  self.cursor = self.conn.cursor()
 
 
 
23
 
24
  def execute(self, query, params=None):
25
  if params is None:
@@ -339,4 +364,7 @@ if __name__ == "__main__":
339
  db.close()
340
 
341
  # Optional: Clean up the database file after the run
 
342
  # os.remove("cms_database.db")
 
 
 
2
  # Copyright 2008-2025 - Volkan Kücükbudak
3
  # Apache License V. 2
4
  # Repo: https://github.com/VolkanSah/PyFundaments
5
+ # root/fundaments/user_handler.py
6
  # A Python module for handling user authentication and session management.
7
 
8
  import sqlite3
 
11
  from passlib.hash import pbkdf2_sha256
12
  import os
13
 
14
+
15
  class Database:
16
  """
17
+ Handles the SQLite database connection and initialization.
18
+ Supports dynamic path selection via environment variables with a
19
+ fallback to the local application directory.
20
  """
21
  def __init__(self, db_name="cms_database.db"):
22
+ # 1. Attempt to load the database path from an environment variable
23
+ # This allows for flexible configuration in production/Docker environments
24
+ env_path = os.getenv("SQLITE_PATH")
25
+
26
+ if env_path:
27
+ # Use the absolute path provided by the environment variable
28
+ full_db_path = os.path.abspath(env_path)
29
+ else:
30
+ # Fallback logic: Locate the 'app' directory relative to this script
31
+ # Expected structure: root/fun/user_handler.py -> root/app/
32
+ base_path = os.path.dirname(os.path.abspath(__file__))
33
+ app_dir = os.path.join(base_path, "..", "app")
34
+ full_db_path = os.path.join(app_dir, db_name)
35
+
36
+ # 2. Ensure the target directory exists before attempting to connect
37
+ # SQLite can create the file, but not the parent folders.
38
+ db_dir = os.path.dirname(full_db_path)
39
+ if db_dir and not os.path.exists(db_dir):
40
+ os.makedirs(db_dir)
41
+
42
+ # Initialize the connection and cursor
43
+ self.conn = sqlite3.connect(full_db_path)
44
  self.cursor = self.conn.cursor()
45
+
46
+ # Log the active database path for debugging purposes
47
+ print(f"Database connected to: {full_db_path}")
48
 
49
  def execute(self, query, params=None):
50
  if params is None:
 
364
  db.close()
365
 
366
  # Optional: Clean up the database file after the run
367
+ # OLD
368
  # os.remove("cms_database.db")
369
+ # NEW : Clean up the database file after the run:
370
+ # os.remove(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "app", "cms_database.db"))