Rename "photo" to "image."
This commit is contained in:
parent
84db8592cb
commit
7d96839af8
11 changed files with 78 additions and 72 deletions
|
|
@ -5,7 +5,7 @@ from werkzeug.utils import secure_filename
|
|||
|
||||
from ..models import Inventory
|
||||
|
||||
from ..models.photo import PhotoAttachable
|
||||
from ..models.image import ImageAttachable
|
||||
|
||||
inventory_headers = {
|
||||
"Date Entered": lambda i: {"text": i.timestamp.strftime("%Y-%m-%d") if i.timestamp else None},
|
||||
|
|
@ -80,8 +80,8 @@ def generate_hashed_filename(file_storage, model_name: str) -> str:
|
|||
original_name = secure_filename(file_storage.filename)
|
||||
return f"{model_name}/{sha}_{original_name}"
|
||||
|
||||
def get_photo_attachable_class_by_name(name: str):
|
||||
for cls in PhotoAttachable.__subclasses__():
|
||||
def get_image_attachable_class_by_name(name: str):
|
||||
for cls in ImageAttachable.__subclasses__():
|
||||
if getattr(cls, '__tablename__', None) == name:
|
||||
return cls
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -3,18 +3,18 @@ import posixpath
|
|||
|
||||
from flask import Blueprint, current_app, request, jsonify
|
||||
|
||||
from .helpers import generate_hashed_filename, get_photo_attachable_class_by_name
|
||||
from .helpers import generate_hashed_filename, get_image_attachable_class_by_name
|
||||
from .. import db
|
||||
from ..models import Photo
|
||||
from ..models import Image
|
||||
|
||||
photo_bp = Blueprint("photo_api", __name__)
|
||||
image_bp = Blueprint("image_api", __name__)
|
||||
|
||||
def save_photo(file, model: str) -> str:
|
||||
def save_image(file, model: str) -> str:
|
||||
assert current_app.static_folder
|
||||
|
||||
hashed_name = generate_hashed_filename(file, model)
|
||||
rel_path = posixpath.join("uploads", "photos", hashed_name)
|
||||
abs_path = os.path.join(current_app.static_folder, "uploads", "photos", rel_path)
|
||||
rel_path = posixpath.join("uploads", "images", hashed_name)
|
||||
abs_path = os.path.join(current_app.static_folder, "uploads", "images", rel_path)
|
||||
|
||||
dir_path = os.path.dirname(abs_path)
|
||||
if not os.path.exists(dir_path):
|
||||
|
|
@ -29,8 +29,8 @@ def save_photo(file, model: str) -> str:
|
|||
file.save(abs_path)
|
||||
return rel_path
|
||||
|
||||
@photo_bp.route("/api/photos", methods=["POST"])
|
||||
def upload_photo():
|
||||
@image_bp.route("/api/images", methods=["POST"])
|
||||
def upload_image():
|
||||
file = request.files.get("file")
|
||||
model = request.form.get("model")
|
||||
model_id = request.form.get("model_id")
|
||||
|
|
@ -39,9 +39,9 @@ def upload_photo():
|
|||
if not file or not model or not model_id:
|
||||
return jsonify({"success": False, "error": "Missing file, model, or model_id"}), 400
|
||||
|
||||
ModelClass = get_photo_attachable_class_by_name(model)
|
||||
ModelClass = get_image_attachable_class_by_name(model)
|
||||
if not ModelClass:
|
||||
return jsonify({"success": False, "error": f"Model '{model}' does not support photo attachments."}), 400
|
||||
return jsonify({"success": False, "error": f"Model '{model}' does not support image attachments."}), 400
|
||||
|
||||
try:
|
||||
model_id = int(model_id)
|
||||
|
|
@ -49,34 +49,40 @@ def upload_photo():
|
|||
return jsonify({"success": False, "error": "model_id must be an integer"}), 400
|
||||
|
||||
# Save file
|
||||
rel_path = save_photo(file, model)
|
||||
rel_path = save_image(file, model)
|
||||
print(rel_path)
|
||||
|
||||
# Create Photo row
|
||||
photo = Photo(filename=rel_path, caption=caption)
|
||||
db.session.add(photo)
|
||||
# Create Image row
|
||||
image = Image(filename=rel_path, caption=caption)
|
||||
db.session.add(image)
|
||||
|
||||
# Attach photo to model
|
||||
# Attach image to model
|
||||
target = db.session.get(ModelClass, model_id)
|
||||
if not target:
|
||||
return jsonify({"success": False, "error": f"No {model} found with ID {model_id}"}), 404
|
||||
|
||||
target.attach_photo(photo)
|
||||
target.attach_image(image)
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({"success": True, "id": photo.id}), 201
|
||||
return jsonify({"success": True, "id": image.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
|
||||
@image_bp.route("/api/images/<int:image_id>", methods=["GET"])
|
||||
def get_image(image_id: int):
|
||||
image = db.session.get(Image, image_id)
|
||||
if not image:
|
||||
return jsonify({"success": False, "error": f"No image found with ID {image_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}"
|
||||
"id": image.id,
|
||||
"filename": image.filename,
|
||||
"caption": image.caption,
|
||||
"timestamp": image.timestamp.isoformat() if image.timestamp else None,
|
||||
"url": f"/static/{image.filename}"
|
||||
})
|
||||
|
||||
@image_bp.route("/api/images/<int:image_id>", methods=["DELETE"])
|
||||
def delete_image(image_id):
|
||||
image = db.session.get(Image, image_id)
|
||||
if not image:
|
||||
return jsonify({"success": False, "error": "Image not found"})
|
||||
Loading…
Add table
Add a link
Reference in a new issue