forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
executable file
·80 lines (60 loc) · 1.88 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
#!/usr/bin/python3
""" View for City objects """
from flask import abort, jsonify, make_response, request
from api.v1.views import app_views
from models import storage
from models.user import User
@app_views.route("/users", methods=["GET"], strict_slashes=False)
def all_user():
"""
Retrieves the list of all User objects:
"""
users = storage.all(User)
user_list = [user.to_dict() for user in users.values()]
return jsonify(user_list)
@app_views.route("/users/<user_id>", methods=["GET"], strict_slashes=False)
def one_user(user_id):
"""
Retrieves an User object
"""
user_dict = storage.get(User, user_id)
if user_dict is None:
abort(404)
return jsonify(user_dict.to_dict())
@app_views.route("/users/<user_id>", methods=["DELETE"])
def delete_user(user_id):
"""
Deletes a User object
"""
user = storage.get(User, user_id)
if user is None:
abort(404)
storage.delete(user)
storage.save()
return jsonify({}), 200
@app_views.route("/users/", methods=["POST"], strict_slashes=False)
def create_user():
"""
create new User object
"""
if not request.get_json():
abort(400, description="Not a json")
data = request.get_json()
if data.get("name", None) is None:
abort(400, description="Missing name")
user = User(**data)
user.save()
return make_response(jsonify(user.to_dict()), 201)
@app_views.route("/users/<user_id>", methods=["PUT"], strict_slashes=False)
def update_user(user_id):
"""update User object"""
user = storage.get(User, user_id)
if not user:
abort(404)
if not request.get_json():
abort(400, description="Not a JSON")
for k, v in request.get_json().items():
if k not in ["id", "created_at", "updated_at"]:
setattr(user, k, v)
storage.save()
return make_response(jsonify(user.to_dict()), 200)