Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -11,7 +11,10 @@ import google.generativeai as genai
|
|
| 11 |
import firebase_admin
|
| 12 |
from firebase_admin import credentials, db, storage, auth
|
| 13 |
import pandas as pd
|
| 14 |
-
import requests
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
app = Flask(__name__)
|
| 17 |
CORS(app)
|
|
@@ -217,6 +220,8 @@ def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes,
|
|
| 217 |
# Data Endpoints for Visualizations
|
| 218 |
# ========================================
|
| 219 |
|
|
|
|
|
|
|
| 220 |
@app.route('/api/user/spending-overview', methods=['GET'])
|
| 221 |
def get_spending_overview():
|
| 222 |
try:
|
|
@@ -390,7 +395,22 @@ def get_user_profile():
|
|
| 390 |
except Exception as e:
|
| 391 |
return jsonify({'error': str(e)}), 500
|
| 392 |
|
| 393 |
-
# Receipt endpoints
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
@app.route('/api/admin/receipt/<string:transaction_id>/view', methods=['GET'])
|
| 395 |
def view_receipt(transaction_id):
|
| 396 |
try:
|
|
@@ -404,22 +424,21 @@ def view_receipt(transaction_id):
|
|
| 404 |
if not image_url:
|
| 405 |
return jsonify({'error': 'No receipt image found for this transaction'}), 404
|
| 406 |
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
if r.status_code != 200:
|
| 410 |
return jsonify({'error': 'Unable to fetch image from storage'}), 500
|
| 411 |
|
| 412 |
return send_file(io.BytesIO(r.content), mimetype='image/jpeg')
|
| 413 |
except Exception as e:
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
transaction_data = transaction_ref.get()
|
| 417 |
-
if not transaction_data:
|
| 418 |
-
return jsonify({'error': 'Transaction not found'}), 404
|
| 419 |
-
|
| 420 |
-
image_url = transaction_data.get('image_url')
|
| 421 |
-
print(image_url)
|
| 422 |
-
return jsonify({'error': str(e)+f"image: {image_url}"}), 500
|
| 423 |
|
| 424 |
@app.route('/api/admin/receipt/<string:transaction_id>/download', methods=['GET'])
|
| 425 |
def download_receipt(transaction_id):
|
|
@@ -434,10 +453,17 @@ def download_receipt(transaction_id):
|
|
| 434 |
if not image_url:
|
| 435 |
return jsonify({'error': 'No receipt image found for this transaction'}), 404
|
| 436 |
|
| 437 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 438 |
if r.status_code != 200:
|
| 439 |
return jsonify({'error': 'Unable to fetch image from storage'}), 500
|
| 440 |
|
|
|
|
| 441 |
return send_file(
|
| 442 |
io.BytesIO(r.content),
|
| 443 |
mimetype='image/jpeg',
|
|
@@ -445,10 +471,11 @@ def download_receipt(transaction_id):
|
|
| 445 |
attachment_filename='receipt.jpg'
|
| 446 |
)
|
| 447 |
except Exception as e:
|
| 448 |
-
print(f"
|
| 449 |
return jsonify({'error': str(e)}), 500
|
| 450 |
|
| 451 |
|
|
|
|
| 452 |
# delete users
|
| 453 |
|
| 454 |
@app.route('/api/admin/users/<string:uid>', methods=['DELETE'])
|
|
|
|
| 11 |
import firebase_admin
|
| 12 |
from firebase_admin import credentials, db, storage, auth
|
| 13 |
import pandas as pd
|
| 14 |
+
import requests
|
| 15 |
+
from urllib.parse import urlparse, unquote
|
| 16 |
+
import datetime
|
| 17 |
+
|
| 18 |
|
| 19 |
app = Flask(__name__)
|
| 20 |
CORS(app)
|
|
|
|
| 220 |
# Data Endpoints for Visualizations
|
| 221 |
# ========================================
|
| 222 |
|
| 223 |
+
|
| 224 |
+
|
| 225 |
@app.route('/api/user/spending-overview', methods=['GET'])
|
| 226 |
def get_spending_overview():
|
| 227 |
try:
|
|
|
|
| 395 |
except Exception as e:
|
| 396 |
return jsonify({'error': str(e)}), 500
|
| 397 |
|
| 398 |
+
# Receipt media endpoints
|
| 399 |
+
def get_blob_from_image_url(image_url):
|
| 400 |
+
"""
|
| 401 |
+
Given an image_url from Firebase Storage (which typically looks like:
|
| 402 |
+
https://firebasestorage.googleapis.com/v0/b/<bucket>/o/receipts%2F<uid>%2F<filename>?alt=media&token=...),
|
| 403 |
+
extract and return the blob path (e.g., receipts/<uid>/<filename>).
|
| 404 |
+
"""
|
| 405 |
+
parsed = urlparse(image_url)
|
| 406 |
+
# Construct the expected prefix using your bucket's name.
|
| 407 |
+
prefix = f"/v0/b/{bucket.name}/o/"
|
| 408 |
+
if parsed.path.startswith(prefix):
|
| 409 |
+
encoded_blob_path = parsed.path[len(prefix):] # e.g., receipts%2Fuid%2Ffilename
|
| 410 |
+
blob_path = unquote(encoded_blob_path) # e.g., receipts/uid/filename
|
| 411 |
+
return blob_path
|
| 412 |
+
return None
|
| 413 |
+
|
| 414 |
@app.route('/api/admin/receipt/<string:transaction_id>/view', methods=['GET'])
|
| 415 |
def view_receipt(transaction_id):
|
| 416 |
try:
|
|
|
|
| 424 |
if not image_url:
|
| 425 |
return jsonify({'error': 'No receipt image found for this transaction'}), 404
|
| 426 |
|
| 427 |
+
blob_path = get_blob_from_image_url(image_url)
|
| 428 |
+
if not blob_path:
|
| 429 |
+
return jsonify({'error': 'Could not determine blob path from URL'}), 500
|
| 430 |
+
|
| 431 |
+
blob = bucket.blob(blob_path)
|
| 432 |
+
# Generate a signed URL valid for 10 minutes
|
| 433 |
+
signed_url = blob.generate_signed_url(expiration=datetime.timedelta(minutes=10))
|
| 434 |
+
r = requests.get(signed_url)
|
| 435 |
if r.status_code != 200:
|
| 436 |
return jsonify({'error': 'Unable to fetch image from storage'}), 500
|
| 437 |
|
| 438 |
return send_file(io.BytesIO(r.content), mimetype='image/jpeg')
|
| 439 |
except Exception as e:
|
| 440 |
+
print(f"View receipt error: {str(e)}")
|
| 441 |
+
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
@app.route('/api/admin/receipt/<string:transaction_id>/download', methods=['GET'])
|
| 444 |
def download_receipt(transaction_id):
|
|
|
|
| 453 |
if not image_url:
|
| 454 |
return jsonify({'error': 'No receipt image found for this transaction'}), 404
|
| 455 |
|
| 456 |
+
blob_path = get_blob_from_image_url(image_url)
|
| 457 |
+
if not blob_path:
|
| 458 |
+
return jsonify({'error': 'Could not determine blob path from URL'}), 500
|
| 459 |
+
|
| 460 |
+
blob = bucket.blob(blob_path)
|
| 461 |
+
signed_url = blob.generate_signed_url(expiration=datetime.timedelta(minutes=10))
|
| 462 |
+
r = requests.get(signed_url)
|
| 463 |
if r.status_code != 200:
|
| 464 |
return jsonify({'error': 'Unable to fetch image from storage'}), 500
|
| 465 |
|
| 466 |
+
# Use attachment_filename if you're on Flask 1.x; if using Flask 2.x, you can replace it with download_name.
|
| 467 |
return send_file(
|
| 468 |
io.BytesIO(r.content),
|
| 469 |
mimetype='image/jpeg',
|
|
|
|
| 471 |
attachment_filename='receipt.jpg'
|
| 472 |
)
|
| 473 |
except Exception as e:
|
| 474 |
+
print(f"Download receipt error: {str(e)}")
|
| 475 |
return jsonify({'error': str(e)}), 500
|
| 476 |
|
| 477 |
|
| 478 |
+
|
| 479 |
# delete users
|
| 480 |
|
| 481 |
@app.route('/api/admin/users/<string:uid>', methods=['DELETE'])
|