From 3153492086011bddd40d7ec432a4aae7fdbd3fa8 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Fri, 11 Jul 2008 14:41:48 -0700 Subject: [PATCH] Basic note reverting now works. --- controller/Notebooks.py | 93 +++++++++++++++++++++++++++++++++++++++++ static/js/Wiki.js | 29 ++++++++++++- 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/controller/Notebooks.py b/controller/Notebooks.py index 9ade5a8..b2b7529 100644 --- a/controller/Notebooks.py +++ b/controller/Notebooks.py @@ -738,6 +738,99 @@ class Notebooks( object ): storage_bytes = user and user.storage_bytes or 0, ) + @expose( view = Json ) + @end_transaction + @grab_user_id + @validate( + notebook_id = Valid_id(), + note_id = Valid_id(), + revision = Valid_revision(), + user_id = Valid_id( none_okay = True ), + ) + def revert_note( self, notebook_id, note_id, revision, user_id ): + """ + Revert the contents of a note to that of an earlier revision, thereby creating a new revision. + The timestamp of the new revision is returned. + + @type notebook_id: unicode + @param notebook_id: id of notebook the note is in + @type note_id: unicode + @param note_id: id of note to revert + @type revision: unicode or NoneType + @param revision: revision timestamp to revert to for the provided note + @type user_id: unicode or NoneType + @param user_id: id of current logged-in user (if any), determined by @grab_user_id + @rtype: json dict + @return: { + 'new_revision': User_revision of the reverted note + 'previous_revision': User_revision immediately before new_revision + 'storage_bytes': current storage usage by user, + } + @raise Access_error: the current user doesn't have access to the given notebook + @raise Validation_error: one of the arguments is invalid + """ + if not self.__users.check_access( user_id, notebook_id, read_write = True ): + raise Access_error() + + user = self.__database.load( User, user_id ) + notebook = self.__database.load( Notebook, notebook_id ) + + if not user or not notebook: + raise Access_error() + + note = self.__database.load( Note, note_id ) + + # check whether the provided note contents have been changed since the previous revision + def update_note( current_notebook, old_note, user ): + # if the revision to revert to is already the newest revision, bail without updating the note + if old_note.revision == note.revision: + new_revision = None + # otherwise, revert the note's contents to that of the older revision + else: + note.contents = old_note.contents + note.user_id = user.object_id + new_revision = User_revision( note.revision, note.user_id, user.username ) + + self.__files.purge_unused( note ) + + return new_revision + + previous_user = self.__database.load( User, note.user_id ) + previous_revision = User_revision( note.revision, note.user_id, previous_user and previous_user.username or None ) + + # if the note is already in the given notebook, load it and revert it + if note and note.notebook_id == notebook.object_id: + old_note = self.__database.load( Note, note_id, revision ) + new_revision = update_note( notebook, old_note, user ) + + # the note is not already in the given notebook, so look for it in the trash + elif note and notebook.trash_id and note.notebook_id == notebook.trash_id: + old_note = self.__database.load( Note, note_id, revision ) + + # undelete the note, putting it back in the given notebook + note.notebook_id = notebook.object_id + note.deleted_from_id = None + + new_revision = update_note( notebook, old_note, user ) + # otherwise, the note doesn't exist + else: + raise Access_error() + + if new_revision: + self.__database.save( note, commit = False ) + user = self.__users.update_storage( user_id, commit = False ) + self.__database.commit() + user.group_storage_bytes = self.__users.calculate_group_storage( user ) + else: + user = None + + return dict( + new_revision = new_revision, + previous_revision = previous_revision, + storage_bytes = user and user.storage_bytes or 0, + contents = note.contents, + ) + @expose( view = Json ) @end_transaction @grab_user_id diff --git a/static/js/Wiki.js b/static/js/Wiki.js index b36a806..624bf2d 100644 --- a/static/js/Wiki.js +++ b/static/js/Wiki.js @@ -718,7 +718,14 @@ Wiki.prototype.create_editor = function ( id, note_text, deleted_from_id, revisi // for read-only notes within read-write notebooks, tack the revision timestamp onto the start of the note text if ( !read_write && this.notebook.read_write && revision ) { var short_revision = this.brief_revision( revision ); - note_text = "

Previous revision from " + short_revision + "

" + note_text; + var note_id = id.split( ' ' )[ 0 ]; + note_text = '

Previous revision from ' + short_revision + '

' + + '
' + + '' + + '' + + '' + + '' + + '
' + note_text; } if ( !read_write && creation ) { @@ -1546,6 +1553,26 @@ Wiki.prototype.submit_form = function ( form ) { self.display_group_settings( result ); } ); } + } else if ( url == "/notebooks/revert_note" ) { + callback = function ( result ) { + self.display_message( "The note has been reverted to an earlier revision." ); + var frame = getElement( "note_" + form.note_id.value ); + + if ( frame ) { + var editor = frame.editor; + self.update_editor_revisions( result, editor ); + editor.mark_clean(); + + if ( result.new_revision ) { + editor.document.body.innerHTML = result.contents; + editor.resize(); + } + + signal( self, "note_saved", editor ); + } + + self.display_storage_usage( result.storage_bytes ); + } } this.invoker.invoke( url, "POST", args, callback, form );