From 2b4d3bf7f523e7e41104455d294e6d952f337067 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 6 Jun 2022 16:38:35 +0300 Subject: [PATCH] sql_app: Implemented access list counter: A counter is associated with every Iot Device. The counter is always increment when a user is allowed or disallowed to use the device, hence, ensuring coherency. It is also now exposed in the IotDoorPollingRequest schema, enabling the Iot Device to fetch the new access list. Signed-off-by: HeshamTB --- sql_app/crud.py | 9 ++++++++- sql_app/main.py | 4 +++- sql_app/models.py | 3 +-- sql_app/schemas.py | 1 + 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index 60bb4b5..44ea430 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -141,4 +141,11 @@ def record_room_sensor_data(db: Session, entry: schemas.IotMonitorRoomInfo): timestamp=datetime.now()) db.add(db_item) db.commit() - db.refresh(db_item) \ No newline at end of file + db.refresh(db_item) + +def increment_door_access_list_counter(db: Session, iot_entity: models.IotEntity): + #setattr(iot_entity, "acces_list_counter", iot_entity.acces_list_counter + 1) + iot_entity.acces_list_counter = iot_entity.acces_list_counter + 1 + db.add(iot_entity) + db.commit() + db.refresh(iot_entity) diff --git a/sql_app/main.py b/sql_app/main.py index a999a11..545aebb 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -113,6 +113,7 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques if not res: raise HTTPException(status_code=500, detail="Could not complete operation") + crud.increment_door_access_list_counter(db, iot_entity) return @app.post("/admin/users/disallowdevice/id", tags=['Admin']) @@ -128,7 +129,8 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq res = crud.remove_user_link_to_iot(db, request.user_id, request.iot_entity_id) if not res: raise HTTPException(status_code=500, detail="Could not complete operation") - + + crud.increment_door_access_list_counter(db, iot_entity) return @app.post("/admin/users/allowdevice/name", tags=['Admin']) diff --git a/sql_app/models.py b/sql_app/models.py index 2365d66..89ecbdc 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -16,7 +16,6 @@ class User(Base): last_token = Column(String, nullable=True) authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users") - #authorized_devices = relationship("IotEntity", secondary= 'user_iot_link') class IotEntity(Base): @@ -27,8 +26,8 @@ class IotEntity(Base): description = Column(String(512)) open_request = Column(Boolean, default=False) time_seconds = Column(Integer, default=10) + acces_list_counter = Column(Integer, default=0) authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices") - #authorized_users = relationship("User", secondary= 'user_iot_link') class UserAuthToIoTDev(Base): __tablename__ = "user_iot_link" diff --git a/sql_app/schemas.py b/sql_app/schemas.py index 79aa70b..eee212f 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -26,6 +26,7 @@ class IotEntity(IotEntityBase): #authorized_users: List[User] = [] open_request: bool # Flag to open time_seconds: int + acces_list_counter: int class Config: orm_mode = True