-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
77 lines (71 loc) · 2.61 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Flask backend with DynamoDB
Client -> Flask -> DynamoDB
"""
import boto3
import json
from flask import Flask
from flask_cors import CORS,cross_origin
from flask import jsonify,request
#Add logging
#https://docs.aws.amazon.com/lambda/latest/dg/python-logging.html
import logging
"""
Defaults
"""
logger = logging.getLogger()
logger.setLevel(logging.INFO)
app = Flask(__name__)
dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
table = dynamodb.Table('citizens')
logger.info("Connected to DynamoDB..")
"""
Get all citizens present in the DB
"""
@app.route("/getcitizens")
@cross_origin()
def get_citizens():
"""
returns all citizen items present in dynamodb
"""
response = table.scan()["Items"]
logger.info("All citizens returned")
return jsonify(response)
@app.route("/getmatchingcitizens")
@cross_origin()
def get_matching_citizens():
"""
returns matching citizen items based on X-volunteer header passed in request
"""
try:
volunteer = request.headers.get('X-volunteer')
except:
return jsonify("X-volunteer header is missing")
logger.info("X-volunteer header is missing")
vaibhav_interests = ['sleeping','home building','garden walks']
arsalan_interests = ['music','politics','science','reading']
senior_list = table.scan()["Items"]
if request.headers['X-volunteer'] == "Vaibhav":
dummy_volunteer_interest_list = vaibhav_interests
matching_list = []
for senior in senior_list:
match = len(set(dummy_volunteer_interest_list) & set(senior['interests'])) / float(len(set(dummy_volunteer_interest_list) | set(senior['interests']))) * 100
if match >= 20:
matching_list.append(senior)
if len(matching_list) == 0:
return(jsonify("No matches found!"))
logger.info("Vaibhav Matching citizens returned")
elif request.headers['X-volunteer'] == "Arsalan":
dummy_volunteer_interest_list = arsalan_interests
matching_list = []
# senior_list = [post for post in posts.find()]
for senior in senior_list:
match = len(set(dummy_volunteer_interest_list) & set(senior['interests'])) / float(len(set(dummy_volunteer_interest_list) | set(senior['interests']))) * 100
if match >= 20:
matching_list.append(senior)
if len(matching_list) == 0:
return jsonify("No matches found!")
logger.info("Arsalan Matching citizens returned")
else:
return jsonify("Send a valid user header!")
return jsonify(matching_list)