Spaces:
Sleeping
Sleeping
ParthivM1 commited on
Commit ·
764ea7f
1
Parent(s): 49624d5
Final for report section2
Browse files
app.py
CHANGED
|
@@ -92,6 +92,15 @@ def process_video_in_background(video_data: bytes, video_guid: str, filename: st
|
|
| 92 |
async def root():
|
| 93 |
return {"message": "API is running"}
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
@app.get("/latest_reports")
|
| 96 |
async def get_latest_reports():
|
| 97 |
try:
|
|
@@ -111,15 +120,13 @@ async def get_latest_reports():
|
|
| 111 |
all_reports = videos + images
|
| 112 |
|
| 113 |
# --- FIX ---
|
| 114 |
-
#
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
# Sort the valid reports
|
| 118 |
-
combined_reports = sorted(valid_reports, key=lambda x: datetime.fromisoformat(x['created_at']), reverse=True)
|
| 119 |
|
| 120 |
return combined_reports[:30]
|
| 121 |
except Exception as e:
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
@app.patch("/report/{guid}/status")
|
| 125 |
async def update_report_status(guid: str, payload: dict = Body(...)):
|
|
|
|
| 92 |
async def root():
|
| 93 |
return {"message": "API is running"}
|
| 94 |
|
| 95 |
+
def safe_date_converter(date_string):
|
| 96 |
+
"""Safely converts an ISO date string to a datetime object, handling errors."""
|
| 97 |
+
if not date_string:
|
| 98 |
+
return datetime.min # Return a very old date for sorting if date is None or empty
|
| 99 |
+
try:
|
| 100 |
+
return datetime.fromisoformat(date_string)
|
| 101 |
+
except (ValueError, TypeError):
|
| 102 |
+
return datetime.min # Return a very old date if the format is invalid
|
| 103 |
+
|
| 104 |
@app.get("/latest_reports")
|
| 105 |
async def get_latest_reports():
|
| 106 |
try:
|
|
|
|
| 120 |
all_reports = videos + images
|
| 121 |
|
| 122 |
# --- FIX ---
|
| 123 |
+
# Safely sort the reports using the new helper function to prevent crashes
|
| 124 |
+
combined_reports = sorted(all_reports, key=lambda x: safe_date_converter(x.get('created_at')), reverse=True)
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
return combined_reports[:30]
|
| 127 |
except Exception as e:
|
| 128 |
+
# This will now only catch truly unexpected errors, not bad dates
|
| 129 |
+
raise HTTPException(status_code=500, detail=f"A severe database error occurred: {str(e)}")
|
| 130 |
|
| 131 |
@app.patch("/report/{guid}/status")
|
| 132 |
async def update_report_status(guid: str, payload: dict = Body(...)):
|