From 6203a6322f4b1ea7d2fbdf739f443328ab08d3ba Mon Sep 17 00:00:00 2001 From: Mohamed Abdel Wedoud Date: Wed, 21 Feb 2024 11:17:53 +0100 Subject: [PATCH] feat(permission-db): add a studies counting endpoint --- antarest/study/service.py | 16 +++++ antarest/study/web/studies_blueprint.py | 92 +++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/antarest/study/service.py b/antarest/study/service.py index 7290cf692d..5433aab1c2 100644 --- a/antarest/study/service.py +++ b/antarest/study/service.py @@ -491,6 +491,22 @@ def get_studies_information( ) } + def count_studies( + self, + study_filter: StudyFilter, + ) -> int: + """ + Get number of matching studies. + Args: + study_filter: filtering parameters + + Returns: total number of studies matching the filtering criteria + """ + total: int = self.repository.count_studies( + study_filter=study_filter, + ) + return total + def _try_get_studies_information(self, study: Study) -> t.Optional[StudyMetadataDTO]: try: return self.storage_service.get_storage(study).get_study_information(study) diff --git a/antarest/study/web/studies_blueprint.py b/antarest/study/web/studies_blueprint.py index cf76765122..b080002007 100644 --- a/antarest/study/web/studies_blueprint.py +++ b/antarest/study/web/studies_blueprint.py @@ -173,6 +173,98 @@ def get_studies( return matching_studies + @bp.get( + "/studies/count", + tags=[APITag.study_management], + summary="Get Studies", + ) + def count_studies( + current_user: JWTUser = Depends(auth.get_current_user), + name: str = Query( + "", + description=( + "Filter studies based on their name." + "Case-insensitive search for studies whose name contains the specified value." + ), + alias="name", + ), + managed: t.Optional[bool] = Query(None, description="Filter studies based on their management status."), + archived: t.Optional[bool] = Query(None, description="Filter studies based on their archive status."), + variant: t.Optional[bool] = Query(None, description="Filter studies based on their variant status."), + versions: str = Query( + "", + description="Comma-separated list of versions for filtering.", + regex=r"^\s*(?:\d+\s*(?:,\s*\d+\s*)*)?$", + ), + users: str = Query( + "", + description="Comma-separated list of user IDs for filtering.", + regex=r"^\s*(?:\d+\s*(?:,\s*\d+\s*)*)?$", + ), + groups: str = Query("", description="Comma-separated list of group IDs for filtering."), + tags: str = Query("", description="Comma-separated list of tags for filtering."), + study_ids: str = Query( + "", + description="Comma-separated list of study IDs for filtering.", + alias="studyIds", + ), + exists: t.Optional[bool] = Query(None, description="Filter studies based on their existence on disk."), + workspace: str = Query("", description="Filter studies based on their workspace."), + folder: str = Query("", description="Filter studies based on their folder."), + ) -> int: + """ + Get the number of studies matching the specified criteria. + + Args: + - `name`: Filter studies based on their name. Case-insensitive search for studies + whose name contains the specified value. + - `managed`: Filter studies based on their management status. + - `archived`: Filter studies based on their archive status. + - `variant`: Filter studies based on their variant status. + - `versions`: Comma-separated list of versions for filtering. + - `users`: Comma-separated list of user IDs for filtering. + - `groups`: Comma-separated list of group IDs for filtering. + - `tags`: Comma-separated list of tags for filtering. + - `studyIds`: Comma-separated list of study IDs for filtering. + - `exists`: Filter studies based on their existence on disk. + - `workspace`: Filter studies based on their workspace. + - `folder`: Filter studies based on their folder. + + Returns: + - An integer representing the total number of studies matching the filters above and the user permissions. + """ + + logger.info("Fetching for matching studies", extra={"user": current_user.id}) + params = RequestParameters(user=current_user) + + user_list = [int(v) for v in _split_comma_separated_values(users)] + + if not params.user: + logger.error("FAIL permission: user is not logged") + raise UserHasNotPermissionError() + + study_filter = StudyFilter( + name=name, + managed=managed, + archived=archived, + variant=variant, + versions=_split_comma_separated_values(versions), + users=user_list, + groups=_split_comma_separated_values(groups), + tags=_split_comma_separated_values(tags), + study_ids=_split_comma_separated_values(study_ids), + exists=exists, + workspace=workspace, + folder=folder, + access_permissions=AccessPermissions.from_params(params), + ) + + matching_studies = study_service.count_studies( + study_filter=study_filter, + ) + + return matching_studies + @bp.get( "/studies/{uuid}/comments", tags=[APITag.study_management],