[AA] Add oneshot to checkAccess function

This commit is contained in:
2026-07-27 16:26:58 +02:00
committed by ahtlon
parent e45abb5c55
commit 5f19409da4

View File

@@ -4,9 +4,9 @@ from sqlmodel import select
from fastapi import Depends, HTTPException, status from fastapi import Depends, HTTPException, status
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload
import sqlalchemy.exc as exc import sqlalchemy.exc as exc
import datetime from datetime import datetime, date, timedelta
from app.services.database import Session, get_session from app.services.database import Session, get_session, add_and_refresh
from app.model.models import * from app.model.models import *
doorIsOpen = True doorIsOpen = True
@@ -28,22 +28,36 @@ def closeDoor():
def isDoorOpen(): def isDoorOpen():
return doorIsOpen return doorIsOpen
def decrementOneshot(db: Session, oneshot: OneShotAccess):
data = oneshot.model_dump()
if data["uses"] > 0:
data["uses"] = data["uses"] - 1
oneshot.sqlmodel_update(oneshot, update=data)
add_and_refresh(db, oneshot)
def checkAccess(key: str, db: Session): def checkAccess(key: str, db: Session):
try: try:
current_weekday = datetime.datetime.weekday(datetime.date.today()) current_weekday = datetime.weekday(date.today())
current_time = datetime.datetime.now() current_time = datetime.now()
card = db.exec(select(Card).where(Card.key == key)).one() card = db.exec(select(Card).where(Card.key == key)).one()
for auth in card.group.accessauths: for auth in card.group.accessauths:
logger.info(f"checking auth: {auth.name}") logger.info(f"checking auth: {auth.name}")
for timetable in auth.timetables: if auth.type == "timetable":
logger.info(f" checking timetable {timetable.id}") for timetable in auth.timetables:
logger.info(f" comparing weekday: CUR:{current_weekday} TT:{timetable.weekday}") logger.info(f" checking timetable {timetable.id}")
if current_weekday == timetable.weekday: logger.info(f" comparing weekday: CUR:{current_weekday} TT:{timetable.weekday}")
starttime = datetime.datetime.combine(datetime.date.today(), timetable.starttime) if current_weekday == timetable.weekday:
endtime = starttime + datetime.timedelta(minutes=timetable.duration) starttime = datetime.combine(date.today(), timetable.starttime)
logger.info(f" comparing time: Start:{starttime} Current:{current_time} End:{endtime}") endtime = starttime + timedelta(minutes=timetable.duration)
if starttime < current_time < endtime: logger.info(f" comparing time: Start:{starttime} Current:{current_time} End:{endtime}")
logger.info("Access Valid!") if starttime < current_time < endtime:
logger.info("Access Valid!")
return True
if auth.type == "oneshot":
logger.info(f' oneshot auth found: {auth.oneshot}')
if current_time < auth.oneshot.ends_at:
if auth.oneshot.uses > 0:
decrementOneshot(db, auth.oneshot)
return True return True
logger.info("No more auths found") logger.info("No more auths found")
return False return False