-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpool.py
189 lines (147 loc) · 5.42 KB
/
pool.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
185
186
187
188
189
"""
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 User, Transaction, TransactionSchema, Pool, PoolSchema
def read_user_pools(user_id):
"""
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
user = (
User.query.filter(User.user_id == user_id)
.one_or_none()
)
# Did we find a person?
if user is not None:
pools = Pool.query\
.filter(Transaction.user_id == user_id)\
.outerjoin(Pool, Transaction.pool_id == Pool.pool_id)\
.order_by(db.desc(Pool.pool_tmodified)).all()
# Serialize the list of notes from our data
pool_schema = PoolSchema(many=True, exclude=[])
data = pool_schema.dump(pools).data
return data
# Otherwise, nope, didn't find that person
else:
abort(404, "Pool not found for user ID: {user_id}")
def read_all(pool_ids=None):
"""
This function responds to a request for /api/people/notes
with the complete list of notes, sorted by note timestamp
:return: json list of all notes for all people
"""
# Query the database for all the notes
if not pool_ids:
pools = Pool.query.order_by(db.desc(Pool.pool_tmodified)).all()
else:
pools = Pool.query \
.filter(Pool.pool_id.in_(pool_ids)) \
.all()
# Serialize the list of notes from our data
pool_schema = PoolSchema(many=True, exclude=[])
data = pool_schema.dump(pools).data
return data
def read_one(pool_id):
"""
This function responds to a request for
/api/user/{user_id}/pools/{pool_id}
with one matching note for the associated person
:param person_id: Id of person the note is related to
:param note_id: Id of the note
:return: json string of note contents
"""
# Query the database for the note
pool = (
Pool.query.filter(Pool.pool_id == pool_id)
.outerjoin(Transaction, Transaction.pool_id == pool_id) \
.outerjoin(User, Transaction.user_id == User.user_id) \
.one_or_none()
)
# Was a note found?
if pool is not None:
pool_schema = PoolSchema()
data = pool_schema.dump(pool).data
return data
# Otherwise, nope, didn't find that note
else:
abort(404, "Pool not found for ID: {pool_id}".format(pool_id=pool_id))
def create(user_id, pool):
"""
This function creates a new note related to the passed in person id.
:param person_id: Id of the person the note is related to
:param note: The JSON containing the note data
:return: 201 on success
"""
# get the parent person
user = User.query.filter(User.user_id == user_id).one_or_none()
# Was a person found?
if user is None:
abort(404, "User not found for Id: {user_id}".format(user_id=user_id))
# Create a note schema instance
schema = PoolSchema()
new_pool = schema.load(pool, session=db.session).data
# Add the note to the person and database
user.pool.append(new_pool)
db.session.commit()
# Serialize and return the newly created note in the response
data = schema.dump(new_pool).data
return data, 201
def update(user_id, pool_id, pool):
"""
This function updates an existing note related to the passed in
person id.
:param person_id: Id of the person the note is related to
:param note_id: Id of the note to update
:param content: The JSON containing the note data
:return: 200 on success
"""
update_pool = (
Pool.query.filter(User.user_id == user_id)
.filter(Pool.pool_id == pool_id)
.one_or_none()
)
# Did we find an existing note?
if update_pool is not None:
# turn the passed in note into a db object
schema = PoolSchema()
update = schema.load(pool, session=db.session).data
# Set the id's to the note we want to update
update.user_id = update_pool.user_id
update.pool_id = update_pool.pool_id
# merge the new object into the old and commit it to the db
db.session.merge(update)
db.session.commit()
# return updated note in the response
data = schema.dump(update_pool).data
return data, 200
# Otherwise, nope, didn't find that note
else:
abort(404, "Pool not found for Id: {pool_id}".format(pool_id=pool_id))
def delete(user_id, pool_id):
"""
This function deletes a note from the note structure
:param person_id: Id of the person the note is related to
:param note_id: Id of the note to delete
:return: 200 on successful delete, 404 if not found
"""
# Get the note requested
pool = (
Pool.query.filter(User.user_id == user_id)
.filter(Pool.pool_id == pool_id)
.one_or_none()
)
# did we find a note?
if pool is not None:
db.session.delete(pool)
db.session.commit()
return make_response(
"Note {pool_id} deleted".format(pool_id=pool_id), 200
)
# Otherwise, nope, didn't find that note
else:
abort(404, "Pool not found for Id: {pool_id}".format(pool_id=pool_id))