diff --git a/pepdbagent/models.py b/pepdbagent/models.py index eb2116d..569fba2 100644 --- a/pepdbagent/models.py +++ b/pepdbagent/models.py @@ -164,15 +164,23 @@ class ProjectRegistryPath(BaseModel): class ViewAnnotation(BaseModel): """ - View annotation model + Project views model """ - project_namespace: str - project_name: str - project_tag: str name: str description: Optional[str] = None - number_of_samples: int + number_of_samples: int = 0 + + +class ProjectViews(BaseModel): + """ + View annotation model + """ + + namespace: str + name: str + tag: str = DEFAULT_TAG + views: List[ViewAnnotation] = [] class CreateViewDictModel(BaseModel): diff --git a/pepdbagent/modules/annotation.py b/pepdbagent/modules/annotation.py index 72c79e9..f727446 100644 --- a/pepdbagent/modules/annotation.py +++ b/pepdbagent/modules/annotation.py @@ -572,26 +572,3 @@ def get_by_rp_list( else: return self.get_by_rp(registry_paths, admin) - - def get_views(self, namespace: str, name: str, tag: str = DEFAULT_TAG) -> List[str]: - """ - Get list of views of the project - - :param namespace: namespace of the project - :param name: name of the project - :param tag: tag of the project - :return: list of views of the project - """ - statement = select(Views.name).where( - Views.project_mapping.has(namespace=namespace, name=name, tag=tag), - and_( - Projects.name == name, - Projects.namespace == namespace, - Projects.tag == tag, - ), - ) - views = self._pep_db_engine.session_execute(statement).all() - if views: - return [v[0] for v in views] - else: - return [] diff --git a/pepdbagent/modules/view.py b/pepdbagent/modules/view.py index 89c5dfa..11b07af 100644 --- a/pepdbagent/modules/view.py +++ b/pepdbagent/modules/view.py @@ -16,7 +16,7 @@ from pepdbagent.exceptions import ViewNotFoundError, SampleAlreadyInView from pepdbagent.db_utils import BaseEngine, Samples, Projects, Views, ViewSampleAssociation -from pepdbagent.models import ViewAnnotation, CreateViewDictModel +from pepdbagent.models import ViewAnnotation, CreateViewDictModel, ProjectViews _LOGGER = logging.getLogger(PKG_NAME) @@ -369,3 +369,37 @@ def get_snap_view( return peppy.Project.from_dict( {"_config": config, "_sample_dict": samples, "_subsample_dict": None} ) + + def get_views_annotation( + self, namespace: str, name: str, tag: str = DEFAULT_TAG + ) -> Union[ProjectViews, None]: + """ + Get list of views of the project + + :param namespace: namespace of the project + :param name: name of the project + :param tag: tag of the project + :return: list of views of the project + """ + statement = select(Views).where( + Views.project_mapping.has(namespace=namespace, name=name, tag=tag), + and_( + Projects.name == name, + Projects.namespace == namespace, + Projects.tag == tag, + ), + ) + views_list = [] + + with Session(self._sa_engine) as session: + views = session.scalars(statement) + for view in views: + views_list.append( + ViewAnnotation( + name=view.name, + description=view.description, + number_of_samples=len(view.samples), + ) + ) + + return ProjectViews(namespace=namespace, name=name, tag=tag, views=views_list) diff --git a/tests/test_pepagent.py b/tests/test_pepagent.py index f77d305..9cc5fa8 100644 --- a/tests/test_pepagent.py +++ b/tests/test_pepagent.py @@ -1182,6 +1182,10 @@ def test_get_snap_view(self, initiate_pepdb_con, namespace, name, sample_name, v def test_get_view_list_from_project( self, initiate_pepdb_con, namespace, name, sample_name, view_name ): + assert ( + len(initiate_pepdb_con.view.get_views_annotation(namespace, name, "default").views) + == 0 + ) initiate_pepdb_con.view.create( "view1", { @@ -1191,4 +1195,8 @@ def test_get_view_list_from_project( "sample_list": [sample_name, "pig_1h"], }, ) - assert initiate_pepdb_con.annotation.get_views(namespace, name, "default")[0] == "view1" + result = initiate_pepdb_con.view.get_views_annotation(namespace, name, "default") + assert ( + len(initiate_pepdb_con.view.get_views_annotation(namespace, name, "default").views) + == 1 + )