Add photo retrieval endpoint and improve photo upload response

This commit is contained in:
Yaro Kasear 2025-07-11 09:44:31 -05:00
parent 3e0faae851
commit ca3417c269
2 changed files with 19 additions and 1 deletions

View file

@ -3,6 +3,7 @@ from flask_sqlalchemy import SQLAlchemy
import logging
import os
db = SQLAlchemy()
logger = logging.getLogger('sqlalchemy.engine')
@ -25,6 +26,8 @@ def create_app():
# db.create_all()
from .routes import main
from .routes.photos import photo_bp
app.register_blueprint(main)
app.register_blueprint(photo_bp)
return app

View file

@ -1,7 +1,7 @@
import os
import datetime
from flask import Blueprint, current_app, request, jsonify
from flask import Blueprint, current_app, request, jsonify, send_from_directory
from .helpers import generate_hashed_filename, get_photo_attachable_class_by_name
from .. import db
@ -58,3 +58,18 @@ def upload_photo():
db.session.commit()
return jsonify({"success": True, "id": photo.id}), 201
@photo_bp.route("/api/photos/<int:photo_id>", methods=["GET"])
def get_photo(photo_id: int):
photo = db.session.get(Photo, photo_id)
if not photo:
return jsonify({"success": False, "error": f"No photo found with ID {photo_id}"}), 404
return jsonify({
"success": True,
"id": photo.id,
"filename": photo.filename,
"caption": photo.caption,
"timestamp": photo.timestamp.isoformat() if photo.timestamp else None,
"url": f"/static/{photo.filename}"
})