diff --git a/inventory-app/api.py b/inventory-app/api.py index 104a68c..167bc5e 100644 --- a/inventory-app/api.py +++ b/inventory-app/api.py @@ -1,6 +1,7 @@ import os import re import const +import threading from datetime import datetime, timedelta from functools import wraps from pymongo import MongoClient @@ -61,7 +62,6 @@ def get_group_cards(): @page_api.route("/api/slack/doorboy", methods=['POST']) def view_slack_doorboy(): - print(request.data) if request.form.get("token") != const.SLACK_VERIFICATION_TOKEN: return "Invalid token was supplied" if request.form.get("channel_id") not in ("C01CWPF5H8W", "CDL9H8Q9W"): @@ -79,7 +79,6 @@ def view_slack_doorboy(): return "Invalid command was supplied" member = None - print(g.users) for user in g.users: if user.slack_id == request.form.get("user_id"): member = user @@ -94,25 +93,29 @@ def view_slack_doorboy(): if door == "outsidedoors": doors = ["backdoor", "frontdoor", "grounddoor"] - for d in doors: - db.eventlog.insert_one({ - "method": "slack", - "approved": approved, - "duration": 5, - "component": "doorboy", - "type": "open-door", - "door": d, - "member_id": member.username, - "member": member.display_name, - "timestamp": datetime.utcnow(), - }) - status = "Permitted" if approved else "Denied" subject = member.display_name - msg = "%s %s door access for %s via Slack bot" % (status, door, subject) - slack_post(msg, "doorboy") + + threading.Thread(target=handle_slack_door_event, args=(doors, approved, member, door, status, subject)).start() if approved: return "Opening %s for %s" % (door, subject) else: return "Permission denied" + +def handle_slack_door_event(doors, approved, member, door, status, subject): + for d in doors: + db.eventlog.insert_one({ + "method": "slack", + "approved": approved, + "duration": 5, + "component": "doorboy", + "type": "open-door", + "door": d, + "member_id": member.username, + "member": member.display_name, + "timestamp": datetime.utcnow(), + }) + + msg = "%s %s door access for %s via Slack bot" % (status, door, subject) + slack_post(msg, "doorboy")