-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcontrollers.py
More file actions
319 lines (249 loc) · 8.31 KB
/
controllers.py
File metadata and controls
319 lines (249 loc) · 8.31 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import bcrypt
from dotenv import find_dotenv, load_dotenv
from flask import jsonify, request
from flask_jwt_extended import (
JWTManager,
create_access_token,
get_jwt_identity,
jwt_required,
)
from googlemaps import Client
from main import app, ma
from models import User, db
load_dotenv(dotenv_path=find_dotenv())
jwt = JWTManager(app)
salt = bcrypt.gensalt()
# Access the environment variables
secret_key = os.getenv("VITE_GOOGLE_MAPS_API_KEY")
gmaps = Client(key=secret_key)
# Auto generate Schema using models
class UserSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = User
matched_users = {}
# searching_users = {
# 1: {
# "currentloc": {"lat": 20.01840125104431, "lng": 72.8372607837342},
# "dest": "asc",
# },
# 9: {
# "currentloc": {"lat": 20.01840125104431, "lng": 72.8372607837342},
# "dest": "asc",
# },
# }
searching_users = {}
@app.post("/api/login")
def login():
# Getting user Creds
userdata = request.get_json()
email = userdata["email"]
password = userdata["password"].encode()
# Getting Creds from db
curr_user = User.query.filter_by(email=email).first()
# checking creds
if curr_user is None:
return jsonify({"msg": "Invalid Email"}), 401
elif bcrypt.checkpw(password, curr_user.password):
# Creating JWT token
access_token = create_access_token(identity=curr_user.id)
return jsonify(access_token=access_token)
else:
return jsonify({"msg": "Bad password"}), 401
# Create User
@app.post("/api/register")
def register():
userdata = request.get_json()
if not userdata["username"] or not userdata["password"]:
return jsonify({"msg": "Please enter username and pasword"}), 400
usr = User.query.filter_by(username=userdata["username"]).first()
em = User.query.filter_by(email=userdata["email"]).first()
if usr or em:
return jsonify({"msg": "User already Exists"}), 400
password = userdata["password"].encode()
# Hashing Password
hashed_pass = bcrypt.hashpw(password, salt)
# commiting Data
db.session.add(
User(
username=userdata["username"],
age=userdata["age"],
gender=userdata["gender"],
pgender=userdata["pgender"],
pagegrp=userdata["pagegrp"],
email=userdata["email"],
password=hashed_pass,
)
)
db.session.commit()
return jsonify("Success")
@app.post("/api/destination")
def destination():
autocomplete_data = request.get_json()
# coordinates = autocomplete_data["geometry"]["location"]
name = autocomplete_data["name"]
return name
@app.post("/api/route")
def generate_route():
locations = request.get_json()
waypoint = locations["waypoint"]
destination = locations["destination"]
# maps_link = f"https://www.google.com/maps/dir/My+Location/{waypoint['lat']},{waypoint['lng']}/{destination['lat']},{destination['lng']} "
maps_link = f"https://www.google.com/maps/dir/My+Location/{waypoint['lat']},{waypoint['lng']}/{destination}"
return maps_link
# Adds users to a searching queue
@app.post("/api/search")
@jwt_required()
def search():
current_userid = get_jwt_identity()
data = request.get_json()
global searching_users
user_obj = {"currentloc": data["loc"], "dest": data["dest"]}
searching_users.update({current_userid: user_obj})
return searching_users
# Compares curr user with all other users in the search queue and matchs with nearest one
@app.get("/api/match")
@jwt_required()
def match():
global searching_users
current_userid = get_jwt_identity()
origin = searching_users[current_userid]["currentloc"]
if origin is None:
return jsonify("Location not enabled"), 400
destinations = []
# print(searching_users)
# print(len(searching_users))
if len(searching_users) < 2:
return jsonify("No other user currently searching"), 401
for user_id, user_data in searching_users.items():
if user_id == current_userid:
continue
currentloc = user_data.get("currentloc", {})
if currentloc:
destination = user_data.get("dest", "")
result = gmaps.distance_matrix(
origins=origin,
destinations=[currentloc],
units="metric",
mode="walking",
)
if result and "rows" in result and result["rows"]:
elements = result["rows"][0]["elements"]
if elements and elements[0]["status"] == "OK":
distance_text = elements[0]["distance"].get("value")
else:
distance_text = "N/A"
else:
distance_text = "N/A"
# print(distance_text)
if (
distance_text < 50000
and destination == searching_users[current_userid]["dest"]
):
response_data = {
"user_id": user_id,
"destination": destination,
"distance": distance_text,
"loc": currentloc,
}
destinations.append(response_data)
# print(len(destinations), destinations)
if len(destinations) == 0:
return jsonify("No user near you!"), 402
destinations = sorted(destinations, key=lambda x: x["distance"])
global matched_users
user1 = {
"user_id": current_userid,
"accepted": False,
"loc": origin,
"matched_id": destinations[0]["user_id"],
}
user2 = {
"user_id": destinations[0]["user_id"],
"accepted": False,
"loc": destinations[0]["loc"],
"matched_id": current_userid,
}
if user1["user_id"] not in matched_users:
matched_users[user1["user_id"]] = user1
if user2["user_id"] not in matched_users:
matched_users[user2["user_id"]] = user2
# print(matched_users)
return matched_users
# Checks if a user has accepted
@app.get("/api/accepted")
@jwt_required()
def origin_accepted():
current_userid = get_jwt_identity()
global matched_users
try:
matched_users[current_userid]["accepted"] = True
return matched_users
except: # noqa
return "no user is matched with current user.", 401
@app.get("/api/reject")
@jwt_required()
def reject():
global matched_users
global searching_users
current_userid = get_jwt_identity()
try:
other_user_id = matched_users[current_userid]["matched_id"]
del searching_users[current_userid]
del matched_users[current_userid]
del searching_users[other_user_id]
del matched_users[other_user_id]
except:
return "Something went wrong. fix it", 400
return jsonify("Rejected")
# Checks if both user accepted
@app.get("/api/consent")
@jwt_required()
def consent():
current_userid = get_jwt_identity()
try:
other_user_id = matched_users[current_userid]["matched_id"]
if (
matched_users[current_userid]["accepted"]
and matched_users[other_user_id]["accepted"]
):
return "both accepted"
else:
return "both did not accepted", 401
except: # noqa
return "no user is matched with current user.", 401
@app.get("/api/id")
@jwt_required()
def id():
current_userid = get_jwt_identity()
user_detail = User.query.filter_by(id=current_userid).first()
# print(user_detail)
if user_detail:
user_data = {
"id": user_detail.id,
"username": user_detail.username,
"age": user_detail.age,
"gender": user_detail.gender,
}
return user_data, 200
else:
return {"message": "User not found"}
# return str(current_userid)
@app.get("/api/id/<int:matchedId>")
# @jwt_required()
def get_user_by_id(matchedId):
user_detail = User.query.filter_by(id=matchedId).first()
if user_detail:
user_data = {
"id": user_detail.id,
"username": user_detail.username,
"age": user_detail.age,
"gender": user_detail.gender,
}
print(user_data)
return user_data, 200
else:
return {"message": "User not found"}, 404
@app.route("/api")
def index():
return "I love automate"