.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'
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']
'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.
"""
# 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"]:
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:
return {
'id': spreadsheet_id,
- 'url': spreadsheet['spreadsheetUrl']
+ 'url': spreadsheet_url
}
def spreadsheet_insert_data(turb, spreadsheet_id, range, text):