]> git.cworth.org Git - turbot/blobdiff - turbot/puzzle.py
Implement a dialog box to edit a puzzle
[turbot] / turbot / puzzle.py
index 3c85df487f7298a42370f13bc5e91d1553dbb1e5..b6e78833098f9ed2c4d5251a82400d01941c9b07 100644 (file)
@@ -5,6 +5,25 @@ from turbot.channel import channel_url
 from boto3.dynamodb.conditions import Key
 import re
 
+def find_puzzle_for_puzzle_id(turb, hunt_id, puzzle_id):
+    """Given a hunt_id and puzzle_id, return that puzzle
+
+    Returns None if no puzzle with the given hunt_id and puzzle_id
+    exists in the database, otherwise a dictionary with all fields
+    from the puzzle's row in the database.
+    """
+
+    response = turb.table.get_item(
+        Key={
+            'hunt_id': hunt_id,
+            'SK': 'puzzle-{}'.format(puzzle_id)
+        })
+
+    if 'Item' in response:
+        return response['Item']
+    else:
+        return None
+
 def find_puzzle_for_url(turb, hunt_id, url):
     """Given a hunt_id and URL, return the puzzle with that URL
 
@@ -69,10 +88,15 @@ def puzzle_blocks(puzzle):
         ', '.join(links), state_str
     )
 
+    # Combining hunt ID and puzzle ID together here is safe because
+    # both IDs are restricted to not contain a hyphen, (see
+    # valid_id_re in interaction.py)
+    hunt_and_puzzle = "{}-{}".format(puzzle['hunt_id'], puzzle['puzzle_id'])
+
     return [
         accessory_block(
             section_block(text_block(puzzle_text)),
-            button_block("✏", puzzle['puzzle_id'])
+            button_block("✏", "edit_puzzle", hunt_and_puzzle)
         )
     ]