Ejemplo n.º 1
0
def create_board_handler(event, context):
    # verify claims
    claims = verify_jwt(event, context)
    if not claims: return Status.forbidden

    dynamodb = boto3.resource('dynamodb')
    user_boards_table = dynamodb.Table('user_boards')
    board_cards_table = dynamodb.Table('board_cards')
    user_id = claims['username']

    # generate a board_id, check if it already exists
    board_id = uuid.uuid4().hex
    for i in range(100):
        response = user_boards_table.scan(
            FilterExpression=Attr('board_id').eq(board_id))
        items = response['Items']
        if len(items) == 0:
            break

        board_id = uuid.uuid4().hex

    # create the board
    user_boards_table.put_item(
        Item={
            'user_id': user_id,
            'board_id': board_id,
            'board_name': 'Untitled',
            'updated_at': str(time.time())
        })

    # create board contents
    board_cards_table.put_item(
        Item={
            'board_id': board_id,
            'board_contents': {
                'columns':
                json.dumps([
                    {
                        'name': 'To Do',
                        'cards': []
                    },
                    {
                        'name': 'Doing',
                        'cards': []
                    },
                    {
                        'name': 'Done',
                        'cards': []
                    },
                ]),
                'name':
                'Untitled'
            }
        })
    connection_id, callback_url = get_current_connection_info(event)
    send_to_connection(Response.create_board_response, {'board_id': board_id},
                       connection_id, callback_url)

    return Status.OK
Ejemplo n.º 2
0
def get_boards_handler(event, context):
    # verify claims
    claims = verify_jwt(event, context)
    if not claims: return Status.forbidden
    user_id = claims['username']
    connection_id, callback_url = get_current_connection_info(event)
    push_boards_to_connection(user_id, connection_id, callback_url)

    return Status.OK
Ejemplo n.º 3
0
def get_board_contents_handler(event, context):
    # verify claims
    claims = verify_jwt(event, context)
    if not claims: return Status.forbidden

    dynamodb = boto3.resource('dynamodb')
    user_boards_table = dynamodb.Table('user_boards')
    board_cards_table = dynamodb.Table('board_cards')
    board_id = json.loads(event.get("body")).get('board_id')
    user_id = claims['username']

    # check that user owns the board
    response = user_boards_table.get_item(Key={
        'user_id': user_id,
        'board_id': board_id
    })
    item = response.get('Item')
    if not item: raise Exception

    # get board
    response = board_cards_table.get_item(Key={
        'board_id': board_id,
    })
    item = response.get('Item')
    if not item: raise Exception

    # send contents to user
    board_contents = item.get('board_contents')

    # load board document from json storage
    if board_contents.get('columns'):
        board_contents['columns'] = json.loads(board_contents.get('columns'))

    data = {'board_contents': board_contents}
    connection_id, callback_url = get_current_connection_info(event)
    send_to_connection(
        event.get("requestContext").get("routeKey"), data, connection_id,
        callback_url)
    return Status.OK
Ejemplo n.º 4
0
def delete_board_handler(event, context):
    # verify claims
    claims = verify_jwt(event, context)
    if not claims: return Status.forbidden

    dynamodb = boto3.resource('dynamodb')
    user_boards_table = dynamodb.Table('user_boards')
    board_cards_table = dynamodb.Table('board_cards')
    board_id = json.loads(event.get("body")).get('board_id')
    user_id = claims['username']

    # check that user owns the board
    response = user_boards_table.get_item(Key={
        'user_id': user_id,
        'board_id': board_id
    })
    item = response.get('Item')
    if not item: raise Exception

    # delete board for this user
    user_boards_table.delete_item(Key={
        'user_id': user_id,
        'board_id': board_id
    })

    # if no one else owns the board delete the contents
    response = user_boards_table.scan(
        FilterExpression=Attr('board_id').eq(board_id))
    items = response['Items']
    if len(items) == 0:
        # delete the board contents
        board_cards_table.delete_item(Key={'board_id': board_id})
    # send response
    connection_id, callback_url = get_current_connection_info(event)
    send_to_connection(Response.delete_board_response, {'board_id': board_id},
                       connection_id, callback_url)
    return Status.OK
Ejemplo n.º 5
0
def handler(event, context):
    connection_id, callback_url = get_current_connection_info(event)
    send_to_connection('pong', 'pong', connection_id, callback_url)
    return Status.OK