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,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"