Exemple #1
0
def is_valid(user_email: str, project_domain: str) -> bool:
    """Verify whether a user has an active subscription to a project.

    Args:
        user_email: The user's email address.
        project_domain: The project's domain name.

    Returns:
        True if the user has an activate subscription to the project.

    """
    pk = db.PartitionKey(ent.User, user_email)
    sk_user = db.SortKey(ent.Sub, project_domain)
    sk_group = db.SortKey(ent.GroupSub, project_domain)

    pk_cond = cond.Key('PK').eq(str(pk))
    sk_cond = cond.Key('SK').eq(str(sk_user)) | cond.Key('SK').eq(str(sk_group))  # noqa 501
    key_cond = pk_cond & sk_cond

    query_arg = db.QueryArg(key_cond, attributes=['IsActive'])
    subs = get_table().query(query_arg)
    for s in subs:
        if s['IsActive']:
            return True
    else:
        return False
Exemple #2
0
def match_primary(table_name: str,
                  primary_key: str,
                  primary_key_val: str or int,
                  index_name: str = None,
                  query_index=False) -> Dict:
    """
    Description: Use to make a query with primary key equal to value
    Link: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.query

    :param table_name: table to search through
    :param primary_key: the primary key of the table
    :param primary_key_val: the value of the primary key to search for
    :return: dict
    """

    table = dynamodb.Table(table_name)
    if not query_index:
        response = table.query(TableName=table_name,
                               KeyConditionExpression=conditions.Key(
                                   primary_key).eq(primary_key_val))
    else:
        response = table.query(IndexName=index_name,
                               KeyConditionExpression=conditions.Key(
                                   primary_key).eq(primary_key_val))
    logger.debug({
        "dynamodb_client": "query_matchPrimary_search_betweenSort",
        "success": True,
        "table_name": table_name,
    })
    return response
Exemple #3
0
 def iter_resources(self, account_id, resource_type=None):
     expr = conditions.Key('AccountId').eq(account_id)
     if resource_type == "security-group":
         expr = expr & conditions.Key('ResourceId').between('sg-', 'vpc-')
     elif resource_type == "vpc":
         expr = expr & conditions.Key('ResourceId').begins_with('vpc-')
     results = self.table.scan(FilterExpression=expr)
     return results['Items']
Exemple #4
0
 def get_order(self, restaurant, date):
     response = self.table.query(
         KeyConditionExpression=conditions.Key('restaurant').eq(restaurant)
         & conditions.Key('date').eq(date))
     if response['Items']:
         order = response['Items'][0]
         return order, "Success"
     else:
         return None, "No order."
Exemple #5
0
 def get_dish_price(self, restaurant, dishname):
     response = self.table.query(
         KeyConditionExpression=conditions.Key('restaurant').eq(restaurant)
                                & conditions.Key('dishname').eq(dishname)
     )
     if response['Items']:
         return response['Items'][0], "Success"
     else:
         return None, "No dish"
def get_books_by_author(author_name: str) -> typing.List[dict]:

    table = boto3.resource("dynamodb").Table(TABLE_NAME)

    response = table.query(
        KeyConditionExpression=conditions.Key("PK").eq(f"AUTHOR#{author_name}") \
            & conditions.Key("SK").begins_with("ISBN")
    )

    return response["Items"]
def get_book_by_isbn(isbn: str) -> dict:

    table = boto3.resource("dynamodb").Table(TABLE_NAME)

    response = table.query(
        KeyConditionExpression=conditions.Key("GSI1PK").eq(f"ISBN#{isbn}") \
            & conditions.Key("GSI1SK").eq(f"ISBN#{isbn}"),
        IndexName="GSI1"
    )

    return response["Items"][0]
def comment_delete(event, context):
    comments_table = DYNAMODB.Table('comments')
    deleted_comment = comments_table.delete_item(
        Key={'id': event['pathParameters']['id']},
        ReturnValues='ALL_OLD').get('Attributes')

    if not deleted_comment:
        return redirect('/dev/')

    # Find child comments
    submission_comments = comments_table.scan(
        FilterExpression=conditions.Key('submission_id')
        .eq(deleted_comment['submission_id']),
        IndexName='CommentSubmissionIndex')['Items']

    by_parent = {}
    for comment in submission_comments:
        by_parent.setdefault(comment.get('parent_id'), []).append(comment)

    queue = by_parent.get(deleted_comment['id'], [])
    with comments_table.batch_writer() as comments_batch:
        while queue:
            comment = queue.pop()
            if comment['id'] in by_parent:
                queue.extend(by_parent[comment['id']])
            comments_batch.delete_item(Key={'id': comment['id']})

    return redirect('/dev/submissions/{}'
                    .format(deleted_comment['submission_id']))
Exemple #9
0
def lambda_handler(event, context):

    encoded_image = event['base64Image']
    decoded_image = base64.b64decode(encoded_image)

    threshold = 80
    # user_id = 'dac'
    user_id = event['userId']
    print(user_id)
    key_name = user_id

    dynamodb = boto3.resource('dynamodb',
                              region_name=region,
                              aws_access_key_id=aws_id,
                              aws_secret_access_key=aws_key)
    table = dynamodb.Table(table_name)
    response = table.query(
        KeyConditionExpression=conditions.Key('user_id').eq(user_id))
    target_name = response['Items'][0]['image_url'].split("/")[4]

    source_face, matches = compare_faces(decoded_image, bucket_name,
                                         target_name, threshold, region)
    print(matches[0]['Similarity'])
    if int(matches[0]['Similarity']) > 80:
        data = {'tokenId': 'AD78hdjjuUIIdkklppPPp00dauyd78', 'expiration': 900}
        return data
    else:
        data = {'tokenId': '', 'expiration': 0}
        return data
def submission_show(event, context):
    body = """<h1>{title} (via <a href="communities/{community}">{community}</a>)</h1>
<p><a href="{url}">{url}</a></p>

<div>
  <a class="btn btn-primary btn-sm" href="comments/new?submission_id={submission_id}">Comment</a>
  <a class="btn btn-danger btn-sm" data-confirm="Are you sure?" data-method="delete" href="submissions/{submission_id}">Delete Submission</a>
</div>

{comments}
"""  # NOQA
    submission_id = event['pathParameters']['id']
    submission_table = DYNAMODB.Table('submissions')
    submission = submission_table.get_item(Key={'id': submission_id})['Item']

    comments_table = DYNAMODB.Table('comments')
    comments = comments_table.scan(
        FilterExpression=conditions.Key('submission_id')
        .eq(submission_id), IndexName='CommentSubmissionIndex')['Items']

    comments_body = ""
    if comments:
        comments_body = """<div>
  Comments:<br>
  {}
</div>
""".format('\n'.join([comment__render(comment)
                      for comment in treeify(comments)]))

    return response(body.format(
        comments=comments_body, community=submission['community'],
        submission_id=submission_id, title=submission['title'],
        url=submission['url']))
Exemple #11
0
    def _pull_down_new_db_entries(self, pref):

        logger.log.info('pulling down new entries')
        this_node_id = pref.get_node_id()
        node_db = nodedb.NodeDB(self.app_data_folder, this_node_id)
        event_table_resource = self.event_table.get_table_resource()

        for node_id in self.node_table.get_all_nodes():
            if node_id != this_node_id:
                most_recent_local = node_db.get_most_recent_entry(node_id)
                if most_recent_local:
                    most_recent_local_mivui = most_recent_local['mivui']
                else:
                    most_recent_local_mivui = 0
                logger.log.info('most_recent_local_mivui for : %s : %d' % (node_id, most_recent_local_mivui))

                query_response = event_table_resource.query(
                    KeyConditionExpression=conditions.Key('originator').eq(node_id) & conditions.Key('mivui').gt(most_recent_local_mivui)
                )
                for q in query_response['Items']:
                    logger.log.info('query_response : %s' % str(q))
                    mtime = q['mtime']
                    if mtime:
                        mtime = maya.parse(mtime).datetime()
                    size = q['size']
                    if size:
                        size = int(size)
                    logger.log.info('new_db_entry : %s' % str(q))
                    node_db.update(int(q['mivui']), q['originator'], int(q['event_type']), int(q['detection']), q['file_path'],
                                   q['src_path'], size, q['file_hash'], mtime, False)
Exemple #12
0
    def query_prefix(self,
                     pk: PartitionKey,
                     sk: PrefixSortKey,
                     global_index: Optional[GlobalSecondaryIndex] = None,
                     attributes: Optional[List[str]] = None,
                     consistent: bool = False,
                     limit: Optional[int] = None) -> List[ItemResult]:
        """Fetch a items from the table based on a sort key prefix.

        Doesn't support pagination.

        Args:
            pk: The partition key.
            sk: The sort key prefix.
            global_index: The global secondary index to query. Defaults to the
                primary index.
            attributes: The attributes to get. Defaults to
                `[self.primary_index.sort_key]` if no `global_index` is
                provided and `[global_index.sort_key]` if it is provided.
            consistent: Whether the read is strongly consistent or not.
            limit: The maximum number of items to fetch. Defaults to 1000.

        Returns:
            The requested items with the `PK` and `SK` prefixes stripped.

        Raises:
            dokklib_db.DatabaseError if there was an error querying DynamoDB.

        """
        if global_index:
            pk_name = global_index.partition_key
            sk_name = global_index.sort_key
        else:
            pk_name = self.primary_index.partition_key
            sk_name = self.primary_index.sort_key

        if not attributes:
            attributes = [sk_name]

        key_condition = cond.Key(pk_name).eq(str(pk)) & \
            cond.Key(sk_name).begins_with(str(sk))
        query_arg = QueryArg(key_condition,
                             global_index=global_index,
                             attributes=attributes,
                             consistent=consistent,
                             limit=limit)
        return self._query(query_arg)
Exemple #13
0
 def get_device_id(self, intent_name: str) -> ObjectId:
     condition = conditions.Key("intent_id").eq(str(intent_name))
     result = self.table.query(IndexName="by_intent_id",
                               KeyConditionExpression=condition)
     if result["ResponseMetadata"]["HTTPStatusCode"] not in range(200, 300):
         raise AlexaRepositoryError(
             "error occurred when retrieving device_id")
     return ObjectId(result["Items"][0]["device_id"])
Exemple #14
0
 def get_user(self, userid):
     response = self.table.query(
         KeyConditionExpression=conditions.Key('userid').eq(userid))
     if response['Items']:
         user = response['Items'][0]
         return user
     else:
         return None
def community_delete(event, context):
    community = event['pathParameters']['name']
    DYNAMODB.Table('communities').delete_item(Key={'title': community})
    comments_table = DYNAMODB.Table('comments')
    submissions_table = DYNAMODB.Table('submissions')
    with submissions_table.batch_writer() as submissions_batch:
        with comments_table.batch_writer() as comments_batch:
            for submission in submissions_table.scan(
                    FilterExpression=conditions.Key('community').eq(community),
                    IndexName='SubmissionCommunityIndex')['Items']:
                submissions_batch.delete_item(Key={'id': submission['id']})
                for comment in comments_table.scan(
                        FilterExpression=conditions.Key('submission_id')
                        .eq(submission['id']),
                        IndexName='CommentSubmissionIndex')['Items']:
                    comments_batch.delete_item(Key={'id': comment['id']})
    return redirect('/dev/')
def get_all_author_information(author_name: str) -> typing.List[dict]:

    table = boto3.resource("dynamodb").Table(TABLE_NAME)

    response = table.query(
        KeyConditionExpression=conditions.Key("PK").eq(f"AUTHOR#{author_name}")
    )

    return response["Items"]
 def find_location(self, restaurant):
     response = self.table.query(
         KeyConditionExpression=conditions.Key('userid').eq(restaurant)
     )
     if response['Items']:
         user = response['Items'][0]
         return user
     else:
         return None
Exemple #18
0
 def get_user_order(self, userid):
     response = self.user_table.query(
         KeyConditionExpression=conditions.Key('user').eq(userid))
     result = []
     for user_order in response['Items']:
         order, message = self.get_order(user_order['restaurant'],
                                         user_order['date'])
         if order:
             result.append(order)
     return result
Exemple #19
0
    def get_dialog(self,
                   intent_name: str,
                   iot_err: int = 0,
                   locale: str = "es-ES") -> Dialog:
        condition = conditions.Key("intent_id").eq(
            intent_name) & conditions.Key("iot_err").eq(iot_err)
        result = self.table.query(IndexName="by_intent_id_and_iot_err",
                                  KeyConditionExpression=condition)

        if result["ResponseMetadata"]["HTTPStatusCode"] not in range(200, 300):
            raise AlexaRepositoryError(
                "error occurred when retrieving dialog details")

        if len(result["Items"]) > 1:
            for item in result["Items"]:
                if item["locale"] == locale:
                    return self._hydrate_record(item)

        return self._hydrate_record(result["Items"][0])
def submission_delete(event, context):
    submission_id = event['pathParameters']['id']
    DYNAMODB.Table('submissions').delete_item(Key={'id': submission_id})
    comments_table = DYNAMODB.Table('comments')
    with comments_table.batch_writer() as batch:
        for item in comments_table.scan(
                FilterExpression=conditions.Key('submission_id')
                .eq(submission_id),
                IndexName='CommentSubmissionIndex')['Items']:
            batch.delete_item(Key={'id': item['id']})
    return redirect('/dev/')
Exemple #21
0
    def add_dish(self, restaurant, dishname, price):
        response = self.table.query(
            KeyConditionExpression=conditions.Key('restaurant').eq(restaurant)
                                   & conditions.Key('dishname').eq(dishname)
        )

        if response['Items']:
            return False, restaurant + " " + dishname + " already exist."

        # print("[" + price + "]")
        response = self.table.put_item(
            Item={
                'restaurant': restaurant,
                'dishname': dishname,
                'price': price
            }
        )
        if response:
            return True, "Success"
        else:
            return False, "Insert fail"
Exemple #22
0
    def position_exists(self, position: int) -> Optional[ObjectId]:
        condition = conditions.Key("position").eq(position)
        result = self.table.query(IndexName="by_position",
                                  KeyConditionExpression=condition)

        if result["ResponseMetadata"]["HTTPStatusCode"] not in range(200, 300):
            raise RepositoryError(
                "error occurred when retrieving device details")

        if result["Items"]:
            return ObjectId(result["Items"][0]["device_id"])
        return None
Exemple #23
0
    def get(self, device_id: ObjectId) -> Device:
        condition = conditions.Key("device_id").eq(str(device_id))
        result = self.table.query(KeyConditionExpression=condition)

        if result["ResponseMetadata"]["HTTPStatusCode"] not in range(200, 300):
            raise RepositoryError(
                "error occurred when retrieving device details")

        if not result["Count"]:
            raise RecordNotFound(f"Device with id {device_id} was not found")

        return self._hydrate_device(result["Items"][0])
Exemple #24
0
 def get_user(self, userid):
     response = self.table.query(
         KeyConditionExpression=conditions.Key('userid').eq(userid))
     if response['Items']:
         user = response['Items'][0]
         user['salt'] = int(user['salt'])
         user['sweet'] = int(user['sweet'])
         user['sour'] = int(user['sour'])
         user['spicy'] = int(user['spicy'])
         return user
     else:
         return None
Exemple #25
0
    def get(self, query: dict) -> dict:
        try:
            response = self.table.query(IndexName=query['index'],
                                        KeyConditionExpression=conditions.Key(
                                            query['key']).eq(query['value']))

        except exceptions.ClientError as error:
            print(
                f'Error when getting Dynamo Item using GSI on Table {self.dynamo_table}',
                error)

        else:
            return response['Items']
Exemple #26
0
 def get_dish(self, restaurant):
     response = self.table.query(
         KeyConditionExpression=conditions.Key('restaurant').eq(restaurant)
     )
     dishes = []
     if response['Items']:
         for i in response['Items']:
             dishes.append([i['dishname'], str(i['price'])])
         return dishes
     else:
         # print(response)
         # todo what to do if no dishes?
         return []
Exemple #27
0
def match_primary_between_sort(table_name, primary_key, primary_key_val,
                               sort_key, sort_key_val_low,
                               sort_key_val_high) -> Dict:
    """
    Description: Use to make a query with primary key equal to value and sort key in between specific values
    Link: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.query

    :param table_name: table to search through
    :param primary_key: the primary key of the table
    :param primary_key_val: the value of the primary key to search for
    :param sort_key: the sort key of the table
    :param sort_key_val_low: the low end of the search range
    :param sort_key_val_high: the high end of the search range
    :return: dict
    """
    try:
        table = dynamodb.Table(table_name)
        response = table.query(
            TableName=table_name,
            KeyConditionExpression=conditions.Key(primary_key).eq(
                primary_key_val)
            & conditions.Key(sort_key).between(sort_key_val_low,
                                               sort_key_val_high),
        )
        logger.debug({
            "dynamodb_client": "query_matchPrimary_search_betweenSort",
            "success": True,
            "table_name": table_name,
        })
        return response
    except ClientError as e:
        err_message = {
            "dynamodb_client": "query_matchPrimary_search_betweenSort",
            "success": False,
            "msg": str(e.args[0]),
        }
        logger.error(err_message)
        raise Exception(err_message)
Exemple #28
0
def make_key_conditions(list_of_keys_dicts):
    """
    Key: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/dynamodb.html#boto3.dynamodb.conditions.Key

    Create a ke expressions concatenated string to be used in dynamodb query.

    :param list_of_keys_dicts: List of MAX TWO dicts, first entry is primary key, second is sort key (key link) ->
    example_entry: {'key':<key>, 'value':<value>, 'operator':<operator>}
    If value requires range, use a tuple for values (<HIGH_VALUE>, <LOW_VALUE>)
    :return: concatenated key condition
    """
    key_conditions: Any = None
    first = True
    for key_dict in list_of_keys_dicts:
        if first:
            key_conditions = _add_condition(conditions.Key(key_dict['key']),
                                            key_dict['operator'],
                                            key_dict['value'])
            first = False
        else:
            key_conditions = key_conditions & _add_condition(
                conditions.Key(key_dict['key']), key_dict['operator'],
                key_dict['value'])
    return key_conditions
def community_show(event, context):
    body = """<h1>{community}</h1>

{listing}

<a class="btn btn-primary" href="submissions/new?community={community}">New Submission</a>
<a class="btn btn-danger" data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="communities/{community}">Delete Community</a>
"""  # NOQA
    community = event['pathParameters']['name']
    table = DYNAMODB.Table('submissions')
    items = table.scan(FilterExpression=conditions.Key('community')
                       .eq(community),
                       IndexName='SubmissionCommunityIndex')['Items']
    return response(body.format(community=community,
                                listing=listing(reversed(items))))
def listing_row(submission):
    comments_table = DYNAMODB.Table('comments')
    results = comments_table.scan(
        FilterExpression=conditions.Key('submission_id')
        .eq(submission['id']), IndexName='CommentSubmissionIndex')
    comment_count = results['Count']
    if comment_count == 1:
        comments = '1 comment'
    elif 'LastEvaluatedKey' in results:
        comments = '{}+ comments'.format(comment_count)
    else:
        comments = '{} comments'.format(comment_count)
    row = """<tr>
  <td><a href="{url}">{title}</a></td>
  <td>{url}</td>
  <td><a href="communities/{community}">{community}</a></td>
  <td><a class="btn btn-primary btn-sm" href="submissions/{id}">{comments}</td>
</tr>
"""  # NOQA
    return row.format(comments=comments, **submission)