Skip to content

Instantly share code, notes, and snippets.

@FlightOfStairs
Created February 25, 2016 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlightOfStairs/1c042c239221730e3fa3 to your computer and use it in GitHub Desktop.
Save FlightOfStairs/1c042c239221730e3fa3 to your computer and use it in GitHub Desktop.
from __future__ import print_function
from boto3.dynamodb.conditions import Key, Attr
import boto3
import json
def lambda_handler(event, context):
instance = event['query']['instance']
dynamo = boto3.resource('dynamodb').Table('draft')
items = dynamo.scan(FilterExpression=Attr('instance').eq(instance))['Items']
if event['query']['operation'] == "list":
incomplete = [item for item in items if "content" not in item]
complete = [item for item in items if "content" in item]
if not incomplete:
return {"state": "complete", "entries": { key: complete[key] for key in ["email", "content"] }}
if incomplete:
return {"state": "incomplete", "emails": [x['email'] for x in incomplete]}
if event['query']['operation'] == "create":
if items:
return {"state": "error", "message": "An instance with that name already exists."}
emails = event["params"]["emails"]
if not emails:
return {"state": "error", "message": "No email addresses provided."}
if len(emails) > 20:
return {"state": "error", "message": "Number of emails limited to 20."}
for email in emails:
# todo email tokens
dynamo.put_item(
Item={
'id': instance + "/" + email,
'instance': instance,
'updatetoken': 'testtoken'
}
)
return {"state": "success", "message": "Created draft."}
if event['query']['operation'] == "complete":
content = event["params"]["content"]
email = event['query']['email']
token = event['query']['token']
if not content:
return {"state": "error", "message": "No content provided."}
dynamo.update_item(
Key={ 'id': instance + "/" + email, 'instance': instance },
UpdateExpression='SET content = :val1',
ConditionExpression='updatetoken = :t',
ExpressionAttributeValues={ ':val1': content, ':t': token }
)
return {"state": "success", "message": "Updated content for " + email + ": " + content}
raise ValueError("Operation not found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment