-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeople.py
185 lines (146 loc) · 5.11 KB
/
people.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
"""
This is the people module and supports all the REST actions for the
people data
"""
from flask import make_response, abort
from config import db
from models import Person, PersonSchema
def read_all():
"""
This function responds to a request for /api/people
with the complete lists of people
:return: json string of list of people
"""
# Create the list of people from our data
people = Person.query.order_by(Person.lname).all()
# Serialize the data for the response
person_schema = PersonSchema(many=True)
data = person_schema.dump(people)
return data
def read_one(person_id):
"""
This function responds to a request for /api/people/{person_id}
with one matching person from people
:param person_id: Id of person to find
:return: person matching id
"""
# Get the person requested
person = Person.query.filter(Person.person_id == person_id).one_or_none()
# Did we find a person?
if person is not None:
# Serialize the data for the response
person_schema = PersonSchema()
data = person_schema.dump(person)
return data
# Otherwise, nope, didn't find that person
else:
abort(
404,
"Person not found for Id: {person_id}".format(person_id=person_id),
)
def create(person):
"""
This function creates a new person in the people structure
based on the passed in person data
:param person: person to create in people structure
:return: 201 on success, 406 on person exists
"""
fname = person.get("fname")
lname = person.get("lname")
existing_person = (
Person.query.filter(Person.fname == fname)
.filter(Person.lname == lname)
.one_or_none()
)
# Can we insert this person?
if existing_person is None:
# Create a person instance using the schema and the passed in person
schema = PersonSchema()
new_person = schema.load(person, session=db.session)
#new_person = Person(fname=fname,lname=lname)
# Add the person to the database
print(new_person)
db.session.add(new_person)
db.session.commit()
# Serialize and return the newly created person in the response
data = schema.dump(new_person)
return data, 201
# Otherwise, nope, person exists already
else:
abort(
409,
"Person {fname} {lname} exists already".format(
fname=fname, lname=lname
),
)
def update(person_id, person):
"""
This function updates an existing person in the people structure
Throws an error if a person with the name we want to update to
already exists in the database.
:param person_id: Id of the person to update in the people structure
:param person: person to update
:return: updated person structure
"""
# Get the person requested from the db into session
update_person = Person.query.filter(
Person.person_id == person_id
).one_or_none()
# Try to find an existing person with the same name as the update
fname = person.get("fname")
lname = person.get("lname")
existing_person = (
Person.query.filter(Person.fname == fname)
.filter(Person.lname == lname)
.one_or_none()
)
# Are we trying to find a person that does not exist?
if update_person is None:
abort(
404,
"Person not found for Id: {person_id}".format(person_id=person_id),
)
# Would our update create a duplicate of another person already existing?
elif (
existing_person is not None and existing_person.person_id != person_id
):
abort(
409,
"Person {fname} {lname} exists already".format(
fname=fname, lname=lname
),
)
# Otherwise go ahead and update!
else:
# turn the passed in person into a db object
schema = PersonSchema()
update = schema.load(person, session=db.session)
# Set the id to the person we want to update
update.person_id = update_person.person_id
# merge the new object into the old and commit it to the db
db.session.merge(update)
db.session.commit()
# return updated person in the response
data = schema.dump(update_person)
return data, 200
def delete(person_id):
"""
This function deletes a person from the people structure
:param person_id: Id of the person to delete
:return: 200 on successful delete, 404 if not found
"""
# Get the person requested
person = Person.query.filter(Person.person_id == person_id).one_or_none()
# Did we find a person?
if person is not None:
db.session.delete(person)
db.session.commit()
return make_response(
"Person {person_id} deleted".format(person_id=person_id), 200
)
# Otherwise, nope, didn't find that person
else:
abort(
404,
"Person not found for Id: {person_id}".format(person_id=person_id),
)