]> git.cworth.org Git - turbot/blobdiff - turbot/sheets.py
Add a welcome message to any new puzzle channel that is created.
[turbot] / turbot / sheets.py
index 8ab302d38579a8f744a475a34a39c769f715813a..fbe9e3d7839d2e52a64d43f471a444efa1869fe3 100644 (file)
@@ -16,18 +16,49 @@ def sheets_create(turb, name):
 
     new_sheet = turb.sheets.create(body=spreadsheet_body).execute()
 
+    # Now that we've created a new sheet, we need to also allow anyone
+    # with the link to the sheet to be able to edit it.
+    turb.permissions.create(fileId=new_sheet["spreadsheetId"],
+                            body={'type': 'anyone', 'role': 'writer'},
+                            fields='id').execute()
+
     return {
         'id': new_sheet["spreadsheetId"],
         '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