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"])
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",
"component": "doorboy",
"token.uid_hash": form.uid_hash.data
"token.uid_hash": form["uid_hash"]
}, {
"$set": {
"last_seen": form.timestamp.data or now,
"last_seen": form.get("timestamp", now)
},
"$setOnInsert": {
"component": "doorboy",
@ -237,17 +236,17 @@ def view_swipe():
token = db.inventory.find_one({
"type": "token",
"component": "doorboy",
"token.uid_hash": form.uid_hash.data
"token.uid_hash": form["uid_hash"]
})
event_swipe = {
"component": "doorboy",
"timestamp": form.timestamp.data,
"door": form.door.data,
"timestamp": form["timestamp"]
"door": form["door"]
"event": "card-swiped",
"success": form.success.data,
"success": form["success"]
"token": {
"uid_hash": form.uid_hash.data
"uid_hash": form["uid_hash"]
},
"inventory": {}
}
@ -257,13 +256,12 @@ def view_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)
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)
msg = "%s %s door access for %s identified by keycard/keyfob" % (status, form["door"], subject)
spam(msg)
return "ok"
raise ValueError()