diff --git a/sql_app/crud.py b/sql_app/crud.py index 4b78790..e330f54 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -6,6 +6,7 @@ from sqlalchemy.orm import Session from . import models, schemas, crypto, auth_helper from datetime import datetime +from warnings import warn # TODO: Data we can collect or log # - Last user connection (link to user) @@ -35,6 +36,8 @@ def get_users(db: Session, skip: int = 0, limit: int = 100): return db.query(models.User).offset(skip).limit(limit).all() def get_access_log_for_door_by_door_mac(db: Session, iot_id: str): + warn("Manual access log read is deprecated. Use device.access_log", + DeprecationWarning, stacklevel=2) return db.query(models.DoorAccessLog)\ .filter(models.DoorAccessLog.iot_id == iot_id).all() diff --git a/sql_app/init_db.py b/sql_app/init_db.py index 333b907..547d81a 100644 --- a/sql_app/init_db.py +++ b/sql_app/init_db.py @@ -111,7 +111,6 @@ def init_user_connections(): crud.get_user(db, 3)] for i in range(3): - print(users[i]) crud.record_user_connection(db, users[i], datetime.now()) crud.record_user_connection(db, users[i], datetime.now()) crud.record_user_connection(db, users[i], datetime.now()) diff --git a/sql_app/main.py b/sql_app/main.py index 9eed5b2..e4636c5 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -303,13 +303,13 @@ def get_all_sensor_history(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return crud.get_sensor_data_for_room(db, skip, limit) -@app.post("/admin/roominfo/accesslog", tags=['Admin']) +@app.post("/admin/roominfo/accesslog",response_model=List[schemas.DoorAccessLog], tags=['Admin']) def get_access_log_for_door(request : schemas.AccessLogRequest, api_key: APIKey = Depends(auth_helper.valid_api_key), db : Session = Depends(get_db)): - device = crud.get_iot_entity(db, request.iot_id) + device: models.IotEntity = crud.get_iot_entity(db, request.iot_id) if not device: raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Iot Entity not found") - return crud.get_access_log_for_door_by_door_mac(db, request.iot_id) + return device.access_log @app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot']) def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, diff --git a/sql_app/models.py b/sql_app/models.py index fbc22e9..aee9f9c 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -16,7 +16,8 @@ class User(Base): last_token = Column(String, nullable=True) connections = relationship("UserConnectionHistory") authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users") - + connections = relationship("UserConnectionHistory") + access_log = relationship("DoorAccessLog", back_populates="user") class IotEntity(Base): __tablename__ = "iot_entities" @@ -30,6 +31,7 @@ class IotEntity(Base): force_close = Column(Boolean, default=False) state = Column(Boolean, default=False) # True is open, False is closed authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices") + access_log = relationship("DoorAccessLog", back_populates="iot_device") class UserAuthToIoTDev(Base): __tablename__ = "user_iot_link" @@ -42,8 +44,10 @@ class DoorAccessLog(Base): __tablename__ = "door_access_log" entry_id = Column(Integer, primary_key=True, index=True) - user_id = Column(Integer, ForeignKey('user_accounts.id')) - iot_id = Column(Integer, ForeignKey('iot_entities.id')) + user_id = Column(Integer, ForeignKey('user_accounts.id'), index=True) + user = relationship("User", back_populates="access_log") + iot_id = Column(Integer, ForeignKey('iot_entities.id'), index=True) + iot_device = relationship("IotEntity", back_populates="access_log") command = Column(String(16)) timestamp = Column(DateTime) @@ -62,7 +66,7 @@ class UserConnectionHistory(Base): __tablename__ = "user_connection_history" reading_id = Column(Integer, primary_key=True) - user_id = Column(Integer,ForeignKey("user_accounts.id"), index=True) + user_id = Column(Integer, ForeignKey("user_accounts.id"), index=True) timestamp = Column(DateTime) # TODO: add ip