-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
195 lines (168 loc) · 6.3 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
from flask import Flask, request, jsonify
from flask_cors import CORS
from firebase_admin import auth, credentials, firestore, initialize_app
import functools
import requests
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
cred = credentials.Certificate("./serviceAccount.json")
default_app = initialize_app(cred)
db = firestore.client()
needs_ref = db.collection("needs")
product_ref = db.collection("products")
def read_env():
env = {}
envfile = open('.env', 'r')
for line in envfile:
data = line.strip().split('=')
env[data[0]] = data[1]
envfile.close()
return env
ENV = read_env()
GOOGLE_API_KEY = ENV['GOOGLE_API_KEY']
def authenticate():
message = {"message": "Authenticate."}
resp = jsonify(message)
resp.status_code = 401
resp.headers["WWW-Authenticate"] = 'Basic realm="My Realm"'
return resp
def requires_authorization(f):
@functools.wraps(f)
def decorated(*args, **kwargs):
id_token = request.headers["Authorization"].split("=").pop()
decoded_token = auth.verify_id_token(id_token)
uid = decoded_token["uid"]
if not uid:
return authenticate()
return f(*args, **kwargs)
return decorated
def filter_from_all(column, filter):
return {
doc.id: doc.to_dict() for doc in needs_ref.where(column, "in", filter).stream()
}
def query_from_fb(columns, values):
mappings = list(zip(columns, values))
mappings = [t for t in mappings if t[1]]
result = []
main_query = None
cat_query = None
prod_query = None
prod_done = False
for mapping in mappings:
if (
mapping[0] == columns[0]
or mapping[0] == columns[1]
or mapping[0] == columns[2]
):
if main_query is None:
main_query = needs_ref.where(mapping[0], "==", mapping[1])
else:
if len(mapping[1].split(",")) > 1:
main_query = main_query.where(
mapping[0], "in", list(map(str.strip, mapping[1].split(",")))
)
else:
main_query = main_query.where(mapping[0], "==", mapping[1])
elif mapping[0] == columns[3]:
prod_query = product_ref.where(
mapping[0], "in", list(map(str.strip, mapping[1].split(",")))
)
# THIS WHOLE LOGIC NEEDS TO BE RE WRITTEN AND SIMPLIFIED WITH THE SIMPLIFIED DB STRUCTURE.
if main_query:
area_data = {doc.id: doc.to_dict() for doc in main_query.stream()}
result = area_data
if cat_query:
cat_data = {doc.id: doc.to_dict() for doc in cat_query.stream()}
cat_list = list(set([l["category"] for l in list(cat_data.values())]))
if prod_query and main_query:
prod_done = True
prod_data = {doc.id: doc.to_dict() for doc in prod_query.stream()}
prod_list = {
id: data
for id, data in prod_data.items()
if data["category"] in cat_list
}
result = list(prod_list.values())
if main_query:
result = {
id: data
for id, data in area_data.items()
if data["products_id"] in list(prod_list.keys())
}
else:
result = {}
elif not prod_query and main_query:
result = {
id: data
for id, data in area_data.items()
if data["category"]
in cat_list # ToDo: There's a bug here: need to fetch the category by querying the product_id in products table.
}
else:
result = {}
if prod_query and not prod_done:
if main_query:
prod_list = list(map(str.strip, mappings[-1][1].split(",")))
result = {
id: data
for id, data in area_data.items()
if data["products_id"] in prod_list
}
else:
result = filter_from_all(mappings[-1][0], prod_list)
return result
def merge_id(id, obj):
obj.update(id=id)
return obj
@app.route("/api/v2/needs/status", methods=["POST"])
def put_needs():
needs = request.get_json()['needs']
to = "" if "to" not in request.args else request.args['to']
print(needs,to)
collection = db.collection("needs")
batch = db.batch()
for need in needs:
ref = collection.document(need)
batch.update(ref, {u'status': to})
query = batch.commit()
#print(query)
return "OK", 200
@app.route("/api/v2/needs", methods=["GET"])
def get_needs():
products = [] if not request.args else request.args['products'].split(",")
area = "" if "area" not in request.args else request.args['area']
query = db.collection("needs")
if( len(products) > 0 ):
query = query.where("products_id", "in", products)
if(area != ""):
query = query.where("location.area", "==", area)
needs = query.stream()
#print(jsonify(response(needs)))
return jsonify([merge_id(doc.id, doc.to_dict()) for doc in needs]), 200
@app.route("/api/v1/needs", methods=["GET"])
# @requires_authorization
def get_needs_by_location():
try:
args = ("area", "suburb", "category", "product")
fb_doc_cols = ("location.area", "location.suburb", "category", "products_id")
values = list(map(request.args.get, args))
if any(values):
return jsonify(query_from_fb(fb_doc_cols, values)), 200
else:
all_needs = {merge_id(doc.id, doc.to_dict()) for doc in needs_ref.stream()}
return jsonify(all_needs), 200
except Exception as e:
return f"An Error Occured: {e}"
@app.route("/ping", methods=["GET"])
def ping():
return "pong"
@app.route("/api/v1/location", methods=["GET"])
def getLocationFromString():
parameters = {'input': request.args.get('address'), 'inputtype': 'textquery', 'key': GOOGLE_API_KEY, 'fields': 'geometry'}
response = requests.get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', params=parameters)
if response.status_code == 200:
return jsonify(response.json()), 200
port = int(os.environ.get("PORT", 5000))
if __name__ == "__main__":
app.run(threaded=True, host="0.0.0.0", port=port, debug=True)