From 9469324da91ad2666be86b1b0ff7cd5d40117665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madis=20M=C3=A4gi?= Date: Thu, 10 Aug 2023 22:56:54 +0300 Subject: [PATCH] Change swipe endpoint to accept json --- inventory-app/doorboy.py | 96 ++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 49 deletions(-) diff --git a/inventory-app/doorboy.py b/inventory-app/doorboy.py index 3651074..47a838c 100644 --- a/inventory-app/doorboy.py +++ b/inventory-app/doorboy.py @@ -211,59 +211,57 @@ class FormSwipe(FlaskForm): @page_doorboy.route("/m/doorboy/swipe", methods=["POST"]) def view_swipe(): - form = FormSwipe(request.form) + form = request.json now = datetime.utcnow() - if form.validate_on_submit(): - # Make sure token exists - db.inventory.update_one({ - "type": "token", + # Make sure token exists + db.inventory.update_one({ + "type": "token", + "component": "doorboy", + "token.uid_hash": form["uid_hash"] + }, { + "$set": { + "last_seen": form.get("timestamp", now) + }, + "$setOnInsert": { "component": "doorboy", - "token.uid_hash": form.uid_hash.data - }, { - "$set": { - "last_seen": form.timestamp.data or now, - }, - "$setOnInsert": { - "component": "doorboy", - "type": "token", - "first_seen": now, - "inventory": { - "claimable": True, - } + "type": "token", + "first_seen": now, + "inventory": { + "claimable": True, } - }, upsert=True) - - # Fetch token to read owner - token = db.inventory.find_one({ - "type": "token", - "component": "doorboy", - "token.uid_hash": form.uid_hash.data - }) - - event_swipe = { - "component": "doorboy", - "timestamp": form.timestamp.data, - "door": form.door.data, - "event": "card-swiped", - "success": form.success.data, - "token": { - "uid_hash": form.uid_hash.data - }, - "inventory": {} } + }, upsert=True) - if token.get("inventory", {}).get("owner", {}).get("username", None): - event_swipe["inventory"]["owner_id"] = token["inventory"]["owner"]["username"] + # Fetch token to read owner + token = db.inventory.find_one({ + "type": "token", + "component": "doorboy", + "token.uid_hash": form["uid_hash"] + }) - db.eventlog.insert_one(event_swipe) + event_swipe = { + "component": "doorboy", + "timestamp": form["timestamp"] + "door": form["door"] + "event": "card-swiped", + "success": form["success"] + "token": { + "uid_hash": form["uid_hash"] + }, + "inventory": {} + } - status = "Permitted" if form.success.data else "Denied" - username = token.get("inventory", {}).get("owner", {}).get("username", None) - if username and username in users_lookup: - subject = users_lookup[username].display_name or username - else: - subject = "Unknown" - msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form.door.data, subject) - spam(msg) - return "ok" - raise ValueError() + if token.get("inventory", {}).get("owner", {}).get("username", None): + event_swipe["inventory"]["owner_id"] = token["inventory"]["owner"]["username"] + + db.eventlog.insert_one(event_swipe) + + status = "Permitted" if form["success"] else "Denied" + username = token.get("inventory", {}).get("owner", {}).get("username", None) + if username and username in users_lookup: + subject = users_lookup[username].display_name or username + else: + subject = "Unknown" + msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form["door"], subject) + spam(msg) + return "ok"