This commit is contained in:
31
app/views.py
31
app/views.py
@@ -1,8 +1,8 @@
|
||||
import logging
|
||||
"""Views for the Flask app."""
|
||||
|
||||
from flask import Blueprint, g, jsonify, request
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from flask import Blueprint, current_app, g, jsonify, request
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.database import get_db_session
|
||||
from app.models import SavedQueue, WatchHistory
|
||||
@@ -40,7 +40,7 @@ def save_queue():
|
||||
return jsonify(message="Data added to saved queue"), 200
|
||||
except SQLAlchemyError as e:
|
||||
db_session.rollback()
|
||||
logging.error(f"Failed to insert data: {e}")
|
||||
current_app.logger.error(f"Failed to insert data: {e}")
|
||||
return jsonify(message="Database error"), 500
|
||||
|
||||
|
||||
@@ -49,10 +49,10 @@ def load_queue():
|
||||
"""
|
||||
Retrieves the saved queue of video URLs.
|
||||
"""
|
||||
logging.debug("Loading saved queue")
|
||||
current_app.logger.debug("Loading saved queue")
|
||||
db_session = g.db_session
|
||||
urls = [entry.video_url for entry in db_session.query(SavedQueue).all()]
|
||||
logging.debug(f"Loaded {len(urls)} URLs from the saved queue")
|
||||
current_app.logger.debug(f"Loaded {len(urls)} URLs from the saved queue")
|
||||
return jsonify(urls), 200
|
||||
|
||||
|
||||
@@ -62,8 +62,8 @@ def add_video():
|
||||
if not all(
|
||||
k in data for k in ["video_url", "video_name", "channel_url", "channel_name"]
|
||||
):
|
||||
logging.error("Missing required fields")
|
||||
logging.error(
|
||||
current_app.logger.error("Missing required fields")
|
||||
current_app.logger.error(
|
||||
"Required fields: video_url, video_name, channel_url, channel_name"
|
||||
)
|
||||
return jsonify(message="Missing required fields"), 400
|
||||
@@ -83,18 +83,15 @@ def add_video():
|
||||
db_session = g.db_session
|
||||
|
||||
try:
|
||||
logging.debug("Adding video to watch history")
|
||||
current_app.logger.debug("Adding video to watch history")
|
||||
db_session.add(new_entry)
|
||||
db_session.commit()
|
||||
logging.debug("Video added to watch history")
|
||||
logging.debug(f"URL: {data['video_url']}")
|
||||
logging.debug(f"Video name: {data['video_name']}")
|
||||
logging.debug(f"Channel URL: {data['channel_url']}")
|
||||
logging.debug(f"Channel name: {data['channel_name']}")
|
||||
current_app.logger.debug("Video added to watch history")
|
||||
current_app.logger.debug("Data: %s", data)
|
||||
return jsonify(message="Video added"), 200
|
||||
except SQLAlchemyError as e:
|
||||
db_session.rollback()
|
||||
logging.error(f"Database error: {e}")
|
||||
current_app.logger.error(f"Database error: {e}")
|
||||
return jsonify(message="Failed to add video"), 500
|
||||
|
||||
|
||||
@@ -139,7 +136,7 @@ def migrate_watch_history():
|
||||
if col_name not in existing_columns:
|
||||
engine.execute(sql)
|
||||
columns_added.append(col_name)
|
||||
logging.info(f"Added column: {col_name}")
|
||||
current_app.logger.info(f"Added column: {col_name}")
|
||||
|
||||
# Now backfill with default values
|
||||
entries = db_session.query(WatchHistory).all()
|
||||
@@ -183,5 +180,5 @@ def migrate_watch_history():
|
||||
|
||||
except SQLAlchemyError as e:
|
||||
db_session.rollback()
|
||||
logging.error(f"Migration failed: {e}")
|
||||
current_app.logger.error(f"Migration failed: {e}")
|
||||
return jsonify(message=f"Failed to migrate watch history: {str(e)}"), 500
|
||||
|
||||
Reference in New Issue
Block a user