-
Notifications
You must be signed in to change notification settings - Fork 1
/
people.py
148 lines (112 loc) · 4.06 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
"""
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, Note
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
"""
# Build the initial query
person = (
Person.query.filter(Person.person_id == person_id)
.outerjoin(Note)
.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, f"Person not found for 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)
# Add the person to the database
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, f"Person {fname} {lname} exists already")
def update(person_id, person):
"""
This function updates an existing person in the people structure
: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()
# Did we find an existing person?
if update_person is not None:
# 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
# Otherwise, nope, didn't find that person
else:
abort(404, f"Person not found for Id: {person_id}")
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(f"Person {person_id} deleted", 200)
# Otherwise, nope, didn't find that person
else:
abort(404, f"Person not found for Id: {person_id}")