-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusers.py
129 lines (119 loc) · 3.54 KB
/
users.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
#!/usr/bin/env python3
""" Module of Users views
"""
from api.v1.views import app_views
from flask import abort, jsonify, request
from models.user import User
@app_views.route('/users', methods=['GET'], strict_slashes=False)
def view_all_users() -> str:
""" GET /api/v1/users
Return:
- list of all User objects JSON represented
"""
all_users = [user.to_json() for user in User.all()]
return jsonify(all_users)
@app_views.route('/users/<user_id>', methods=['GET'], strict_slashes=False)
def view_one_user(user_id: str = None) -> str:
""" GET /api/v1/users/:id
Path parameter:
- User ID
Return:
- User object JSON represented
- 404 if the User ID doesn't exist
"""
if user_id is None:
abort(404)
if user_id == "me":
if request.current_user is None:
abort(404)
user = request.current_user
return jsonify(user.to_json())
user = User.get(user_id)
if user is None:
abort(404)
if request.current_user is None:
abort(404)
return jsonify(user.to_json())
@app_views.route('/users/<user_id>', methods=['DELETE'], strict_slashes=False)
def delete_user(user_id: str = None) -> str:
""" DELETE /api/v1/users/:id
Path parameter:
- User ID
Return:
- empty JSON is the User has been correctly deleted
- 404 if the User ID doesn't exist
"""
if user_id is None:
abort(404)
user = User.get(user_id)
if user is None:
abort(404)
user.remove()
return jsonify({}), 200
@app_views.route('/users', methods=['POST'], strict_slashes=False)
def create_user() -> str:
""" POST /api/v1/users/
JSON body:
- email
- password
- last_name (optional)
- first_name (optional)
Return:
- User object JSON represented
- 400 if can't create the new User
"""
rj = None
error_msg = None
try:
rj = request.get_json()
except Exception as e:
rj = None
if rj is None:
error_msg = "Wrong format"
if error_msg is None and rj.get("email", "") == "":
error_msg = "email missing"
if error_msg is None and rj.get("password", "") == "":
error_msg = "password missing"
if error_msg is None:
try:
user = User()
user.email = rj.get("email")
user.password = rj.get("password")
user.first_name = rj.get("first_name")
user.last_name = rj.get("last_name")
user.save()
return jsonify(user.to_json()), 201
except Exception as e:
error_msg = "Can't create User: {}".format(e)
return jsonify({'error': error_msg}), 400
@app_views.route('/users/<user_id>', methods=['PUT'], strict_slashes=False)
def update_user(user_id: str = None) -> str:
""" PUT /api/v1/users/:id
Path parameter:
- User ID
JSON body:
- last_name (optional)
- first_name (optional)
Return:
- User object JSON represented
- 404 if the User ID doesn't exist
- 400 if can't update the User
"""
if user_id is None:
abort(404)
user = User.get(user_id)
if user is None:
abort(404)
rj = None
try:
rj = request.get_json()
except Exception as e:
rj = None
if rj is None:
return jsonify({'error': "Wrong format"}), 400
if rj.get('first_name') is not None:
user.first_name = rj.get('first_name')
if rj.get('last_name') is not None:
user.last_name = rj.get('last_name')
user.save()
return jsonify(user.to_json()), 200