| import sqlite3 |
| import os |
| import json |
| from datetime import datetime |
| |
| |
|
|
| |
| |
| ROOT_DIR = os.environ.get('WORKSPACE_ROOT', '.') |
| PERSONNEL_FOLDER = os.path.join(ROOT_DIR, 'Database/personnel_data') |
| DB_PATH = os.path.join(PERSONNEL_FOLDER, 'personnelDetails.db') |
|
|
| |
| os.makedirs(PERSONNEL_FOLDER, exist_ok=True) |
|
|
| |
| def init_db(): |
| """Creates the SQL table if it doesn't exist.""" |
| conn = sqlite3.connect(DB_PATH) |
| cursor = conn.cursor() |
| |
| |
| cursor.execute(''' |
| CREATE TABLE IF NOT EXISTS users ( |
| email TEXT PRIMARY KEY, |
| name TEXT, |
| location TEXT, |
| github_description TEXT, |
| languages TEXT, |
| cv_file_path TEXT, |
| access_token TEXT, |
| refresh_token TEXT, |
| updated_at TEXT, |
| is_registered BOOLEAN |
| ) |
| ''') |
| conn.commit() |
| conn.close() |
|
|
| |
| init_db() |
|
|
| |
| def check_user_exists(email: str) -> bool: |
| """ |
| Checks if we already have this user's information in our database. |
| Returns True if they exist, False otherwise. |
| """ |
| conn = sqlite3.connect(DB_PATH) |
| cursor = conn.cursor() |
| |
| cursor.execute("SELECT is_registered FROM users WHERE email = ?", (email,)) |
| result = cursor.fetchone() |
| conn.close() |
| |
| |
| if result and result[0] == 1: |
| return True |
| return False |
|
|
| |
| def save_user_profile( |
| email: str, |
| name: str, |
| location: str, |
| github_description: str, |
| languages: list, |
| cv_file_bytes: bytes, |
| cv_filename: str, |
| access_token: str, |
| refresh_token: str |
| ): |
| """ |
| Saves or updates the user profile and their Google tokens in the SQL database. |
| """ |
| |
| conn = sqlite3.connect(DB_PATH) |
| cursor = conn.cursor() |
| cursor.execute("SELECT cv_file_path FROM users WHERE email = ?", (email,)) |
| row = cursor.fetchone() |
| old_cv_path = row[0] if row else None |
| conn.close() |
|
|
| |
| cv_file_path = None |
| if cv_file_bytes and cv_filename: |
| |
| safe_filename = f"{email.replace('@', '_at_')}_{cv_filename}" |
| cv_file_path = os.path.join(PERSONNEL_FOLDER, safe_filename) |
| |
| |
| if old_cv_path and old_cv_path != cv_file_path and os.path.exists(old_cv_path): |
| try: |
| os.remove(old_cv_path) |
| print(f"ποΈ Deleted old CV: {old_cv_path}") |
| except Exception as e: |
| print(f"β Failed to delete old CV: {e}") |
| |
| with open(cv_file_path, "wb") as f: |
| f.write(cv_file_bytes) |
| |
| |
| |
| languages_str = json.dumps(languages) |
| updated_at = datetime.utcnow().isoformat() |
| is_registered = True |
| |
| |
| |
| conn = sqlite3.connect(DB_PATH) |
| cursor = conn.cursor() |
| |
| cursor.execute(''' |
| INSERT INTO users ( |
| email, name, location, github_description, languages, |
| cv_file_path, access_token, refresh_token, updated_at, is_registered |
| ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| ON CONFLICT(email) DO UPDATE SET |
| name=excluded.name, |
| location=excluded.location, |
| github_description=excluded.github_description, |
| languages=excluded.languages, |
| cv_file_path=COALESCE(excluded.cv_file_path, users.cv_file_path), |
| access_token=excluded.access_token, |
| refresh_token=excluded.refresh_token, |
| updated_at=excluded.updated_at, |
| is_registered=excluded.is_registered |
| ''', ( |
| email, name, location, github_description, languages_str, |
| cv_file_path, access_token, refresh_token, updated_at, is_registered |
| )) |
| |
| conn.commit() |
| conn.close() |
| |
| print(f"β
User {email} successfully saved to SQL Database!") |
| |
| |
| |
| |
| if cv_file_path: |
| try: |
| import threading |
| from ResumeProcessor import process_resume |
| print(f"Processing resume for {email} via LLM...", flush=True) |
| |
| def run_llm_task(): |
| try: |
| process_resume(cv_file_path, email) |
| except Exception as e: |
| print(f"[WARNING] Background ResumeProcessor failed: {e}") |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| t = threading.Thread(target=run_llm_task) |
| t.daemon = False |
| t.start() |
| except ImportError as ie: |
| print(f"[WARNING] ResumeProcessor not available (missing dependency?): {ie}") |
| except Exception as e: |
| print(f"[WARNING] ResumeProcessor failed to start: {e}") |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |