-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhousehold.py
319 lines (265 loc) · 11.7 KB
/
household.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Creates the household-related endpoints.
"""
from logging import getLogger
from backend import db
from models.models import Household
from flask import request
from flask_restful import Resource, reqparse
from sqlalchemy.exc import SQLAlchemyError, InvalidRequestError, NoResultFound
from datetime import datetime, timezone
import json
logger = getLogger()
# Initialize a parser for the request parameters
base_parser = reqparse.RequestParser(trim=True)
# `id` is required for most requests
base_parser.add_argument("id", type=int, nullable=False, store_missing=False,
required=True)
def add_household_fields_to_parser(provided_parser) -> reqparse:
"""Adds the remaining household fields to a given parser."""
logger.debug("Adding household fields to the parser")
provided_parser.add_argument("nickname", type=str)
provided_parser.add_argument("first_names", type=str)
provided_parser.add_argument("surname", type=str)
provided_parser.add_argument("address_to", type=str)
provided_parser.add_argument("formal_name", type=str)
provided_parser.add_argument("relationship", type=str)
provided_parser.add_argument("relationship_type", type=str)
provided_parser.add_argument("family_side", type=str)
provided_parser.add_argument("kids", type=str)
provided_parser.add_argument("pets", type=str)
provided_parser.add_argument("should_receive_holiday_card", type=str)
provided_parser.add_argument("notes", type=str)
logger.debug("Done adding household fields to the parser")
return provided_parser
class HouseholdCollectionApi(Resource):
"""
Endpoint: /api/v1/all_households
Methods: GET
"""
@staticmethod
def get() -> json:
"""Return all households from the database. No arguments should be provided."""
logger.debug("Start of HouseholdCollectionAPI.GET")
# Retrieve all households from the db, sorted by id
try:
households = Household.query.order_by(Household.id).all()
logger.info(f"Successfully retrieved data for {households.__len__()} households.")
except SQLAlchemyError as e:
error_msg = f"SQLAlchemyError retrieving data: {e}"
logger.info(error_msg)
logger.debug("End of HouseholdCollectionAPI.GET")
return error_msg, 500
except BaseException as e:
error_msg = f"BaseException retrieving data: {e}"
logger.info(error_msg)
logger.debug("End of HouseholdCollectionAPI.GET")
return error_msg, 500
# Compile these data into a list
try:
output = []
for hh in households:
output.append(hh.to_dict())
logger.debug("End of HouseholdAPI.GET")
return output, 200
except BaseException as e:
error_msg = f"Error compiling data into a list of `dict` to return: {e}"
logger.info(error_msg)
logger.debug("End of HouseholdCollectionAPI.GET")
return error_msg, 500
class HouseholdApi(Resource):
"""
Endpoint: /api/v1/household
Methods: GET, POST, PUT, DELETE
"""
@staticmethod
def get() -> json:
"""
Returns a single Household record, based on the provided household id.
REQUIRED ARGUMENTS
key: id, type: int
"""
logger.debug("Start of HouseholdAPI.GET")
logger.debug(request)
# No need for additional parser args since the only required arg is `id`
# Parse the arguments provided
args = base_parser.parse_args()
# Validate that a household id was provided
try:
household_id = args["id"]
logger.debug(f"Household with id={household_id} was read successfully")
except KeyError as e:
logger.info(f"Error parsing household id: no value was provided. {e}")
return f"Must provide a household id.", 400
# Retrieve the selected record
try:
household = Household.query.get(household_id)
if household:
# Record successfully returned from the db
logger.info(f"Found the requested household!")
logger.debug(f"Household: {household.to_dict()}")
logger.debug("End of HouseholdAPI.GET")
return household.to_dict(), 200
else:
# No record with this id exists in the db
logger.info(f"No household found with id={household_id}.")
logger.debug("End of HouseholdAPI.GET")
return f"No household found with id={household_id}.", 404
except (InvalidRequestError, NoResultFound, AttributeError) as e:
error_msg = f"No household found with id={household_id}.\n{e}"
logger.info(error_msg)
logger.debug(f"End of HouseholdAPI.GET")
return error_msg, 404
@staticmethod
def post() -> json:
"""
Add a new household record to the database
REQUIRED ARGUMENTS
key: nickname, type: str
OPTIONAL ARGUMENTS
key: first_names, type: str
key: surname, type: str
key: address_to, type: str
key: formal_name, type: str
key: relationship, type: str
key: relationship_type, type: str
key: family_side, type: str
key: kids, type: str
key: pets, type: str
key: should_receive_holiday_card, type: int
key: notes, type: str
"""
# Add the other household args to our parser
parser = add_household_fields_to_parser(base_parser)
# Remove the `id` arg from our parser; the database will generate this
parser.remove_argument("id")
logger.debug(f"parser keys: {parser.args.__str__()}")
# Parse the arguments provided
args = parser.parse_args()
# Create a new Household record using the provided data
try:
logger.debug(f"Attempting to create a Household from the provided data.")
new_household = Household(**args)
db.session.add(new_household)
db.session.flush()
logger.info(f"New record flushed has id={new_household.id}")
logger.debug(f"Added new household record to the db session")
# Commit this new record so the db generates an id
logger.debug("Attempting to commit data")
db.session.commit()
logger.debug("Commit completed")
logger.info(f"New record successfully created: {new_household.to_dict()}")
# Return the household_id to the requester
logger.debug("End of HouseholdAPI.POST")
return new_household.id, 201
except SQLAlchemyError as e:
error_msg = f"Unable to create a new Household record.\n{e}"
logger.debug(error_msg)
logger.debug("End of HouseholdAPI.POST")
return error_msg, 500
@staticmethod
def put() -> json:
"""
Update an existing record using the provided household id.
REQUIRED ARGUMENTS
key: id, type: int
key: first_names, type: str
key: surname, type: str
key: address_to, type: str
key: formal_name, type: str
key: relationship, type: str
key: relationship_type, type: str
key: family_side, type: str
key: kids, type: str
key: pets, type: str
key: should_receive_holiday_card, type: int
key: notes, type: str
"""
# Add the other household fields to the expected arguments
parser = add_household_fields_to_parser(base_parser)
# Parse the arguments provided
args = parser.parse_args()
# Validate that a household_id was provided
try:
household_id = args["id"]
logger.debug(f"Household_id={household_id} was read successfully")
except KeyError as e:
logger.info(f"Error parsing household_id: no value was provided. {e}")
return "Must provide a value for household_id.", 400
try:
# Retrieve the specified household record
household = Household.query.get(household_id)
# Update this record with the provided data
household.nickname = args["nickname"]
household.first_names = args["first_names"]
household.surname = args["surname"]
household.address_to = args["address_to"]
household.formal_name = args["formal_name"]
household.relationship = args["relationship"]
household.relationship_type = args["relationship_type"]
household.family_side = args["family_side"]
household.kids = args["kids"]
household.pets = args["pets"]
household.should_receive_holiday_card = args["should_receive_holiday_card"]
household.notes = args["notes"]
household.last_modified = datetime.now(timezone.utc)
except SQLAlchemyError as e:
error_msg = f"Unable to update the Household record.\n{e}"
logger.debug(error_msg)
logger.debug("End of HouseholdAPI.PUT")
return error_msg, 500
try:
# Commit these changes to the db
logger.debug("Attempting to commit db changes")
db.session.commit()
logger.info("Changes saved to the database")
logger.debug("End of HouseholdAPI.PUT")
return household.id, 200
except SQLAlchemyError as e:
error_msg = f"Unable to update Household record.\n{e}"
logger.debug(error_msg)
logger.debug("End of HouseholdAPI.PUT")
return error_msg, 500
@staticmethod
def delete() -> json:
"""
Deletes a single Household record, based on the provided household id.
REQUIRED ARGUMENTS
key: id, type: int
"""
# No need for additional parser args since the only required arg is `id`
# Parse the arguments provided
args = base_parser.parse_args()
# Validate that a household id was provided
try:
household_id = args["id"]
logger.debug(f"Household id={household_id} was provided.")
except KeyError as e:
error_msg = f"Missing household id.\n{e}"
logger.info(error_msg)
logger.debug(f"End of HouseholdAPI.DELETE")
return error_msg, 400
try:
# Retrieve the selected record
household_to_delete = Household.query.get(household_id)
if household_to_delete:
# Record successfully returned from the db
logger.debug(f"Household record found. Attempting to delete it.")
db.session.delete(household_to_delete)
logger.debug("About to commit this delete to the db.")
db.session.commit()
logger.debug("Commit completed.")
logger.info("Household record successfully deleted.")
logger.debug("End of HouseholdAPI.GET")
return f"Successfully deleted household id: {household_id}", 200
else:
# No record with this id exists in the db
error_msg = f"No household found with id={household_id}."
logger.debug(error_msg)
logger.debug("End of HouseholdAPI.GET")
return error_msg, 404
except (InvalidRequestError, NoResultFound, AttributeError) as e:
error_msg = f"No household found with id={household_id}.\n{e}"
logger.debug(error_msg)
logger.debug(f"End of HouseholdAPI.GET")
return error_msg, 404