Change swipe endpoint to accept json
All checks were successful
ci/woodpecker/manual/woodpecker Pipeline was successful

This commit is contained in:
Madis Mägi 2023-08-10 22:56:54 +03:00
parent 9aadb8f24d
commit 9469324da9

View File

@ -211,17 +211,16 @@ class FormSwipe(FlaskForm):
@page_doorboy.route("/m/doorboy/swipe", methods=["POST"]) @page_doorboy.route("/m/doorboy/swipe", methods=["POST"])
def view_swipe(): def view_swipe():
form = FormSwipe(request.form) form = request.json
now = datetime.utcnow() now = datetime.utcnow()
if form.validate_on_submit():
# Make sure token exists # Make sure token exists
db.inventory.update_one({ db.inventory.update_one({
"type": "token", "type": "token",
"component": "doorboy", "component": "doorboy",
"token.uid_hash": form.uid_hash.data "token.uid_hash": form["uid_hash"]
}, { }, {
"$set": { "$set": {
"last_seen": form.timestamp.data or now, "last_seen": form.get("timestamp", now)
}, },
"$setOnInsert": { "$setOnInsert": {
"component": "doorboy", "component": "doorboy",
@ -237,17 +236,17 @@ def view_swipe():
token = db.inventory.find_one({ token = db.inventory.find_one({
"type": "token", "type": "token",
"component": "doorboy", "component": "doorboy",
"token.uid_hash": form.uid_hash.data "token.uid_hash": form["uid_hash"]
}) })
event_swipe = { event_swipe = {
"component": "doorboy", "component": "doorboy",
"timestamp": form.timestamp.data, "timestamp": form["timestamp"]
"door": form.door.data, "door": form["door"]
"event": "card-swiped", "event": "card-swiped",
"success": form.success.data, "success": form["success"]
"token": { "token": {
"uid_hash": form.uid_hash.data "uid_hash": form["uid_hash"]
}, },
"inventory": {} "inventory": {}
} }
@ -257,13 +256,12 @@ def view_swipe():
db.eventlog.insert_one(event_swipe) db.eventlog.insert_one(event_swipe)
status = "Permitted" if form.success.data else "Denied" status = "Permitted" if form["success"] else "Denied"
username = token.get("inventory", {}).get("owner", {}).get("username", None) username = token.get("inventory", {}).get("owner", {}).get("username", None)
if username and username in users_lookup: if username and username in users_lookup:
subject = users_lookup[username].display_name or username subject = users_lookup[username].display_name or username
else: else:
subject = "Unknown" subject = "Unknown"
msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form.door.data, subject) msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form["door"], subject)
spam(msg) spam(msg)
return "ok" return "ok"
raise ValueError()