def put(self, item_id, file_data=None, comment=None, label=''): # TODO - SECURE THIS workspace = tmpl_context.workspace try: item_saved = False api = ContentApi(tmpl_context.current_user) item = api.get_one(int(item_id), self._item_type, workspace) # TODO - D.A. - 2015-03-19 # refactor this method in order to make code easier to understand if comment and label: updated_item = api.update_content( item, label if label else item.label, comment if comment else '' ) 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()) 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: 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 post(self, label='', file_data=None): # TODO - SECURE THIS workspace = tmpl_context.workspace api = ContentApi(tmpl_context.current_user) file = api.create(ContentType.File, workspace, tmpl_context.folder, label) api.update_file_data(file, file_data.filename, file_data.type, file_data.file.read()) api.save(file, ActionDescription.CREATION) tg.flash(_('File created'), CST.STATUS_OK) tg.redirect(tg.url('/workspaces/{}/folders/{}/files/{}').format(tmpl_context.workspace_id, tmpl_context.folder_id, file.content_id))
def post(self, label='', file_data=None): # TODO - SECURE THIS workspace = tmpl_context.workspace api = ContentApi(tmpl_context.current_user) file = api.create(ContentType.File, workspace, tmpl_context.folder, label) api.update_file_data(file, file_data.filename, file_data.type, file_data.file.read()) api.save(file, ActionDescription.CREATION) tg.flash(_('File created'), CST.STATUS_OK) tg.redirect( tg.url('/workspaces/{}/folders/{}/files/{}').format( tmpl_context.workspace_id, tmpl_context.folder_id, file.content_id))
def post(self, label='', file_data=None): # TODO - SECURE THIS workspace = tmpl_context.workspace folder = tmpl_context.folder api = ContentApi(tmpl_context.current_user) with DBSession.no_autoflush: file = api.create(ContentType.File, workspace, folder, label) api.update_file_data(file, 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(file): return render_invalid_integrity_chosen_path( file.get_label_as_file(), ) api.save(file, ActionDescription.CREATION) tg.flash(_('File created'), CST.STATUS_OK) redirect = '/workspaces/{}/folders/{}/files/{}' tg.redirect( tg.url(redirect).format(tmpl_context.workspace_id, tmpl_context.folder_id, file.content_id))
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_('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_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)