diff --git a/controller/Notebooks.py b/controller/Notebooks.py index f6fdf80..a1d00b6 100644 --- a/controller/Notebooks.py +++ b/controller/Notebooks.py @@ -123,6 +123,7 @@ class Notebooks( object ): yield dict( notebook = notebook, + startup_notes = notebook.startup_notes, note = note, ) diff --git a/controller/Users.py b/controller/Users.py index c2e459e..9224ce4 100644 --- a/controller/Users.py +++ b/controller/Users.py @@ -5,7 +5,7 @@ from model.Notebook import Notebook from model.Note import Note from Scheduler import Scheduler from Expose import expose -from Validate import validate, Valid_string, Validation_error +from Validate import validate, Valid_string, Valid_bool, Validation_error from Database import Valid_id from Updater import update_client, wait_for_update from Expire import strongly_expire @@ -259,13 +259,16 @@ class Users( object ): @async @update_client @validate( + include_startup_notes = Valid_bool(), user_id = Valid_id( none_okay = True ), ) - def current( self, user_id ): + def current( self, include_startup_notes, user_id ): """ Return information on the currently logged-in user. If not logged in, default to the anonymous user. + @type include_startup_notes: bool + @param include_startup_notes: True to return startup notes for the first notebook @type user_id: unicode @param user_id: id of current logged-in user (if any), determined by @grab_user_id @rtype: json dict @@ -295,6 +298,7 @@ class Users( object ): yield dict( user = user, notebooks = notebooks, + startup_notes = include_startup_notes and len( notebooks ) > 0 and notebooks[ 0 ].startup_notes or [], http_url = self.__http_url, ) diff --git a/controller/test/Test_notebooks.py b/controller/test/Test_notebooks.py index b11dbc1..f13a729 100644 --- a/controller/test/Test_notebooks.py +++ b/controller/test/Test_notebooks.py @@ -93,10 +93,11 @@ class Test_notebooks( Test_controller ): ) notebook = result[ "notebook" ] + startup_notes = result[ "startup_notes" ] assert notebook.object_id == self.notebook.object_id - assert len( notebook.startup_notes ) == 1 - assert notebook.startup_notes[ 0 ] == self.note + assert len( startup_notes ) == 1 + assert startup_notes[ 0 ] == self.note def test_contents_with_note( self ): self.login() @@ -107,10 +108,11 @@ class Test_notebooks( Test_controller ): ) notebook = result[ "notebook" ] + startup_notes = result[ "startup_notes" ] assert notebook.object_id == self.notebook.object_id - assert len( notebook.startup_notes ) == 1 - assert notebook.startup_notes[ 0 ] == self.note + assert len( startup_notes ) == 1 + assert startup_notes[ 0 ] == self.note note = result[ "note" ] @@ -129,10 +131,11 @@ class Test_notebooks( Test_controller ): ) notebook = result[ "notebook" ] + startup_notes = result[ "startup_notes" ] assert notebook.object_id == self.notebook.object_id - assert len( notebook.startup_notes ) == 1 - assert notebook.startup_notes[ 0 ] == self.note + assert len( startup_notes ) == 1 + assert startup_notes[ 0 ] == self.note note = result[ "note" ] diff --git a/controller/test/Test_users.py b/controller/test/Test_users.py index c2696e8..cd80550 100644 --- a/controller/test/Test_users.py +++ b/controller/test/Test_users.py @@ -3,6 +3,7 @@ from Test_controller import Test_controller from controller.Scheduler import Scheduler from model.User import User from model.Notebook import Notebook +from model.Note import Note class Test_users( Test_controller ): @@ -36,6 +37,10 @@ class Test_users( Test_controller ): self.database.next_id( self.scheduler.thread ) self.anon_notebook = Notebook( ( yield Scheduler.SLEEP ), u"anon notebook" ) + self.database.next_id( self.scheduler.thread ) + self.startup_note = Note( ( yield Scheduler.SLEEP ), u"contents go here" ) + self.anon_notebook.add_note( self.startup_note ) + self.anon_notebook.add_startup_note( self.startup_note ) self.database.next_id( self.scheduler.thread ) self.user = User( ( yield Scheduler.SLEEP ), self.username, self.password, self.email_address, self.notebooks ) @@ -57,7 +62,7 @@ class Test_users( Test_controller ): assert result[ u"redirect" ].startswith( u"/notebooks/" ) assert result[ u"authenticated" ] - def test_current_after_signup( self ): + def test_current_after_signup( self, include_startup_notes = False ): result = self.http_post( "/users/signup", dict( username = self.new_username, password = self.new_password, @@ -69,7 +74,10 @@ class Test_users( Test_controller ): new_notebook_id = result[ u"redirect" ].split( u"/notebooks/" )[ -1 ] - result = self.http_get( "/users/current", session_id = session_id ) + result = self.http_get( + "/users/current?include_startup_notes=%s" % include_startup_notes, + session_id = session_id, + ) assert result[ u"user" ].username == self.new_username notebooks = result[ u"notebooks" ] @@ -83,6 +91,16 @@ class Test_users( Test_controller ): assert len( notebook.notes ) == 1 assert len( notebook.startup_notes ) == 1 + startup_notes = result[ "startup_notes" ] + if include_startup_notes: + assert len( startup_notes ) == 1 + assert startup_notes[ 0 ] == self.startup_note + else: + assert startup_notes == [] + + def test_current_with_startup_notes_after_signup( self ): + self.test_current_after_signup( include_startup_notes = True ) + def test_signup_with_different_passwords( self ): result = self.http_post( "/users/signup", dict( username = self.new_username, @@ -130,7 +148,7 @@ class Test_users( Test_controller ): assert result[ u"redirect" ] == self.settings[ u"global" ].get( u"luminotes.http_url" ) + u"/" assert result[ u"deauthenticated" ] - def test_current_after_login( self ): + def test_current_after_login( self, include_startup_notes = False ): result = self.http_post( "/users/login", dict( username = self.username, password = self.password, @@ -138,15 +156,40 @@ class Test_users( Test_controller ): ) ) session_id = result[ u"session_id" ] - result = self.http_get( "/users/current", session_id = session_id ) + result = self.http_get( + "/users/current?include_startup_notes=%s" % include_startup_notes, + session_id = session_id, + ) assert result[ u"user" ] == self.user assert result[ u"notebooks" ] == [ self.anon_notebook ] + self.notebooks assert result[ u"http_url" ] == self.settings[ u"global" ].get( u"luminotes.http_url" ) - def test_current_without_login( self ): - result = self.http_get( "/users/current" ) + startup_notes = result[ "startup_notes" ] + if include_startup_notes: + assert len( startup_notes ) == 1 + assert startup_notes[ 0 ] == self.startup_note + else: + assert startup_notes == [] + + def test_current_with_startup_notes_after_login( self ): + self.test_current_after_login( include_startup_notes = True ) + + def test_current_without_login( self, include_startup_notes = False ): + result = self.http_get( + "/users/current?include_startup_notes=%s" % include_startup_notes, + ) assert result[ u"user" ].username == "anonymous" assert result[ u"notebooks" ] == [ self.anon_notebook ] assert result[ u"http_url" ] == self.settings[ u"global" ].get( u"luminotes.http_url" ) + + startup_notes = result[ "startup_notes" ] + if include_startup_notes: + assert len( startup_notes ) == 1 + assert startup_notes[ 0 ] == self.startup_note + else: + assert startup_notes == [] + + def test_current_with_startup_notes_without_login( self ): + self.test_current_without_login( include_startup_notes = True ) diff --git a/model/Notebook.py b/model/Notebook.py index f4f434e..10b5534 100644 --- a/model/Notebook.py +++ b/model/Notebook.py @@ -162,10 +162,14 @@ class Notebook( Persistent ): def to_dict( self ): d = Persistent.to_dict( self ) + + # as an optimization, don't include the revisions list because it's not + # currently used anywhere for Notebook objects + del d[ "revisions_list" ] + d.update( dict( name = self.__name, trash = self.__trash, - startup_notes = copy( self.startup_notes ), read_write = True, ) ) diff --git a/model/test/Test_note.py b/model/test/Test_note.py index 4f2da7e..9635c0a 100644 --- a/model/test/Test_note.py +++ b/model/test/Test_note.py @@ -59,3 +59,6 @@ class Test_note( object ): assert d.get( "contents" ) == self.contents assert d.get( "title" ) == self.title assert d.get( "deleted_from" ) == None + assert d.get( "object_id" ) == self.note.object_id + assert d.get( "revision" ) + assert d.get( "revisions_list" ) diff --git a/model/test/Test_notebook.py b/model/test/Test_notebook.py index 0b7839f..899990a 100644 --- a/model/test/Test_notebook.py +++ b/model/test/Test_notebook.py @@ -158,8 +158,10 @@ class Test_notebook( object ): assert d.get( "name" ) == self.name assert d.get( "trash" ) == self.trash - assert d.get( "startup_notes" ) == [] assert d.get( "read_write" ) == True + assert d.get( "object_id" ) == self.notebook.object_id + assert d.get( "revision" ) + assert d.get( "revisions_list" ) == None def test_to_dict_with_startup_notes( self ): self.notebook.add_note( self.note ) @@ -169,5 +171,7 @@ class Test_notebook( object ): assert d.get( "name" ) == self.name assert d.get( "trash" ) == self.trash - assert d.get( "startup_notes" ) == [ self.note ] assert d.get( "read_write" ) == True + assert d.get( "object_id" ) == self.notebook.object_id + assert d.get( "revision" ) + assert d.get( "revisions_list" ) == None diff --git a/static/js/Wiki.js b/static/js/Wiki.js index 0309407..50c428f 100644 --- a/static/js/Wiki.js +++ b/static/js/Wiki.js @@ -25,10 +25,15 @@ function Wiki() { }, function( result ) { self.populate( result ); } ); + var include_startup_notes = false; + } else { + var include_startup_notes = true; } // get info on the current user (logged-in or anonymous) - this.invoker.invoke( "/users/current", "GET", null, + this.invoker.invoke( "/users/current", "GET", { + "include_startup_notes": include_startup_notes + }, function( result ) { self.display_user( result ); } ); } @@ -41,7 +46,7 @@ Wiki.prototype.display_user = function ( result ) { // if no notebook id was requested, then just display the user's default notebook if ( !this.notebook_id ) { this.notebook_id = result.notebooks[ 0 ].object_id; - this.populate( { "notebook" : result.notebooks[ 0 ] } ); + this.populate( { "notebook" : result.notebooks[ 0 ], "startup_notes": result.startup_notes } ); } if ( result.user.username == "anonymous" ) @@ -130,8 +135,8 @@ Wiki.prototype.populate = function ( result ) { // create an editor for each startup note in the received notebook, focusing the first one var focus = true; - for ( var i in this.notebook.startup_notes ) { - var note = this.notebook.startup_notes[ i ]; + for ( var i in result.startup_notes ) { + var note = result.startup_notes[ i ]; if ( !note ) continue; this.startup_notes[ note.object_id ] = true; @@ -148,7 +153,7 @@ Wiki.prototype.populate = function ( result ) { if ( result.note ) this.create_editor( result.note.object_id, result.note.contents, result.note.deleted_from, result.note.revisions_list, undefined, read_write, false, true ); - if ( !this.notebook.trash && this.notebook.startup_notes.length == 0 && !result.note ) + if ( !this.notebook.trash && result.startup_notes.length == 0 && !result.note ) this.display_message( "There are no notes here." ) }