]> git.cworth.org Git - turbot/commitdiff
Create puzzle sheets in the hunt folder
authorCarl Worth <cworth@cworth.org>
Sun, 10 Jan 2021 21:24:25 +0000 (13:24 -0800)
committerCarl Worth <cworth@cworth.org>
Sun, 10 Jan 2021 21:28:34 +0000 (13:28 -0800)
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
turbot/sheets.py

index 8f9f12c4d4d130c45ed57f9dafa19b258587cf82..adf7c9e04853f0a3561bf96749af14e05b4157ee 100644 (file)
@@ -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']
index 831998d6f52c581c71b21d3b3daf67c475003d78..56adef6cf8a7433ef6614b99b1fb285ba06e15af 100644 (file)
@@ -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):