|
8 | 8 | from playhouse import shortcuts
|
9 | 9 | import json
|
10 | 10 | import logging
|
| 11 | +import datetime |
11 | 12 |
|
12 | 13 | #Shamelessly taken from http://flask.pocoo.org/snippets/45/ - works well
|
13 | 14 | def request_wants_json():
|
@@ -241,5 +242,66 @@ def checkvalid(machine, card):
|
241 | 242 | else:
|
242 | 243 | return("no result!")
|
243 | 244 | '''
|
| 245 | +''' |
| 246 | +LOGGING |
| 247 | +''' |
| 248 | + |
| 249 | +@app.route('/log/new', methods=['POST']) |
| 250 | +def logusage(): |
| 251 | + try: |
| 252 | + # Force parsing as JSON even if content header is |
| 253 | + # incorrect, because we don't accept anything else. |
| 254 | + json = request.get_json(force=True) |
| 255 | + except Exception as ex: |
| 256 | + # Return a bad request if we aren't getting our JSON. |
| 257 | + # This could happen if the content type isn't set properly or if the JSON |
| 258 | + # is particularly malformed such that it can't even be |
| 259 | + print ("Bad JSON request.") |
| 260 | + print(ex) |
| 261 | + return "{\"error\":\"Bad JSON\"}", 400 # 400 BAD REQUEST |
| 262 | + |
| 263 | + try: |
| 264 | + log = Log() |
| 265 | + log.endtime = datetime.datetime.now() |
| 266 | + log.starttime = log.endtime - datetime.timedelta(seconds=int(json["elapsed"])) |
| 267 | + log.machineuid = json["machineuid"] |
| 268 | + |
| 269 | + # Check that the machine UID exists and is associated with a machine. |
| 270 | + machineQuery = Machine.select().where(Machine.machineuid == log.machineuid) |
| 271 | + if machineQuery.exists() is False: |
| 272 | + print ("Bad request, Machine UID doesn't exist.") |
| 273 | + return "{\"error\":\"Machine UID doesn't exist\"}", 400 # 400 BAD REQUEST |
| 274 | + |
| 275 | + # Check that the Card UID exists and is associated with a user UID. This is what |
| 276 | + # we need to associate the log request with. |
| 277 | + log.useruid = User.select(User.useruid).where(User.carduid == json["carduid"]).limit(1) |
| 278 | + if log.useruid is None: |
| 279 | + print ("Bad request, Card UID doesn't exist.") |
| 280 | + return "{\"error\":\"Card UID doesn't exist\"}", 400 # 400 BAD REQUEST |
| 281 | + |
| 282 | + # If we're debugging the App it would probably be nice to see all the json |
| 283 | + # and also to save it in the database for future lookup. |
| 284 | + # Otherwise store a readable note of seconds used. |
| 285 | + if app.debug is True: |
| 286 | + print(json) |
| 287 | + log.notes = json |
| 288 | + else: |
| 289 | + log.notes = json["elapsed"] + "s used" |
| 290 | + |
| 291 | + except (TypeError, ValueError) as ex: |
| 292 | + print ("TypeError or ValueError when populating new log from JSON.") |
| 293 | + print (json) |
| 294 | + print (ex) |
| 295 | + return "{\"error\":\"Bad JSON - bad value type or a value conversion error occurred.\"}", 400 # 400 BAD REQUEST |
| 296 | + |
| 297 | + try: |
| 298 | + log.save() |
| 299 | + except Exception as ex: |
| 300 | + # Had a problem saving the data to the database, |
| 301 | + # so return an internal server error. |
| 302 | + print("Error creating log in database.") |
| 303 | + print(ex) |
| 304 | + return "0", 500 # 500 SERVER ERROR |
244 | 305 |
|
245 |
| -#@app.route('/induct') |
| 306 | + # Return created response |
| 307 | + return "1", 201 # 201 CREATED |
0 commit comments