-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_utils.py
180 lines (143 loc) · 5.69 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# db_utils.py
import logging
from sqlalchemy.exc import SQLAlchemyError
from db_schema import SessionLocal, User, Resume, Application
import streamlit as st
from datetime import datetime
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def handle_db_operation(operation):
"""Decorator for database operations with comprehensive error handling"""
def wrapper(*args, **kwargs):
db = SessionLocal()
try:
# Log the operation details
logger.info(f"Attempting database operation: {operation.__name__}")
logger.info(f"Arguments: {args}, Keyword Arguments: {kwargs}")
result = operation(db, *args, **kwargs)
# Commit only if no exception occurred
db.commit()
logger.info(f"Operation {operation.__name__} completed successfully")
return result
except SQLAlchemyError as e:
# Rollback the transaction
db.rollback()
# Log detailed error information
logger.error(f"Database error in {operation.__name__}: {str(e)}")
logger.error(f"Full exception details:", exc_info=True)
# Display user-friendly error
st.error(f"A database error occurred: {str(e)}")
return None
except Exception as e:
# Catch any other unexpected errors
db.rollback()
logger.error(f"Unexpected error in {operation.__name__}: {str(e)}")
logger.error(f"Full exception details:", exc_info=True)
st.error(f"An unexpected error occurred: {str(e)}")
return None
finally:
# Always close the session
db.close()
return wrapper
@handle_db_operation
def get_or_create_user(db, email):
"""Get existing user or create new one with improved error handling"""
try:
# Attempt to find existing user
user = db.query(User).filter(User.email == email).first()
if not user:
# Create new user if not exists
user = User(email=email)
db.add(user)
# Update last login timestamp
user.last_login = datetime.utcnow()
return user
except Exception as e:
logger.error(f"Error in get_or_create_user: {str(e)}")
raise
@handle_db_operation
def save_resume(db, user_email:str, file_content, file_name):
"""Save or update user's resume with comprehensive validation"""
try:
# Validate inputs
if not file_content:
raise ValueError("File content cannot be empty")
# Deactivate existing active resumes for this user
existing_resumes = db.query(Resume).filter(
Resume.user_email == user_email,
Resume.is_active == True
).all()
for resume in existing_resumes:
resume.is_active = False
# Create new resume entry
new_resume = Resume(
user_email=user_email,
file_name=file_name,
content=file_content,
is_active=True
)
db.add(new_resume)
return new_resume
except Exception as e:
logger.error(f"Error saving resume: {str(e)}")
raise
@handle_db_operation
def get_active_resume(db, user_email:str):
"""Get user's current active resume"""
try:
# Explicitly refresh the session to ensure it's active
db.expire_on_commit = False
resume = db.query(Resume).filter(
Resume.user_email == user_email,
Resume.is_active == True
).first()
return resume
except Exception as e:
logger.error(f"Error retrieving active resume: {str(e)}")
raise
@handle_db_operation
def save_application(db, user_email, company_name, role, job_description,
cover_letter=None, networking_email=None, resume_review=None):
"""Save application details with validation"""
if not all([company_name, role, job_description]):
raise ValueError("Company name, role, and job description are required")
application = Application(
user_email=user_email,
company_name=company_name,
role=role,
job_description=job_description,
cover_letter=cover_letter,
networking_email=networking_email,
resume_review=resume_review,
status='Created'
)
db.add(application)
return application
@handle_db_operation
def get_user_applications(db, user_email):
"""Get all applications for a user with improved session handling"""
cache_key = f"user_applications_{user_email}"
# Check session state cache first
if cache_key in st.session_state:
return st.session_state[cache_key]
# Retrieve applications and explicitly load data before session closes
applications = db.query(Application).filter(
Application.user_email == user_email
).order_by(Application.created_at.desc()).all()
# Convert to a list of dictionaries to detach from session
application_dicts = [
{
'company_name': app.company_name,
'role': app.role,
'status': app.status,
'created_at': app.created_at,
'resume_review': app.resume_review,
'cover_letter': app.cover_letter,
'networking_email': app.networking_email
} for app in applications
]
# Store in session state
if application_dicts:
st.session_state[cache_key] = application_dicts
return application_dicts