Spaces:
Running
Running
Update db/helpers.py
Browse files- db/helpers.py +20 -26
db/helpers.py
CHANGED
|
@@ -6,31 +6,31 @@ from sqlalchemy import func
|
|
| 6 |
from db.models import User, ContentHistory, Feedback
|
| 7 |
from db.connection import get_db, SessionLocal
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# ------------------------------------
|
| 12 |
-
def create_user(user_id):
|
| 13 |
-
"""Create a new user in the database"""
|
| 14 |
with next(get_db()) as db:
|
| 15 |
try:
|
| 16 |
-
user = User(id=user_id)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return True
|
| 21 |
except SQLAlchemyError as e:
|
| 22 |
db.rollback()
|
| 23 |
-
|
| 24 |
return False
|
| 25 |
|
| 26 |
-
def ensure_user_exists(user_id):
|
| 27 |
-
"""Ensure user exists in database, create if needed"""
|
| 28 |
-
with next(get_db()) as db:
|
| 29 |
-
user = db.query(User).filter_by(id=user_id).first()
|
| 30 |
-
if not user:
|
| 31 |
-
return create_user(user_id)
|
| 32 |
-
return True
|
| 33 |
-
|
| 34 |
# ------------------------------------
|
| 35 |
# β
Content History
|
| 36 |
# ------------------------------------
|
|
@@ -38,9 +38,6 @@ def get_user_history(user_id):
|
|
| 38 |
"""Get user's content history from database"""
|
| 39 |
with next(get_db()) as db:
|
| 40 |
try:
|
| 41 |
-
# Ensure user exists first
|
| 42 |
-
ensure_user_exists(user_id)
|
| 43 |
-
|
| 44 |
history = db.query(ContentHistory).filter_by(user_id=user_id).order_by(ContentHistory.created_at.desc()).all()
|
| 45 |
print(f"β
Loaded {len(history)} history entries for user {user_id}")
|
| 46 |
return history
|
|
@@ -49,15 +46,12 @@ def get_user_history(user_id):
|
|
| 49 |
return []
|
| 50 |
|
| 51 |
def save_content_to_history(data):
|
| 52 |
-
"""Save content to history"""
|
| 53 |
with next(get_db()) as db:
|
| 54 |
try:
|
| 55 |
-
# Ensure user exists
|
| 56 |
-
ensure_user_exists(data["user_id"])
|
| 57 |
-
|
| 58 |
content = ContentHistory(
|
| 59 |
id=uuid.uuid4(),
|
| 60 |
-
user_id=data["user_id"],
|
| 61 |
prompt=data["prompt"],
|
| 62 |
output=data["output"],
|
| 63 |
user_type=data["user_type"],
|
|
|
|
| 6 |
from db.models import User, ContentHistory, Feedback
|
| 7 |
from db.connection import get_db, SessionLocal
|
| 8 |
|
| 9 |
+
def ensure_user_exists(user_id):
|
| 10 |
+
"""Ensure user exists in database - compatibility function for existing code"""
|
|
|
|
|
|
|
|
|
|
| 11 |
with next(get_db()) as db:
|
| 12 |
try:
|
| 13 |
+
user = db.query(User).filter_by(id=user_id).first()
|
| 14 |
+
if not user:
|
| 15 |
+
# Create a minimal user record if it doesn't exist
|
| 16 |
+
# This maintains compatibility with existing code that expects this function
|
| 17 |
+
user = User()
|
| 18 |
+
user.id = uuid.UUID(user_id)
|
| 19 |
+
user.fullname = "Legacy User"
|
| 20 |
+
user.username = f"user_{user_id[:8]}"
|
| 21 |
+
user.password_hash = ""
|
| 22 |
+
user.salt = ""
|
| 23 |
+
|
| 24 |
+
db.add(user)
|
| 25 |
+
db.commit()
|
| 26 |
+
print(f"β
Created legacy user record: {user_id}")
|
| 27 |
+
return True
|
| 28 |
return True
|
| 29 |
except SQLAlchemyError as e:
|
| 30 |
db.rollback()
|
| 31 |
+
print(f"β Error ensuring user exists: {e}")
|
| 32 |
return False
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# ------------------------------------
|
| 35 |
# β
Content History
|
| 36 |
# ------------------------------------
|
|
|
|
| 38 |
"""Get user's content history from database"""
|
| 39 |
with next(get_db()) as db:
|
| 40 |
try:
|
|
|
|
|
|
|
|
|
|
| 41 |
history = db.query(ContentHistory).filter_by(user_id=user_id).order_by(ContentHistory.created_at.desc()).all()
|
| 42 |
print(f"β
Loaded {len(history)} history entries for user {user_id}")
|
| 43 |
return history
|
|
|
|
| 46 |
return []
|
| 47 |
|
| 48 |
def save_content_to_history(data):
|
| 49 |
+
"""Save content to history - user_id comes from authenticated session"""
|
| 50 |
with next(get_db()) as db:
|
| 51 |
try:
|
|
|
|
|
|
|
|
|
|
| 52 |
content = ContentHistory(
|
| 53 |
id=uuid.uuid4(),
|
| 54 |
+
user_id=data["user_id"], # From authenticated session
|
| 55 |
prompt=data["prompt"],
|
| 56 |
output=data["output"],
|
| 57 |
user_type=data["user_type"],
|