app.py - add first photos api route
This commit is contained in:
1 parent
269366b7b6
commit
e7fad500f9
3 files changed
+98
-35
No files matched your search
+27
-1
@@ -1,5 +1,6 @@
|
||||
from flask import Flask, request, jsonify
|
||||
from database import *
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@@ -151,7 +152,6 @@ def settings_collection():
|
||||
print(result)
|
||||
return jsonify(result), 201
|
||||
|
||||
|
||||
@app.route("/settings/<string:name>", methods=["GET", "PUT"])
|
||||
def setting_detail(name: str):
|
||||
"""Retrieve or update a single setting.
|
||||
@@ -177,6 +177,32 @@ def setting_detail(name: str):
|
||||
print(result)
|
||||
return jsonify(result)
|
||||
|
||||
# ----------------------------- Photos -----------------------------
|
||||
|
||||
@app.route('/api/providers/<provider_id>/photos', methods=['POST'])
|
||||
def upload_photos(provider_id):
|
||||
if 'photos' not in request.files:
|
||||
return jsonify({'error': 'No files uploaded'}), 400
|
||||
|
||||
# Secure filename + save to persistent storage
|
||||
upload_folder = f'{os.path.dirname(__file__)}/photos'
|
||||
os.makedirs(upload_folder, exist_ok=True)
|
||||
|
||||
photo_paths = []
|
||||
for file in request.files.getlist('photos'):
|
||||
if file.filename == '':
|
||||
continue
|
||||
|
||||
filename = secure_filename(file.filename)
|
||||
file.save(os.path.join(upload_folder, filename))
|
||||
photo_paths.append(f"/uploads/{filename}")
|
||||
|
||||
# Store paths in DB (e.g., PostgreSQL JSON field)
|
||||
db.execute("UPDATE providers SET photos = %s WHERE id = %s",
|
||||
(json.dumps(photo_paths), provider_id))
|
||||
|
||||
return jsonify({'success': True, 'paths': photo_paths}), 200
|
||||
|
||||
# Run the app if this file is executed directly
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=False)
|
||||
Reference in new issue
Block a user