-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
359 lines (305 loc) · 11.8 KB
/
app.py
File metadata and controls
359 lines (305 loc) · 11.8 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import connexion
from database import init_db
import logging
from flask import request, jsonify, current_app
from services.user_service import UserService
import json
db_session = None
def _get_response(message: str, code: int, is_custom_obj: bool = False):
"""
This method contains the code to make a new response for flask view
:param message: Message response
:param code: Code result
:return: a json object that look like {"result": "OK"}
"""
if is_custom_obj is False:
return {"result": message}, code
return message, code
def serialize(obj):
return dict([(k, v) for k, v in obj.__dict__.items() if k[0] != "_"])
# time objects are not serializeble in JSON so they are changed in strings
def JSON_serialization(obj_dict):
for key, value in obj_dict.items():
if str(type(value)) == "<class 'datetime.time'>":
obj_dict.update({key: value.strftime("%H:%M")})
elif str(type(value)) == "<class 'datetime.datetime'>":
obj_dict.update({key: value.strftime("%Y-%m-%dT%H:%M:%SZ")})
elif str(type(value)) == "<class 'decimal.Decimal'>":
obj_dict.update({key: float(value)})
return obj_dict
def list_obj_json(name_list, list_objs):
objects = []
for obj in list_objs:
objects.append(JSON_serialization(serialize(obj)))
if len(name_list) == 0:
return objects
else:
list_json = json.dumps({name_list: objects})
return json.loads(list_json)
def create_user():
"""
This flask method give the possibility to create a new customer
with a json request
:return: the correct response that looks like {"result": "OK"}, 200
:return:
"""
if request.method == "POST":
json = request.get_json()
email = json["email"]
phone = json["phone"]
user = UserService.user_is_present(db_session, email, phone)
if user is not None:
return _get_response(
"User with email {} and/or phone already exist".format(email, phone),
500,
)
user = UserService.create_user(db_session, json)
if user is not None:
return _get_response("OK", 200)
else:
return _get_response(
"User not created because we have an error on server", 500
)
return _get_response("Resource not found", 404)
def create_operator():
"""
This flask method give the possibility to create a new operator
with a json request
:return: the correct response that looks like {"result": "OK"}, 200
"""
if request.method == "POST":
json = request.get_json()
email = json["email"]
phone = json["phone"]
user = UserService.user_is_present(db_session, email, phone)
if user is not None:
return _get_response(
"User with email {} and/or phone already exist".format(email, phone),
412,
)
user = UserService.create_user(db_session, json, 2)
if user is not None:
return _get_response("OK", 200)
else:
return _get_response(
"User not created because we have an error on server", 500
)
return _get_response("Resource not found", 404)
def modify_user():
"""
This API method contains the logic to modify a new user
:return: the correct response
"""
json = request.get_json()
current_app.logger.debug("Modify user with id {}".format(id))
current_app.logger.debug("Request content \n{}".format(json))
if request.method == "PUT":
user = UserService.modify_user(db_session, json)
if user is not None:
current_app.logger.debug(
"User after modify operation \n{}".format(user.serialize())
)
is_positive = UserService.user_is_positive(db_session, user.id)
json_user = user.serialize()
if is_positive is None:
json_user["is_positive"] = False
else:
json_user["is_positive"] = True
return _get_response(json_user, 200, True)
current_app.logger.debug("User is none? {}".format(user is None))
return _get_response("Resource not found", 404)
def delete_user(id):
"""
This API method contains the logic to delete a user on database
:return: the correct response that looks like {"result": "OK"}, 200
"""
if request.method == "DELETE":
user = UserService.user_is_present_with_id(db_session, id)
if user is None:
current_app.logger.warning(
"The user with id {} doesn't exist, I can not delete it".format(id)
)
return _get_response("User doesn't exist", 404)
UserService.delete_user(db_session, id)
return _get_response("OK", 200)
return _get_response("Resource not found", 404)
def login_user():
"""
This API method contains the logic authenticate the user
:return: the correct response contains the user with id role
"""
if request.method == "POST":
json = request.get_json()
user = UserService.user_login(
db_session, email=json["email"], password=json["password"]
)
if user is None:
return _get_response(
"User with email {} not present".format(json["email"]), 404
)
is_positive = UserService.user_is_positive(db_session, user.id)
json_user = user.serialize()
if is_positive is None:
json_user["is_positive"] = False
else:
json_user["is_positive"] = True
return _get_response(json_user, 200, True)
return _get_response("Resource not found", 404)
def user_is_present_by_email():
"""
This method perform the research of the user by email
:return: the correct response.
"""
if request.method == "POST":
json = request.get_json()
current_app.logger.debug("Request received: {}".format(json))
email = json["email"]
current_app.logger.debug("User email {}".format(email))
user = UserService.user_is_present(db_session, email)
if user is None:
return _get_response("User not found", 404)
is_positive = UserService.user_is_positive(db_session, user.id)
json_user = user.serialize()
if is_positive is None:
json_user["is_positive"] = False
else:
json_user["is_positive"] = True
return _get_response(json_user, 200, True)
return _get_response("User not found", 404)
def user_is_present_by_phone():
"""
This method perform the research of the user by phone
:return: :return: the correct response.
"""
if request.method == "POST":
json = request.get_json()
current_app.logger.debug("Request received: {}".format(json))
phone = json["phone"]
current_app.logger.debug("User phone {}".format(phone))
user = UserService.user_is_present(db_session, phone=phone)
if user is None:
return _get_response("User not found", 404)
is_positive = UserService.user_is_positive(db_session, user.id)
json_user = user.serialize()
if is_positive is None:
json_user["is_positive"] = False
else:
json_user["is_positive"] = True
return _get_response(json_user, 200, True)
return _get_response("User not found", 404)
def get_role_by_id(role_id):
role = UserService.get_role_value(db_session, role_id)
if role is None:
return _get_response("Role not found", 404)
return _get_response(role.serialize(), 200, True)
def get_user_by_email():
"""
thi method returns the user givene the email in the POST request
"""
json = request.get_json()
user = UserService.get_user_by_email(db_session, json["email"])
if user is None:
return _get_response("User not found", 404)
is_positive = UserService.user_is_positive(db_session, user.id)
json_user = user.serialize()
if is_positive is None:
json_user["is_positive"] = False
else:
json_user["is_positive"] = True
return _get_response(json_user, 200, True)
def get_user_by_id(id):
user = UserService.get_user_by_id(db_session, id)
if user is None:
if user is None:
return _get_response("User not found", 404)
is_positive = UserService.user_is_positive(db_session, user.id)
json_user = user.serialize()
if is_positive is None:
json_user["is_positive"] = False
else:
json_user["is_positive"] = True
return _get_response(json_user, 200, True)
def report_positive():
users = UserService.report_positive(db_session)
json_users = list_obj_json("users", users)
return _get_response(json_users, 200, True)
def mark_positive(key, value):
if key == "email":
user_email = value
user_phone = None
elif key == "phone":
user_phone = value
user_email = None
else:
return _get_response("Bad Request", 400)
result = UserService.mark_user_as_positive(db_session, user_email, user_phone)
if result is False:
return _get_response("User already positive", 409)
return _get_response("OK", 200)
def unmark_a_positive_user():
body = request.get_json()
if body["key"] == "email":
user = UserService.user_is_present(db_session=db_session, email=body["value"])
elif body["key"] == "phone":
user = UserService.user_is_present(db_session=db_session, phone=body["value"])
else:
return _get_response("Bad Request", 400)
if user is None or user.role_id != 3:
return _get_response("User not found", 404)
if UserService.unmark_user_as_not_positive(db_session, user.id) is False:
return _get_response("User not positive", 404)
else:
return _get_response("Ok", 200)
def check_user_is_positive(key, value):
if key == "email":
user = UserService.user_is_present(db_session=db_session, email=value)
elif key == "phone":
user = UserService.user_is_present(db_session=db_session, phone=value)
else:
return _get_response("Bad Request", 400)
if user is None or user.role_id != 3:
return _get_response("User not found", 404)
positive = UserService.user_is_positive(db_session, user.id)
if positive is None:
return _get_response("Not positive", 200)
else:
return _get_response("Positive", 200)
def get_positive_info(key, value):
if key == "email":
user = UserService.user_is_present(db_session=db_session, email=value)
elif key == "phone":
user = UserService.user_is_present(db_session=db_session, phone=value)
else:
return _get_response("Bad Request", 400)
if user is None or user.role_id != 3:
return _get_response("User not found", 404)
positive = UserService.user_is_positive(db_session, user.id)
if positive is None:
return _get_response("Information not found", 404)
else:
return _get_response(positive.serialize(), 200, True)
# --------- END API definition --------------------------
logging.basicConfig(level=logging.DEBUG)
app = connexion.App(__name__)
if "GOUOUTSAFE_TEST" in os.environ and os.environ["GOUOUTSAFE_TEST"] == "1":
db_session = init_db("sqlite:///tests/user.db")
else:
db_session = init_db("sqlite:///user.db")
app.add_api("swagger.yml")
# set the WSGI application callable to allow using uWSGI:
# uwsgi --http :8080 -w app
application = app.app
def _init_flask_app(flask_app, conf_type: str = "config.DebugConfiguration"):
"""
This method init the flask app
:param flask_app:
"""
flask_app.config.from_object(conf_type)
@application.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
if __name__ == "__main__":
# _init_flask_app(application)
_init_flask_app(application, "config.BaseConfiguration")
app.run(port=5002)