Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -221,21 +221,47 @@ def validate_and_save_transaction(uid, user_data, data, file_hash, image_bytes,
|
|
| 221 |
# ========================================
|
| 222 |
|
| 223 |
|
| 224 |
-
|
| 225 |
@app.route('/api/user/spending-overview', methods=['GET'])
|
| 226 |
def get_spending_overview():
|
| 227 |
try:
|
| 228 |
uid = verify_token(request.headers.get('Authorization', '').split(' ')[1])
|
| 229 |
transactions_ref = db.reference('transactions')
|
| 230 |
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
if df.empty:
|
| 234 |
-
return jsonify({
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y', errors='coerce')
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
|
|
|
|
|
|
| 239 |
return jsonify({
|
| 240 |
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 241 |
'recent_transactions': df.sort_values(by='timestamp', ascending=False)
|
|
@@ -243,10 +269,8 @@ def get_spending_overview():
|
|
| 243 |
.to_dict(orient='records')
|
| 244 |
})
|
| 245 |
except Exception as e:
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
|
| 249 |
-
return jsonify({'error': str(e) + f'transactions:{transactions}'}), 500
|
| 250 |
|
| 251 |
# ... (previous imports remain the same)
|
| 252 |
|
|
|
|
| 221 |
# ========================================
|
| 222 |
|
| 223 |
|
|
|
|
| 224 |
@app.route('/api/user/spending-overview', methods=['GET'])
|
| 225 |
def get_spending_overview():
|
| 226 |
try:
|
| 227 |
uid = verify_token(request.headers.get('Authorization', '').split(' ')[1])
|
| 228 |
transactions_ref = db.reference('transactions')
|
| 229 |
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get()
|
| 230 |
+
|
| 231 |
+
if transactions is None:
|
| 232 |
+
return jsonify({
|
| 233 |
+
'daily_spending': [],
|
| 234 |
+
'recent_transactions': []
|
| 235 |
+
})
|
| 236 |
|
| 237 |
+
# Create dataframe
|
| 238 |
+
df = pd.DataFrame.from_dict(transactions, orient='index')
|
| 239 |
+
|
| 240 |
+
# Check if dataframe is empty
|
| 241 |
if df.empty:
|
| 242 |
+
return jsonify({
|
| 243 |
+
'daily_spending': [],
|
| 244 |
+
'recent_transactions': []
|
| 245 |
+
})
|
| 246 |
+
|
| 247 |
+
# Check if all the data is here, otherwise we delete the data.
|
| 248 |
+
df.dropna(subset=['uid','total','items','date', 'receipt_number', 'timestamp','hash', 'manual_entry'], inplace=True)
|
| 249 |
+
if df.empty:
|
| 250 |
+
return jsonify({
|
| 251 |
+
'daily_spending': [],
|
| 252 |
+
'recent_transactions': []
|
| 253 |
+
})
|
| 254 |
+
|
| 255 |
df['date'] = pd.to_datetime(df['date'], format='%d/%m/%Y', errors='coerce')
|
| 256 |
+
df.dropna(subset=['date'], inplace=True)
|
| 257 |
+
if df.empty:
|
| 258 |
+
return jsonify({
|
| 259 |
+
'daily_spending': [],
|
| 260 |
+
'recent_transactions': []
|
| 261 |
+
})
|
| 262 |
|
| 263 |
+
daily_spending = df.groupby(df['date'].dt.date)['total'].sum().reset_index()
|
| 264 |
+
|
| 265 |
return jsonify({
|
| 266 |
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 267 |
'recent_transactions': df.sort_values(by='timestamp', ascending=False)
|
|
|
|
| 269 |
.to_dict(orient='records')
|
| 270 |
})
|
| 271 |
except Exception as e:
|
| 272 |
+
return jsonify({'error': str(e)}), 500
|
| 273 |
+
|
|
|
|
|
|
|
| 274 |
|
| 275 |
# ... (previous imports remain the same)
|
| 276 |
|