Update main.py
Browse files
main.py
CHANGED
|
@@ -558,7 +558,6 @@ def generate_sozo_report(project_id):
|
|
| 558 |
if not uid:
|
| 559 |
return jsonify({'error': 'Unauthorized'}), 401
|
| 560 |
|
| 561 |
-
# Check user credits first
|
| 562 |
user_ref = db.reference(f'users/{uid}')
|
| 563 |
user_data = user_ref.get()
|
| 564 |
if not user_data:
|
|
@@ -574,13 +573,11 @@ def generate_sozo_report(project_id):
|
|
| 574 |
if not project_data or project_data.get('uid') != uid:
|
| 575 |
return jsonify({'error': 'Project not found or unauthorized'}), 404
|
| 576 |
|
| 577 |
-
# --- THE AUTHORITATIVE LOCK ---
|
| 578 |
current_status = project_data.get('status')
|
| 579 |
-
if current_status in ['generating_report', 'generating_video']:
|
| 580 |
logger.warning(f"User {uid} attempted to generate a report for project {project_id} which is already in progress (status: {current_status}).")
|
| 581 |
-
return jsonify({'error': '
|
| 582 |
|
| 583 |
-
# Set the lock BEFORE starting the heavy work
|
| 584 |
project_ref.update({'status': 'generating_report'})
|
| 585 |
logger.info(f"Project {project_id} status locked to 'generating_report'.")
|
| 586 |
|
|
@@ -597,29 +594,44 @@ def generate_sozo_report(project_id):
|
|
| 597 |
bucket
|
| 598 |
)
|
| 599 |
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
except Exception as e:
|
| 621 |
logger.error(f"CRITICAL error generating report for project {project_id}: {traceback.format_exc()}")
|
| 622 |
-
# Make sure to unlock the project on failure
|
| 623 |
db.reference(f'sozo_projects/{project_id}').update({
|
| 624 |
'status': 'failed',
|
| 625 |
'error': str(e)
|
|
|
|
| 558 |
if not uid:
|
| 559 |
return jsonify({'error': 'Unauthorized'}), 401
|
| 560 |
|
|
|
|
| 561 |
user_ref = db.reference(f'users/{uid}')
|
| 562 |
user_data = user_ref.get()
|
| 563 |
if not user_data:
|
|
|
|
| 573 |
if not project_data or project_data.get('uid') != uid:
|
| 574 |
return jsonify({'error': 'Project not found or unauthorized'}), 404
|
| 575 |
|
|
|
|
| 576 |
current_status = project_data.get('status')
|
| 577 |
+
if current_status in ['generating_report', 'generating_video', 'generating_slides']:
|
| 578 |
logger.warning(f"User {uid} attempted to generate a report for project {project_id} which is already in progress (status: {current_status}).")
|
| 579 |
+
return jsonify({'error': 'A process is already running for this project.'}), 409
|
| 580 |
|
|
|
|
| 581 |
project_ref.update({'status': 'generating_report'})
|
| 582 |
logger.info(f"Project {project_id} status locked to 'generating_report'.")
|
| 583 |
|
|
|
|
| 594 |
bucket
|
| 595 |
)
|
| 596 |
|
| 597 |
+
# --- Graceful Fallback Save & Charge Logic ---
|
| 598 |
+
try:
|
| 599 |
+
# Happy Path: Try to save everything
|
| 600 |
+
full_update_data = {
|
| 601 |
+
'status': 'draft',
|
| 602 |
+
'rawMarkdown': draft_data.get('raw_md'),
|
| 603 |
+
'chartUrls': draft_data.get('chartUrls'),
|
| 604 |
+
'dataContext': draft_data.get('data_context')
|
| 605 |
+
}
|
| 606 |
+
project_ref.update(full_update_data)
|
| 607 |
+
user_ref.update({'credits': current_credits - 2})
|
| 608 |
+
logger.info(f"Project {project_id} successfully updated with full data. User {uid} charged 2 credits.")
|
| 609 |
+
return jsonify({
|
| 610 |
+
'success': True,
|
| 611 |
+
'project': {**project_data, **full_update_data},
|
| 612 |
+
'credits_remaining': current_credits - 2
|
| 613 |
+
}), 200
|
| 614 |
+
except Exception as save_error:
|
| 615 |
+
# Fallback Path: Save only the essentials if the full save fails
|
| 616 |
+
logger.warning(f"Failed to save full project data for {project_id} due to: {save_error}. Saving degraded version.")
|
| 617 |
+
degraded_update_data = {
|
| 618 |
+
'status': 'draft',
|
| 619 |
+
'rawMarkdown': draft_data.get('raw_md'),
|
| 620 |
+
'chartUrls': draft_data.get('chartUrls'),
|
| 621 |
+
'dataContext': None,
|
| 622 |
+
'warning': 'Report generated, but context data failed to save. Downstream features may be affected.'
|
| 623 |
+
}
|
| 624 |
+
project_ref.update(degraded_update_data)
|
| 625 |
+
user_ref.update({'credits': current_credits - 2})
|
| 626 |
+
logger.info(f"Project {project_id} successfully updated with DEGRADED data. User {uid} charged 2 credits.")
|
| 627 |
+
return jsonify({
|
| 628 |
+
'success': True,
|
| 629 |
+
'project': {**project_data, **degraded_update_data},
|
| 630 |
+
'credits_remaining': current_credits - 2
|
| 631 |
+
}), 200
|
| 632 |
+
|
| 633 |
except Exception as e:
|
| 634 |
logger.error(f"CRITICAL error generating report for project {project_id}: {traceback.format_exc()}")
|
|
|
|
| 635 |
db.reference(f'sozo_projects/{project_id}').update({
|
| 636 |
'status': 'failed',
|
| 637 |
'error': str(e)
|