Add photo retrieval endpoint and improve photo upload response
This commit is contained in:
parent
3e0faae851
commit
ca3417c269
2 changed files with 19 additions and 1 deletions
|
@ -3,6 +3,7 @@ from flask_sqlalchemy import SQLAlchemy
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
|
||||||
logger = logging.getLogger('sqlalchemy.engine')
|
logger = logging.getLogger('sqlalchemy.engine')
|
||||||
|
@ -25,6 +26,8 @@ def create_app():
|
||||||
# db.create_all()
|
# db.create_all()
|
||||||
|
|
||||||
from .routes import main
|
from .routes import main
|
||||||
|
from .routes.photos import photo_bp
|
||||||
app.register_blueprint(main)
|
app.register_blueprint(main)
|
||||||
|
app.register_blueprint(photo_bp)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import datetime
|
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 .helpers import generate_hashed_filename, get_photo_attachable_class_by_name
|
||||||
from .. import db
|
from .. import db
|
||||||
|
@ -58,3 +58,18 @@ def upload_photo():
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return jsonify({"success": True, "id": photo.id}), 201
|
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}"
|
||||||
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue