def test_update(self): created_content = self.test_create() content = DBSession.query(Content).filter(Content.id == created_content.id).one() eq_(1, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count()) with new_revision(content): time.sleep(0.00001) content.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED' DBSession.flush() eq_(2, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1').count()) eq_(1, DBSession.query(Content).filter(Content.id == created_content.id).count()) with new_revision(content): time.sleep(0.00001) content.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED_2' content.label = 'TEST_CONTENT_1_UPDATED_2' DBSession.flush() eq_(1, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'TEST_CONTENT_1_UPDATED_2').count()) eq_(1, DBSession.query(Content).filter(Content.id == created_content.id).count()) revision_1 = DBSession.query(ContentRevisionRO)\ .filter(ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1').one() revision_2 = DBSession.query(ContentRevisionRO)\ .filter(ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1_UPDATED').one() revision_3 = DBSession.query(ContentRevisionRO)\ .filter(ContentRevisionRO.description == 'TEST_CONTENT_DESCRIPTION_1_UPDATED_2').one() # Updated dates must be different ok_(revision_1.updated < revision_2.updated < revision_3.updated) # Created dates must be equal ok_(revision_1.created == revision_2.created == revision_3.created)
def test_unit__search_exclude_content_under_deleted_or_archived_parents__ok( self): admin = DBSession.query(User).filter( User.email == '*****@*****.**').one() workspace = self._create_workspace_and_test('workspace_1', admin) folder_1 = self._create_content_and_test('folder_1', workspace=workspace, type=ContentType.Folder) folder_2 = self._create_content_and_test('folder_2', workspace=workspace, type=ContentType.Folder) page_1 = self._create_content_and_test('foo', workspace=workspace, type=ContentType.Page, parent=folder_1) page_2 = self._create_content_and_test('bar', workspace=workspace, type=ContentType.Page, parent=folder_2) api = ContentApi(admin) foo_result = api.search(['foo']).all() eq_(1, len(foo_result)) ok_(page_1 in foo_result) bar_result = api.search(['bar']).all() eq_(1, len(bar_result)) ok_(page_2 in bar_result) with new_revision(folder_1): api.delete(folder_1) with new_revision(folder_2): api.archive(folder_2) # Actually ContentApi.search don't filter it foo_result = api.search(['foo']).all() eq_(1, len(foo_result)) ok_(page_1 in foo_result) bar_result = api.search(['bar']).all() eq_(1, len(bar_result)) ok_(page_2 in bar_result) # ContentApi offer exclude_unavailable method to do it foo_result = api.search(['foo']).all() api.exclude_unavailable(foo_result) eq_(0, len(foo_result)) bar_result = api.search(['bar']).all() api.exclude_unavailable(bar_result) eq_(0, len(bar_result))
def test_search_in_label_or_description(self): # HACK - D.A. - 2015-03-09 # This test is based on a bug which does NOT return results found # at root of a workspace (eg a folder) uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) a = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', True) p1 = api.create(ContentType.Page, workspace, a, 'this is dummy label content', True) p2 = api.create(ContentType.Page, workspace, a, 'Hey ! Jon !', True) with new_revision(p1): p1.description = 'This is some amazing test' with new_revision(p2): p2.description = 'What\'s up ?' api.save(p1) api.save(p2) id1 = p1.content_id id2 = p2.content_id eq_(1, DBSession.query(Workspace).filter(Workspace.label == 'test workspace').count()) eq_(1, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is randomized folder').count()) eq_(2, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'this is dummy label content').count()) eq_(1, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'This is some amazing test').count()) eq_(2, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.label == 'Hey ! Jon !').count()) eq_(1, DBSession.query(ContentRevisionRO).filter(ContentRevisionRO.description == 'What\'s up ?').count()) res = api.search(['dummy', 'jon']) eq_(2, len(res.all())) eq_(True, id1 in [o.content_id for o in res.all()]) eq_(True, id2 in [o.content_id for o in res.all()])
def move_file(self, destpath): workspace = self.provider.get_workspace_from_path( normpath(destpath), WorkspaceApi(self.user) ) parent = self.provider.get_parent_from_path( normpath(destpath), self.content_api, workspace ) with new_revision(self.content): if basename(destpath) != self.getDisplayName(): self.content_api.update_content(self.content, re.sub('\.[^\.]+$', '', self.provider.transform_to_bdd(basename(destpath)))) self.content_api.save(self.content) else: self.content_api.move( item=self.content, new_parent=parent, must_stay_in_same_workspace=False, new_workspace=workspace ) transaction.commit()
def put_delete(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._parent_url.format(item.workspace_id, item.parent_id) tmp_url = self._std_url.format(item.workspace_id, item.content_id) undo_url = tmp_url + '/put_delete_undo' deleted_msg = '{} deleted. ' \ '<a class="alert-link" href="{}">Cancel action</a>' msg = _(deleted_msg).format(self._item_type_label, undo_url) with new_revision(item): content_api.delete(item) content_api.save(item, ActionDescription.DELETION) tg.flash(msg, CST.STATUS_OK, no_escape=True) tg.redirect(next_url) except ValueError as e: back_url = self._std_url.format(item.workspace_id, item.content_id) msg = _('{} not deleted: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(back_url)
def put_archive(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._parent_url.format(item.workspace_id, item.parent_id) undo_url = self._std_url.format( item.workspace_id, item.content_id) + '/put_archive_undo' msg = _( '{} archived. <a class="alert-link" href="{}">Cancel action</a>' ).format(self._item_type_label, undo_url) with new_revision(item): content_api.archive(item) content_api.save(item, ActionDescription.ARCHIVING) tg.flash(msg, CST.STATUS_OK, no_escape=True) # TODO allow to come back tg.redirect(next_url) except ValueError as e: next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) msg = _('{} not archived: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(next_url)
def test_search_in_description(self): # HACK - D.A. - 2015-03-09 # This test is based on a bug which does NOT return results found # at root of a workspace (eg a folder) uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) a = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', True) p = api.create(ContentType.Page, workspace, a, 'this is dummy label content', True) with new_revision(p): p.description = 'This is some amazing test' api.save(p) original_id = p.content_id res = api.search(['dummy']) eq_(1, len(res.all())) item = res.all()[0] eq_(original_id, item.content_id)
def put(self, item_id, label='',content=''): # TODO - SECURE THIS workspace = tmpl_context.workspace try: api = ContentApi(tmpl_context.current_user) item = api.get_one(int(item_id), self._item_type, workspace) with new_revision(item): api.update_content(item, label, content) if not self._path_validation.validate_new_content(item): return render_invalid_integrity_chosen_path( item.get_label(), ) api.save(item, ActionDescription.REVISION) msg = _('{} updated').format(self._item_type_label) tg.flash(msg, CST.STATUS_OK) tg.redirect(self._std_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item.content_id)) except SameValueError as e: msg = _('{} not updated: the content did not change').format(self._item_type_label) tg.flash(msg, CST.STATUS_WARNING) tg.redirect(self._err_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item_id)) except ValueError as e: msg = _('{} not updated - error: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(self._err_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item_id))
def put_delete_undo(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) # Here we do not filter deleted items content_api = ContentApi(tmpl_context.current_user, True, True) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._std_url.format(item.workspace_id, item.content_id) msg = _('{} undeleted.').format(self._item_type_label) with new_revision(item): content_api.undelete(item) content_api.save(item, ActionDescription.UNDELETION) tg.flash(msg, CST.STATUS_OK) tg.redirect(next_url) except ValueError as e: logger.debug(self, 'Exception: {}'.format(e.__str__)) back_url = self._parent_url.format(item.workspace_id, item.parent_id) msg = _('{} not un-deleted: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(back_url)
def test_set_status_ok(self): uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) c = api.create(ContentType.Folder, workspace, None, 'parent', True) with new_revision(c): for new_status in [ 'open', 'closed-validated', 'closed-unvalidated', 'closed-deprecated' ]: api.set_status(c, new_status) eq_(new_status, c.status) eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
def put_archive_undo(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user, True) # Here we do not filter archived items item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) msg = _('{} unarchived.').format(self._item_type_label) with new_revision(item): content_api.unarchive(item) content_api.save(item, ActionDescription.UNARCHIVING) tg.flash(msg, CST.STATUS_OK) tg.redirect(next_url) except ValueError as e: msg = _('{} not un-archived: {}').format(self._item_type_label, str(e)) next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) # We still use std url because the item has not been archived tg.flash(msg, CST.STATUS_ERROR) tg.redirect(next_url)
def put_delete_undo(self, item_id): require_current_user_is_owner(int(item_id)) item_id = int(item_id) # Here we do not filter deleted items content_api = ContentApi(tmpl_context.current_user, True, True) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) next_or_back = '/workspaces/{}/folders/{}/threads/{}' try: next_url = tg.url(next_or_back).format(tmpl_context.workspace_id, tmpl_context.folder_id, tmpl_context.thread_id) msg = _('{} undeleted.').format(self._item_type_label) with new_revision(item): content_api.undelete(item) content_api.save(item, ActionDescription.UNDELETION) tg.flash(msg, CST.STATUS_OK) tg.redirect(next_url) except ValueError as e: logger.debug(self, 'Exception: {}'.format(e.__str__)) back_url = tg.url(next_or_back).format(tmpl_context.workspace_id, tmpl_context.folder_id, tmpl_context.thread_id) msg = _('{} not un-deleted: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(back_url)
def put_delete(self, item_id): require_current_user_is_owner(int(item_id)) # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) next_or_back = '/workspaces/{}/folders/{}/threads/{}' try: next_url = tg.url(next_or_back).format(tmpl_context.workspace_id, tmpl_context.folder_id, tmpl_context.thread_id) undo_str = '{}/comments/{}/put_delete_undo' undo_url = tg.url(undo_str).format(next_url, item_id) msg_str = ('{} deleted. ' '<a class="alert-link" href="{}">Cancel action</a>') msg = _(msg_str).format(self._item_type_label, undo_url) with new_revision(item): content_api.delete(item) content_api.save(item, ActionDescription.DELETION) tg.flash(msg, CST.STATUS_OK, no_escape=True) tg.redirect(next_url) except ValueError as e: back_url = tg.url(next_or_back).format(tmpl_context.workspace_id, tmpl_context.folder_id, tmpl_context.thread_id) msg = _('{} not deleted: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(back_url)
def put(self, item_id, label='', content=''): # INFO - D.A. This method is a raw copy of # TODO - SECURE THIS workspace = tmpl_context.workspace try: api = ContentApi(tmpl_context.current_user) item = api.get_one(int(item_id), self._item_type, workspace) with new_revision(item): api.update_content(item, label, content) api.save(item, ActionDescription.REVISION) msg = _('{} updated').format(self._item_type_label) tg.flash(msg, CST.STATUS_OK) tg.redirect( self._std_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item.content_id)) except SameValueError as e: msg = _('{} not updated: the content did not change').format( self._item_type_label) tg.flash(msg, CST.STATUS_WARNING) tg.redirect( self._err_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item_id)) except ValueError as e: msg = _('{} not updated - error: {}').format( self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect( self._err_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item_id))
def test_search_in_description(self): # HACK - D.A. - 2015-03-09 # This test is based on a bug which does NOT return results found # at root of a workspace (eg a folder) uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) a = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', True) p = api.create(ContentType.Page, workspace, a, 'this is dummy label content', True) with new_revision(p): p.description = 'This is some amazing test' api.save(p) original_id = p.content_id res = api.search(['dummy']) eq_(1, len(res.all())) item = res.all()[0] eq_(original_id, item.content_id)
def test_query(self): content1 = self.test_create() with new_revision(content1): content1.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED' DBSession.flush() content2 = self.test_create(key='2') with new_revision(content2): content2.description = 'TEST_CONTENT_DESCRIPTION_2_UPDATED' DBSession.flush() workspace1 = DBSession.query(Workspace).filter( Workspace.label == 'TEST_WORKSPACE_1').one() workspace2 = DBSession.query(Workspace).filter( Workspace.label == 'TEST_WORKSPACE_2').one() # To get Content in database we have to join Content and ContentRevisionRO with particular condition: # Join have to be on most recent revision join_sub_query = DBSession.query(ContentRevisionRO.revision_id)\ .filter(ContentRevisionRO.content_id == Content.id)\ .order_by(ContentRevisionRO.revision_id.desc())\ .limit(1)\ .correlate(Content) base_query = DBSession.query(Content)\ .join(ContentRevisionRO, and_(Content.id == ContentRevisionRO.content_id, ContentRevisionRO.revision_id == join_sub_query)) pattern = 'TEST_CONTENT_DESCRIPTION_%_UPDATED' eq_(2, base_query.filter(Content.description.like(pattern)).count()) eq_(1, base_query.filter(Content.workspace == workspace1).count()) eq_(1, base_query.filter(Content.workspace == workspace2).count()) content1_from_query = base_query.filter( Content.workspace == workspace1).one() eq_(content1.id, content1_from_query.id) eq_('TEST_CONTENT_DESCRIPTION_1_UPDATED', content1_from_query.description) user_admin = DBSession.query(User).filter( User.email == '*****@*****.**').one() api = ContentApi(None) content1_from_api = api.get_one(content1.id, ContentType.Page, workspace1)
def move_recursively(self, item: Content, new_parent: Content, new_workspace: Workspace): self.move(item, new_parent, False, new_workspace) self.save(item, do_notify=False) for child in item.children: with new_revision(child): self.move_recursively(child, item, new_workspace) return
def test_archive(self): uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) item = api.create(ContentType.Folder, workspace, None, 'not_archived', True) item2 = api.create(ContentType.Folder, workspace, None, 'to_archive', True) uid = user.user_id wid = workspace.workspace_id transaction.commit() # Refresh instances after commit user = uapi.get_one(uid) workspace = WorkspaceApi(user).get_one(wid) api = ContentApi(user) items = api.get_all(None, ContentType.Any, workspace) eq_(2, len(items)) items = api.get_all(None, ContentType.Any, workspace) with new_revision(items[0]): api.archive(items[0]) transaction.commit() # Refresh instances after commit user = uapi.get_one(uid) workspace = WorkspaceApi(user).get_one(wid) api = ContentApi(user) items = api.get_all(None, ContentType.Any, workspace) eq_(1, len(items)) transaction.commit() # Refresh instances after commit user = uapi.get_one(uid) workspace = WorkspaceApi(user).get_one(wid) api = ContentApi(user) # Test that the item is still available if "show deleted" is activated api = ContentApi(None, show_archived=True) items = api.get_all(None, ContentType.Any, workspace) eq_(2, len(items))
def update_file(self): """ Called when we're updating an existing content; we create a new revision and update the file content """ with new_revision(self._content): self._api.update_file_data( self._content, self._file_name, util.guessMimeType(self._content.file_name), self._file_stream.read()) self._api.save(self._content, ActionDescription.EDITION)
def test_unit__search_exclude_content_under_deleted_or_archived_parents__ok(self): admin = DBSession.query(User).filter(User.email == '*****@*****.**').one() workspace = self._create_workspace_and_test('workspace_1', admin) folder_1 = self._create_content_and_test('folder_1', workspace=workspace, type=ContentType.Folder) folder_2 = self._create_content_and_test('folder_2', workspace=workspace, type=ContentType.Folder) page_1 = self._create_content_and_test('foo', workspace=workspace, type=ContentType.Page, parent=folder_1) page_2 = self._create_content_and_test('bar', workspace=workspace, type=ContentType.Page, parent=folder_2) api = ContentApi(admin) foo_result = api.search(['foo']).all() eq_(1, len(foo_result)) ok_(page_1 in foo_result) bar_result = api.search(['bar']).all() eq_(1, len(bar_result)) ok_(page_2 in bar_result) with new_revision(folder_1): api.delete(folder_1) with new_revision(folder_2): api.archive(folder_2) # Actually ContentApi.search don't filter it foo_result = api.search(['foo']).all() eq_(1, len(foo_result)) ok_(page_1 in foo_result) bar_result = api.search(['bar']).all() eq_(1, len(bar_result)) ok_(page_2 in bar_result) # ContentApi offer exclude_unavailable method to do it foo_result = api.search(['foo']).all() api.exclude_unavailable(foo_result) eq_(0, len(foo_result)) bar_result = api.search(['bar']).all() api.exclude_unavailable(bar_result) eq_(0, len(bar_result))
def test_query(self): content1 = self.test_create() with new_revision(content1): content1.description = 'TEST_CONTENT_DESCRIPTION_1_UPDATED' DBSession.flush() content2 = self.test_create(key='2') with new_revision(content2): content2.description = 'TEST_CONTENT_DESCRIPTION_2_UPDATED' DBSession.flush() workspace1 = DBSession.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_1').one() workspace2 = DBSession.query(Workspace).filter(Workspace.label == 'TEST_WORKSPACE_2').one() # To get Content in database we have to join Content and ContentRevisionRO with particular condition: # Join have to be on most recent revision join_sub_query = DBSession.query(ContentRevisionRO.revision_id)\ .filter(ContentRevisionRO.content_id == Content.id)\ .order_by(ContentRevisionRO.revision_id.desc())\ .limit(1)\ .correlate(Content) base_query = DBSession.query(Content)\ .join(ContentRevisionRO, and_(Content.id == ContentRevisionRO.content_id, ContentRevisionRO.revision_id == join_sub_query)) eq_(2, base_query.count()) eq_(1, base_query.filter(Content.workspace == workspace1).count()) eq_(1, base_query.filter(Content.workspace == workspace2).count()) content1_from_query = base_query.filter(Content.workspace == workspace1).one() eq_(content1.id, content1_from_query.id) eq_('TEST_CONTENT_DESCRIPTION_1_UPDATED', content1_from_query.description) user_admin = DBSession.query(User).filter(User.email == '*****@*****.**').one() api = ContentApi(None) content1_from_api = api.get_one(content1.id, ContentType.Page, workspace1)
def action(self): try: # When undeleting/unarchiving we except a content with the new name to not exist, thus if we # don't get an error and the database request send back a result, we stop the action self.content_api.get_one_by_label_and_parent(self._new_name, self.content.parent) raise DAVError(HTTP_FORBIDDEN) except NoResultFound: with new_revision(self.content): self.content_api.update_content(self.content, self._new_name) self._actions[self._type](self.content) self.content_api.save(self.content, self._type) transaction.commit()
def test_archive(self): uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) item = api.create(ContentType.Folder, workspace, None, 'not_archived', True) item2 = api.create(ContentType.Folder, workspace, None, 'to_archive', True) uid = user.user_id wid = workspace.workspace_id transaction.commit() # Refresh instances after commit user = uapi.get_one(uid) workspace = WorkspaceApi(user).get_one(wid) api = ContentApi(user) items = api.get_all(None, ContentType.Any, workspace) eq_(2, len(items)) items = api.get_all(None, ContentType.Any, workspace) with new_revision(items[0]): api.archive(items[0]) transaction.commit() # Refresh instances after commit user = uapi.get_one(uid) workspace = WorkspaceApi(user).get_one(wid) api = ContentApi(user) items = api.get_all(None, ContentType.Any, workspace) eq_(1, len(items)) transaction.commit() # Refresh instances after commit user = uapi.get_one(uid) workspace = WorkspaceApi(user).get_one(wid) api = ContentApi(user) # Test that the item is still available if "show deleted" is activated api = ContentApi(None, show_archived=True) items = api.get_all(None, ContentType.Any, workspace) eq_(2, len(items))
def action(self): try: # When undeleting/unarchiving we except a content with the new name to not exist, thus if we # don't get an error and the database request send back a result, we stop the action self.content_api.get_one_by_label_and_parent( self._new_name, self.content.parent, self.content.workspace) raise DAVError(HTTP_FORBIDDEN) except NoResultFound: with new_revision(self.content): self.content_api.update_content(self.content, self._new_name) self._actions[self._type](self.content) self.content_api.save(self.content, self._type) transaction.commit()
def update_file(self): """ Called when we're updating an existing content; we create a new revision and update the file content """ with new_revision(self._content): self._api.update_file_data( self._content, self._file_name, util.guessMimeType(self._content.file_name), self._file_stream.read() ) self._api.save(self._content, ActionDescription.REVISION)
def put_status(self, item_id, status): item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: with new_revision(item): content_api.set_status(item, status) content_api.save(item, ActionDescription.STATUS_UPDATE) msg = _('{} status updated').format(self._item_type_label) tg.flash(msg, CST.STATUS_OK) tg.redirect(self._std_url.format(item.workspace_id, item.parent_id, item.content_id)) except ValueError as e: msg = _('{} status not updated: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(self._err_url.format(item.workspace_id, item.parent_id, item.content_id))
def test_set_status_unknown_status(self): uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) c = api.create(ContentType.Folder, workspace, None, 'parent', True) with new_revision(c): api.set_status(c, 'unknown-status')
def update_event( self, calendar: Calendar, event: iCalendarEvent, event_name: str, current_user: User, ) -> Content: """ Update Content Event :param calendar: Event calendar owner :param event: ICS event :param event_name: Event name (ID) like 20160602T083511Z-18100-1001-1-71_Bastien-20160602T083516Z.ics :param current_user: Current modification asking user :return: Updated Content """ workspace = None if isinstance(calendar, WorkspaceCalendar): workspace = calendar.related_object elif isinstance(calendar, UserCalendar): pass else: raise UnknownCalendarType('Type "{0}" is not implemented' .format(type(calendar))) content_api = ContentApi( current_user, force_show_all_types=True, disable_user_workspaces_filter=True ) content = content_api.find_one_by_unique_property( property_name='name', property_value=event_name, workspace=workspace ) with new_revision(content): self.populate_content_with_event( content, event, event_name ) content.revision_type = ActionDescription.EDITION DBSession.flush() transaction.commit() return content
def test_set_status_unknown_status(self): uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) c = api.create(ContentType.Folder, workspace, None, 'parent', True) with new_revision(c): api.set_status(c, 'unknown-status')
def put(self, folder_id, label, can_contain_folders=False, can_contain_threads=False, can_contain_files=False, can_contain_pages=False): # TODO - SECURE THIS workspace = tmpl_context.workspace api = ContentApi(tmpl_context.current_user) next_url = '' try: folder = api.get_one(int(folder_id), ContentType.Folder, workspace) subcontent = dict( folder=True if can_contain_folders == 'on' else False, thread=True if can_contain_threads == 'on' else False, file=True if can_contain_files == 'on' else False, page=True if can_contain_pages == 'on' else False) with new_revision(folder): if label != folder.label: # TODO - D.A. - 2015-05-25 # Allow to set folder description api.update_content(folder, label, folder.description) api.set_allowed_content(folder, subcontent) if not self._path_validation.validate_new_content(folder): return render_invalid_integrity_chosen_path( folder.get_label(), ) api.save(folder) tg.flash(_('Folder updated'), CST.STATUS_OK) next_url = self.url(folder.content_id) except Exception as e: tg.flash( _('Folder not updated: {}').format(str(e)), CST.STATUS_ERROR) next_url = self.url(int(folder_id)) tg.redirect(next_url)
def test_set_status_ok(self): uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) c = api.create(ContentType.Folder, workspace, None, 'parent', True) with new_revision(c): for new_status in ['open', 'closed-validated', 'closed-unvalidated', 'closed-deprecated']: api.set_status(c, new_status) eq_(new_status, c.status) eq_(ActionDescription.STATUS_UPDATE, c.revision_type)
def delete_event_with_name(self, event_name: str, current_user: User)\ -> Content: """ Delete Content Event :param event_name: Event name (ID) like 20160602T083511Z-18100-1001-1-71_Bastien-20160602T083516Z.ics :param current_user: Current deletion asking user :return: Deleted Content """ content_api = ContentApi(current_user, force_show_all_types=True) content = content_api.find_one_by_unique_property( property_name='name', property_value=event_name, workspace=None) with new_revision(content): content_api.delete(content) DBSession.flush() transaction.commit() return content
def update_event( self, calendar: Calendar, event: iCalendarEvent, event_name: str, current_user: User, ) -> Content: """ Update Content Event :param calendar: Event calendar owner :param event: ICS event :param event_name: Event name (ID) like 20160602T083511Z-18100-1001-1-71_Bastien-20160602T083516Z.ics :param current_user: Current modification asking user :return: Updated Content """ workspace = None if isinstance(calendar, WorkspaceCalendar): workspace = calendar.related_object elif isinstance(calendar, UserCalendar): pass else: raise UnknownCalendarType('Type "{0}" is not implemented'.format( type(calendar))) content_api = ContentApi(current_user, force_show_all_types=True, disable_user_workspaces_filter=True) content = content_api.find_one_by_unique_property( property_name='name', property_value=event_name, workspace=workspace) with new_revision(content): self.populate_content_with_event(content, event, event_name) content.revision_type = ActionDescription.EDITION DBSession.flush() transaction.commit() return content
def put_archive_undo(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user, True) # Here we do not filter archived items item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) msg = _('{} unarchived.').format(self._item_type_label) with new_revision(item): content_api.unarchive(item) content_api.save(item, ActionDescription.UNARCHIVING) tg.flash(msg, CST.STATUS_OK) tg.redirect(next_url ) except ValueError as e: msg = _('{} not un-archived: {}').format(self._item_type_label, str(e)) next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) # We still use std url because the item has not been archived tg.flash(msg, CST.STATUS_ERROR) tg.redirect(next_url)
def put_archive(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user) item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._parent_url.format(item.workspace_id, item.parent_id) undo_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id)+'/put_archive_undo' msg = _('{} archived. <a class="alert-link" href="{}">Cancel action</a>').format(self._item_type_label, undo_url) with new_revision(item): content_api.archive(item) content_api.save(item, ActionDescription.ARCHIVING) tg.flash(msg, CST.STATUS_OK, no_escape=True) # TODO allow to come back tg.redirect(next_url) except ValueError as e: next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) msg = _('{} not archived: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(next_url)
def move_file(self, destpath): workspace = self.content.workspace parent = self.content.parent with new_revision(self.content): if basename(destpath) != self.getDisplayName(): new_given_file_name = transform_to_bdd(basename(destpath)) new_file_name, new_file_extension = \ os.path.splitext(new_given_file_name) self.content_api.update_content( self.content, new_file_name, ) self.content.file_extension = new_file_extension self.content_api.save(self.content) else: workspace_api = WorkspaceApi(self.user) content_api = ContentApi(self.user) destination_workspace = self.provider.get_workspace_from_path( destpath, workspace_api, ) destination_parent = self.provider.get_parent_from_path( destpath, content_api, destination_workspace, ) self.content_api.move( item=self.content, new_parent=destination_parent, must_stay_in_same_workspace=False, new_workspace=destination_workspace ) transaction.commit()
def put_status(self, item_id, status): item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user) try: item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) with new_revision(item): content_api.set_status(item, status) content_api.save(item, ActionDescription.STATUS_UPDATE) msg = _('{} status updated').format(self._item_type_label) tg.flash(msg, CST.STATUS_OK) tg.redirect(self._std_url.format(item.workspace_id, item.parent_id, item.content_id)) except ValueError as e: msg = _('{} status not updated: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(self._err_url.format(item.workspace_id, item.parent_id, item.content_id)) except NoResultFound as e: # probably the content is deleted or archived => forbidden to update status content_api = ContentApi( tmpl_context.current_user, show_archived=True, show_deleted=True ) item = content_api.get_one( item_id, self._item_type, tmpl_context.workspace ) next_url = self._std_url.format( item.workspace_id, item.parent_id, item.content_id ) msg = _('{} status not updated: the operation ' 'is not allowed on deleted/archived content').format( self._item_type_label ) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(next_url)
def put_delete_undo(self, item_id): # TODO - CHECK RIGHTS item_id = int(item_id) content_api = ContentApi(tmpl_context.current_user, True, True) # Here we do not filter deleted items item = content_api.get_one(item_id, self._item_type, tmpl_context.workspace) try: next_url = self._std_url.format(item.workspace_id, item.parent_id, item.content_id) msg = _('{} undeleted.').format(self._item_type_label) with new_revision(item): content_api.undelete(item) content_api.save(item, ActionDescription.UNDELETION) tg.flash(msg, CST.STATUS_OK) tg.redirect(next_url) except ValueError as e: logger.debug(self, 'Exception: {}'.format(e.__str__)) back_url = self._parent_url.format(item.workspace_id, item.parent_id) msg = _('{} not un-deleted: {}').format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect(back_url)
def move_file(self, destpath): workspace = self.content.workspace parent = self.content.parent with new_revision(self.content): if basename(destpath) != self.getDisplayName(): new_given_file_name = transform_to_bdd(basename(destpath)) new_file_name, new_file_extension = \ os.path.splitext(new_given_file_name) self.content_api.update_content( self.content, new_file_name, ) self.content.file_extension = new_file_extension self.content_api.save(self.content) else: workspace_api = WorkspaceApi(self.user) content_api = ContentApi(self.user) destination_workspace = self.provider.get_workspace_from_path( destpath, workspace_api, ) destination_parent = self.provider.get_parent_from_path( destpath, content_api, destination_workspace, ) self.content_api.move(item=self.content, new_parent=destination_parent, must_stay_in_same_workspace=False, new_workspace=destination_workspace) transaction.commit()
def move_folder(self, destpath): workspace_api = WorkspaceApi(self.user) workspace = self.provider.get_workspace_from_path( normpath(destpath), workspace_api) parent = self.provider.get_parent_from_path(normpath(destpath), self.content_api, workspace) with new_revision(self.content): if basename(destpath) != self.getDisplayName(): self.content_api.update_content( self.content, transform_to_bdd(basename(destpath))) self.content_api.save(self.content) else: if workspace.workspace_id == self.content.workspace.workspace_id: self.content_api.move(self.content, parent) else: self.content_api.move_recursively(self.content, parent, workspace) transaction.commit()
def move_file(self, destpath): workspace = self.provider.get_workspace_from_path( normpath(destpath), WorkspaceApi(self.user)) parent = self.provider.get_parent_from_path(normpath(destpath), self.content_api, workspace) with new_revision(self.content): if basename(destpath) != self.getDisplayName(): self.content_api.update_content( self.content, re.sub('\.[^\.]+$', '', self.provider.transform_to_bdd(basename(destpath)))) self.content_api.save(self.content) else: self.content_api.move(item=self.content, new_parent=parent, must_stay_in_same_workspace=False, new_workspace=workspace) transaction.commit()
def delete_event_with_name(self, event_name: str, current_user: User)\ -> Content: """ Delete Content Event :param event_name: Event name (ID) like 20160602T083511Z-18100-1001-1-71_Bastien-20160602T083516Z.ics :param current_user: Current deletion asking user :return: Deleted Content """ content_api = ContentApi(current_user, force_show_all_types=True) content = content_api.find_one_by_unique_property( property_name='name', property_value=event_name, workspace=None ) with new_revision(content): content_api.delete(content) DBSession.flush() transaction.commit() return content
def move_folder(self, destpath): workspace_api = WorkspaceApi(self.user) workspace = self.provider.get_workspace_from_path( normpath(destpath), workspace_api ) parent = self.provider.get_parent_from_path( normpath(destpath), self.content_api, workspace ) with new_revision(self.content): if basename(destpath) != self.getDisplayName(): self.content_api.update_content(self.content, transform_to_bdd(basename(destpath))) self.content_api.save(self.content) else: if workspace.workspace_id == self.content.workspace.workspace_id: self.content_api.move(self.content, parent) else: self.content_api.move_recursively(self.content, parent, workspace) transaction.commit()
def test_delete_undelete(self): uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user1 = uapi.create_user(email='this.is@user', groups=groups, save_now=True) u1id = user1.user_id workspace = WorkspaceApi(user1).create_workspace('test workspace', save_now=True) wid = workspace.workspace_id user2 = uapi.create_user() user2.email = '*****@*****.**' uapi.save(user2) RoleApi(user1).create_one(user2, workspace, UserRoleInWorkspace.CONTENT_MANAGER, with_notif=True, flush=True) # show archived is used at the top end of the test api = ContentApi(user1, show_deleted=True) p = api.create(ContentType.File, workspace, None, 'this_is_a_page', True) u1id = user1.user_id u2id = user2.user_id pcid = p.content_id poid = p.owner_id transaction.commit() #### user1 = UserApi(None).get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) content = api.get_one(pcid, ContentType.Any, workspace) eq_(u1id, content.owner_id) eq_(poid, content.owner_id) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2, show_deleted=True) content2 = api2.get_one(pcid, ContentType.Any, workspace) with new_revision(content2): api2.delete(content2) api2.save(content2) transaction.commit() #### user1 = UserApi(None).get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) # show archived is used at the top end of the test api = ContentApi(user1, show_deleted=True) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2, show_deleted=True) updated = api2.get_one(pcid, ContentType.Any, workspace) eq_( u2id, updated.owner_id, 'the owner id should be {} (found {})'.format( u2id, updated.owner_id)) eq_(True, updated.is_deleted) eq_(ActionDescription.DELETION, updated.revision_type) #### updated2 = api.get_one(pcid, ContentType.Any, workspace) with new_revision(updated2): api.undelete(updated2) api.save(updated2) eq_(False, updated2.is_deleted) eq_(ActionDescription.UNDELETION, updated2.revision_type) eq_(u1id, updated2.owner_id)
def put(self, item_id, folder_id='0'): """ :param item_id: :param folder_id: id of the folder, in a style like 'workspace_14__content_1586' :return: """ # TODO - SECURE THIS workspace = tmpl_context.workspace item_id = int(item_id) new_workspace, new_parent = convert_id_into_instances(folder_id) if new_workspace != workspace: # check that user is at least # - content manager in current workspace # - content manager in new workspace user = tmpl_context.current_user if user.get_role(workspace) < UserRoleInWorkspace.CONTENT_MANAGER: tg.flash(_('You are not allowed ' 'to move this folder'), CST.STATUS_ERROR) tg.redirect(self.parent_controller.url(item_id)) if user.get_role( new_workspace) < UserRoleInWorkspace.CONTENT_MANAGER: tg.flash( _('You are not allowed to move ' 'this folder to this workspace'), CST.STATUS_ERROR) tg.redirect(self.parent_controller.url(item_id)) api = ContentApi(tmpl_context.current_user) item = api.get_one(item_id, ContentType.Any, workspace) with new_revision(item): api.move_recursively(item, new_parent, new_workspace) next_url = tg.url('/workspaces/{}/folders/{}'.format( new_workspace.workspace_id, item_id)) if new_parent: tg.flash( _('Item moved to {} (workspace {})').format( new_parent.label, new_workspace.label), CST.STATUS_OK) else: tg.flash( _('Item moved to workspace {}').format( new_workspace.label)) tg.redirect(next_url) else: # Default move inside same workspace api = ContentApi(tmpl_context.current_user) item = api.get_one(item_id, ContentType.Any, workspace) with new_revision(item): api.move(item, new_parent) next_url = self.parent_controller.url(item_id) if new_parent: tg.flash( _('Item moved to {}').format(new_parent.label), CST.STATUS_OK) else: tg.flash(_('Item moved to workspace root')) tg.redirect(next_url)
def put(self, item_id, file_data=None, comment=None, label=None): # TODO - SECURE THIS workspace = tmpl_context.workspace try: api = ContentApi(tmpl_context.current_user) item = api.get_one(int(item_id), self._item_type, workspace) label_changed = False if label is not None and label != item.label: label_changed = True if label is None: label = '' # TODO - D.A. - 2015-03-19 # refactor this method in order to make code easier to understand with new_revision(item): if (comment and label) or (not comment and label_changed): updated_item = api.update_content( item, label if label else item.label, comment if comment else '') # Display error page to user if chosen label is in conflict if not self._path_validation.validate_new_content( updated_item, ): return render_invalid_integrity_chosen_path( updated_item.get_label_as_file(), ) api.save(updated_item, ActionDescription.EDITION) # This case is the default "file title and description # update" In this case the file itself is not revisionned else: # So, now we may have a comment and/or a file revision if comment and '' == label: comment_item = api.create_comment(workspace, item, comment, do_save=False) if not isinstance(file_data, FieldStorage): api.save(comment_item, ActionDescription.COMMENT) else: # The notification is only sent # if the file is NOT updated # # If the file is also updated, # then a 'file revision' notification will be sent. api.save(comment_item, ActionDescription.COMMENT, do_notify=False) if isinstance(file_data, FieldStorage): api.update_file_data(item, file_data.filename, file_data.type, file_data.file.read()) # Display error page to user if chosen label is in # conflict if not self._path_validation.validate_new_content( item, ): return render_invalid_integrity_chosen_path( item.get_label_as_file(), ) api.save(item, ActionDescription.REVISION) msg = _('{} updated').format(self._item_type_label) tg.flash(msg, CST.STATUS_OK) tg.redirect( self._std_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item.content_id)) except ValueError as e: error = '{} not updated - error: {}' msg = _(error).format(self._item_type_label, str(e)) tg.flash(msg, CST.STATUS_ERROR) tg.redirect( self._err_url.format(tmpl_context.workspace_id, tmpl_context.folder_id, item_id))
def test_update_file_data(self): uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user1 = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user1).create_workspace('test workspace', save_now=True) wid = workspace.workspace_id user2 = uapi.create_user() user2.email = '*****@*****.**' uapi.save(user2) RoleApi(user1).create_one(user2, workspace, UserRoleInWorkspace.CONTENT_MANAGER, with_notif=True, flush=True) # Test starts here api = ContentApi(user1) p = api.create(ContentType.File, workspace, None, 'this_is_a_page', True) u1id = user1.user_id u2id = user2.user_id pcid = p.content_id poid = p.owner_id api.save(p) transaction.commit() # Refresh instances after commit user1 = uapi.get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) api = ContentApi(user1) content = api.get_one(pcid, ContentType.Any, workspace) eq_(u1id, content.owner_id) eq_(poid, content.owner_id) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2) content2 = api2.get_one(pcid, ContentType.Any, workspace) with new_revision(content2): api2.update_file_data(content2, 'index.html', 'text/html', b'<html>hello world</html>') api2.save(content2) transaction.commit() # Refresh instances after commit user1 = uapi.get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) updated = api.get_one(pcid, ContentType.Any, workspace) eq_( u2id, updated.owner_id, 'the owner id should be {} (found {})'.format( u2id, updated.owner_id)) eq_('index.html', updated.file_name) eq_('text/html', updated.file_mimetype) eq_(b'<html>hello world</html>', updated.file_content) eq_(ActionDescription.REVISION, updated.revision_type)
def test_search_in_label_or_description(self): # HACK - D.A. - 2015-03-09 # This test is based on a bug which does NOT return results found # at root of a workspace (eg a folder) uapi = UserApi(None) groups = [ GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN) ] user = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user).create_workspace('test workspace', save_now=True) api = ContentApi(user) a = api.create(ContentType.Folder, workspace, None, 'this is randomized folder', True) p1 = api.create(ContentType.Page, workspace, a, 'this is dummy label content', True) p2 = api.create(ContentType.Page, workspace, a, 'Hey ! Jon !', True) with new_revision(p1): p1.description = 'This is some amazing test' with new_revision(p2): p2.description = 'What\'s up ?' api.save(p1) api.save(p2) id1 = p1.content_id id2 = p2.content_id eq_( 1, DBSession.query(Workspace).filter( Workspace.label == 'test workspace').count()) eq_( 1, DBSession.query(ContentRevisionRO).filter( ContentRevisionRO.label == 'this is randomized folder').count()) eq_( 2, DBSession.query(ContentRevisionRO).filter( ContentRevisionRO.label == 'this is dummy label content').count()) eq_( 1, DBSession.query(ContentRevisionRO).filter( ContentRevisionRO.description == 'This is some amazing test').count()) eq_( 2, DBSession.query(ContentRevisionRO).filter( ContentRevisionRO.label == 'Hey ! Jon !').count()) eq_( 1, DBSession.query(ContentRevisionRO).filter( ContentRevisionRO.description == 'What\'s up ?').count()) res = api.search(['dummy', 'jon']) eq_(2, len(res.all())) eq_(True, id1 in [o.content_id for o in res.all()]) eq_(True, id2 in [o.content_id for o in res.all()])
def test_delete_undelete(self): uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user1 = uapi.create_user(email='this.is@user', groups=groups, save_now=True) u1id = user1.user_id workspace = WorkspaceApi(user1).create_workspace('test workspace', save_now=True) wid = workspace.workspace_id user2 = uapi.create_user() user2.email = '*****@*****.**' uapi.save(user2) RoleApi(user1).create_one(user2, workspace, UserRoleInWorkspace.CONTENT_MANAGER, with_notif=True, flush=True) # show archived is used at the top end of the test api = ContentApi(user1, show_deleted=True) p = api.create(ContentType.File, workspace, None, 'this_is_a_page', True) u1id = user1.user_id u2id = user2.user_id pcid = p.content_id poid = p.owner_id transaction.commit() #### user1 = UserApi(None).get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) content = api.get_one(pcid, ContentType.Any, workspace) eq_(u1id, content.owner_id) eq_(poid, content.owner_id) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2, show_deleted=True) content2 = api2.get_one(pcid, ContentType.Any, workspace) with new_revision(content2): api2.delete(content2) api2.save(content2) transaction.commit() #### user1 = UserApi(None).get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) # show archived is used at the top end of the test api = ContentApi(user1, show_deleted=True) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2, show_deleted=True) updated = api2.get_one(pcid, ContentType.Any, workspace) eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id)) eq_(True, updated.is_deleted) eq_(ActionDescription.DELETION, updated.revision_type) #### updated2 = api.get_one(pcid, ContentType.Any, workspace) with new_revision(updated2): api.undelete(updated2) api.save(updated2) eq_(False, updated2.is_deleted) eq_(ActionDescription.UNDELETION, updated2.revision_type) eq_(u1id, updated2.owner_id)
def test_update_file_data(self): uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user1 = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user1).create_workspace('test workspace', save_now=True) wid = workspace.workspace_id user2 = uapi.create_user() user2.email = '*****@*****.**' uapi.save(user2) RoleApi(user1).create_one(user2, workspace, UserRoleInWorkspace.CONTENT_MANAGER, with_notif=True, flush=True) # Test starts here api = ContentApi(user1) p = api.create(ContentType.File, workspace, None, 'this_is_a_page', True) u1id = user1.user_id u2id = user2.user_id pcid = p.content_id poid = p.owner_id api.save(p) transaction.commit() # Refresh instances after commit user1 = uapi.get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) api = ContentApi(user1) content = api.get_one(pcid, ContentType.Any, workspace) eq_(u1id, content.owner_id) eq_(poid, content.owner_id) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2) content2 = api2.get_one(pcid, ContentType.Any, workspace) with new_revision(content2): api2.update_file_data(content2, 'index.html', 'text/html', b'<html>hello world</html>') api2.save(content2) transaction.commit() # Refresh instances after commit user1 = uapi.get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) updated = api.get_one(pcid, ContentType.Any, workspace) eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id)) eq_('this_is_a_page.html', updated.file_name) eq_('text/html', updated.file_mimetype) eq_(b'<html>hello world</html>', updated.file_content) eq_(ActionDescription.REVISION, updated.revision_type)
def test_update(self): uapi = UserApi(None) groups = [GroupApi(None).get_one(Group.TIM_USER), GroupApi(None).get_one(Group.TIM_MANAGER), GroupApi(None).get_one(Group.TIM_ADMIN)] user1 = uapi.create_user(email='this.is@user', groups=groups, save_now=True) workspace = WorkspaceApi(user1).create_workspace('test workspace', save_now=True) wid = workspace.workspace_id user2 = uapi.create_user() user2.email = '*****@*****.**' uapi.save(user2) RoleApi(user1).create_one(user2, workspace, UserRoleInWorkspace.CONTENT_MANAGER, with_notif=False, flush=True) # Test starts here api = ContentApi(user1) p = api.create(ContentType.Page, workspace, None, 'this_is_a_page', True) u1id = user1.user_id u2id = user2.user_id pcid = p.content_id poid = p.owner_id transaction.commit() # Refresh instances after commit user1 = uapi.get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) api = ContentApi(user1) content = api.get_one(pcid, ContentType.Any, workspace) eq_(u1id, content.owner_id) eq_(poid, content.owner_id) u2 = UserApi(None).get_one(u2id) api2 = ContentApi(u2) content2 = api2.get_one(pcid, ContentType.Any, workspace) with new_revision(content2): api2.update_content(content2, 'this is an updated page', 'new content') api2.save(content2) transaction.commit() # Refresh instances after commit user1 = uapi.get_one(u1id) workspace = WorkspaceApi(user1).get_one(wid) api = ContentApi(user1) updated = api.get_one(pcid, ContentType.Any, workspace) eq_(u2id, updated.owner_id, 'the owner id should be {} (found {})'.format(u2id, updated.owner_id)) eq_('this is an updated page', updated.label) eq_('new content', updated.description) eq_(ActionDescription.EDITION, updated.revision_type)