fmt with ruff

This commit is contained in:
2025-08-08 01:14:46 +03:00
parent d6807e3f01
commit abffe7c594
5 changed files with 130 additions and 92 deletions

View File

@@ -3,10 +3,13 @@ from requests.exceptions import RequestException
import os
import requests
SLACK_DOORLOG_CALLBACK = os.environ["SLACK_DOORLOG_CALLBACK"] # webhook logs to private channel or "DEV" to print to console.
# webhook logs to private channel or "DEV" to print to console.
SLACK_DOORLOG_CALLBACK = os.environ["SLACK_DOORLOG_CALLBACK"]
def add_slack_routes(app):
app.app.register_listener(slack_log, "after_server_start") # consumes SLACK_DOORLOG_CALLBACK and app.ctx.db
app.app.register_listener(slack_log, "after_server_start")
def slack_post(msg):
if SLACK_DOORLOG_CALLBACK == "DEV":
@@ -14,30 +17,40 @@ def slack_post(msg):
return
try:
requests.post(SLACK_DOORLOG_CALLBACK, json={"text": msg }).raise_for_status()
requests.post(SLACK_DOORLOG_CALLBACK, json={"text": msg}).raise_for_status()
except RequestException as e:
print(f"[SLACK]: {e}")
def approvedStr(approved: bool) -> str:
if approved:
return "Permitted"
return "Denied"
# consumes SLACK_DOORLOG_CALLBACK and app.ctx.db
async def slack_log(app, loop):
pipeline = [{
"$match": {
"operationType": "insert",
pipeline = [
{
"$match": {
"operationType": "insert",
}
}
}]
]
while True:
try:
async with app.ctx.db.eventlog.watch(pipeline) as stream:
async for event in stream:
ev = event["fullDocument"]
msg = "%s %s access for %s via %s" % (approvedStr(ev["approved"]), ev["door"], ev["user"]["name"], ev("method"))
msg = "%s %s access for %s via %s" % (
approvedStr(ev["approved"]),
ev["door"],
ev["user"]["name"],
ev("method"),
)
slack_post(msg)
except PyMongoError as e:
print(e)