Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -303,68 +303,87 @@ def get_spending_overview():
|
|
| 303 |
transactions_ref = db.reference('transactions')
|
| 304 |
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get() or {}
|
| 305 |
|
| 306 |
-
#
|
| 307 |
-
transactions_list = [
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
})
|
| 314 |
|
| 315 |
# Create a DataFrame
|
| 316 |
-
df = pd.DataFrame(transactions_list)
|
| 317 |
-
|
| 318 |
-
# Handle date formatting (only if the date column exists)
|
| 319 |
-
if 'date' in df.columns:
|
| 320 |
-
# Convert to datetime, but keep non-convertible values
|
| 321 |
-
df['date'] = pd.to_datetime(df['date'], errors='coerce')
|
| 322 |
-
# Filter out rows with invalid dates
|
| 323 |
-
df = df.dropna(subset=['date'])
|
| 324 |
-
|
| 325 |
-
if not df.empty:
|
| 326 |
-
# Convert datetime to ISO format
|
| 327 |
-
df['date'] = df['date'].apply(lambda d: d.isoformat())
|
| 328 |
-
# Extract date part for grouping
|
| 329 |
-
df['date_only'] = df['date'].apply(lambda d: d.split("T")[0])
|
| 330 |
|
| 331 |
-
# Return empty response if we don't have valid data after processing
|
| 332 |
if df.empty:
|
| 333 |
return jsonify({
|
| 334 |
'daily_spending': [],
|
| 335 |
'recent_transactions': []
|
| 336 |
})
|
| 337 |
|
| 338 |
-
#
|
| 339 |
-
if '
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
else:
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
-
# Sort
|
| 346 |
if 'timestamp' in df.columns:
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
| 350 |
|
| 351 |
-
# Replace NaN
|
| 352 |
daily_spending = daily_spending.replace({np.nan: None})
|
| 353 |
recent_transactions = recent_transactions.replace({np.nan: None})
|
| 354 |
|
| 355 |
-
#
|
| 356 |
-
|
| 357 |
-
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 358 |
-
'recent_transactions': recent_transactions.drop(columns=['date_only'], errors='ignore').to_dict(orient='records')
|
| 359 |
-
}
|
| 360 |
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
except Exception as e:
|
|
|
|
| 364 |
return jsonify({'error': str(e)}), 500
|
| 365 |
|
| 366 |
|
| 367 |
-
|
| 368 |
# ========================================
|
| 369 |
# Modified verify_admin function (now checks database is_admin flag)
|
| 370 |
# ========================================
|
|
|
|
| 303 |
transactions_ref = db.reference('transactions')
|
| 304 |
transactions = transactions_ref.order_by_child('uid').equal_to(uid).get() or {}
|
| 305 |
|
| 306 |
+
# Convert to list for easier processing
|
| 307 |
+
transactions_list = []
|
| 308 |
+
for tx_id, tx_data in transactions.items():
|
| 309 |
+
# Add ID to the transaction data
|
| 310 |
+
tx_item = tx_data.copy()
|
| 311 |
+
tx_item['id'] = tx_id
|
| 312 |
+
transactions_list.append(tx_item)
|
|
|
|
| 313 |
|
| 314 |
# Create a DataFrame
|
| 315 |
+
df = pd.DataFrame(transactions_list) if transactions_list else pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
|
|
|
|
| 317 |
if df.empty:
|
| 318 |
return jsonify({
|
| 319 |
'daily_spending': [],
|
| 320 |
'recent_transactions': []
|
| 321 |
})
|
| 322 |
|
| 323 |
+
# Handle date parsing without dropping invalid dates
|
| 324 |
+
if 'date' in df.columns:
|
| 325 |
+
# Create a temporary column for parsed dates
|
| 326 |
+
df['parsed_date'] = pd.to_datetime(df['date'], errors='coerce')
|
| 327 |
+
|
| 328 |
+
# For rows with invalid dates, set a default date
|
| 329 |
+
default_date = pd.Timestamp('2000-01-01')
|
| 330 |
+
mask = df['parsed_date'].isna()
|
| 331 |
+
|
| 332 |
+
if mask.any():
|
| 333 |
+
print(f"Found {mask.sum()} transactions with invalid dates")
|
| 334 |
+
# Keep the original string in 'date' column, but use default for calculations
|
| 335 |
+
df.loc[mask, 'parsed_date'] = default_date
|
| 336 |
+
|
| 337 |
+
# Create ISO format dates for valid dates
|
| 338 |
+
df['date_iso'] = df['parsed_date'].apply(lambda d: d.isoformat() if pd.notnull(d) else '2000-01-01T00:00:00')
|
| 339 |
+
|
| 340 |
+
# Extract date part for daily spending calculations
|
| 341 |
+
df['date_only'] = df['date_iso'].apply(lambda d: d.split('T')[0])
|
| 342 |
else:
|
| 343 |
+
# If no date column exists, create default values
|
| 344 |
+
df['date_only'] = '2000-01-01'
|
| 345 |
+
df['date_iso'] = '2000-01-01T00:00:00'
|
| 346 |
+
|
| 347 |
+
# For daily spending, group by date
|
| 348 |
+
daily_spending = df.groupby('date_only')['total'].sum().reset_index()
|
| 349 |
+
daily_spending.rename(columns={'date_only': 'date'}, inplace=True)
|
| 350 |
|
| 351 |
+
# Sort transactions by timestamp if available
|
| 352 |
if 'timestamp' in df.columns:
|
| 353 |
+
df = df.sort_values(by='timestamp', ascending=False)
|
| 354 |
+
|
| 355 |
+
# Select the most recent transactions
|
| 356 |
+
recent_transactions = df.head(10)
|
| 357 |
|
| 358 |
+
# Replace NaN with None for JSON serialization
|
| 359 |
daily_spending = daily_spending.replace({np.nan: None})
|
| 360 |
recent_transactions = recent_transactions.replace({np.nan: None})
|
| 361 |
|
| 362 |
+
# Ensure proper column selections for the response
|
| 363 |
+
recent_tx_dict = recent_transactions.to_dict(orient='records')
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
+
# Make sure each transaction has the original date string
|
| 366 |
+
for tx in recent_tx_dict:
|
| 367 |
+
# Remove temporary columns
|
| 368 |
+
if 'parsed_date' in tx:
|
| 369 |
+
del tx['parsed_date']
|
| 370 |
+
if 'date_only' in tx:
|
| 371 |
+
del tx['date_only']
|
| 372 |
+
if 'date_iso' in tx and 'date' not in tx:
|
| 373 |
+
tx['date'] = tx['date_iso']
|
| 374 |
+
del tx['date_iso']
|
| 375 |
+
elif 'date_iso' in tx:
|
| 376 |
+
del tx['date_iso']
|
| 377 |
|
| 378 |
+
return jsonify({
|
| 379 |
+
'daily_spending': daily_spending.to_dict(orient='records'),
|
| 380 |
+
'recent_transactions': recent_tx_dict
|
| 381 |
+
})
|
| 382 |
except Exception as e:
|
| 383 |
+
print(f"Error in spending overview: {e}")
|
| 384 |
return jsonify({'error': str(e)}), 500
|
| 385 |
|
| 386 |
|
|
|
|
| 387 |
# ========================================
|
| 388 |
# Modified verify_admin function (now checks database is_admin flag)
|
| 389 |
# ========================================
|