Update interview_history.py
Browse files- interview_history.py +24 -36
interview_history.py
CHANGED
|
@@ -1,32 +1,19 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
import firebase_admin
|
| 4 |
-
from firebase_admin import
|
| 5 |
-
import json
|
| 6 |
import datetime
|
|
|
|
| 7 |
|
| 8 |
-
# ---
|
| 9 |
-
#
|
| 10 |
-
# It's generally good practice to initialize once in your main app entry point.
|
| 11 |
-
# If auth.py already initializes it, you can remove the initialization block below.
|
| 12 |
-
# Otherwise, uncomment and adjust the path.
|
| 13 |
-
if not firebase_admin._apps:
|
| 14 |
-
try:
|
| 15 |
-
# Make sure the path to your service account key is correct
|
| 16 |
-
cred = credentials.Certificate("prepgenie-64134-firebase-adminsdk-fbsvc-3370ac4ab9.json") # Update path if needed
|
| 17 |
-
firebase_admin.initialize_app(cred)
|
| 18 |
-
print("Firebase (for history) initialized successfully.")
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print(f"Error initializing Firebase for history: {e}")
|
| 21 |
-
# Handle error appropriately
|
| 22 |
-
|
| 23 |
-
# Get a Firestore client instance
|
| 24 |
try:
|
| 25 |
db = firestore.client()
|
| 26 |
-
print("Firestore client
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
print(f"Error
|
| 29 |
db = None
|
|
|
|
| 30 |
|
| 31 |
def save_interview_history(user_id, interview_data):
|
| 32 |
"""
|
|
@@ -43,13 +30,18 @@ def save_interview_history(user_id, interview_data):
|
|
| 43 |
"questions": list,
|
| 44 |
"answers": list,
|
| 45 |
"feedback": list,
|
| 46 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
| 49 |
Returns:
|
| 50 |
bool: True if successful, False otherwise.
|
| 51 |
"""
|
| 52 |
-
if not db:
|
| 53 |
print("Firestore client not available. Cannot save history.")
|
| 54 |
return False
|
| 55 |
if not user_id:
|
|
@@ -59,17 +51,13 @@ def save_interview_history(user_id, interview_data):
|
|
| 59 |
try:
|
| 60 |
# Reference to the user's document in the 'users' collection
|
| 61 |
user_ref = db.collection('users').document(user_id)
|
| 62 |
-
|
| 63 |
-
# Ensure the user document exists (optional, Firestore can create it)
|
| 64 |
-
# user_ref.set({}, merge=True) # Create empty doc if it doesn't exist
|
| 65 |
-
|
| 66 |
# Add the interview data to a subcollection 'interview_history'
|
| 67 |
history_ref = user_ref.collection('interview_history')
|
| 68 |
-
|
| 69 |
# Add the data with an auto-generated ID
|
| 70 |
-
# interview_data should already contain a timestamp
|
| 71 |
history_ref.add(interview_data)
|
| 72 |
-
|
| 73 |
print(f"Interview history saved for user {user_id}")
|
| 74 |
return True
|
| 75 |
except Exception as e:
|
|
@@ -88,7 +76,7 @@ def load_interview_history(user_id, limit=5):
|
|
| 88 |
list: A list of dictionaries, each representing an interview record.
|
| 89 |
Returns an empty list if no history is found or on error.
|
| 90 |
"""
|
| 91 |
-
if not db:
|
| 92 |
print("Firestore client not available. Cannot load history.")
|
| 93 |
return []
|
| 94 |
if not user_id:
|
|
@@ -98,10 +86,10 @@ def load_interview_history(user_id, limit=5):
|
|
| 98 |
try:
|
| 99 |
# Reference to the user's interview history subcollection
|
| 100 |
history_ref = db.collection('users').document(user_id).collection('interview_history')
|
| 101 |
-
|
| 102 |
# Query the history, ordered by timestamp descending (most recent first), limited
|
| 103 |
docs = history_ref.order_by('timestamp', direction=firestore.Query.DESCENDING).limit(limit).stream()
|
| 104 |
-
|
| 105 |
history_list = []
|
| 106 |
for doc in docs:
|
| 107 |
# Convert Firestore document to dictionary
|
|
@@ -109,7 +97,7 @@ def load_interview_history(user_id, limit=5):
|
|
| 109 |
# Add the document ID if needed (optional)
|
| 110 |
# interview_record['doc_id'] = doc.id
|
| 111 |
history_list.append(interview_record)
|
| 112 |
-
|
| 113 |
print(f"Loaded {len(history_list)} interview records for user {user_id}")
|
| 114 |
return history_list
|
| 115 |
except Exception as e:
|
|
@@ -126,7 +114,7 @@ def load_interview_history(user_id, limit=5):
|
|
| 126 |
# "questions": ["Question 1?", "Question 2?"],
|
| 127 |
# "answers": ["Answer 1", "Answer 2"],
|
| 128 |
# "feedback": ["Feedback 1", "Feedback 2"],
|
| 129 |
-
#
|
| 130 |
# }
|
| 131 |
# save_interview_history(user_uid, interview_summary_data)
|
| 132 |
|
|
|
|
| 1 |
+
# PrepGenie/interview_history.py
|
|
|
|
| 2 |
import firebase_admin
|
| 3 |
+
from firebase_admin import firestore
|
|
|
|
| 4 |
import datetime
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
+
# --- Firestore Client ---
|
| 8 |
+
# Assumes Firebase Admin is initialized in app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
db = firestore.client()
|
| 11 |
+
print("Firestore client for history initialized successfully.")
|
| 12 |
+
FIRESTORE_AVAILABLE = True
|
| 13 |
except Exception as e:
|
| 14 |
+
print(f"Error initializing Firestore client for history: {e}")
|
| 15 |
db = None
|
| 16 |
+
FIRESTORE_AVAILABLE = False
|
| 17 |
|
| 18 |
def save_interview_history(user_id, interview_data):
|
| 19 |
"""
|
|
|
|
| 30 |
"questions": list,
|
| 31 |
"answers": list,
|
| 32 |
"feedback": list,
|
| 33 |
+
"interactions": dict,
|
| 34 |
+
"metrics_list": list,
|
| 35 |
+
"final_metrics": dict,
|
| 36 |
+
"average_rating": float,
|
| 37 |
+
"evaluation_report": str,
|
| 38 |
+
# ... other relevant data ...
|
| 39 |
}
|
| 40 |
|
| 41 |
Returns:
|
| 42 |
bool: True if successful, False otherwise.
|
| 43 |
"""
|
| 44 |
+
if not FIRESTORE_AVAILABLE or not db:
|
| 45 |
print("Firestore client not available. Cannot save history.")
|
| 46 |
return False
|
| 47 |
if not user_id:
|
|
|
|
| 51 |
try:
|
| 52 |
# Reference to the user's document in the 'users' collection
|
| 53 |
user_ref = db.collection('users').document(user_id)
|
| 54 |
+
|
|
|
|
|
|
|
|
|
|
| 55 |
# Add the interview data to a subcollection 'interview_history'
|
| 56 |
history_ref = user_ref.collection('interview_history')
|
| 57 |
+
|
| 58 |
# Add the data with an auto-generated ID
|
|
|
|
| 59 |
history_ref.add(interview_data)
|
| 60 |
+
|
| 61 |
print(f"Interview history saved for user {user_id}")
|
| 62 |
return True
|
| 63 |
except Exception as e:
|
|
|
|
| 76 |
list: A list of dictionaries, each representing an interview record.
|
| 77 |
Returns an empty list if no history is found or on error.
|
| 78 |
"""
|
| 79 |
+
if not FIRESTORE_AVAILABLE or not db:
|
| 80 |
print("Firestore client not available. Cannot load history.")
|
| 81 |
return []
|
| 82 |
if not user_id:
|
|
|
|
| 86 |
try:
|
| 87 |
# Reference to the user's interview history subcollection
|
| 88 |
history_ref = db.collection('users').document(user_id).collection('interview_history')
|
| 89 |
+
|
| 90 |
# Query the history, ordered by timestamp descending (most recent first), limited
|
| 91 |
docs = history_ref.order_by('timestamp', direction=firestore.Query.DESCENDING).limit(limit).stream()
|
| 92 |
+
|
| 93 |
history_list = []
|
| 94 |
for doc in docs:
|
| 95 |
# Convert Firestore document to dictionary
|
|
|
|
| 97 |
# Add the document ID if needed (optional)
|
| 98 |
# interview_record['doc_id'] = doc.id
|
| 99 |
history_list.append(interview_record)
|
| 100 |
+
|
| 101 |
print(f"Loaded {len(history_list)} interview records for user {user_id}")
|
| 102 |
return history_list
|
| 103 |
except Exception as e:
|
|
|
|
| 114 |
# "questions": ["Question 1?", "Question 2?"],
|
| 115 |
# "answers": ["Answer 1", "Answer 2"],
|
| 116 |
# "feedback": ["Feedback 1", "Feedback 2"],
|
| 117 |
+
# # ... other fields ...
|
| 118 |
# }
|
| 119 |
# save_interview_history(user_uid, interview_summary_data)
|
| 120 |
|