From 5f54f1ee53b8ae1b9d93df3f56c6a992f1d1bf0a Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Sun, 10 Jan 2021 13:24:25 -0800 Subject: [PATCH] Create puzzle sheets in the hunt folder This will be much nicer to keep our content organized within Google drive. There will be one folder for each hunt rather than all the various hunts' puzzles all mixed together at the root level. --- turbot/events.py | 6 +++++- turbot/sheets.py | 46 ++++++++++++---------------------------------- 2 files changed, 17 insertions(+), 35 deletions(-) diff --git a/turbot/events.py b/turbot/events.py index 8f9f12c..adf7c9e 100644 --- a/turbot/events.py +++ b/turbot/events.py @@ -191,6 +191,10 @@ def puzzle_channel_created(turb, channel_name, channel_id): .format(channel_id, puzzle['sheet_url'])) return lambda_success + # We need hunt from the database to know which folder to create + # the sheet in. + hunt = find_hunt_for_hunt_id(turb, puzzle['hunt_id']) + # Before launching into sheet creation, indicate that we're doing this # in the database. This way, if we take too long to create the sheet # and Slack retries the event, that next event will see this 'pending' @@ -200,7 +204,7 @@ def puzzle_channel_created(turb, channel_name, channel_id): turb.table.put_item(Item=puzzle) # Create a sheet for the puzzle - sheet = sheets_create_for_puzzle(turb, puzzle) + sheet = sheets_create_for_puzzle(turb, puzzle, hunt['folder_id']) # Update the database with the URL of the sheet puzzle['sheet_url'] = sheet['url'] diff --git a/turbot/sheets.py b/turbot/sheets.py index 831998d..56adef6 100644 --- a/turbot/sheets.py +++ b/turbot/sheets.py @@ -48,33 +48,7 @@ def sheets_create(turb, name, folder_id): 'url': url } -def create_spreadsheet(turb, name): - """ - Returns the request's dict which has at least the following keys: - - ['spreadsheetId']: ID for this spreadsheet - ['spreadsheetUrl']: URL of this spreadsheet - ['sheets'][0]['properties']['sheetId']: ID of first sheet inside - """ - - # Create a new spreadsheet - spreadsheet_body = { - 'properties': { - 'title': name - } - } - - spreadsheet = turb.sheets.create(body=spreadsheet_body).execute() - - # Now that we've created a new spreadsheet, we need to also allow - # anyone with the link to the sheet to be able to edit it. - turb.permissions.create(fileId=spreadsheet["spreadsheetId"], - body={'type': 'anyone', 'role': 'writer'}, - fields='id').execute() - - return spreadsheet - -def sheets_create_for_puzzle(turb, puzzle): +def sheets_create_for_puzzle(turb, puzzle, folder_id): """Creates a new sheet for a puzzle of the given name Like sheets_create(), but also copies the puzzle template sheet. @@ -84,11 +58,16 @@ def sheets_create_for_puzzle(turb, puzzle): """ # First create the new spreadsheet - spreadsheet = create_spreadsheet(turb, puzzle['name']) - spreadsheet_id = spreadsheet['spreadsheetId'] + spreadsheet = sheets_create(turb, puzzle['name'], folder_id) + spreadsheet_id = spreadsheet['id'] + spreadsheet_url = spreadsheet['url'] - # Then, copy some useful sheets over from the Template spreadsheet + # And fetch the individual "sheets" from the spreadsheet + spreadsheet = turb.sheets.get(spreadsheetId=spreadsheet_id, + fields='sheets').execute() + first_sheet_id = spreadsheet['sheets'][0]['properties']['sheetId'] + # Then, copy some useful sheets over from the Template spreadsheet response = turb.sheets.get(spreadsheetId=PUZZLE_TEMPLATE_ID).execute() for sheet in response["sheets"]: @@ -106,9 +85,8 @@ def sheets_create_for_puzzle(turb, puzzle): sheet_name = puzzle['name'] rename_sheet(turb, spreadsheet_id, res['sheetId'], sheet_name) - # Next, delete the blank sheet that's was created before the template - sheet_id = spreadsheet['sheets'][0]['properties']['sheetId'] - delete_sheet(turb, spreadsheet_id, sheet_id) + # Next, delete the blank sheet made at spreadsheet creation time + delete_sheet(turb, spreadsheet_id, first_sheet_id) # Insert some useful links into the sheet where expected if 'url' in puzzle: @@ -127,7 +105,7 @@ def sheets_create_for_puzzle(turb, puzzle): return { 'id': spreadsheet_id, - 'url': spreadsheet['spreadsheetUrl'] + 'url': spreadsheet_url } def spreadsheet_insert_data(turb, spreadsheet_id, range, text): -- 2.43.0