-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
282 lines (233 loc) · 10.2 KB
/
card.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
"""Defines the card-related endpoints."""
from logging import getLogger
from datetime import date, datetime, timezone
from backend import db
from models.models import Card
from flask import request
from flask_restful import Resource, reqparse
from sqlalchemy.exc import SQLAlchemyError, InvalidRequestError, NoResultFound
import json
logger = getLogger()
# Initialize a parser for the request parameters
parser = reqparse.RequestParser(trim=True)
class CardCollectionApi(Resource):
"""
Endpoint: /api/v1/all_cards
Methods: GET
"""
@staticmethod
def get() -> json:
"""Return all cards from the database"""
logger.debug("Start of CardCollectionAPI.GET")
logger.debug(request)
# Retrieve all cards from the db, sorted by id
try:
cards = Card.query.order_by(Card.id).all()
logger.info("cards retrieved successfully!")
except SQLAlchemyError as e:
error_msg = f"SQLAlchemyError retrieving data: {e}"
logger.info(error_msg)
logger.debug("End of CardCollectionAPI.GET")
return error_msg, 500
except BaseException as e:
error_msg = f"BaseException retrieving data: {e}"
logger.info(error_msg)
logger.debug("End of CardCollectionAPI.GET")
return error_msg, 500
# Compile these data into a list
try:
output = []
for card in cards:
output.append(card.to_dict())
logger.debug("End of CardAPI.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 CardCollectionAPI.GET")
return error_msg, 500
class CardApi(Resource):
"""
Endpoint: /api/v1/card
Methods: GET, POST, PUT, DELETE
"""
@staticmethod
def get() -> json:
"""Return data for the specified card id"""
logger.debug(f"Start of CardAPI.GET")
logger.debug(request)
# Define the parameters used by this endpoint
parser.add_argument("id", type=int, nullable=False, store_missing=False,
required=True)
# Parse the provided arguments
args = parser.parse_args()
logger.debug(f"Args parsed successfully: {args.__str__()}")
# Validate that a card id was provided
try:
card_id = args["id"]
logger.debug(f"Card id={card_id} was read successfully")
except KeyError as e:
error_msg = f"Error parsing card id: no value was provided. {e}"
logger.info(error_msg)
return error_msg, 400
# Retrieve the selected record
try:
card = Card.query.get(card_id)
if card:
# Record successfully returned from the db
logger.info(f"Found the requested card: {card.to_dict()}")
logger.debug("End of CardAPI.GET")
return card.to_dict(), 200
else:
# No record with this id exists in the db
logger.debug(f"No records found for card id={card_id}.")
logger.debug("End of CardAPI.GET")
return f"No records found for card id={card_id}.", 404
except (InvalidRequestError, NoResultFound, AttributeError) as e:
error_msg = f"No records found for card id={card_id}.\n{e}"
logger.debug(error_msg)
logger.debug(f"End of CardAPI.GET")
return error_msg, 404
@staticmethod
def post() -> json:
"""Add a new card record to the database"""
logger.debug(f"Start of CardAPI.POST")
logger.debug(request)
# Define the parameters used by this endpoint
parser.add_argument("type", type=str)
parser.add_argument("status", type=str, default="New", required=True, nullable=False)
parser.add_argument("event_id", type=int)
parser.add_argument("gift_id", type=int)
parser.add_argument("household_id", type=int)
parser.add_argument("address_id", type=int)
parser.add_argument("date_sent", type=date)
parser.add_argument("notes", type=str)
# Parse the arguments provided
args = parser.parse_args()
logger.debug(f"Args parsed successfully: {args.__str__()}")
# Create a new Card record using the provided data
try:
logger.debug(f"Attempting to create a Card from the args.")
new_card = Card(**args.__str__())
logger.info(f"New record successfully created: {new_card.to_dict()}")
except SQLAlchemyError as e:
error_msg = f"Unable to create a new Card record.\n{e}."
logger.info(error_msg)
logger.debug("End of CardAPI.POST")
return error_msg, 500
# If the new card's status is "Sent" and no value was provided for `date_sent`, set
# the `date_sent` attribute to today.
try:
if new_card.status.lower() == "sent" and not new_card.date_sent:
new_card.date_sent = datetime.now(timezone.utc).date()
# Commit this new record so the db generates an id
logger.debug("Attempting to commit data")
db.session.commit()
logger.debug("Commit completed")
# Return the newly generated id
logger.debug("End of CardAPI.POST")
return new_card.id, 201
except SQLAlchemyError as e:
error_msg = f"Unable to create a new Card record.\n{e}"
logger.info(error_msg)
logger.debug("End of CardAPI.POST")
return error_msg, 500
@staticmethod
def put() -> json:
"""Update an existing record by card id"""
logger.debug(f"Start of CardAPI.PUT")
logger.debug(request)
# Define the parameters used by this endpoint
parser.add_argument("type", type=str)
parser.add_argument("status", type=str, required=True, nullable=False)
parser.add_argument("event_id", type=int)
parser.add_argument("gift_id", type=int)
parser.add_argument("household_id", type=int)
parser.add_argument("address_id", type=int)
parser.add_argument("date_sent", type=date)
parser.add_argument("notes", type=str)
# Parse the arguments provided
args = parser.parse_args()
logger.debug(f"Args parsed successfully: {args.__str__()}")
# Validate that a card id was provided
try:
card_id = args["id"]
logger.debug(f"Card id={card_id} was read successfully")
except KeyError as e:
error_msg = f"Error parsing card id: no value was provided. {e}"
logger.info(error_msg)
logger.debug("End of CardAPI.PUT")
return error_msg, 400
try:
# Retrieve the specified card record
card = Card.query.get(card_id)
# Update this record with the provided data
card.type = args["type"]
card.status = args["status"]
card.event_id = args["event_id"]
card.gift_id = args["gift_id"]
card.households = args["households"]
card.address_id = args["address_id"]
card.date_sent = args["date_sent"]
card.date_sent = args["notes"]
# If the card's status wasn't already "Sent",
# and the card's status is being updated to "Sent",
# and no value was provided for `date_sent`
# then set the `date_sent` attribute to today.
if card.status.lower() != "sent" and args["status"].__str__().lower() == "sent" and not args["date_sent"]:
card.date_sent = datetime.now(timezone.utc).date()
# 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 CardAPI.PUT")
return card.id, 200
except SQLAlchemyError as e:
error_msg = f"Unable to update Card record.\n{e}"
logger.info(error_msg)
logger.debug("End of CardAPI.PUT")
return error_msg, 500
@staticmethod
def delete() -> json:
"""Delete the specified record by card id"""
logger.debug(f"Start of CardAPI.DELETE")
logger.debug(request)
# Define the parameters used by this endpoint
parser.add_argument("id", type=int, nullable=False, store_missing=False,
required=True)
# Parse the provided arguments
args = parser.parse_args()
logger.debug(f"Args parsed successfully: {args.__str__()}")
# Validate that a card id was provided
try:
card_id = args["id"]
logger.debug(f"Card id={card_id} was read successfully")
except KeyError as e:
error_msg = f"Error parsing card id: no value was provided. {e}"
logger.info(error_msg)
logger.debug(f"End of CardAPI.DELETE")
return error_msg, 400
try:
# Retrieve the selected record
card = Card.query.get(card_id)
if card:
# Record successfully returned from the db
logger.debug(f"Card record found. Attempting to delete it.")
card.delete()
logger.debug("About to commit this delete to the db.")
db.session.commit()
logger.debug("Commit completed.")
logger.info("Card record successfully deleted.")
logger.debug("End of CardAPI.GET")
return card.to_dict(), 200
else:
# No record with this id exists in the db
error_msg = f"No record found for card id={card_id}."
logger.debug(error_msg)
logger.debug("End of CardAPI.GET")
return error_msg, 404
except (InvalidRequestError, NoResultFound, AttributeError) as e:
error_msg = f"No record found for card id={card_id}.\n{e}"
logger.debug(error_msg)
logger.debug(f"End of CardAPI.GET")
return error_msg, 404