-
Notifications
You must be signed in to change notification settings - Fork 0
/
people.py
136 lines (110 loc) · 3.36 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
# -*- coding: utf-8 -*-
from datetime import datetime
# 3rd party modules
from flask import make_response
from flask import abort
def get_timestamp():
return datetime.now().strftime(("%Y-%m-%d %H:%M:%S"))
# Data to serve with our API
PEOPLE = {
"曹": {
"fname": "阿瞒",
"lname": "曹",
"timestamp": get_timestamp(),
},
"周": {
"fname": "公瑾",
"lname": "周",
"timestamp": get_timestamp(),
},
"赵": {
"fname": "子龙",
"lname": "赵",
"timestamp": get_timestamp(),
},
}
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
result = [PEOPLE[key] for key in sorted(PEOPLE.keys())]
print(result)
return result
def read_one(lname):
"""
This function responds to a request for /api/people/{lname}
with one matching person from people
:param lname: last name of person to find
:return: person matching last name
"""
# Does the person exist in people?
if lname in PEOPLE:
person = PEOPLE.get(lname)
# otherwise, nope, not found
else:
abort(
404, "Person with last name {lname} not found".format(lname=lname)
)
return person
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
"""
lname = person.get("lname", None)
fname = person.get("fname", None)
# Does the person exist already?
if lname not in PEOPLE and lname is not None:
PEOPLE[lname] = {
"lname": lname,
"fname": fname,
"timestamp": get_timestamp(),
}
return make_response(
"{lname} successfully created".format(lname=lname), 201
)
# Otherwise, they exist, that's an error
else:
abort(
406,
"Peron with last name {lname} already exists".format(lname=lname),
)
def update(lname, person):
"""
This function updates an existing person in the people structure
:param lname: last name of person to update in the people structure
:param person: person to update
:return: updated person structure
"""
# Does the person exist in people?
if lname in PEOPLE:
PEOPLE[lname]["fname"] = person.get("fname")
PEOPLE[lname]["timestamp"] = get_timestamp()
return PEOPLE[lname]
# otherwise, nope, that's an error
else:
abort(
404, "Person with last name {lname} not found".format(lname=lname)
)
def delete(lname):
"""
This function deletes a person from the people structure
:param lname: last name of person to delete
:return: 200 on successful delete, 404 if not found
"""
# Does the person to delete exist?
if lname in PEOPLE:
del PEOPLE[lname]
return make_response(
"{lname} successfully deleted".format(lname=lname), 200
)
# Otherwise, nope, person to delete not found
else:
abort(
404, "Person with last name {lname} not found".format(lname=lname)
)