-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
196 lines (153 loc) · 6.38 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
196
from flask import Flask,request
from flask_cors import CORS
from flask_restful import Api,Resource,abort
from pymongo import MongoClient
from bson import ObjectId
from bson import json_util
import datetime
import json
import os
import form_schema
import bcrypt
import jwt
from jwt.exceptions import InvalidSignatureError,ExpiredSignatureError
from werkzeug.exceptions import Unauthorized,BadRequest
from dotenv import load_dotenv
import gpxpy
app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
api = Api(app)
load_dotenv()
access_token_secret_key = os.environ.get('ACCESS_TOKEN_SECRET_KEY')
mongo_url = os.environ.get('MONGO_URL')
client = MongoClient(mongo_url)
database = client["haata_ai_database"]
registration_form_schema = form_schema.RegistrationFormSchema()
log_in_form_schema = form_schema.LogInFormSchema()
class Register(Resource):
def post(self):
data = request.json
if registration_form_schema.validate(data):
print(registration_form_schema.validate(data))
abort(400,message="Invalid input")
user_info = database.users.find_one({"email":data["email"]})
if user_info == None:
data["password"] = bcrypt.hashpw(data["password"].encode("utf-8"),bcrypt.gensalt())
database.users.insert_one(data)
token = jwt.encode({"email":data['email'],"exp":datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(hours=6)},access_token_secret_key,algorithm = "HS256")
return {"access_token":token}
else:
abort(409,message = "This email is already in use")
class LogIn(Resource):
def post(self):
data = request.json
if log_in_form_schema.validate(data):
abort(400,message="Invalid input")
user_info = database.users.find_one({"email":data["email"]})
if user_info == None:
abort(401,message = "User with this email does not exist")
else:
if bcrypt.checkpw(data["password"].encode("utf-8"),user_info["password"]):
token = jwt.encode({"email":data['email'],"exp":datetime.datetime.now(tz=datetime.timezone.utc) + datetime.timedelta(hours=6)},access_token_secret_key,algorithm = "HS256")
return {"access_token":token}
else:
abort(401,message="Wrong password")
class WalkabilityDataEntry(Resource):
def post(self):
try:
sent_files = request.files
if len(dict(sent_files)) == 0:
raise("No file attached")
elif len(dict(sent_files)) > 1:
raise("Too many files attached")
if 'path' not in dict(sent_files):
raise BadRequest("The key of the sent file must be path")
if sent_files['path'].filename.split('.')[1] != "gpx":
raise BadRequest("Unsopported file type")
gpx_file = sent_files['path'].read()
form_data = request.form
if len(dict(form_data)) == 0:
raise BadRequest("Rating not provided")
if'path_rating' not in dict(form_data):
raise BadRequest("The key of the sent file must be path_rating")
path_rating = form_data.to_dict(flat=True)['path_rating']
path_rating = json.loads(path_rating)
#validate path_rating using schema here
token = request.headers.get('Authorization')
if token == None:
raise Unauthorized("Authorization required")
payload = jwt.decode(token,key=access_token_secret_key,verify=True,algorithms = ["HS256"])
user_info = database.users.find_one({"email":payload["email"]})
if user_info == None:
abort(401,message = "User with this email does not exist")
gpx = gpxpy.parse(gpx_file)
coordinates = []
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
coordinates.append({"latitude":point.latitude,"longitude":point.longitude,"elavation":point.elevation,"time":point.time.strftime("%Y-%m-%d")})
database.paths.insert_one({"coordinates":coordinates,"rating":path_rating['rating'],"permanent_obstacle_count":path_rating['permanent_obstacle_count'],"temporary_obstacle_count":path_rating['temporary_obstacle_count'],"hazard":path_rating['hazard'],"cleanliness":path_rating['cleanliness'],"safety":path_rating['safety'],"congestion":path_rating['congestion'],"width":path_rating['width'],"user_id":str(user_info['_id'])})
return {"message":"Successfully created"}
except BadRequest as e:
abort(400,message=e.description)
except Unauthorized as e:
abort(401,message=e.description)
except InvalidSignatureError as e:
abort(498,message="Invalid token")
except ExpiredSignatureError as e:
abort(401,message='Token expired')
except Exception as e:
abort(400,message="Could not process request")
class CreatedPaths(Resource):
def get(self):
try:
token = request.headers.get('Authorization')
if token == None:
raise Unauthorized("Authorization required")
payload = jwt.decode(token,key=access_token_secret_key,verify=True,algorithms = ["HS256"])
user_info = database.users.find_one({"email":payload["email"]})
if user_info == None:
abort(401,message = "User with this email does not exist")
documents = database.paths.find({"user_id":str(user_info["_id"])})
paths = []
for document in documents:
document.pop("_id")
document.pop("user_id")
paths.append(document)
return {"paths":paths}
except Unauthorized as e:
abort(401,message=e.description)
except InvalidSignatureError as e:
abort(498,message="Invalid token")
except ExpiredSignatureError as e:
abort(401,message='Token expired')
except Exception as e:
abort(400,message="Could not process request")
class UserInfo(Resource):
def get(self):
try:
token = request.headers.get('Authorization')
if token == None:
raise Unauthorized("Authorization required")
payload = jwt.decode(token,key=access_token_secret_key,verify=True,algorithms = ["HS256"])
user_info = database.users.find_one({"email":payload["email"]})
if user_info == None:
abort(401,message = "User with this email does not exist")
user_info.pop("_id")
user_info.pop("password")
return user_info
except Unauthorized as e:
abort(401,message=e.description)
except InvalidSignatureError as e:
abort(498,message="Invalid token")
except ExpiredSignatureError as e:
abort(401,message='Token expired')
except Exception as e:
abort(400,message="Could not process request")
api.add_resource(Register,"/register")
api.add_resource(LogIn,"/login")
api.add_resource(WalkabilityDataEntry,"/walkabilitydataentry")
api.add_resource(UserInfo,"/userinfo")
api.add_resource(CreatedPaths,"/createdpaths")
if __name__ == "__main__":
app.run()