sql_app: Use more relations in database
Instead of using manual db lookup for access log, use device.access_list to fetch the data from access_log table. This does the SQL query underneath. To do so, instructions from SQLAlchemey were followed to to many-to-one relations for users and devices, respectevly. Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
ae5f4e040d
commit
6a3d9d9e95
@ -6,6 +6,7 @@ from sqlalchemy.orm import Session
|
|||||||
from . import models, schemas, crypto, auth_helper
|
from . import models, schemas, crypto, auth_helper
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from warnings import warn
|
||||||
|
|
||||||
# TODO: Data we can collect or log
|
# TODO: Data we can collect or log
|
||||||
# - Last user connection (link to user)
|
# - 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()
|
return db.query(models.User).offset(skip).limit(limit).all()
|
||||||
|
|
||||||
def get_access_log_for_door_by_door_mac(db: Session, iot_id: str):
|
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)\
|
return db.query(models.DoorAccessLog)\
|
||||||
.filter(models.DoorAccessLog.iot_id == iot_id).all()
|
.filter(models.DoorAccessLog.iot_id == iot_id).all()
|
||||||
|
|
||||||
|
@ -111,7 +111,6 @@ def init_user_connections():
|
|||||||
crud.get_user(db, 3)]
|
crud.get_user(db, 3)]
|
||||||
|
|
||||||
for i in range(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())
|
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())
|
||||||
|
@ -303,13 +303,13 @@ def get_all_sensor_history(skip: int = 0, limit: int = 100,
|
|||||||
db: Session = Depends(get_db)):
|
db: Session = Depends(get_db)):
|
||||||
return crud.get_sensor_data_for_room(db, skip, limit)
|
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,
|
def get_access_log_for_door(request : schemas.AccessLogRequest,
|
||||||
api_key: APIKey = Depends(auth_helper.valid_api_key),
|
api_key: APIKey = Depends(auth_helper.valid_api_key),
|
||||||
db : Session = Depends(get_db)):
|
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")
|
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'])
|
@app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot'])
|
||||||
def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
|
def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
|
||||||
|
@ -16,7 +16,8 @@ class User(Base):
|
|||||||
last_token = Column(String, nullable=True)
|
last_token = Column(String, nullable=True)
|
||||||
connections = relationship("UserConnectionHistory")
|
connections = relationship("UserConnectionHistory")
|
||||||
authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users")
|
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):
|
class IotEntity(Base):
|
||||||
__tablename__ = "iot_entities"
|
__tablename__ = "iot_entities"
|
||||||
@ -30,6 +31,7 @@ class IotEntity(Base):
|
|||||||
force_close = Column(Boolean, default=False)
|
force_close = Column(Boolean, default=False)
|
||||||
state = Column(Boolean, default=False) # True is open, False is closed
|
state = Column(Boolean, default=False) # True is open, False is closed
|
||||||
authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices")
|
authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices")
|
||||||
|
access_log = relationship("DoorAccessLog", back_populates="iot_device")
|
||||||
|
|
||||||
class UserAuthToIoTDev(Base):
|
class UserAuthToIoTDev(Base):
|
||||||
__tablename__ = "user_iot_link"
|
__tablename__ = "user_iot_link"
|
||||||
@ -42,8 +44,10 @@ class DoorAccessLog(Base):
|
|||||||
__tablename__ = "door_access_log"
|
__tablename__ = "door_access_log"
|
||||||
|
|
||||||
entry_id = Column(Integer, primary_key=True, index=True)
|
entry_id = Column(Integer, primary_key=True, index=True)
|
||||||
user_id = Column(Integer, ForeignKey('user_accounts.id'))
|
user_id = Column(Integer, ForeignKey('user_accounts.id'), index=True)
|
||||||
iot_id = Column(Integer, ForeignKey('iot_entities.id'))
|
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))
|
command = Column(String(16))
|
||||||
timestamp = Column(DateTime)
|
timestamp = Column(DateTime)
|
||||||
|
|
||||||
@ -62,7 +66,7 @@ class UserConnectionHistory(Base):
|
|||||||
__tablename__ = "user_connection_history"
|
__tablename__ = "user_connection_history"
|
||||||
|
|
||||||
reading_id = Column(Integer, primary_key=True)
|
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)
|
timestamp = Column(DateTime)
|
||||||
# TODO: add ip
|
# TODO: add ip
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user