def _get_candidate_workspace(self, user: User, request: 'TracimRequest') -> Workspace: """ Get current workspace from request :param user: User who want to check the workspace :param request: pyramid request :return: current workspace """ workspace_id = '' try: if 'new_workspace_id' in request.json_body: workspace_id = request.json_body['new_workspace_id'] if not isinstance(workspace_id, int): if workspace_id.isdecimal(): workspace_id = int(workspace_id) else: raise InvalidWorkspaceId( 'workspace_id is not a correct integer') # nopep8 if not workspace_id: raise WorkspaceNotFoundInTracimRequest( 'No new_workspace_id property found in body') # nopep8 wapi = WorkspaceApi(current_user=user, session=request.dbsession, config=request.registry.settings['CFG']) workspace = wapi.get_one(workspace_id) except JSONDecodeError as exc: raise WorkspaceNotFound('Invalid JSON content') from exc except NoResultFound as exc: raise WorkspaceNotFound('Workspace {} does not exist ' 'or is not visible for this user'.format( workspace_id)) from exc return workspace
def _get_current_workspace(self, user: User, request: 'TracimRequest') -> Workspace: """ Get current workspace from request :param user: User who want to check the workspace :param request: pyramid request :return: current workspace """ workspace_id = '' try: if 'workspace_id' in request.matchdict: workspace_id_str = request.matchdict['workspace_id'] if not isinstance( workspace_id_str, str) or not workspace_id_str.isdecimal(): # nopep8 raise InvalidWorkspaceId( 'workspace_id is not a correct integer') # nopep8 workspace_id = int(request.matchdict['workspace_id']) if not workspace_id: raise WorkspaceNotFoundInTracimRequest( 'No workspace_id property found in request') # nopep8 wapi = WorkspaceApi(current_user=user, session=request.dbsession, config=request.registry.settings['CFG']) workspace = wapi.get_one(workspace_id) except NoResultFound as exc: raise WorkspaceNotFound('Workspace {} does not exist ' 'or is not visible for this user'.format( workspace_id)) from exc return workspace
def contents_read_status(self, context, request: TracimRequest, hapic_data=None): # nopep8 """ get user_read status of contents """ app_config = request.registry.settings['CFG'] content_filter = hapic_data.query api = ContentApi( current_user=request.candidate_user, # User session=request.dbsession, config=app_config, ) wapi = WorkspaceApi( current_user=request.candidate_user, # User session=request.dbsession, config=app_config, ) workspace = None if hapic_data.path.workspace_id: workspace = wapi.get_one(hapic_data.path.workspace_id) last_actives = api.get_last_active( workspace=workspace, limit=None, before_datetime=None, content_ids=hapic_data.query.contents_ids or None) return [ api.get_content_in_context(content) for content in last_actives ]
def delete_comment(self, context, request: TracimRequest, hapic_data=None): """ Delete comment """ app_config = request.registry.settings['CFG'] api = ContentApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) wapi = WorkspaceApi( current_user=request.current_user, session=request.dbsession, config=app_config, ) workspace = wapi.get_one(hapic_data.path.workspace_id) parent = api.get_one(hapic_data.path.content_id, content_type=ContentType.Any, workspace=workspace) comment = api.get_one( hapic_data.path.comment_id, content_type=ContentType.Comment, workspace=workspace, parent=parent, ) with new_revision(session=request.dbsession, tm=transaction.manager, content=comment): api.delete(comment) return
def get_workspace(user: User, request: Request) -> typing.Optional[Workspace]: """ Get current workspace from request :param user: User who want to check the workspace :param request: pyramid request :return: """ workspace_id = '' try: if 'workspace_id' not in request.json_body: return None workspace_id = request.json_body['workspace_id'] wapi = WorkspaceApi(current_user=user, session=request.dbsession) workspace = wapi.get_one(workspace_id) except JSONDecodeError: raise WorkspaceNotFound('Bad json body') except NoResultFound: raise WorkspaceNotFound( 'Workspace {} does not exist ' 'or is not visible for this user'.format(workspace_id) ) return workspace