def handler(event, context):
    if 'body' not in event:
        return default_resp(ValidationError("Missing body."))

    try:
        body_from_json = json.loads(event['body'])
    except ValueError:
        return default_resp(ValidationError("Unable to parse request"))

    try:
        ret_body = validated_body(body_from_json, SUBMIT_FIELDS)
    except ValidationError, e:
        return default_resp(e)
def handler(event, context):
    if 'body' not in event:
        return default_resp(ValidationError("Missing body."))

    try:
        body_from_json = json.loads(event['body'])
    except (ValueError, TypeError) as e:
        LOG.exception("Unable to parse body: {}".format(e.message))
        return default_resp(ValidationError("Unable to parse request"))

    try:
        ret_body = validated_body(body_from_json, UPDATE_FIELDS)
        if ret_body[update.action] not in ACTION_FIELDS:
            raise ValidationError("{} is not a valid action.".format(
                ret_body[update.action]))
    except ValidationError, e:
        return default_resp(e)
def get_item_from_id(key_id):
    dynamo_submissions = boto3.resource('dynamodb').Table(
        SUBMISSIONS_TABLE_NAME)
    try:
        q = dynamo_submissions.get_item(Key={'id': key_id})
    except Exception, e:
        LOG.exception("Error querying the database for {}: {}".format(
            key_id, e.message))
        raise ValidationError("Database error.")
def add_updates_for_action(item):
    dynamo_updates = boto3.resource('dynamodb').Table(UPDATES_TABLE_NAME)
    t = int(time.time() * 100)
    try:
        item_to_put = merge_two_dicts(item, {'create_time': t})
        dynamo_updates.put_item(Item=item_to_put)
    except Exception, e:
        LOG.exception("Error updating the {} table with {} : {} ".format(
            UPDATES_TABLE_NAME, item_to_put, e.message))
        raise ValidationError("Database error.")
def delete_item(key_id):
    dynamo_submissions = boto3.resource('dynamodb').Table(
        SUBMISSIONS_TABLE_NAME)
    try:
        dynamo_submissions.delete_item(Key={'id': key_id})
    except Exception, e:
        LOG.exception(
            "Error deleting from the {} table with key={} : {} ".format(
                SUBMISSIONS_TABLE_NAME, key_id, e.message))
        raise ValidationError("Database error.")
def increment_action(key_id, action):
    dynamo_submissions = boto3.resource('dynamodb').Table(
        SUBMISSIONS_TABLE_NAME)
    t = int(time.time() * 100)
    try:
        q = dynamo_submissions.update_item(
            Key={'id': key_id},
            UpdateExpression="ADD {} :val SET update_time = :ut".format(
                action),
            ExpressionAttributeValues={
                ':ut': t,
                ':val': decimal.Decimal(1)
            },
            ReturnValues="UPDATED_NEW")
    except Exception, e:
        LOG.exception(
            "Error updating the database for key {} action {} : {}".format(
                key_id, action, e.message))
        raise ValidationError("Database error.")
LOG = logging.getLogger()
LOG.setLevel(logging.WARN)


def get_item_from_id(key_id):
    dynamo_submissions = boto3.resource('dynamodb').Table(
        SUBMISSIONS_TABLE_NAME)
    try:
        q = dynamo_submissions.get_item(Key={'id': key_id})
    except Exception, e:
        LOG.exception("Error querying the database for {}: {}".format(
            key_id, e.message))
        raise ValidationError("Database error.")
    if 'Item' not in q:
        raise ValidationError("Unable to find id: {}".format(key_id))
    return q['Item']


def increment_action(key_id, action):
    dynamo_submissions = boto3.resource('dynamodb').Table(
        SUBMISSIONS_TABLE_NAME)
    t = int(time.time() * 100)
    try:
        q = dynamo_submissions.update_item(
            Key={'id': key_id},
            UpdateExpression="ADD {} :val SET update_time = :ut".format(
                action),
            ExpressionAttributeValues={
                ':ut': t,
                ':val': decimal.Decimal(1)