예제 #1
0
    def test_reset(self):
        a = Attr("myattr")
        self.assert_condition_expression_build(a.eq("foo"), "#n0 = :v0", {"#n0": "myattr"}, {":v0": "foo"})

        self.assert_condition_expression_build(a.eq("foo"), "#n1 = :v1", {"#n1": "myattr"}, {":v1": "foo"})

        self.builder.reset()
        self.assert_condition_expression_build(a.eq("foo"), "#n0 = :v0", {"#n0": "myattr"}, {":v0": "foo"})
예제 #2
0
 def test_build_or(self):
     a = Attr("myattr")
     a2 = Attr("myattr2")
     self.assert_condition_expression_build(
         a.eq("foo") | a2.eq("bar"),
         "(#n0 = :v0 OR #n1 = :v1)",
         {"#n0": "myattr", "#n1": "myattr2"},
         {":v0": "foo", ":v1": "bar"},
     )
예제 #3
0
    def test_reset(self):
        a = Attr('myattr')
        self.assert_condition_expression_build(
            a.eq('foo'), '#n0 = :v0', {'#n0': 'myattr'}, {':v0': 'foo'})

        self.assert_condition_expression_build(
            a.eq('foo'), '#n1 = :v1', {'#n1': 'myattr'}, {':v1': 'foo'})

        self.builder.reset()
        self.assert_condition_expression_build(
            a.eq('foo'), '#n0 = :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #4
0
class TestA(TestK):
    def setUp(self):
        self.attr = Attr("mykey")
        self.attr2 = Attr("myotherkey")
        self.value = "foo"
        self.value2 = "foo2"

    def test_ne(self):
        self.assertEqual(self.attr.ne(self.value), NotEquals(self.attr, self.value))

    def test_is_in(self):
        self.assertEqual(self.attr.is_in([self.value]), In(self.attr, [self.value]))

    def test_exists(self):
        self.assertEqual(self.attr.exists(), AttributeExists(self.attr))

    def test_not_exists(self):
        self.assertEqual(self.attr.not_exists(), AttributeNotExists(self.attr))

    def test_contains(self):
        self.assertEqual(self.attr.contains(self.value), Contains(self.attr, self.value))

    def test_size(self):
        self.assertEqual(self.attr.size(), Size(self.attr))

    def test_attribute_type(self):
        self.assertEqual(self.attr.attribute_type(self.value), AttributeType(self.attr, self.value))
예제 #5
0
 def test_build_double_nested_and_or(self):
     a = Attr('myattr')
     a2 = Attr('myattr2')
     self.assert_condition_expression_build(
         (a.eq('foo') & a2.eq('foo2')) | (a.eq('bar') & a2.eq('bar2')),
         '((#n0 = :v0 AND #n1 = :v1) OR (#n2 = :v2 AND #n3 = :v3))',
         {'#n0': 'myattr', '#n1': 'myattr2', '#n2': 'myattr',
          '#n3': 'myattr2'},
         {':v0': 'foo', ':v1': 'foo2', ':v2': 'bar', ':v3': 'bar2'})
예제 #6
0
 def test_build_double_nested_and_or(self):
     a = Attr("myattr")
     a2 = Attr("myattr2")
     self.assert_condition_expression_build(
         (a.eq("foo") & a2.eq("foo2")) | (a.eq("bar") & a2.eq("bar2")),
         "((#n0 = :v0 AND #n1 = :v1) OR (#n2 = :v2 AND #n3 = :v3))",
         {"#n0": "myattr", "#n1": "myattr2", "#n2": "myattr", "#n3": "myattr2"},
         {":v0": "foo", ":v1": "foo2", ":v2": "bar", ":v3": "bar2"},
     )
예제 #7
0
 def test_build_attr_list(self):
     a = Attr("MyList[0]")
     self.assert_condition_expression_build(a.eq("foo"), "#n0[0] = :v0", {"#n0": "MyList"}, {":v0": "foo"})
예제 #8
0
def grab_jobs():
    table = dynamodb.Table('jobs')
    response = table.scan(FilterExpression=Attr('company').eq("ObjectRocket"))
    return response['Items']
예제 #9
0
 def test_build_attr_map(self):
     a = Attr('MyMap.MyKey')
     self.assert_condition_expression_build(
         a.eq('foo'), '#n0.#n1 = :v0', {'#n0': 'MyMap', '#n1': 'MyKey'},
         {':v0': 'foo'})
예제 #10
0
 def test_build_or(self):
     a = Attr('myattr')
     a2 = Attr('myattr2')
     self.assert_condition_expression_build(
         a.eq('foo') | a2.eq('bar'), '(#n0 = :v0 OR #n1 = :v1)',
         {'#n0': 'myattr', '#n1': 'myattr2'}, {':v0': 'foo', ':v1': 'bar'})
예제 #11
0
 def test_build_contains(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.contains('foo'), 'contains(#n0, :v0)',
         {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #12
0
 def test_build_expression_between(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.between('foo', 'foo2'), '#n0 BETWEEN :v0 AND :v1',
         {'#n0': 'myattr'}, {':v0': 'foo', ':v1': 'foo2'})
예제 #13
0
 def setUp(self):
     self.attr = Attr('mykey')
     self.attr2 = Attr('myotherkey')
     self.value = 'foo'
     self.value2 = 'foo2'
예제 #14
0
def hasItem(code,name):
    global dynamodb
    table = dynamodb.Table('Item')
    response = table.scan(FilterExpression=Key('code').eq(code)&Attr('item').contains(name))
    return response['Items']>0
예제 #15
0
def getDynamoDBProduct(category):
    global dynamodb
    table = dynamodb.Table('product')
    response = table.scan(FilterExpression = Attr('category_id').eq(category))
    return response['Items']
    def get_purchase_orders_by_order_date_range(self,
                                                min_order_date,
                                                max_order_date=None,
                                                supplier_id=None,
                                                distributors=[]):
        obj_type = 'purchase-orders'

        min_order_date = maya.parse(min_order_date.split('T')[0]).epoch

        if max_order_date:
            max_order_date = maya.parse(max_order_date.split('T')[0]).epoch

        if supplier_id:
            if max_order_date:
                query = {
                    'KeyConditionExpression':
                    Key('supplier_id').eq(supplier_id)
                    & Key('obj_type').eq(obj_type),
                    'FilterExpression':
                    Attr('latest').eq(True) & Attr('active').eq(True) &
                    Attr('order_date').between(min_order_date, max_order_date),
                    'IndexName':
                    'by_supplier_id_and_obj_type'
                }
            else:
                query = {
                    'KeyConditionExpression':
                    Key('supplier_id').eq(supplier_id)
                    & Key('obj_type').eq(obj_type),
                    'FilterExpression':
                    Attr('latest').eq(True) & Attr('active').eq(True)
                    & Attr('order_date').gt(min_order_date),
                    'IndexName':
                    'by_supplier_id_and_obj_type'
                }
            response = self._storage.get_items(query)
            response_items = response['Items']
        else:
            response_items = []
            for distributor in distributors:
                if max_order_date:
                    query = {
                        'KeyConditionExpression':
                        Key('distributor_id').eq(distributor)
                        & Key('obj_type').eq(obj_type),
                        'FilterExpression':
                        Attr('latest').eq(True)
                        & Attr('active').eq(True) & Attr('order_date').between(
                            min_order_date, max_order_date),
                        'IndexName':
                        'by_distributor_id_and_obj_type'
                    }
                else:
                    query = {
                        'KeyConditionExpression':
                        Key('distributor_id').eq(distributor)
                        & Key('obj_type').eq(obj_type),
                        'FilterExpression':
                        Attr('latest').eq(True) & Attr('active').eq(True)
                        & Attr('order_date').gt(min_order_date),
                        'IndexName':
                        'by_distributor_id_and_obj_type'
                    }
                response = self._storage.get_items(query)
                response_items.extend(response['Items'])

        if supplier_id:
            print("supplier:{}".format(response_items))
        else:
            print("distributor:{}".format(response_items))

        purchase_orders_obj = []

        for item in response_items:
            # The 4 lines below can be uncommented if we move
            # from ALL to KEYS_ONLY for the table
            # entity_id = item['EntityID']
            # purchase_order = self._storage.get(table, entity_id)
            # purchase_order = clean(purchase_order)
            purchase_order = json_util.loads(clean(item))
            purchase_order['order_date'] = maya.to_iso8601(
                datetime.utcfromtimestamp(
                    purchase_order['order_date'])).split('T')[0]
            if "pack_date" in purchase_order:
                purchase_order['pack_date'] = maya.to_iso8601(
                    datetime.utcfromtimestamp(
                        purchase_order['pack_date'])).split('T')[0]
            if "ship_date" in purchase_order:
                purchase_order['ship_date'] = maya.to_iso8601(
                    datetime.utcfromtimestamp(
                        purchase_order['ship_date'])).split('T')[0]
            purchase_orders_obj.append(purchase_order)

        return purchase_orders_obj
예제 #17
0
def lambda_handler(event, context):
    # TODO implement
    
    queryStringParameters = event['queryStringParameters']

    #get company id
    
    Student_ID   = int(queryStringParameters['sid'])
    
    
    student_skills = []
    matched_placements = []
    
    ####### Gather student skills ######
    
    table = dynamodb.Table('Skills')
    
    #scan for given student id
    response = table.scan(FilterExpression=Attr('Student_ID').eq(Student_ID))
    
    if response['Count'] == 0:
        return {
            "statusCode": 500,
            'body': json.dumps('Internal server error- No item found in Skills table *-*')
        }
    else:
        for item in response['Items']:
            student_skills.append(item['skills'])
            
            
    ############Scan all jobs ################
    
    table = dynamodb.Table('Jobs')
    
    response = table.scan()
    
    if response['Count'] == 0:
        return {
            "statusCode": 500,
            'body': json.dumps('Internal server error- There is no placement to show *-*')
        }
    else:
        for item in response['Items']:
            Placement_ID = int(item['ID'])
            Company_ID   = int(item['Company_ID'])
            
            #######For every placement, collect their skills #######
            job_skills = []
            
            table = dynamodb.Table('Skills_jobs')
            
            db_response = table.scan(FilterExpression=Attr('Jobs_Company_ID').eq(Company_ID)&Attr('Jobs_ID').eq(Placement_ID))
            
            for attribute in db_response['Items']:
                job_skills.append(attribute['name'])
            
            #we got skills for that placement
            percentage_of_matching = matching(student_skills, job_skills)
            
            ##### if percentage is high ################
            if percentage_of_matching > 30:
                matched_placements.append((Company_ID, Placement_ID, percentage_of_matching))
            
    ###sort placements based on their matching rate
    
    matched_placements = sorted(matched_placements, key=lambda x: x[2], reverse=True)
    
    ##we got ids for matching placements 
    ##get all placements
    http = urllib3.PoolManager()
    placements = []
    
    for item in matched_placements:
        cid = item[0]
        pid = item[1]
        
        url = "https://hp8xm3yzr0.execute-api.us-east-2.amazonaws.com/prod/getsingleplacement?cid={}&pid={}".format(cid, pid)
        r = http.request('GET', url)
        
        data = r.data.decode('utf-8')
        data = json.loads(data)
        
        placements.append(data)
    
    responseBody = {}
    responseBody['placements'] = placements
    
    return {
        'statusCode': 200,
        'body': json.dumps(responseBody)
    }
예제 #18
0
def lambda_handler(event, context):
    msgBody = json.loads(event['Records'][0]['Sns']['Message'])
    if not ('visitor_id' in msgBody):
        return {
            'statusCode': 400,
            'body': "The input event must have a visitor_id"
        }
    visitor_id = msgBody['visitor_id']

    if not ('experience_id' in msgBody):
        return {
            'statusCode': 400,
            'body': "The input event must have a experience_id"
        }
    experience_id = msgBody['experience_id']

    dynamodb = boto3.resource('dynamodb',
                              aws_access_key_id=ACCESS_KEY,
                              aws_secret_access_key=SECRET_KEY,
                              aws_session_token=SESSION_TOKEN)

    visitorEventTable = dynamodb.Table('fssi2019-dynamodb-visitor_event_ts')
    occupancyTable = dynamodb.Table('fssi2019-dynamodb-occupancy')

    # Get all events with the visitor_id.
    # Note: Scan reads all records from DynamoDB. Maybe set up a secondary index.
    visitorEvents = visitorEventTable.scan(
        FilterExpression=Attr('visitor_id').eq(visitor_id))

    # Get the visitor event with the latest timestamp.
    latestEvent = None
    for e in visitorEvents['Items']:
        if latestEvent == None or e['timestamp'] > latestEvent['timestamp']:
            latestEvent = e

    print("Debug: latestEvent: " + str(latestEvent))
    messageExit = None
    # Figure out whether the event means that the visitor is entering the experience.
    if latestEvent == None:
        isEntering = True
    elif latestEvent['experience_id'] == experience_id:
        if latestEvent['event'] == 'exit':
            # Re-entering this experience.
            isEntering = True
        else:
            # Now exiting.
            isEntering = False
    else:
        # The latest event was for a different experience. We have to be
        # entering this experience.
        isEntering = True
        if latestEvent['event'] != 'exit':
            # The visitor must have exited the other experience without scanning.
            # Add an exit event for the other experience and remove from occupancy.
            visitorEventTable.put_item(
                Item={
                    'timestamp': Decimal(time.time()),
                    'visitor_id': visitor_id,
                    'experience_id': latestEvent['experience_id'],
                    'event': 'exit',
                    'confidence': Decimal(1.0)
                })
            messageExit = {
                "defualt": {
                    "event": "exit",
                    "experience_id": latestEvent['experience_id'],
                    "visitor_id": visitor_id
                }
            }
            occupancyResponse = occupancyTable.get_item(
                Key={'id': latestEvent['experience_id']})
            if 'Item' in occupancyResponse:
                occupancy = occupancyResponse['Item']['occupancy']
                occupancy.discard(visitor_id)
                if len(occupancy) == 0:
                    # The empty set is not allowed, so remove the entry.
                    occupancyTable.delete_item(
                        Key={'id': latestEvent['experience_id']})
                else:
                    occupancyTable.put_item(Item={
                        'id': latestEvent['experience_id'],
                        'occupancy': occupancy
                    })

    # Add a new visitor event.
    visitorEventTable.put_item(
        Item={
            'timestamp': Decimal(time.time()),
            'visitor_id': visitor_id,
            'experience_id': experience_id,
            'event': 'entry' if isEntering else 'exit',
            'confidence': Decimal(1.0)
        })

    mySnsClient = boto3.client('sns')
    if isEntering:
        message = {
            "defualt": {
                "event": "entry",
                "experience_id": experience_id,
                "visitor_id": visitor_id
            }
        }
    else:
        message = message = {
            "defualt": {
                "event": "exit",
                "experience_id": experience_id,
                "visitor_id": visitor_id
            }
        }
    response = mySnsClient.publish(
        TopicArn=
        'arn:aws:sns:us-west-1:756428767688:fssi2019-sns-visitor-event',
        Message=json.dumps(message))

    if messageExit is not None:
        response = mySnsClient.publish(
            TopicArn=
            'arn:aws:sns:us-west-1:756428767688:fssi2019-sns-visitor-event',
            Message=json.dumps(messageExit))

    # Update the occupancy table for the experience based on isEntering.
    occupancyResponse = occupancyTable.get_item(Key={'id': experience_id})
    if 'Item' in occupancyResponse:
        occupancy = occupancyResponse['Item']['occupancy']
        if isEntering:
            occupancy.add(visitor_id)
        else:
            occupancy.discard(visitor_id)

        if len(occupancy) == 0:
            # The empty set is not allowed, so remove the entry.
            occupancyTable.delete_item(Key={'id': experience_id})
        else:
            occupancyTable.put_item(Item={
                'id': experience_id,
                'occupancy': occupancy
            })
    else:
        # The is no entry for the experience_id. Create it if is entering.
        if isEntering:
            occupancyTable.put_item(Item={
                'id': experience_id,
                'occupancy': set([visitor_id])
            })

    return {'statusCode': 200, 'body': json.dumps({'event': event})}
예제 #19
0
    table = dynamodb.Table('WorldPlayers')

    try:
        response = table.get_item(Attr={'MatchID': 749, 'Player Name': nombre})
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        return response['Item']


dynamodb = None
if not dynamodb:

    dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")

table = dynamodb.Table('WorldMatches')
responseMatch = table.scan(FilterExpression=Attr('Stage').eq("Final")
                           & (Attr('Home Team Name').eq("Argentina")
                              | Attr('Away Team Name').eq("Argentina")))

datos = responseMatch['Items']

tablePlayers = dynamodb.Table('WorldPlayersFinal')
responsePlayers = tablePlayers.scan(
    FilterExpression=Attr('Team Initials').eq("ARG"))

datosPlayers = responsePlayers['Items']

for i in datosPlayers:
    print(i['Player Name'])
예제 #20
0
def list_job_events_handler(event, context):
    # Input parsing
    job_id = event["pathParameters"]["job_id"]
    qs = event.get("queryStringParameters")
    mvqs = event.get("multiValueQueryStringParameters")
    if not qs:
        qs = {}
        mvqs = {}
    page_size = int(qs.get("page_size", 20))
    start_at = qs.get("start_at", "0")
    # Check the job exists
    job = table.get_item(Key={
        "Id": job_id,
        "Sk": job_id,
    }).get("Item")
    if not job:
        return {"statusCode": 404}

    watermark_boundary_mu = (job.get("JobFinishTime", utc_timestamp()) +
                             1) * 1000

    # Check the watermark is not "future"
    if int(start_at.split("#")[0]) > watermark_boundary_mu:
        raise ValueError(
            "Watermark {} is out of bounds for this job".format(start_at))

    # Apply filters
    filter_expression = Attr("Type").eq("JobEvent")
    user_filters = mvqs.get("filter", [])
    for f in user_filters:
        k, v = f.split("=")
        filter_expression = filter_expression & Attr(k).begins_with(v)

    # Because result may contain both JobEvent and Job items, we request max page_size+1 items then apply the type
    # filter as FilterExpression. We then limit the list size to the requested page size in case the number of
    # items after filtering is still page_size+1 i.e. the Job item wasn't on the page.
    items = []
    query_start_key = str(start_at)
    last_evaluated = None
    last_query_size = 0
    while len(items) < page_size:
        resp = table.query(
            KeyConditionExpression=Key("Id").eq(job_id),
            ScanIndexForward=True,
            FilterExpression=filter_expression,
            Limit=100 if len(user_filters) else page_size + 1,
            ExclusiveStartKey={
                "Id": job_id,
                "Sk": query_start_key
            },
        )
        results = resp.get("Items", [])
        last_query_size = len(results)
        items.extend(results[:page_size - len(items)])
        query_start_key = resp.get("LastEvaluatedKey", {}).get("Sk")
        if not query_start_key:
            break
        last_evaluated = query_start_key

    next_start = _get_watermark(items, start_at, page_size, job["JobStatus"],
                                last_evaluated, last_query_size)

    resp = {
        k: v
        for k, v in {
            "JobEvents": items,
            "NextStart": next_start
        }.items() if v is not None
    }

    return {"statusCode": 200, "body": json.dumps(resp, cls=DecimalEncoder)}
예제 #21
0
 def setUp(self):
     self.attr = Attr("mykey")
     self.attr2 = Attr("myotherkey")
     self.value = "foo"
     self.value2 = "foo2"
예제 #22
0
 def get_grant(code_key, code):
     user = USERS_TABLE.scan(
         FilterExpression=Attr(code_key).eq(code)).get('Items')
     if user:
         return user[0]
예제 #23
0
 def test_build_expression_lte(self):
     a1 = Attr('myattr')
     self.assert_condition_expression_build(
         a1.lte('foo'), '#n0 <= :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #24
0
 def get_token_by_access_id(oa_access_token):
     user = USERS_TABLE.scan(
         FilterExpression=Attr('query_token').eq(oa_access_token)
         | Attr('stream_token').eq(oa_access_token)).get('Items')
     if user:
         return user[0]
예제 #25
0
 def test_build_expression_in(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.is_in([1, 2, 3]), '#n0 IN (:v0, :v1, :v2)',
         {'#n0': 'myattr'}, {':v0': 1, ':v1': 2, ':v2': 3})
예제 #26
0
    def query_items(self, query=None, only_fields_with_values=True):
        filter_expression = None
        if query and len(query) > 1:
            for field, value in iteritems(query):
                if isinstance(
                        value,
                        dict) and 'condition' in value and 'value' in value:
                    condition = value['condition']
                    value = value['value']
                else:
                    condition = 'eq'

                if not value and only_fields_with_values:
                    continue

                if condition == 'eq':
                    filter_expression &= Attr(field).eq(value)
                if condition == 'ne':
                    filter_expression &= Attr(field).ne(value)
                if condition == 'lt':
                    filter_expression &= Attr(field).lt(value)
                if condition == 'lte':
                    filter_expression &= Attr(field).lte(value)
                if condition == 'gt':
                    filter_expression &= Attr(field).gt(value)
                if condition == 'gte':
                    filter_expression &= Attr(field).gte(value)
                if condition == 'begins_with':
                    filter_expression &= Attr(field).begins_with(value)
                if condition == 'between':
                    filter_expression &= Attr(field).between(value)
                if condition == 'ne':
                    filter_expression &= Attr(field).ne(value)
                if condition == 'is_in':
                    filter_expression &= Attr(field).is_in(value)
                if condition == 'contains':
                    filter_expression &= Attr(field).contains(value)
                else:
                    raise Exception(
                        'Invalid filter condition: {0}'.format(condition))

        if filter_expression:
            response = self.table.scan(FilterExpression=filter_expression)
        else:
            response = self.table.scan()

        if response and 'Items' in response:
            return response['Items']
        else:
            return None
예제 #27
0
 def test_build_size_with_other_conditons(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.size().eq(5), 'size(#n0) = :v0', {'#n0': 'myattr'}, {':v0': 5})
예제 #28
0
def customer_dashboard(customer_username, customer_id, cid):
    #if the user tries to access someone elses dashboard
    if cid != customer_id:
        error = "Unable to look at another customer's dashboard, please sign in if you haven't done so already"
        flash(u'Unable to access dashboard, please login to your account',
              'error')
        return redirect(url_for('login'))

    #Tables to be used
    customer_table = dynamodb.Table('customer')
    restaurant_table = dynamodb.Table('restaurant')
    order_table = dynamodb.Table('order')

    #Query the customers data and remove the password before passing to client side
    customer_data = customer_table.query(
        KeyConditionExpression=Key('customer_id').eq(customer_id))
    customer_data['Items'][0]['password'] = None

    # Search filter for the start time and end time
    if request.method == 'POST':
        order_date = request.form['order_date'] if request.form[
            'order_date'] else None
        order_time = request.form['order_time'] if request.form[
            'order_time'] else None
        end_date = request.form['end_date'] if request.form[
            'end_date'] else None
        end_time = request.form['end_time'] if request.form[
            'end_time'] else None

        # If the user inputs an order_date and end_date
        if order_date and end_date:
            order_date_time = ""
            end_date_time = ""

            if order_time:
                order_date_time = "%s, %s" % (order_date, order_time)
            else:
                order_date_time = "%s, %s" % (order_date, "00:00:00")

            if end_time:
                end_date_time = "%s, %s" % (end_date, end_time)
            else:
                end_date_time = "%s, %s" % (end_date, "00:00:00")

            # Scan the order table for a complete list of orders that are between the end_date_time and order_date_time
            order_data = order_table.scan(
                FilterExpression=Key('customer_id').eq(customer_id)
                & Attr('order_time').between(order_date_time, end_date_time))

            restaurant_id = ""

            # Get the name of the restaurant for every order that is pulled in the order_data
            for item in order_data['Items']:
                for key, value in item.items():
                    if key == "restaurant_id":
                        restaurant_id = value
                        restaurant_data = restaurant_table.query(
                            KeyConditionExpression=Key('restaurant_id').eq(
                                restaurant_id))
                item['restaurant_name'] = restaurant_data['Items'][0][
                    'restaurant_name']

            return render_template('customer_dashboard.html',
                                   customer_username=customer_username,
                                   customer_id=customer_id,
                                   customer_info=customer_data,
                                   orders=order_data)

        # If the user inputs an order_date and not an end_date
        elif order_date and not end_date:
            order_date_time = ""

            if order_time:
                order_date_time = "%s, %s" % (order_date, order_time)
            else:
                order_date_time = "%s, %s" % (order_date, "00:00:00")

            # Scan the order table for a complete list of orders that are after the order_date_time
            order_data = order_table.scan(
                FilterExpression=Attr('customer_id').eq(customer_id)
                & Attr('order_time').gte(order_date_time))

            restaurant_id = ""

            # Get the name of the restaurant for every order that is pulled in the order_data
            for item in order_data['Items']:
                for key, value in item.items():
                    if key == "restaurant_id":
                        restaurant_id = value
                        restaurant_data = restaurant_table.query(
                            KeyConditionExpression=Key('restaurant_id').eq(
                                restaurant_id))
                item['restaurant_name'] = restaurant_data['Items'][0][
                    'restaurant_name']

            return render_template('customer_dashboard.html',
                                   customer_username=customer_username,
                                   customer_id=customer_id,
                                   customer_info=customer_data,
                                   orders=order_data)

        # If the user inputs not an order_date and an end_date
        elif not order_date and end_date:
            end_date_time = ""

            if end_time:
                end_date_time = "%s, %s" % (end_date, end_time)
            else:
                end_date_time = "%s, %s" % (end_date, "00:00:00")

            # Scan the order table for a complete list of orders that are before the end_date_time
            order_data = order_table.scan(
                FilterExpression=Attr('customer_id').eq(customer_id)
                & Attr('order_time').lte(end_date_time))

            restaurant_id = ""

            # Get the name of the restaurant for every order that is pulled in the order_data
            for item in order_data['Items']:
                for key, value in item.items():
                    if key == "restaurant_id":
                        restaurant_id = value
                        restaurant_data = restaurant_table.query(
                            KeyConditionExpression=Key('restaurant_id').eq(
                                restaurant_id))
                item['restaurant_name'] = restaurant_data['Items'][0][
                    'restaurant_name']

            return render_template('customer_dashboard.html',
                                   customer_username=customer_username,
                                   customer_id=customer_id,
                                   customer_info=customer_data,
                                   orders=order_data)

        # if there is no order_date or end_date
        else:
            # Scan the order table for a complete list of orders for a customer
            order_data = order_table.scan(
                FilterExpression=Attr('customer_id').eq(customer_id))

            restaurant_id = ""

            # Get the name of the restaurant for every order that is pulled in the order_data
            for item in order_data['Items']:
                for key, value in item.items():
                    if key == "restaurant_id":
                        restaurant_id = value
                        restaurant_data = restaurant_table.query(
                            KeyConditionExpression=Key('restaurant_id').eq(
                                restaurant_id))
                item['restaurant_name'] = restaurant_data['Items'][0][
                    'restaurant_name']

            return render_template('customer_dashboard.html',
                                   customer_username=customer_username,
                                   customer_id=customer_id,
                                   customer_info=customer_data,
                                   orders=order_data)

    # if there is no order_date or end_date
    else:
        # Scan the order table for a complete list of orders for a customer
        order_data = order_table.scan(
            FilterExpression=Attr('customer_id').eq(customer_id))

        restaurant_id = ""

        # Get the name of the restaurant for every order that is pulled in the order_data
        for item in order_data['Items']:
            for key, value in item.items():
                if key == "restaurant_id":
                    restaurant_id = value
                    restaurant_data = restaurant_table.query(
                        KeyConditionExpression=Key('restaurant_id').eq(
                            restaurant_id))
            item['restaurant_name'] = restaurant_data['Items'][0][
                'restaurant_name']

        return render_template('customer_dashboard.html',
                               customer_username=customer_username,
                               customer_id=customer_id,
                               customer_info=customer_data,
                               orders=order_data)
예제 #29
0
 def test_build_attribute_with_attr_value(self):
     a = Attr('myattr')
     value = Attr('myreference')
     self.assert_condition_expression_build(
         a.eq(value), '#n0 = #n1',
         {'#n0': 'myattr', '#n1': 'myreference'}, {})
def lambda_handler(event, context):
    #parse body of request body
    requestBody = event['body']
    requestBody = json.loads(requestBody)

    ###          FOR JOB TABLE             ###
    Company_ID = requestBody['Company_ID']
    #Company_name     = requestBody['Company_name']
    title = requestBody['title']
    description = requestBody['description']
    category = requestBody['category']
    contact_info = requestBody['contact_info']
    department_name = requestBody['department_name']
    degree = requestBody['degree']
    #experience       = requestBody['experience']
    location = requestBody['location']
    salaries = requestBody['salaries']

    ###   FOR SKILLS  TABLE  ###
    skills = requestBody['skills']
    ###  FOR BENEFITS TABLE  ###
    benefits = requestBody['benefits']
    ###  FOR  CERTIFICATES TABLE  ###
    certificates = requestBody['certificates']

    ###################   GET COMPANY NAME    ##########################

    table = dynamodb.Table('Company')

    response = table.scan(FilterExpression=Attr('ID').eq(Company_ID))

    if response['Count'] == 0:
        return {
            "statusCode":
            500,
            "body":
            json.dumps(
                "Internal Server Error - No matching company name for given company id *-*"
            )
        }
    else:
        Company_name = response['Items'][0]['name']

    ####################    ADD  PLACEMENT    ##########################

    table = dynamodb.Table('Jobs')

    response = table.scan()

    if response['Count'] == 0:
        #no item in Jobs table

        Jobs_ID = 0
    else:
        #there are items in table
        #find max id and increase by 1

        data = response['Items']
        x = []
        for item in data:
            x.append(item['ID'])
        Jobs_ID = max(x) + 1
        # ID assigned #

    ###  add the new item ###

    try:
        print("Adding new item")
        db_response = table.put_item(
            Item={
                'ID': Jobs_ID,
                'Company_ID': Company_ID,
                'Company_name': Company_name,
                'description': description,
                'category': category,
                'contact_info': contact_info,
                'department_name': department_name,
                'degree': degree,
                'location': location,
                'job_creation_timestamp': int(time.time()),
                'salaries': salaries,
                'title': title,
                'location': location
            })
        # TODO: write code...
    except ClientError as e:
        print(e.response['Error']['Message'])
        return {
            'statusCode': 500,
            'body': json.dumps("Internal Server Error *-*")
        }

    #######################   ADD  SKILLS   ##################################

    table = dynamodb.Table('Skills_jobs')

    ### FIND ID  ###

    for skill in skills:

        response = table.scan()

        if response['Count'] == 0:
            #no item in Jobs table

            ID = 0
        else:
            #there are items in table
            #find max id and increase by 1

            data = response['Items']
            x = []
            for item in data:
                x.append(item['ID'])
            ID = max(x) + 1
            # ID assigned #

        ###  add the new item ###

        try:
            print("Adding new item")
            db_response = table.put_item(
                Item={
                    'ID': ID,
                    'Jobs_ID': Jobs_ID,
                    'Jobs_Company_ID': Company_ID,
                    'name': skill
                })
            # TODO: write code...
        except ClientError as e:
            print(e.response['Error']['Message'])
            return {
                'statusCode': 500,
                'body': json.dumps("Internal Server Error *-*")
            }

    ###############    ADD  BENEFITS     ####################################

    table = dynamodb.Table('Benefits')

    for benefit in benefits:

        response = table.scan()

        if response['Count'] == 0:
            #no item in Jobs table

            ID = 0
        else:
            #there are items in table
            #find max id and increase by 1

            data = response['Items']
            x = []
            for item in data:
                x.append(item['ID'])
            ID = max(x) + 1
            # ID assigned #

        ###  add the new item ###

        try:
            print("Adding new item")
            db_response = table.put_item(
                Item={
                    'ID': ID,
                    'Jobs_ID': Jobs_ID,
                    'Jobs_Company_ID': Company_ID,
                    'name': benefit
                })
            # TODO: write code...
        except ClientError as e:
            print(e.response['Error']['Message'])
            return {
                'statusCode': 500,
                'body': json.dumps("Internal Server Error *-*")
            }

    ################   ADD CERTIFICATES   ##################################

    if not certificates:
        return {
            "statusCode": 200,
            "body": json.dumps({"Placement_ID": int(Jobs_ID)})
        }
    else:

        table = dynamodb.Table('Certificates_job')

        for certificate in certificates:

            response = table.scan()

            if response['Count'] == 0:
                #no item in Jobs table

                ID = 0
            else:
                #there are items in table
                #find max id and increase by 1

                data = response['Items']
                x = []
                for item in data:
                    x.append(item['ID'])
                ID = max(x) + 1
                # ID assigned #

            ###  add the new item ###

            try:
                print("Adding new item")
                db_response = table.put_item(
                    Item={
                        'ID': ID,
                        'Jobs_ID': Jobs_ID,
                        'Jobs_Company_ID': Company_ID,
                        'name': certificate
                    })
                # TODO: write code...
            except ClientError as e:
                print(e.response['Error']['Message'])
                return {
                    'statusCode': 500,
                    'body': json.dumps("Internal Server Error *-*")
                }

    ###   EVERYTHING IS OK ####

    return {
        "statusCode": 200,
        "body": json.dumps({"Placement_ID": int(Jobs_ID)})
    }
예제 #31
0
 def test_build_nested_attr_map_list(self):
     a = Attr('MyMap.MyList[2].MyElement')
     self.assert_condition_expression_build(
         a.eq('foo'), '#n0.#n1[2].#n2 = :v0',
         {'#n0': 'MyMap', '#n1': 'MyList', '#n2': 'MyElement'},
         {':v0': 'foo'})
예제 #32
0
def read_items(table):
    items = table.scan(FilterExpression=Attr('Id').gte(0))
    return items['Items']
예제 #33
0
 def test_build_attr_map(self):
     a = Attr("MyMap.MyKey")
     self.assert_condition_expression_build(
         a.eq("foo"), "#n0.#n1 = :v0", {"#n0": "MyMap", "#n1": "MyKey"}, {":v0": "foo"}
     )
예제 #34
0
    def live_test(self) -> None:
        """Upload an EICAR test file to BinaryAlert which should trigger a YARA match alert.

        Raises:
            TestFailureError: If the live test failed (YARA match not found).
        """
        bucket_name = self._config.binaryalert_s3_bucket_name
        test_filename = 'eicar_test_{}.txt'.format(uuid.uuid4())
        s3_identifier = 'S3:{}:{}'.format(bucket_name, test_filename)

        print('Uploading EICAR test file {}...'.format(s3_identifier))
        bucket = boto3.resource('s3').Bucket(bucket_name)
        bucket.put_object(Body=EICAR_STRING.encode('UTF-8'),
                          Key=test_filename,
                          Metadata={'filepath': test_filename})

        table_name = '{}_binaryalert_matches'.format(self._config.name_prefix)
        print('EICAR test file uploaded! Connecting to table DynamoDB:{}...'.
              format(table_name))
        table = boto3.resource('dynamodb').Table(table_name)
        eicar_sha256 = hashlib.sha256(EICAR_STRING.encode('UTF-8')).hexdigest()
        dynamo_record_found = False

        for attempt in range(1, 11):
            time.sleep(5)
            print(
                '\t[{}/10] Querying DynamoDB table for the expected YARA match entry...'
                .format(attempt))
            items = table.query(
                Select='ALL_ATTRIBUTES',
                Limit=1,
                ConsistentRead=True,
                ScanIndexForward=
                False,  # Sort by AnalyzerVersion descending (e.g. newest first).
                KeyConditionExpression=Key('SHA256').eq(eicar_sha256),
                FilterExpression=Attr('S3Objects').contains(
                    s3_identifier)).get('Items')

            if items:
                print(
                    '\nSUCCESS: Expected DynamoDB entry for the EICAR file was found!\n'
                )
                dynamo_record_found = True
                pprint.pprint(items[0])

                print('\nRemoving DynamoDB EICAR entry...')
                lambda_version = items[0]['AnalyzerVersion']
                table.delete_item(Key={
                    'SHA256': eicar_sha256,
                    'AnalyzerVersion': lambda_version
                })
                break
            elif attempt == 10:
                print(
                    '\nFAIL: Expected DynamoDB entry for the EICAR file was *not* found!\n'
                )

        print('Removing EICAR test file from S3...')
        bucket.delete_objects(Delete={'Objects': [{'Key': test_filename}]})

        if dynamo_record_found:
            print(
                '\nLive test succeeded! Verify the alert was sent to your SNS subscription(s).'
            )
        else:
            raise TestFailureError(
                '\nLive test failed! See https://binaryalert.io/troubleshooting-faq.html'
            )
예제 #35
0
 def test_build_nested_attr_map_list(self):
     a = Attr("MyMap.MyList[2].MyElement")
     self.assert_condition_expression_build(
         a.eq("foo"), "#n0.#n1[2].#n2 = :v0", {"#n0": "MyMap", "#n1": "MyList", "#n2": "MyElement"}, {":v0": "foo"}
     )
예제 #36
0
파일: handler.py 프로젝트: nickmpaz/kan.sh
def update_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']
    board_contents = json.loads(event.get("body")).get('board_contents')

    # dump board document to string for storage
    if board_contents.get('columns'):
        board_contents['columns'] = json.dumps(board_contents.get('columns'))

    # 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

    # update board contents
    board_cards_table.update_item(
        Key={'board_id': board_id},
        UpdateExpression='SET board_contents = :board_contents',
        ExpressionAttributeValues={':board_contents': board_contents})

    # set updated_at attribute of board for user who made the update
    user_boards_table.update_item(
        Key={
            'user_id': user_id,
            'board_id': board_id
        },
        UpdateExpression='SET updated_at = :updated_at',
        ExpressionAttributeValues={':updated_at': str(time.time())})

    name_changed = False
    if board_contents.get('name') != item.get('board_name'):
        name_changed = True

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

    # get all board owners
    response = user_boards_table.scan(
        FilterExpression=Attr('board_id').eq(board_id))
    items = response['Items']

    for owner in items:
        curr_user_id = owner.get('user_id')

        # if name changed update board for each user
        if name_changed:
            user_boards_table.update_item(
                Key={
                    'user_id': curr_user_id,
                    'board_id': board_id
                },
                UpdateExpression='SET board_name = :board_name',
                ExpressionAttributeValues={
                    ':board_name': board_contents.get('name')
                })

        # push new board contents to user if they are connected
        connection_id, callback_url = get_connection_info(curr_user_id)
        if connection_id:
            send_to_connection(Response.update_board_contents_response,
                               {'board_contents': board_contents},
                               connection_id, callback_url)

    return Status.OK
def archive_restore():
    config = configparser.ConfigParser()
    config.read('configini.ini')
    aws_config = config['AWS']
    ## Connect to SQS and get the message queue
    try:
        sqs = boto3.resource('sqs', region_name=aws_config['AWS_REGION_NAME'])
        huangxy_queue = sqs.get_queue_by_name(
            QueueName=aws_config['AWS_SQS_ARCHIVE_RETRIEVE'])
    except Exception as e:
        raise e
        return
    # Poll the message queue in a loop
    while True:
        # Attempt to read a message from the queue
        response = huangxy_queue.receive_messages(WaitTimeSeconds=20)
        # If message read, extract job parameters from the message body as before
        if response:
            print('Get response successfully.')
            try:
                msg = json.loads(json.loads(response[0].body)['Message'])
                retrieve_id = msg['JobId']
                archive_id = msg['ArchiveId']
            except Exception as e:
                raise e
                return
            try:
                client_glacier = boto3.client(
                    'glacier', region_name=aws_config['AWS_REGION_NAME'])
                glacier_job_output = client_glacier.get_job_output(
                    vaultName=aws_config['AWS_GLACIER_VAULT'],
                    jobId=retrieve_id)
                print('Get job out of glacier successfully.')
            except Exception as e:
                raise e
                return
            # archive_id is not a key, so I can only use table scan to get the s3_result_key and job_id.
            try:
                dynamodb = boto3.resource(
                    'dynamodb', region_name=aws_config['AWS_REGION_NAME'])
                annotation_table = dynamodb.Table(
                    aws_config['AWS_DYNAMODB_ANNOTATIONS_TABLE'])
                res = annotation_table.scan(
                    FilterExpression=Attr('archive_id').eq(archive_id))
                if not res['Items']:
                    response[0].delete()
                    print('No annotation.')
                else:
                    s3_result_key = str(res['Items'][0]['s3_key_result_file'])
                    job_id = res['Items'][0]['job_id']
            except Exception as e:
                raise e
                return
            # put job to s3 again
            try:
                client_s3 = boto3.client(
                    's3',
                    region_name=aws_config['AWS_REGION_NAME'],
                    config=Config(signature_version='s3v4'))
                # Response Syntax
                # {
                #     'DeleteMarker': True|False,
                #     'VersionId': 'string',
                #     'RequestCharged': 'requester'
                # }
                s3_upload_response = client_s3.put_object(
                    Body=glacier_job_output['body'].read(),
                    Bucket=aws_config['AWS_S3_RESULTS_BUCKET'],
                    Key=s3_result_key)
                print('Upload to S3 successfully.')
            except Exception as e:
                raise e
                return
            # update database.
            try:
                annotation_table.update_item(
                    Key={'job_id': job_id},
                    AttributeUpdates={'archive_id': {
                        'Action': 'DELETE'
                    }})
                print('Update database successfully.')
            except Exception as e:
                raise e
                return
            # After all done, delete SQS
            response[0].delete()
            print('Delete SQS successfully.')
        else:
            print('There is no response.')
예제 #38
0
    def query_items(self, query=None, only_fields_with_values=True, query_chunk_limit=-1):
        """
        gets items from database (this actually does scan, not query.  See query_raw() for true query)
        :param query:
        :param only_fields_with_values: 
        :param query_chunk_limit: not an absolute count, but a threshold where we stop fetching more chunks
                        (if negative then no limit, but will read all chunks)
        :return:
        """
        filter_expression = None
        if query and len(query) >= 1:
            for field, value in iteritems(query):
                value2 = None
                if isinstance(value, dict) and 'condition' in value and 'value' in value:
                    condition_str = value['condition']
                    value = value['value']
                    if condition_str == "between":
                        value, value2 = value["value"], value["value2"]
                else:
                    condition_str = 'eq'

                if not value and only_fields_with_values:
                    continue

                if condition_str == 'eq':
                    condition = Attr(field).eq(value)
                elif condition_str == 'ne':
                    condition = Attr(field).ne(value)
                elif condition_str == 'lt':
                    condition = Attr(field).lt(value)
                elif condition_str == 'lte':
                    condition = Attr(field).lte(value)
                elif condition_str == 'gt':
                    condition = Attr(field).gt(value)
                elif condition_str == 'gte':
                    condition = Attr(field).gte(value)
                elif condition_str == 'begins_with':
                    condition = Attr(field).begins_with(value)
                elif condition_str == 'between':
                    condition = Attr(field).between(value, value2)
                elif condition_str == 'is_in':
                    condition = Attr(field).is_in(value)
                elif condition_str == 'contains':
                    condition = Attr(field).contains(value)
                else:
                    raise Exception('Invalid filter condition: {0}'.format(condition_str))

                if filter_expression is None:
                    filter_expression = condition
                else:
                    filter_expression &= condition

        if filter_expression is not None:
            response = self.table.scan(
                FilterExpression=filter_expression
            )
        else:
            response = self.table.scan()

        if not response or not('Items' in response):
            return None

        # finished if there is no more data to read
        if not('LastEvaluatedKey' in response):
            return response['Items']

        items = response['Items']

        # read chunks until end or threshold is reached
        while 'LastEvaluatedKey' in response:
            if filter_expression is not None:
                response = self.table.scan(
                    FilterExpression=filter_expression,
                    ExclusiveStartKey=response['LastEvaluatedKey']
                )
            else:
                response = self.table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])

            if response and ('Items' in response):
                items += response['Items']

            item_count = len(items)
            if (query_chunk_limit >= 0) and (item_count >= query_chunk_limit):
                break

        return items
예제 #39
0
class TestA(TestK):
    def setUp(self):
        self.attr = Attr('mykey')
        self.attr2 = Attr('myotherkey')
        self.value = 'foo'
        self.value2 = 'foo2'

    def test_ne(self):
        self.assertEqual(self.attr.ne(self.value),
                         NotEquals(self.attr, self.value))

    def test_is_in(self):
        self.assertEqual(self.attr.is_in([self.value]),
                         In(self.attr, [self.value]))

    def test_exists(self):
        self.assertEqual(self.attr.exists(), AttributeExists(self.attr))

    def test_not_exists(self):
        self.assertEqual(self.attr.not_exists(), AttributeNotExists(self.attr))

    def test_contains(self):
        self.assertEqual(self.attr.contains(self.value),
                         Contains(self.attr, self.value))

    def test_size(self):
        self.assertEqual(self.attr.size(), Size(self.attr))

    def test_attribute_type(self):
        self.assertEqual(self.attr.attribute_type(self.value),
                         AttributeType(self.attr, self.value))

    def test_ne_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.ne(self.value)
        comp2 = attr_copy.ne(self.value)
        self.assertEqual(comp, comp2)

    def test_is_in_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.is_in([self.value])
        comp2 = attr_copy.is_in([self.value])
        self.assertEqual(comp, comp2)

    def test_exists_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.exists()
        comp2 = attr_copy.exists()
        self.assertEqual(comp, comp2)

    def test_not_exists_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.not_exists()
        comp2 = attr_copy.not_exists()
        self.assertEqual(comp, comp2)

    def test_contains_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.contains(self.value)
        comp2 = attr_copy.contains(self.value)
        self.assertEqual(comp, comp2)

    def test_size_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.size()
        comp2 = attr_copy.size()
        self.assertEqual(comp, comp2)

    def test_attribute_type_equality(self):
        attr_copy = copy.deepcopy(self.attr)
        comp = self.attr.attribute_type(self.value)
        comp2 = attr_copy.attribute_type(self.value)
        self.assertEqual(comp, comp2)
예제 #40
0
table = dynamodb.Table('users')
#myclient.connect()
while continue_reading:
    # Scan for cards
    time.sleep(1)
    (status, TagType) = TAGRead.MFRC522_Request(TAGRead.PICC_REQIDL)

    # If a card is found
    if status == TAGRead.MI_OK:
        print("Card detected")

    # Get the UID of the card
    (status, uid) = TAGRead.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == TAGRead.MI_OK:
        RFID_value = "Card read uid: " + str(uid[0]) + str(uid[1]) + str(
            uid[2]) + str(uid[3])
        #        response = table.query(
        #        KeyConditionExpression=Key('RFID_value').eq(RFID_value)
        #        )
        response = table.scan(
            FilterExpression=Attr('RFID_value').lt(RFID_value))
        items = response['Items']
        print(items)
        #       myclient.publish("harry_policy_new",rfid_value, 0)
        #      print("MQTT Client connection success!")
        # Print UID
        print("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]))
예제 #41
0
def lambda_handler(event, context):
    # TODO implement
    print(event)
    matchid = event['MatchId']
    print(type(matchid))
    dynamodb = boto3.resource('dynamodb')
    match_table = dynamodb.Table('dr11-publicmatch')
    matchResponse = match_table.scan(
        FilterExpression=Attr('MatchId').eq(int(matchid)))
    print(matchResponse)
    team1_list = list(matchResponse['Items'][0]['Team1 Players'])

    for i in range(len(team1_list)):
        team1_list[i] = int(team1_list[i])

    team2_list = list(matchResponse['Items'][0]['Team2 Players'])
    for i in range(len(team2_list)):
        team2_list[i] = int(team2_list[i])

    print(team1_list)
    print(team2_list)
    Cricket_Player_table = dynamodb.Table('dr11-playerdata')
    playersResponse = Cricket_Player_table.scan(
        FilterExpression=(Attr('playerid').eq(team1_list[0])
                          | Attr('playerid').eq(team1_list[1])
                          | Attr('playerid').eq(team1_list[2])
                          | Attr('playerid').eq(team1_list[3])
                          | Attr('playerid').eq(team1_list[4])
                          | Attr('playerid').eq(team1_list[5])
                          | Attr('playerid').eq(team1_list[6])
                          | Attr('playerid').eq(team1_list[7])
                          | Attr('playerid').eq(team1_list[8])
                          | Attr('playerid').eq(team1_list[9])
                          | Attr('playerid').eq(team1_list[10])
                          | Attr('playerid').eq(team2_list[0])
                          | Attr('playerid').eq(team2_list[1])
                          | Attr('playerid').eq(team2_list[2])
                          | Attr('playerid').eq(team2_list[3])
                          | Attr('playerid').eq(team2_list[4])
                          | Attr('playerid').eq(team2_list[5])
                          | Attr('playerid').eq(team2_list[6])
                          | Attr('playerid').eq(team2_list[7])
                          | Attr('playerid').eq(team2_list[8])
                          | Attr('playerid').eq(team2_list[9])
                          | Attr('playerid').eq(team2_list[10])))
    print('**********')
    print(playersResponse)
    #print(scanExpression)
    return {
        'statusCode': 200,
        'headers': {
            "Access-Control-Allow-Origin":
            "*",  # Required for CORS support to work 
            "Access-Control-Allow-Credentials":
            True  # Required for cookies, authorization headers with HTTPS  
        },
        'body': json.dumps(playersResponse['Items'])
        #'body': json.dumps('Hello from Lambda!')
    }
        mail.send(msg)


dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table('Events')
event_response = table.query(
    IndexName='item_type_index',
    KeyConditionExpression=Key('item_type').eq('host'))
current_timestamp = int(time.time())
if len(event_response['Items']) != 0:
    for i in event_response['Items']:
        if current_timestamp < int(
                i["start_time"]) < current_timestamp + 60 * 60:
            host_response = table.query(
                KeyConditionExpression=Key('username').eq(i["username"]),
                FilterExpression=Attr('item_type').eq('account'))
            recipients = []
            print(host_response)
            for host in host_response['Items']:
                recipients.append(host['email'])

            group_response = table.query(
                IndexName='item_type_index',
                KeyConditionExpression=Key('item_type').eq('participant')
                & Key('start_time').eq(i["start_time"]))
            for participant in group_response['Items']:
                participant_response = table.query(
                    KeyConditionExpression=Key('username').eq(
                        participant["username"]),
                    FilterExpression=Attr('item_type').eq('account'))
예제 #43
0
 def test_build_expression_begins_with(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.begins_with('foo'), 'begins_with(#n0, :v0)',
         {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #44
0
def get_medicine(table_name, user_id):
    table = dynamodb.Table(table_name)
    response = table.scan(
        FilterExpression=Attr('user_id').eq(user_id)
    )
    return response["Items"]
예제 #45
0
 def test_build_expression_ne(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.ne('foo'), '#n0 <> :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #46
0
def map_ids_to_card_name(card_ids):
    table = boto3.resource('dynamodb').Table('PayWise_Cards')
    response = table.scan(FilterExpression=Attr('card_id').is_in(card_ids),
                          ProjectionExpression='card_name,card_img,card_url')
    return response['Items']
예제 #47
0
 def test_build_expression_not_exists(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.not_exists(), 'attribute_not_exists(#n0)', {'#n0': 'myattr'}, {})
예제 #48
0
import boto3
from boto3.dynamodb.conditions import Key, Attr

AWS_KEY = "<enter-here>"
AWS_SECRET = "<enter-here>"
REGION = "us-east-1"

dynamodb = boto3.resource('dynamodb',
                          aws_access_key_id=AWS_KEY,
                          aws_secret_access_key=AWS_SECRET,
                          region_name=REGION)

table = dynamodb.Table('SensorData')

response = table.scan(
    FilterExpression=Key('Timestamp').gt('2017-05-24 14:41:35'))

items = response['Items']
print(items)

response = table.scan(FilterExpression=Attr('Temperature').gt(80))

items = response['Items']
print(items)
예제 #49
0
 def test_build_size(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.size(), 'size(#n0)', {'#n0': 'myattr'}, {})
def scan_dynamo(table_name, attribute, value_to_equal):
    response = dynamodb.Table(table_name).scan(
        FilterExpression=Attr(attribute).eq(value_to_equal))
    return response
예제 #51
0
 def test_build_attribute_type(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         a.attribute_type('foo'), 'attribute_type(#n0, :v0)',
         {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #52
0
    def update_faculty_demo_chart(dept):

        resp = table.query(
            KeyConditionExpression='PK = :pk AND SK BETWEEN :lower AND :upper',
            ExpressionAttributeValues={
                ':pk': f'DEPT#{dept}',
                ':lower': 'DATA#FACULTY_DATA#2005',
                ':upper': f'DATA#FACULTY_DATA#{int(MAX_FISCAL_YEAR) + 1}$',
            },
            FilterExpression=Attr('ten_stat').eq('Tenured')
            | Attr('ten_stat').eq('NTBOT'),
            ProjectionExpression='fte, ten_stat, percent_fem, percent_urm',
            ScanIndexForward=True,
        )

        data = resp['Items']
        chart_data = []
        x_axis = make_academic_year_range(0, MAX_YEAR_ID)

        # Construct charts without a loop to preserve y values for further calculation

        y_axis_bar_t = [
            item.get('fte') for item in data
            if item.get('ten_stat') == 'Tenured'
        ]

        chart_data.append(
            go.Bar(
                name='Tenured',
                x=x_axis,
                y=y_axis_bar_t,
                text=[f' {round(float(i))} ' for i in y_axis_bar_t
                      ],  # pad with spaces to prevent labels from rotating
                textposition='inside',
                hovertext=[f'Tenured: {i}' for i in y_axis_bar_t],
                hoverinfo='text',
                marker=dict(
                    color=faculty_colors.get('Tenured'),
                    line=dict(color='floralwhite', width=1),
                )))

        y_axis_bar_nt = [
            item.get('fte') for item in data if item.get('ten_stat') == 'NTBOT'
        ]

        chart_data.append(
            go.Bar(
                name='NTBOT',
                x=x_axis,
                y=y_axis_bar_nt,
                text=[f' {round(float(i))} ' for i in y_axis_bar_nt
                      ],  # pad with spaces to prevent labels from rotating
                textposition='inside',
                hovertext=[f'NTBOT: {i}' for i in y_axis_bar_nt],
                hoverinfo='text',
                marker=dict(
                    color=faculty_colors.get('NTBOT'),
                    line=dict(color='floralwhite', width=1),
                )))

        # LINE PLOTS

        y_axis_line_t = [
            round(float(item.get('percent_fem')) *
                  100) if item.get('percent_fem') is not None else None
            for item in data if item.get('ten_stat') == 'Tenured'
        ]
        hover_labels = [
            f'{i}%' if i is not None else None for i in y_axis_line_t
        ]
        text_labels = make_text_labels(hover_labels)

        chart_data.append(
            go.Scatter(name='% Tenured Female',
                       x=x_axis,
                       y=y_axis_line_t,
                       mode='lines+markers+text',
                       text=text_labels,
                       textposition='top center',
                       textfont=dict(color=colors.get('orange2'), ),
                       hovertext=hover_labels,
                       hoverinfo='text',
                       marker=dict(color=colors.get('orange2'), ),
                       yaxis='y2'))

        y_axis_line_nt = [
            round(float(item.get('percent_fem')) *
                  100) if item.get('percent_fem') is not None else None
            for item in data if item.get('ten_stat') == 'NTBOT'
        ]
        hover_labels = [
            f'{i}%' if i is not None else None for i in y_axis_line_nt
        ]
        text_labels = make_text_labels(hover_labels)

        chart_data.append(
            go.Scatter(name='% NTBOT Female',
                       x=x_axis,
                       y=y_axis_line_nt,
                       mode='lines+markers+text',
                       text=text_labels,
                       textposition='top center',
                       textfont=dict(color=colors.get('red1'), ),
                       hovertext=hover_labels,
                       hoverinfo='text',
                       marker=dict(color=colors.get('red1'), ),
                       yaxis='y2'))

        # URM line calculation
        # (Tenured FTE * Tenured % URM + NTBOT FTE * NTBOT% URM) / (Tenured FTE + NTBOT FTE)

        urm_t = [
            float(item.get('percent_urm'))
            if item.get('percent_urm') is not None else None for item in data
            if item.get('ten_stat') == 'Tenured'
        ]

        urm_nt = [
            float(item.get('percent_urm'))
            if item.get('percent_urm') is not None else None for item in data
            if item.get('ten_stat') == 'NTBOT'
        ]

        y_axis_line_urm = []

        for t_fte, nt_fte, t_urm, nt_urm in zip(y_axis_bar_t, y_axis_bar_nt,
                                                urm_t, urm_nt):

            calc = None
            if t_urm is not None and nt_urm is not None:
                calc = (float(t_fte) * t_urm + float(nt_fte) * nt_urm) / (
                    float(t_fte) + float(nt_fte))
            elif t_urm is not None and nt_urm is None:
                calc = (float(t_fte) * t_urm +
                        float(nt_fte) * 0) / (float(t_fte) + float(nt_fte))
            elif nt_urm is not None and t_urm is None:
                calc = (float(t_fte) * 0 + float(nt_fte) * nt_urm) / (
                    float(t_fte) + float(nt_fte))

            if calc is None:
                y_axis_line_urm.append(None)
            else:
                y_axis_line_urm.append(round(calc * 100))

        hover_labels = [
            f'{i}%' if i is not None else None for i in y_axis_line_urm
        ]
        text_labels = make_text_labels(hover_labels)

        chart_data.append(
            go.Scatter(name='% NTBOT and Tenured URM',
                       x=x_axis,
                       y=y_axis_line_urm,
                       mode='lines+markers+text',
                       text=text_labels,
                       textposition='top center',
                       textfont=dict(color=colors.get('teal1'), ),
                       hovertext=hover_labels,
                       hoverinfo='text',
                       marker=dict(color=colors.get('teal1'), ),
                       yaxis='y2'))

        chart_layout = go.Layout(
            barmode='stack',
            xaxis=axes(),
            yaxis=axes(title='FTE', ),
            yaxis2=axes(
                title='% FTE',
                overlaying='y',
                side='right',
                rangemode='tozero',
                showgrid=False,
            ),
            legend={
                'traceorder': 'normal',
                'x': 1.05
            },  # By default x is 1.02 which will make it overlap with the 2nd y-axis
            margin=margin(),
        )

        return {'data': chart_data, 'layout': chart_layout}
예제 #53
0
 def test_build_not(self):
     a = Attr('myattr')
     self.assert_condition_expression_build(
         ~a.eq('foo'), '(NOT #n0 = :v0)',
         {'#n0': 'myattr'}, {':v0': 'foo'})
예제 #54
0
def get_recent_signups():
    current_epoch_in_seconds = int(time.time())
    search_filter = Attr('expire_on').gt(current_epoch_in_seconds)
    return table.scan(FilterExpression=search_filter, Limit=3)['Items']
예제 #55
0
 def test_build_with_is_key_condition_throws_error(self):
     a = Attr('myattr')
     with self.assertRaises(DynamoDBNeedsKeyConditionError):
         self.builder.build_expression(a.eq('foo'), is_key_condition=True)
예제 #56
0
def scan_database(service, service_two, service_three, gender, age, location):

    # set age and gender to match value in database
    if (age.lower() == 'adult'):
        age = 'Adult'
    else:
        age = 'Minor (Under 18)'

    if (gender.lower() == 'male'):
        gender = 'Male'
    else:
        gender = 'Female'

    # set location to match value in database
    if any(state['abbreviation'] == location.lower() for state in state_data):
        location = location.upper()
    elif any(state['name'] == location.lower() for state in state_data):
        match = next(d for d in state_data if d['name'] == location.lower())
        location = match['abbreviation'].upper()

    # set services to match value in database
    if any(s['id'] == service for s in service_data):
        match = next(d for d in service_data if d['id'] == service)
        service = match['name'].lower()

    if any(s['id'] == service_two for s in service_data):
        match = next(d for d in service_data if d['id'] == service_two)
        service_two = match['name'].lower()

    if any(s['id'] == service_three for s in service_data):
        match = next(d for d in service_data if d['id'] == service_three)
        service_three = match['name'].lower()

    # scan database
    if service_two is None and service_three is None:
        response = table.scan(
            FilterExpression=Attr('populationsDetail').contains(
                'Sex Trafficking')
            & Attr('servicesDetail').contains(format_services(service))
            & Attr('populationsDetail').contains(gender)
            & Attr('populationsDetail').contains(age)
            & Attr('state').eq(location))
    elif service_three is None:
        response = table.scan(
            FilterExpression=Attr('populationsDetail').contains(
                'Sex Trafficking')
            & Attr('servicesDetail').contains(format_services(service))
            & Attr('servicesDetail').contains(format_services(service_two))
            & Attr('populationsDetail').contains(gender)
            & Attr('populationsDetail').contains(age)
            & Attr('state').eq(location))
    else:
        response = table.scan(
            FilterExpression=Attr('populationsDetail').contains(
                'Sex Trafficking')
            & Attr('servicesDetail').contains(format_services(service))
            & Attr('servicesDetail').contains(format_services(service_two))
            & Attr('servicesDetail').contains(format_services(service_three))
            & Attr('populationsDetail').contains(gender)
            & Attr('populationsDetail').contains(age)
            & Attr('state').eq(location))

    items = response['Items']
    #print (json.dumps(items, indent=4, sort_keys=True))
    return items
예제 #57
0
 def test_build_attr_list(self):
     a = Attr('MyList[0]')
     self.assert_condition_expression_build(
         a.eq('foo'), '#n0[0] = :v0', {'#n0': 'MyList'}, {':v0': 'foo'})
예제 #58
0
 def test_build_attribute_with_attr_value(self):
     a = Attr("myattr")
     value = Attr("myreference")
     self.assert_condition_expression_build(a.eq(value), "#n0 = #n1", {"#n0": "myattr", "#n1": "myreference"}, {})
예제 #59
0
def lambda_handler(event, context):
    """Starting Handler"""
    params = parse_qs(event['body'])
    token = params['token'][0]
    if token != EXPECTED_TOKEN:
        LOGGER.error("Request token (%s) does not match expected", token)
        return respond(Exception('Invalid request token'))

    user = params['user_name'][0]
    #command = params['command'][0]
    #channel = params['channel_name'][0]
    command_text = params['text'][0]
    arg = command_text.split(' ')
    helpmsg = DEPLOY_HELP + DESCRIBECLUSTERS_HELP + LIST_HELP + TASKS_HELP + CREATESERVICE_HELP
    deployusers = RESOURCE_DYN.Table(DEPLOYUSERS_TABLE)

    if arg[0] == "ping":
        return ping(arg[1], user)

    elif arg[0] == "hi":
        return respondchannel(None, "Hi %s!" % (user))

    elif arg[0] == "describe":
        if arg[1] == "cluster":
            return describeclusters()
        if arg[1] == "config":
            appname = arg[2] + '-' + SHORT_ENV
            return describeappconfig(appname)

    elif arg[0] == "list":
        if arg[1] == "instances":
            return listinstances()
        elif arg[1] == "clusters":
            return listclusters()
        elif arg[1] == "services":
            return listservices()

    elif arg[0] == "task":
        getdeployusers = deployusers.scan(
            Select="ALL_ATTRIBUTES",
            FilterExpression=Attr('username').eq(user))
        if not getdeployusers['Items']:
            return respondchannel(
                None,
                'You have no permission to perform this task, %s' % (user))
        else:
            if arg[1] == "register":
                appname = arg[2] + '-' + SHORT_ENV
                return taskregister(appname, arg[3])
            elif arg[1] == "update":
                appname = arg[2] + '-' + SHORT_ENV
                return taskupdate(appname, arg[3])

    elif arg[0] == "deploy":
        print '*** User starting deploy: ' + user
        getdeployusers = deployusers.scan(
            Select="ALL_ATTRIBUTES",
            FilterExpression=Attr('username').eq(user))
        if not getdeployusers['Items']:
            print '*** You have no permission to deploy, %s' % (user)
            return respondchannel(
                None, 'You have no permission to deploy, %s' % (user))
        else:
            if arg[1] == "backend":
                appname = arg[2] + '-' + SHORT_ENV
                return deploybackend(user, appname, arg[3])
            if arg[1] == "dashboard":
                print '*** Starting Deploy \n '
                return deployfrontend(user, arg[2], FRONT_BUCKET,
                                      DASHBOARD_RELEASES_BUCKET, arg[1])
            if arg[1] == "rider":
                return deployfrontend(user, arg[2], RIDER_BUCKET,
                                      RIDER_RELEASES_BUCKET, arg[1])

    elif arg[0] == "create":
        print '*** User starting deploy: ' + user
        getdeployusers = deployusers.scan(
            Select="ALL_ATTRIBUTES",
            FilterExpression=Attr('username').eq(user))
        if not getdeployusers['Items']:
            print '*** You have no permission to deploy, %s' % (user)
            return respondchannel(
                None, 'You have no permission to deploy, %s' % (user))
        else:
            if arg[1] == "service":
                appname = arg[2] + '-' + SHORT_ENV
                return createservice(user, appname, arg[3])

    elif arg[0] == "help":
        topmsg = "Always use `/luke` or `luke_staging` before every command\n\n See reference:\n\n"
        return respondchannel(None, helpmsg, topmsg)

    else:
        return respondchannel(None, helpmsg,
                              "*UNRECOGNIZED COMMAND*\n\n See reference:")
예제 #60
0
def lambda_handler(event, context):
    
    logger.info("Event: " + json.dumps(event))
    
    if event['httpMethod'] == "GET":
        
        idGateway = event['pathParameters']['gateway']
        
        try:
            
            table = dynamodb.Table('iot_scanner')
            
            response = table.scan(
                FilterExpression=Attr('idGateway').eq(idGateway) & Attr('eventTime').eq(int(event['queryStringParameters']['last_update']))
            )
            
            items = list()
            
            for i in response['Items']:
                logger.info(i)
                i['eventTime'] = int(i['eventTime'])
                for tracking in i['tracking']:
                    for key, value in tracking['info'].items():
                        if isinstance(value, decimal.Decimal):
                            tracking['info'][key] = float(value)
                items.append(i)
            
            statusCode = 200
            mensaje = items
            
        except Exception as e:
            
            logger.info("Error: " + str(e))
            statusCode = 500
            mensaje = "Bad request."
    
    if event['httpMethod'] == "POST":
        
        try:
            params = json.loads(event['body']) if not isinstance(event['body'], dict) else event['body']
            table = dynamodb.Table('iot_scanner')
            logger.info("Params: " + json.dumps(event['body']))
        
            trackingLst = list()
        
            for info in params['beaconScan']:
                trackingLst.append(info)
        
            dbItem = { 
                "_id": uuid.uuid4().hex,
                "idGateway": event['pathParameters']['gateway'],
                "eventTime": params['eventTime'],
                "tracking": trackingLst
            }
        
            table.put_item(
                Item = dbItem
            )
            
            statusCode = 200
            mensaje = "Success."
            
        except Exception as e:
            
            logger.info("Error: " + str(e))
            statusCode = 500
            mensaje = "Bad request."
            
            
    content = {
        "requestDate": datetime.datetime.utcnow().strftime('%m/%d/%Y %H:%M:%S'),
        "mensaje": mensaje
    }
    
    return {
        "isBase64Encoded": False,
        "statusCode": statusCode,
        "body": json.dumps(content),
        "headers": {
            'Content-Type': 'application/json',
            'charset': 'utf8',
            'Access-Control-Allow-Origin': '*'
        }
    }