コード例 #1
0
def endpoint(event, lambda_context):
    client = boto3.client('dynamodb', region_name='us-east-1')

    response = client.scan(
        TableName=os.environ.get('IDEAS_TABLE_NAME', 'dailyidea-ideas-dev'),
        FilterExpression="attribute_not_exists(strippedContent)",
    )

    num_updated = 0

    for idea in response['Items']:
        content = idea.get('content', {}).get('S')

        if not content:
            continue

        num_updated += 1
        stripped_content = strip_html(content)
        client.update_item(
            TableName=os.environ.get('IDEAS_TABLE_NAME', 'dailyidea-ideas-dev'),
            Key={
                'ideaId': {'S': idea['ideaId']['S']},
                'userId': {'S': idea['userId']['S']}
            },
            UpdateExpression='SET strippedContent = :strippedContent',
            ExpressionAttributeValues={
                ':strippedContent': {'S': stripped_content}
            }
        )

    print("UPDATED", num_updated)

    return {}
コード例 #2
0
def endpoint(event, lambda_context):
    ctx = event.get('ctx')
    arguments = ctx.get('arguments')
    title = arguments.get('title')
    slug = slugify(title)
    content = sanitize_idea_content(arguments.get('content', None))
    stripped_content = None
    if content:
        stripped_content = strip_html(content)

    idea_id = arguments.get('ideaId', None)
    idea_owner_id = arguments.get('ideaOwnerId', None)
    tags = arguments.get('tags', list())
    new_image_attachments = arguments.get('imageAttachments', list())
    new_file_attachments = arguments.get('fileAttachments', list())
    editor_id = ctx.get('identity').get('username')
    if editor_id != idea_owner_id:
        return {
            'result': {
                'ok': False,
                'error': 'You do not have permission to edit this idea'
            }
        }
    if len(tags) > 100:
        return {'result': {'ok': False, 'error': 'Too much tags'}}
    client = boto3.client('dynamodb', region_name='us-east-1')
    s3 = boto3.client('s3')
    idea = client.get_item(TableName=os.environ.get('IDEAS_TABLE_NAME'),
                           Key={
                               'ideaId': {
                                   "S": idea_id
                               },
                               'userId': {
                                   "S": idea_owner_id
                               }
                           })['Item']

    client.update_item(
        TableName=os.environ.get('IDEAS_TABLE_NAME'),
        Key={
            'ideaId': {
                "S": idea_id
            },
            'userId': {
                "S": idea_owner_id
            }
        },
        UpdateExpression=
        'SET title = :title, slug = :slug, strippedContent = :strippedContent, content = :content, updatedDate = :updatedDate, fileAttachments=:fileAttachments, imageAttachments=:imageAttachments, previewImage=:previewImage',
        ExpressionAttributeValues={
            ":title": {
                "S": title
            },
            ":slug": {
                "S": slug
            },
            ":strippedContent": {
                "S": stripped_content
            } if stripped_content else {
                "NULL": True
            },
            ":content": {
                "S": content
            } if content else {
                "NULL": True
            },
            ":updatedDate": {
                "S": datetime.datetime.now().isoformat()
            },
            ":fileAttachments": {
                "SS": new_file_attachments
            } if len(new_file_attachments) else {
                "NULL": True
            },
            ":imageAttachments": {
                "SS": new_image_attachments
            } if len(new_image_attachments) else {
                "NULL": True
            },
            ":previewImage": {
                "S": new_image_attachments[0]
            } if len(new_image_attachments) else {
                "NULL": True
            },
        })
    current_file_attachments = idea['fileAttachments'].get('SS', [])
    for file_attachment in current_file_attachments:
        if file_attachment not in new_file_attachments:
            s3.delete_object(Bucket=os.environ.get('USER_UPLOADS_BUCKET'),
                             Key=file_attachment)
    current_idea_tags = list(
        map(
            lambda t: t['tag']['S'],
            client.query(TableName=os.environ.get('TAGS_TABLE_NAME'),
                         IndexName='ideaTags',
                         ProjectionExpression='tag',
                         KeyConditionExpression="ideaId = :ideaId",
                         ExpressionAttributeValues={":ideaId": {
                             "S": idea_id
                         }})['Items']))
    if len(current_idea_tags):
        prepared_tags = prepare_idea_tags_for_delete_request(
            current_idea_tags, idea_id)
        for tags_chunk in chunks(prepared_tags, BATCH_WRITE_CHUNK_SIZE):
            client.batch_write_item(
                RequestItems={os.environ.get('TAGS_TABLE_NAME'): tags_chunk})
    if len(tags):
        prepared_tags = prepare_idea_tags_for_put_request(
            tags, idea_owner_id, idea_id)
        for tags_chunk in chunks(prepared_tags, BATCH_WRITE_CHUNK_SIZE):
            client.batch_write_item(
                RequestItems={os.environ.get('TAGS_TABLE_NAME'): tags_chunk})

    return {'result': {'ok': True}, 'idea': {'ideaId': idea_id, 'slug': slug}}
コード例 #3
0
ファイル: create_idea.py プロジェクト: Watson-B/dailyidea.com
def endpoint(event, lambda_context):
    ctx = event.get('ctx')
    arguments = ctx.get('arguments')
    title = arguments.get('title')
    slug = slugify(title)
    content = sanitize_idea_content(arguments.get('content', None))
    stripped_content = None
    if content:
        stripped_content = strip_html(content)

    tags = arguments.get('tags', list())
    image_attachments = arguments.get('imageAttachments', list())
    file_attachments = arguments.get('fileAttachments', list())

    if len(tags) > 100:
        raise Exception('Too much tags')
    is_private = arguments.get('isPrivate', False)

    client = boto3.client('dynamodb', region_name='us-east-1')
    idea_id = str(uuid.uuid4())
    short_id = find_unique_short_id()
    creator_id = ctx.get('identity').get('username')

    creator_account = client.get_item(
        TableName=os.environ.get('USERS_TABLE_NAME'),
        Key={'userId': {
            "S": creator_id
        }},
    )['Item']
    creator_name_raw = creator_account.get('name', None)
    creator_slug = creator_account.get('slug').get('S')
    creator_avatar = creator_account.get('avatar').get(
        'S') if creator_account.get('avatar') else None
    creator_name = creator_name_raw.get(
        'S') if creator_name_raw else 'Anonymous User'

    client.put_item(TableName=os.environ.get('IDEAS_TABLE_NAME'),
                    Item={
                        'sortKey': {
                            "S": 'idea'
                        },
                        'ideaId': {
                            "S": idea_id
                        },
                        'shortId': {
                            "S": short_id
                        },
                        'userId': {
                            "S": creator_id
                        },
                        "title": {
                            "S": title
                        },
                        "slug": {
                            "S": slug
                        },
                        "content": {
                            "S": content
                        } if content else {
                            "NULL": True
                        },
                        "strippedContent": {
                            "S": stripped_content
                        } if stripped_content else {
                            "NULL": True
                        },
                        "ideaDate": {
                            "S": datetime.datetime.now().isoformat()
                        },
                        "createdDate": {
                            "S": datetime.datetime.now().isoformat()
                        },
                        "likesCount": {
                            "N": "0"
                        },
                        "savesCount": {
                            "N": "0"
                        },
                        "commentsCount": {
                            "N": "0"
                        },
                        "authorName": {
                            "S": creator_name
                        },
                        "authorSlug": {
                            "S": creator_slug
                        },
                        "authorAvatar": {
                            "S": creator_avatar
                        } if creator_avatar else {
                            "NULL": True
                        },
                        "visibility": {
                            "S": "PRIVATE" if is_private else "PUBLIC"
                        },
                        "imageAttachments": {
                            "SS": image_attachments
                        } if len(image_attachments) else {
                            "NULL": True
                        },
                        "fileAttachments": {
                            "SS": file_attachments
                        } if len(image_attachments) else {
                            "NULL": True
                        },
                        "previewImage": {
                            "S": image_attachments[0]
                        } if len(image_attachments) else {
                            "NULL": True
                        },
                    })
    client.update_item(
        TableName=os.environ.get('USERS_TABLE_NAME'),
        Key={'userId': {
            "S": creator_id
        }},
        UpdateExpression="ADD #ideascreatedfield :plusOne",
        ExpressionAttributeNames={"#ideascreatedfield": 'ideasCreated'},
        ExpressionAttributeValues={":plusOne": {
            "N": '1'
        }},
    )
    if len(tags):
        prepared_tags = prepare_idea_tags_for_put_request(
            tags, creator_id, idea_id)
        for tags_chunk in chunks(prepared_tags, BATCH_WRITE_CHUNK_SIZE):
            client.batch_write_item(
                RequestItems={os.environ.get('TAGS_TABLE_NAME'): tags_chunk})

    return {'ideaId': idea_id, 'shortId': short_id, 'slug': slug}