Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -624,7 +624,147 @@ async def process_photo(background_tasks: BackgroundTasks, image_id: str = Form(
|
|
| 624 |
logging.error(f"Unexpected error in process_photo for user {user_id}, image {image_id}: {str(e)}")
|
| 625 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 626 |
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
@app.post("/sync-data")
|
| 629 |
async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
|
| 630 |
try:
|
|
|
|
| 624 |
logging.error(f"Unexpected error in process_photo for user {user_id}, image {image_id}: {str(e)}")
|
| 625 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 626 |
|
| 627 |
+
|
| 628 |
+
async def update_user_sheet(spreadsheet_id: str, sheet_id: int, sheet_name: str, data: List[List[str]], is_reset: Optional[bool] = False):
|
| 629 |
+
try:
|
| 630 |
+
logging.info(f"Updating sheet '{sheet_name}' in spreadsheet {spreadsheet_id}")
|
| 631 |
+
|
| 632 |
+
if not spreadsheet_id or not sheet_id or not sheet_name or not data:
|
| 633 |
+
raise ValueError("Missing required parameters for updating user sheet")
|
| 634 |
+
|
| 635 |
+
no_of_rows = len(data)
|
| 636 |
+
no_of_cols = len(data[0]) if data else 0
|
| 637 |
+
|
| 638 |
+
if is_reset:
|
| 639 |
+
sheet_name = f"{sheet_name} {datetime.now(kst).strftime('%Y-%m-%d %H:%M')}"
|
| 640 |
+
try:
|
| 641 |
+
duplicate_requests = [{
|
| 642 |
+
"duplicateSheet": {
|
| 643 |
+
"sourceSheetId": contacts_ss,
|
| 644 |
+
"newSheetName": sheet_name
|
| 645 |
+
}
|
| 646 |
+
}]
|
| 647 |
+
duplicate_response = sheet_service.spreadsheets().batchUpdate(
|
| 648 |
+
spreadsheetId=spreadsheet_id,
|
| 649 |
+
body={
|
| 650 |
+
'requests': duplicate_requests
|
| 651 |
+
}
|
| 652 |
+
).execute()
|
| 653 |
+
sheet_id = duplicate_response['replies'][0]['duplicateSheet']['properties']['sheetId']
|
| 654 |
+
logging.info(f"Created new sheet '{sheet_name}' with ID {sheet_id}")
|
| 655 |
+
except HttpError as e:
|
| 656 |
+
logging.error(f"Error duplicating sheet: {e}")
|
| 657 |
+
raise HTTPException(status_code=500, detail=f"Failed to create new sheet: {str(e)}")
|
| 658 |
+
|
| 659 |
+
requests = [
|
| 660 |
+
{
|
| 661 |
+
"updateSheetProperties": {
|
| 662 |
+
"properties": {
|
| 663 |
+
"sheetId": sheet_id,
|
| 664 |
+
"hidden": False
|
| 665 |
+
},
|
| 666 |
+
"fields": "hidden"
|
| 667 |
+
}
|
| 668 |
+
},
|
| 669 |
+
{
|
| 670 |
+
"insertDimension": {
|
| 671 |
+
"range": {
|
| 672 |
+
"sheetId": sheet_id,
|
| 673 |
+
"dimension": "ROWS",
|
| 674 |
+
"startIndex": 1,
|
| 675 |
+
"endIndex": no_of_rows + 1
|
| 676 |
+
},
|
| 677 |
+
"inheritFromBefore": False,
|
| 678 |
+
}
|
| 679 |
+
}
|
| 680 |
+
]
|
| 681 |
+
|
| 682 |
+
try:
|
| 683 |
+
sheet_service.spreadsheets().batchUpdate(
|
| 684 |
+
spreadsheetId=spreadsheet_id,
|
| 685 |
+
body={
|
| 686 |
+
'requests': requests
|
| 687 |
+
}
|
| 688 |
+
).execute()
|
| 689 |
+
logging.info(f"Inserted {no_of_rows} rows in sheet '{sheet_name}'")
|
| 690 |
+
except HttpError as e:
|
| 691 |
+
logging.error(f"Error updating sheet properties or inserting rows: {e}")
|
| 692 |
+
raise HTTPException(status_code=500, detail=f"Failed to prepare sheet for data update: {str(e)}")
|
| 693 |
+
|
| 694 |
+
update_body = {
|
| 695 |
+
'values': data
|
| 696 |
+
}
|
| 697 |
+
start_column = 'A'
|
| 698 |
+
end_column = chr(ord(start_column) + no_of_cols - 1)
|
| 699 |
+
range_to_update = f'{sheet_name}!{start_column}2:{end_column}{no_of_rows + 1}'
|
| 700 |
+
|
| 701 |
+
try:
|
| 702 |
+
result = sheet_service.spreadsheets().values().update(
|
| 703 |
+
spreadsheetId=spreadsheet_id,
|
| 704 |
+
range=range_to_update,
|
| 705 |
+
valueInputOption='USER_ENTERED',
|
| 706 |
+
body=update_body
|
| 707 |
+
).execute()
|
| 708 |
+
logging.info(f"Updated {result.get('updatedCells')} cells in sheet '{sheet_name}'")
|
| 709 |
+
except HttpError as e:
|
| 710 |
+
logging.error(f"Error updating cell values: {e}")
|
| 711 |
+
raise HTTPException(status_code=500, detail=f"Failed to update sheet data: {str(e)}")
|
| 712 |
+
|
| 713 |
+
# Format the cells
|
| 714 |
+
for row_index, row in enumerate(data):
|
| 715 |
+
for col_index, cell_value in enumerate(row):
|
| 716 |
+
if cell_value.startswith('=HYPERLINK'):
|
| 717 |
+
color = {"red": 0, "green": 0, "blue": 1} # Blue for hyperlinks
|
| 718 |
+
else:
|
| 719 |
+
color = {"red": 0, "green": 0, "blue": 0} # Black for other text
|
| 720 |
+
|
| 721 |
+
requests = [
|
| 722 |
+
{
|
| 723 |
+
"repeatCell": {
|
| 724 |
+
"range": {
|
| 725 |
+
"sheetId": sheet_id,
|
| 726 |
+
"startRowIndex": row_index + 1,
|
| 727 |
+
"endRowIndex": row_index + 2,
|
| 728 |
+
"startColumnIndex": col_index,
|
| 729 |
+
"endColumnIndex": col_index + 1
|
| 730 |
+
},
|
| 731 |
+
"cell": {
|
| 732 |
+
"userEnteredFormat": {
|
| 733 |
+
"textFormat": {
|
| 734 |
+
"foregroundColor": color
|
| 735 |
+
}
|
| 736 |
+
}
|
| 737 |
+
},
|
| 738 |
+
"fields": "userEnteredFormat.textFormat.foregroundColor"
|
| 739 |
+
}
|
| 740 |
+
}
|
| 741 |
+
]
|
| 742 |
+
|
| 743 |
+
try:
|
| 744 |
+
sheet_service.spreadsheets().batchUpdate(
|
| 745 |
+
spreadsheetId=spreadsheet_id,
|
| 746 |
+
body={
|
| 747 |
+
'requests': requests
|
| 748 |
+
}
|
| 749 |
+
).execute()
|
| 750 |
+
except HttpError as e:
|
| 751 |
+
logging.error(f"Error formatting cells: {e}")
|
| 752 |
+
# We'll continue even if formatting fails, as it's not critical
|
| 753 |
+
logging.warning("Cell formatting failed, but data was updated successfully")
|
| 754 |
+
|
| 755 |
+
logging.info(f"Successfully updated and formatted sheet '{sheet_name}'")
|
| 756 |
+
return True
|
| 757 |
+
|
| 758 |
+
except ValueError as ve:
|
| 759 |
+
logging.error(f"Value error in update_user_sheet: {ve}")
|
| 760 |
+
raise HTTPException(status_code=400, detail=str(ve))
|
| 761 |
+
except HttpError as he:
|
| 762 |
+
logging.error(f"Google Sheets API error in update_user_sheet: {he}")
|
| 763 |
+
raise HTTPException(status_code=500, detail=f"Google Sheets API error: {str(he)}")
|
| 764 |
+
except Exception as e:
|
| 765 |
+
logging.error(f"Unexpected error in update_user_sheet: {e}")
|
| 766 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 767 |
+
|
| 768 |
@app.post("/sync-data")
|
| 769 |
async def sync_data(data_json: str = Form(...), user_id: str = Form(...)):
|
| 770 |
try:
|