From ba89ed17846cbd77fd707447199225e0b0bca817 Mon Sep 17 00:00:00 2001 From: EG264877 Date: Fri, 12 Jul 2024 09:24:41 +0200 Subject: [PATCH] refactor: :recycle: Improve CRUD functions --- thermal_events/crud/base.py | 10 ++++------ thermal_events/crud/thermal_event.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/thermal_events/crud/base.py b/thermal_events/crud/base.py index 3d21691..0f96316 100644 --- a/thermal_events/crud/base.py +++ b/thermal_events/crud/base.py @@ -59,20 +59,18 @@ def get(self, id: Any) -> Optional[ModelType]: with session_scope() as session: return session.query(self.model).filter(self.model.id == id).first() - def get_multi(self, *, skip: int = 0, limit: int = 100) -> List[ModelType]: - """Get multiple objects with optional pagination. + def get_multi(self, ids) -> List[ModelType]: + """Get multiple objects given their ids. Args: - skip (int, optional): The number of objects to skip. Defaults to 0. - limit (int, optional): The maximum number of objects to retrieve. Defaults - to 100. + ids (list): The list of ids of objects to query. Returns: List[ModelType]: The list of retrieved objects. """ with session_scope() as session: - return session.query(self.model).offset(skip).limit(limit).all() + return session.query(self.model).filter(self.model.id.in_(ids)).all() def create(self, obj_in: Union[list, ModelType]) -> None: """Create a new object or a list of objects. diff --git a/thermal_events/crud/thermal_event.py b/thermal_events/crud/thermal_event.py index c1fcdd5..487c935 100644 --- a/thermal_events/crud/thermal_event.py +++ b/thermal_events/crud/thermal_event.py @@ -378,6 +378,22 @@ def change_analysis_status(self, event_id: int, new_status: str): ) session.commit() + def change_severity(self, event_id: int, new_severity: str): + """Change the severity of a ThermalEvent. + + Args: + event_id (int): + id of the ThermalEvent to update. + new_severity (str): + New severity value. + + """ + with session_scope() as session: + session.query(ThermalEvent).filter_by(id=event_id).update( + {"severity": new_severity} + ) + session.commit() + def update(self, obj_in: Union[ThermalEvent, List[ThermalEvent]]) -> None: """Update an existing object or a list of ThermalEvent objects.