From 63b8b210af4243a52612d1c8032866ca76e74d61 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 23 Oct 2020 07:18:31 -0700 Subject: [PATCH] Add some links to the puzzle sheet when originally creating it Linking both to the original puzzle as well as to the slack channel. --- turbot/events.py | 3 ++- turbot/sheets.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/turbot/events.py b/turbot/events.py index 8d70a21..0a1eb09 100644 --- a/turbot/events.py +++ b/turbot/events.py @@ -243,10 +243,11 @@ def puzzle_channel_created(turb, puzzle_channel_name, puzzle_channel_id): # and Slack retries the event, that next event will see this 'pending' # string and cleanly return (eliminating all future retries). item['sheet_url'] = 'pending' + item['channel_url'] = channel_url(puzzle_channel_id) puzzle_table.put_item(Item=item) # Create a sheet for the puzzle - sheet = turbot.sheets.sheets_create_for_puzzle(turb, item['name']) + sheet = turbot.sheets.sheets_create_for_puzzle(turb, item) # Update the database with the URL of the sheet item['sheet_url'] = sheet['url'] diff --git a/turbot/sheets.py b/turbot/sheets.py index f2a9681..fbe9e3d 100644 --- a/turbot/sheets.py +++ b/turbot/sheets.py @@ -27,13 +27,38 @@ def sheets_create(turb, name): 'url': new_sheet["spreadsheetUrl"] } -def sheets_create_for_puzzle(turb, name): +def sheets_create_for_puzzle(turb, puzzle): """Creates a new sheet for a puzzle of the given name - Like sheets_create(), but also copies the puzzle template sheet.""" + Like sheets_create(), but also copies the puzzle template sheet. + + Here, 'puzzle' is a dict that must have a 'name' key and may optionally + have a 'channel_url' or 'url' key.""" # First create the new sheet - new_sheet = sheets_create(turb, name) + new_sheet = sheets_create(turb, puzzle['name']) + + # Insert some useful links into the sheet + if 'url' in puzzle: + url_text = "Original puzzle is at: {}".format(puzzle['url']) + else: + url_text = '' + + if 'channel_url' in puzzle: + channel_url_text = "Discussion for this puzzle is at: {}".format( + puzzle['channel_url']) + else: + channel_url_text = '' + + turb.sheets.values().append( + spreadsheetId=new_sheet['id'], + range='A1:A2', + valueInputOption='USER_ENTERED', + insertDataOption='INSERT_ROWS', + body={ + 'range': 'A1:A2', + 'values': [[url_text], [channel_url_text]] + }).execute() # Copy some sheets from the Template spreadsheet -- 2.45.2