def workspaces(self, context, request: TracimRequest, hapic_data=None): """ Returns the list of all workspaces. This route is for admin only. Standard users must use their own route: /api/users/me/workspaces Filtering by parent_ids is possible through this endpoint """ app_config = request.registry.settings["CFG"] # type: CFG wapi = WorkspaceApi( current_user=request.current_user, session=request.dbsession, config=app_config # User ) workspaces = wapi.get_all_children( parent_ids=hapic_data.query.parent_ids) return [ wapi.get_workspace_with_context(workspace) for workspace in workspaces ]
class WorkspaceOnlyContainer(WebdavContainer): """ Container that can get children workspace """ def __init__( self, path: str, environ: dict, label: str, workspace: typing.Optional[Workspace], provider: "TracimDavProvider", tracim_context: "WebdavTracimContext", list_orphan_workspaces: bool = False, ) -> None: """ Some rules: - if workspace given is None, return workspaces with no parent - if workspace given is correct, return children workspaces of this workspace - if list_orphan_workspaces is True, it adds user-known workspaces without any user-known parent to the list. - in case of workspace collision, only the first named workspace (sorted by workspace_id from lower to higher) will be returned """ self.path = path self.environ = environ self.workspace = workspace self.tracim_context = tracim_context self.user = tracim_context.current_user self.session = tracim_context.dbsession self.label = label self.provider = provider self.workspace_api = WorkspaceApi( current_user=self.user, session=self.session, force_role=True, config=tracim_context.app_config, ) self.list_orphan_workspaces = list_orphan_workspaces # Internal methods def _get_members( self, already_existing_names: typing.Optional[typing.List[str]] = None ) -> typing.List[Workspace]: members_names = already_existing_names or [] # type: List[str] members = [] workspace_id = self.workspace.workspace_id if self.workspace else 0 # type: int workspace_children = list( self.workspace_api.get_all_children([workspace_id])) if self.list_orphan_workspaces: workspace_children.extend( self.workspace_api.get_user_orphan_workspaces(self.user)) for workspace in workspace_children: workspace_label = workspace.filemanager_filename if workspace_label in members_names: # INFO - G.M - 2020-09-24 # We decide to show only first same name workspace (in order of get_all() which is workspace # id order). So, same name workspace are not supported from webdav but doesn't cause # much trouble: only one workspace is accessible without any issue. continue else: members_names.append(workspace_label) members.append(workspace) return members def _generate_child_workspace_resource( self, parent_path: str, child_workspace: Workspace) -> "WorkspaceResource": workspace_label = webdav_convert_file_name_to_display( child_workspace.filemanager_filename) path = add_trailing_slash(parent_path) workspace_path = "{}{}".format(path, workspace_label) return get_workspace_resource( path=workspace_path, environ=self.environ, workspace=child_workspace, tracim_context=self.tracim_context, label=workspace_label, ) # Container methods def createEmptyResource(self, name: str): raise NotImplementedError() def createCollection(self, label: str) -> "FolderResource": raise NotImplementedError() def getMemberNames(self) -> List[str]: """ This method returns the names (here workspace's labels) of all its children """ # INFO - G.M - 2020-14-10 - Unclear if this method is really used by wsgidav members_names = [] # type: List[str] for workspace in self._get_members(): members_names.append( webdav_convert_file_name_to_display( workspace.filemanager_filename)) return members_names def getMember(self, label: str) -> typing.Optional[_DAVResource]: """ Access to a specific members """ return self.provider.getResourceInst( "%s/%s" % (self.path, webdav_convert_file_name_to_display(label)), self.environ) def getMemberList(self): """ This method is called by wsgidav when requesting with a depth > 0, it will return a list of _DAVResource of all its direct children """ members = [] for workspace in self._get_members(): members.append( self._generate_child_workspace_resource( parent_path=self.path, child_workspace=workspace)) return members