]> git.cworth.org Git - turbot/blobdiff - turbot/interaction.py
Add a "make flake" recipe and clean up some style bugs flake8 noticed
[turbot] / turbot / interaction.py
index a929ac553fef804c6312dcb10dc107f58fd386d3..343eca623461b74da4fbe8d8f0b9a5001b59a58a 100644 (file)
@@ -95,32 +95,48 @@ def new_hunt_submission(turb, payload, metadata):
                                 "Hunt ID can only contain lowercase letters, "
                                 + "numbers, and underscores")
 
-    # Check to see if the hunts table exists
-    hunts_table = turb.db.Table("hunts")
-
+    # Check to see if the turbot table exists
     try:
-        exists = hunts_table.table_status in ("CREATING", "UPDATING",
-                                              "ACTIVE")
+        exists = turb.table.table_status in ("CREATING", "UPDATING",
+                                             "ACTIVE")
     except ClientError:
         exists = False
 
-    # Create the hunts table if necessary.
+    # Create the turbot table if necessary.
     if not exists:
-        hunts_table = turb.db.create_table(
-            TableName='hunts',
+        turb.table = turb.db.create_table(
+            TableName='turbot',
             KeySchema=[
-                {'AttributeName': 'channel_id', 'KeyType': 'HASH'},
+                {'AttributeName': 'PK', 'KeyType': 'HASH'},
+                {'AttributeName': 'SK', 'KeyType': 'RANGE'},
             ],
             AttributeDefinitions=[
+                {'AttributeName': 'PK', 'AttributeType': 'S'},
+                {'AttributeName': 'SK', 'AttributeType': 'S'},
                 {'AttributeName': 'channel_id', 'AttributeType': 'S'},
             ],
             ProvisionedThroughput={
                 'ReadCapacityUnits': 5,
                 'WriteCapacityUnits': 5
-            }
+            },
+            GlobalSecondaryIndexes=[
+                {
+                    'IndexName': 'channel_id_index',
+                    'KeySchema': [
+                        {'AttributeName': 'channel_id', 'KeyType': 'HASH'}
+                    ],
+                    'Projection': {
+                        'ProjectionType': 'ALL'
+                    },
+                    'ProvisionedThroughput': {
+                        'ReadCapacityUnits': 5,
+                        'WriteCapacityUnits': 5
+                    }
+                }
+            ]
         )
         return submission_error("hunt_id",
-                                "Still bootstrapping hunts table. Try again.")
+                                "Still bootstrapping turbot table. Try again.")
 
     # Create a channel for the hunt
     try:
@@ -135,12 +151,13 @@ def new_hunt_submission(turb, payload, metadata):
     # Insert the newly-created hunt into the database
     # (leaving it as non-active for now until the channel-created handler
     #  finishes fixing it up with a sheet and a companion table)
-    hunts_table.put_item(
+    turb.table.put_item(
         Item={
-            'channel_id': channel_id,
+            "PK": "hunt-{}".format(hunt_id),
+            "SK": "hunt-{}".format(hunt_id),
+            "channel_id": channel_id,
             "active": False,
             "name": name,
-            "hunt_id": hunt_id,
             "url": url
         }
     )
@@ -242,7 +259,7 @@ def channel_is_hunt(turb, channel_id):
     Returns a dict (filled with database entries) if there is a hunt
     for this channel, otherwise returns None."""
 
-    return get_table_item(turb, "hunts", 'channel_id', channel_id)
+    return get_table_item(turb, "channel_id_index", 'channel_id', channel_id)
 
 def find_hunt_for_hunt_id(turb, hunt_id):
     """Given a hunt ID find the database for for that hunt
@@ -252,18 +269,14 @@ def find_hunt_for_hunt_id(turb, hunt_id):
     (channel_id, active, hunt_id, name, url, sheet_url, etc.).
 
     """
-    hunts_table = turb.db.Table("hunts")
+    turbot_table = turb.db.Table("turbot")
 
-    response = hunts_table.scan(
-        FilterExpression='hunt_id = :hunt_id',
-        ExpressionAttributeValues={':hunt_id': hunt_id}
-    )
+    response = turbot_table.get_item(Key={'PK': 'hunt-{}'.format(hunt_id)})
 
-    if 'Items' in response and len(response['Items']):
-        item = response['Items'][0]
-        return item
-
-    return None
+    if 'Item' in response:
+        return response['Item']
+    else:
+        return None
 
 def find_hunt_for_channel(turb, channel_id, channel_name):
     """Given a channel ID/name find the id/name of the hunt for this channel
@@ -469,7 +482,8 @@ def solved(turb, body, args):
     set_channel_topic(turb, puzzle)
 
     # And rename the sheet to prefix with "SOLVED: "
-    turbot.sheets.renameSheet(turb, puzzle['sheet_url'], 'SOLVED: ' + puzzle['name'])
+    turbot.sheets.renameSheet(turb, puzzle['sheet_url'],
+                              'SOLVED: ' + puzzle['name'])
 
     # Finally, rename the Slack channel to add the suffix '-solved'
     channel_name = "{}-{}-solved".format(