Skip to content

Commit

Permalink
feat(permission-db): add a studies counting endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mabw-rte committed Feb 21, 2024
1 parent df8c249 commit 6203a63
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
16 changes: 16 additions & 0 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
92 changes: 92 additions & 0 deletions antarest/study/web/studies_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 6203a63

Please sign in to comment.