def test_unit__crud_caller__ok__workspace(self, session): hook = WorkspaceHookImpl() session.context.plugin_manager.register(hook) owner = User(email="john") session.add(owner) session.flush() workspace = Workspace(label="Hello", owner_id=owner.user_id) session.add(workspace) session.flush() hook.mock_hooks.assert_called_with("created", workspace=workspace, context=session.context) workspace.label = "World" session.flush() hook.mock_hooks.assert_called_with("modified", workspace=workspace, context=session.context) session.delete(workspace) session.flush() hook.mock_hooks.assert_called_with("deleted", workspace=workspace, context=session.context)
def update_workspace( self, workspace: Workspace, label: str, description: str, save_now: bool = False, agenda_enabled: bool = None, ) -> Workspace: """ Update workspace :param workspace: workspace to update :param label: new label of workspace :param description: new description :param save_now: database flush :return: updated workspace """ if not label: raise EmptyLabelNotAllowed("Workspace label cannot be empty") workspace.label = label workspace.description = description if agenda_enabled is not None: workspace.agenda_enabled = agenda_enabled if save_now: self.save(workspace) return workspace
def update_workspace( self, workspace: Workspace, label: typing.Optional[str] = None, description: typing.Optional[str] = None, save_now: bool = False, agenda_enabled: typing.Optional[bool] = None, ) -> Workspace: """ Update workspace :param workspace: workspace to update :param label: new label of workspace :param description: new description :param save_now: database flush :return: updated workspace """ if label is not None: if label == "": raise EmptyLabelNotAllowed("Workspace label cannot be empty") if (self._session.query(Workspace).filter( Workspace.label == label ).filter(Workspace.workspace_id != workspace.workspace_id).count() > 0): raise WorkspaceLabelAlreadyUsed( "A workspace with label {} already exist.".format(label)) workspace.label = label if description is not None: workspace.description = description if agenda_enabled is not None: workspace.agenda_enabled = agenda_enabled workspace.updated = datetime.utcnow() if save_now: self.save(workspace) return workspace
def delete(self, workspace: Workspace, flush=True): workspace.is_deleted = True label = "{label}-{action}-{date}".format( label=workspace.label, action="deleted", date=current_date_for_filename()) workspace.label = label if flush: self._session.flush()
def create_workspace( self, label: str = "", description: str = "", agenda_enabled: bool = True, public_download_enabled: bool = True, public_upload_enabled: bool = True, access_type: WorkspaceAccessType = WorkspaceAccessType.CONFIDENTIAL, default_user_role: WorkspaceRoles = WorkspaceRoles.READER, parent: Workspace = None, save_now: bool = False, ) -> Workspace: if not self._user or not self._user_allowed_to_create_new_workspaces( self._user): raise UserNotAllowedToCreateMoreWorkspace( "User not allowed to create more workspace") if not label: raise EmptyLabelNotAllowed("Workspace label cannot be empty") if access_type not in self._config.WORKSPACE__ALLOWED_ACCESS_TYPES: raise DisallowedWorkspaceAccessType( 'Access type "{}" is not allowed for this workspace'.format( access_type.name)) workspace = Workspace() workspace.label = label workspace.description = description workspace.agenda_enabled = agenda_enabled workspace.public_download_enabled = public_download_enabled workspace.public_upload_enabled = public_upload_enabled workspace.created = datetime.utcnow() workspace.updated = datetime.utcnow() workspace.owner = self._user workspace.access_type = access_type workspace.default_user_role = default_user_role workspace.parent = parent # By default, we force the current user to be the workspace manager # And to receive email notifications role_api = RoleApi(session=self._session, current_user=self._user, config=self._config) with self._session.no_autoflush: role = role_api.create_one( self._user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, with_notif=True, flush=False, ) self._session.add(workspace) self._session.add(role) if save_now: self._session.flush() return workspace
def create_workspace( self, label: str='', description: str='', calendar_enabled: bool=False, save_now: bool=False, ) -> Workspace: if not label: raise EmptyLabelNotAllowed('Workspace label cannot be empty') if self._session.query(Workspace).filter(Workspace.label == label).count() > 0: # nopep8 raise WorkspaceLabelAlreadyUsed( 'A workspace with label {} already exist.'.format(label) ) workspace = Workspace() workspace.label = label workspace.description = description workspace.calendar_enabled = calendar_enabled # By default, we force the current user to be the workspace manager # And to receive email notifications role_api = RoleApi( session=self._session, current_user=self._user, config=self._config ) role = role_api.create_one( self._user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, with_notif=True, ) self._session.add(workspace) self._session.add(role) if save_now: self._session.flush() # TODO - G.M - 28-03-2018 - [Calendar] Reenable calendar stuff # if calendar_enabled: # self._ensure_calendar_exist(workspace) # else: # self._disable_calendar(workspace) return workspace
def create_workspace( self, label: str = '', description: str = '', calendar_enabled: bool = False, save_now: bool = False, ) -> Workspace: if not label: raise EmptyLabelNotAllowed('Workspace label cannot be empty') if self._session.query(Workspace).filter( Workspace.label == label).count() > 0: # nopep8 raise WorkspaceLabelAlreadyUsed( 'A workspace with label {} already exist.'.format(label)) workspace = Workspace() workspace.label = label workspace.description = description workspace.calendar_enabled = calendar_enabled # By default, we force the current user to be the workspace manager # And to receive email notifications role_api = RoleApi(session=self._session, current_user=self._user, config=self._config) role = role_api.create_one( self._user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, with_notif=True, ) self._session.add(workspace) self._session.add(role) if save_now: self._session.flush() # TODO - G.M - 28-03-2018 - [Calendar] Reenable calendar stuff # if calendar_enabled: # self._ensure_calendar_exist(workspace) # else: # self._disable_calendar(workspace) return workspace
def update_workspace( self, workspace: Workspace, label: typing.Optional[str] = None, description: typing.Optional[str] = None, save_now: bool = False, agenda_enabled: typing.Optional[bool] = None, public_upload_enabled: typing.Optional[bool] = None, public_download_enabled: typing.Optional[bool] = None, default_user_role: typing.Optional[WorkspaceRoles] = None, ) -> Workspace: """ Update workspace :param workspace: workspace to update :param label: new label of workspace :param description: new description :param save_now: database flush :return: updated workspace """ if label is not None: if label == "": raise EmptyLabelNotAllowed("Workspace label cannot be empty") workspace.label = label if description is not None: workspace.description = description if agenda_enabled is not None: workspace.agenda_enabled = agenda_enabled if public_upload_enabled is not None: workspace.public_upload_enabled = public_upload_enabled if public_download_enabled is not None: workspace.public_download_enabled = public_download_enabled if default_user_role: workspace.default_user_role = default_user_role workspace.updated = datetime.utcnow() if save_now: self.save(workspace) return workspace
def create_workspace( self, label: str = "", description: str = "", agenda_enabled: bool = True, save_now: bool = False, ) -> Workspace: if not label: raise EmptyLabelNotAllowed("Workspace label cannot be empty") if self._session.query(Workspace).filter( Workspace.label == label).count() > 0: raise WorkspaceLabelAlreadyUsed( "A workspace with label {} already exist.".format(label)) workspace = Workspace() workspace.label = label workspace.description = description workspace.agenda_enabled = agenda_enabled workspace.created = datetime.utcnow() workspace.updated = datetime.utcnow() # By default, we force the current user to be the workspace manager # And to receive email notifications role_api = RoleApi(session=self._session, current_user=self._user, config=self._config) role = role_api.create_one(self._user, workspace, UserRoleInWorkspace.WORKSPACE_MANAGER, with_notif=True) self._session.add(workspace) self._session.add(role) if save_now: self._session.flush() return workspace
def update_workspace( self, workspace: Workspace, label: str, description: str, save_now: bool=False, ) -> Workspace: """ Update workspace :param workspace: workspace to update :param label: new label of workspace :param description: new description :param save_now: database flush :return: updated workspace """ if not label: raise EmptyLabelNotAllowed('Workspace label cannot be empty') workspace.label = label workspace.description = description if save_now: self.save(workspace) return workspace