witten
/
luminotes
Archived
1
0
Fork 0

When saving a model.Notebook or model.Note to the database, set the user_id field based on the current user.

This commit is contained in:
Dan Helfman 2008-01-01 01:44:54 +00:00
parent 6135b3216d
commit a354eadcbb
8 changed files with 135 additions and 18 deletions

3
NEWS
View File

@ -1,3 +1,6 @@
1.1.0: January ??, 2007
*
1.0.4: December 30, 2007
* Ability to invite people to view your notebook.
* When the web browser is resized, all notes are automatically resized as well.

View File

@ -456,7 +456,7 @@ class Notebooks( object ):
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, startup ):
def update_note( current_notebook, old_note, startup, user_id ):
# the note hasn't been changed, so bail without updating it
if contents.replace( u"\n", u"" ) == old_note.contents.replace( u"\n", "" ) and startup == old_note.startup:
new_revision = None
@ -469,6 +469,7 @@ class Notebooks( object ):
note.rank = self.__database.select_one( float, notebook.sql_highest_rank() ) + 1
else:
note.rank = None
note.user_id = user_id
new_revision = note.revision
@ -479,7 +480,7 @@ class Notebooks( object ):
old_note = self.__database.load( Note, note_id, previous_revision )
previous_revision = note.revision
new_revision = update_note( notebook, old_note, startup )
new_revision = update_note( notebook, old_note, startup, user_id )
# 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:
@ -490,7 +491,7 @@ class Notebooks( object ):
note.notebook_id = notebook.object_id
note.deleted_from_id = None
new_revision = update_note( notebook, old_note, startup )
new_revision = update_note( notebook, old_note, startup, user_id )
# otherwise, create a new note
else:
if startup:
@ -499,7 +500,7 @@ class Notebooks( object ):
rank = None
previous_revision = None
note = Note.create( note_id, contents, notebook_id = notebook.object_id, startup = startup, rank = rank )
note = Note.create( note_id, contents, notebook_id = notebook.object_id, startup = startup, rank = rank, user_id = user_id )
new_revision = note.revision
if new_revision:
@ -556,6 +557,7 @@ class Notebooks( object ):
note.startup = True
else:
note.notebook_id = None
note.user_id = user_id
self.__database.save( note, commit = False )
user = self.__users.update_storage( user_id, commit = False )
@ -610,6 +612,7 @@ class Notebooks( object ):
note.notebook_id = note.deleted_from_id
note.deleted_from_id = None
note.startup = True
note.user_id = user_id
self.__database.save( note, commit = False )
user = self.__users.update_storage( user_id, commit = False )
@ -657,6 +660,8 @@ class Notebooks( object ):
note.startup = True
else:
note.notebook_id = None
note.user_id = user_id
self.__database.save( note, commit = False )
user = self.__users.update_storage( user_id, commit = False )
@ -813,11 +818,11 @@ class Notebooks( object ):
def __create_notebook( self, name, user, commit = True ):
# create the notebook along with a trash
trash_id = self.__database.next_id( Notebook, commit = False )
trash = Notebook.create( trash_id, u"trash" )
trash = Notebook.create( trash_id, u"trash", user_id = user.object_id )
self.__database.save( trash, commit = False )
notebook_id = self.__database.next_id( Notebook, commit = False )
notebook = Notebook.create( notebook_id, name, trash_id )
notebook = Notebook.create( notebook_id, name, trash_id, user_id = user.object_id )
self.__database.save( notebook, commit = False )
# record the fact that the user has access to their new notebook
@ -873,6 +878,8 @@ class Notebooks( object ):
raise Access_error()
notebook.name = name
notebook.user_id = user_id
self.__database.save( notebook, commit = False )
self.__database.commit()
@ -915,6 +922,8 @@ class Notebooks( object ):
raise Access_error()
notebook.deleted = True
notebook.user_id = user_id
self.__database.save( notebook, commit = False )
# redirect to a remaining undeleted notebook, or if there isn't one, create an empty notebook
@ -1001,6 +1010,8 @@ class Notebooks( object ):
raise Access_error()
notebook.deleted = False
notebook.user_id = user_id
self.__database.save( notebook, commit = False )
self.__database.commit()

View File

@ -26,37 +26,42 @@ class Test_notebooks( Test_controller ):
self.anonymous = None
self.session_id = None
self.make_notebooks()
self.make_users()
self.make_notebooks()
self.make_invites()
self.database.commit()
def make_notebooks( self ):
self.trash = Notebook.create( self.database.next_id( Notebook ), u"trash" )
user_id = self.user.object_id
self.trash = Notebook.create( self.database.next_id( Notebook ), u"trash", user_id = user_id )
self.database.save( self.trash, commit = False )
self.notebook = Notebook.create( self.database.next_id( Notebook ), u"notebook", self.trash.object_id )
self.notebook = Notebook.create( self.database.next_id( Notebook ), u"notebook", self.trash.object_id, user_id = user_id )
self.database.save( self.notebook, commit = False )
note_id = self.database.next_id( Note )
self.note = Note.create( note_id, u"<h3>my title</h3>blah", notebook_id = self.notebook.object_id, startup = True )
self.note = Note.create( note_id, u"<h3>my title</h3>blah", notebook_id = self.notebook.object_id, startup = True, user_id = user_id )
self.database.save( self.note, commit = False )
note_id = self.database.next_id( Note )
self.note2 = Note.create( note_id, u"<h3>other title</h3>whee", notebook_id = self.notebook.object_id )
self.note2 = Note.create( note_id, u"<h3>other title</h3>whee", notebook_id = self.notebook.object_id, user_id = user_id )
self.database.save( self.note2, commit = False )
self.anon_notebook = Notebook.create( self.database.next_id( Notebook ), u"anon_notebook" )
self.anon_notebook = Notebook.create( self.database.next_id( Notebook ), u"anon_notebook", user_id = user_id )
self.database.save( self.anon_notebook, commit = False )
self.database.execute( self.user.sql_save_notebook( self.notebook.object_id, read_write = True, owner = True ) )
self.database.execute( self.user.sql_save_notebook( self.notebook.trash_id, read_write = True, owner = True ) )
self.database.execute( self.user.sql_save_notebook( self.anon_notebook.object_id, read_write = False, owner = False ) )
def make_users( self ):
self.user = User.create( self.database.next_id( User ), self.username, self.password, self.email_address )
self.database.save( self.user, commit = False )
self.database.execute( self.user.sql_save_notebook( self.notebook.object_id, read_write = True, owner = True ) )
self.database.execute( self.user.sql_save_notebook( self.notebook.trash_id, read_write = True, owner = True ) )
self.anonymous = User.create( self.database.next_id( User ), u"anonymous" )
self.database.save( self.anonymous, commit = False )
self.database.execute( self.user.sql_save_notebook( self.anon_notebook.object_id, read_write = False, owner = False ) )
def make_invites( self ):
self.invite = Invite.create(
self.database.next_id( Invite ), self.user.object_id, self.notebook.object_id,
u"skinner@example.com", read_write = True, owner = False,
@ -725,6 +730,7 @@ class Test_notebooks( Test_controller ):
assert note.title == "new title"
assert note.contents == new_note_contents
assert note.startup == startup
assert note.user_id == self.user.object_id
if startup:
assert note.rank == 0
@ -810,6 +816,7 @@ class Test_notebooks( Test_controller ):
assert note.title == "new title"
assert note.contents == new_note_contents
assert note.deleted_from_id == None
assert note.user_id == self.user.object_id
# make sure the old title can no longer be loaded from the trash
result = self.http_post( "/notebooks/load_note_by_title/", dict(
@ -870,6 +877,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.title == "new title"
assert note.contents == new_note_contents
assert note.user_id == self.user.object_id
assert note.revision == previous_revision
def test_save_unchanged_deleted_note( self, startup = False ):
@ -920,6 +928,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.title == "new title"
assert note.contents == new_note_contents
assert note.user_id == self.user.object_id
assert note.revision == previous_revision
assert note.deleted_from_id == None
@ -973,6 +982,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.title == "new title"
assert note.contents == new_note_contents
assert note.user_id == self.user.object_id
assert note.revision > previous_revision
assert note.startup == ( not startup )
@ -1025,6 +1035,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.title == "new title"
assert note.contents == new_note_contents
assert note.user_id == self.user.object_id
assert note.revision == previous_revision
def test_save_note_from_an_older_revision( self ):
@ -1090,6 +1101,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.title == "new new title"
assert note.contents == new_note_contents
assert note.user_id == self.user.object_id
def test_save_note_with_unknown_notebook( self ):
self.login()
@ -1139,6 +1151,7 @@ class Test_notebooks( Test_controller ):
assert note.title == new_note.title
assert note.contents == new_note.contents
assert note.startup == startup
assert note.user_id == self.user.object_id
if startup:
assert note.rank == 0
@ -1185,6 +1198,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == new_note.object_id
assert note.title == new_note.title
assert note.contents == expected_contents
assert note.user_id == self.user.object_id
def test_save_new_note_with_bad_characters( self ):
self.login()
@ -1219,6 +1233,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == new_note.object_id
assert note.title == new_note.title
assert note.contents == contents + " bar"
assert note.user_id == self.user.object_id
def test_save_two_new_notes( self, startup = False ):
self.login()
@ -1266,6 +1281,7 @@ class Test_notebooks( Test_controller ):
assert note.title == new_note.title
assert note.contents == new_note.contents
assert note.startup == startup
assert note.user_id == self.user.object_id
if startup:
assert note.rank == 1 # one greater than the previous new note's rank
@ -1296,6 +1312,22 @@ class Test_notebooks( Test_controller ):
assert result[ "note" ] is None
assert result[ "note_id_in_trash" ] == self.note.object_id
# test that the deleted note can be loaded from the trash
result = self.http_post( "/notebooks/load_note/", dict(
notebook_id = self.notebook.trash_id,
note_id = self.note.object_id,
), session_id = self.session_id )
note = result[ "note" ]
assert note
assert note.object_id == self.note.object_id
assert note.title == self.note.title
assert note.contents == self.note.contents
assert note.startup == self.note.startup
assert note.deleted_from_id == self.notebook.object_id
assert note.user_id == self.user.object_id
def test_delete_note_from_trash( self ):
self.login()
@ -1406,6 +1438,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.deleted_from_id == None
assert note.notebook_id == self.notebook.object_id
assert note.user_id == self.user.object_id
# test that the revision of the note from when it was deleted is loadable
result = self.http_post( "/notebooks/load_note/", dict(
@ -1419,6 +1452,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.deleted_from_id == self.notebook.object_id
assert note.notebook_id == self.trash.object_id
assert note.user_id == self.user.object_id
def test_undelete_note_that_is_not_deleted( self ):
self.login()
@ -1431,7 +1465,7 @@ class Test_notebooks( Test_controller ):
assert result.get( "error" ) == None
# test that the "undeleted" is where it should be
# test that the "undeleted" note is where it should be
result = self.http_post( "/notebooks/load_note/", dict(
notebook_id = self.notebook.object_id,
note_id = self.note.object_id,
@ -1477,6 +1511,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.deleted_from_id == self.notebook.object_id
assert note.notebook_id == self.notebook.trash_id
assert note.user_id == self.user.object_id
def test_undelete_unknown_note( self ):
self.login()
@ -1497,6 +1532,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.deleted_from_id == None
assert note.notebook_id == self.notebook.object_id
assert note.user_id == self.user.object_id
def test_undelete_note_from_incorrect_notebook( self ):
self.login()
@ -1524,6 +1560,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.deleted_from_id == self.notebook.object_id
assert note.notebook_id == self.notebook.trash_id
assert note.user_id == self.user.object_id
def test_undelete_note_that_is_not_deleted_from_id_incorrect_notebook( self ):
self.login()
@ -1546,6 +1583,7 @@ class Test_notebooks( Test_controller ):
assert note.object_id == self.note.object_id
assert note.deleted_from_id == None
assert note.notebook_id == self.notebook.object_id
assert note.user_id == self.user.object_id
def test_delete_all_notes( self ):
self.login()
@ -1573,6 +1611,31 @@ class Test_notebooks( Test_controller ):
assert result[ "note" ] is None
# test that all notes can be loaded from the trash
result = self.http_post( "/notebooks/load_note/", dict(
notebook_id = self.notebook.trash_id,
note_id = self.note.object_id,
), session_id = self.session_id )
note = result.get( "note" )
assert note
assert note.object_id == self.note.object_id
assert note.deleted_from_id == self.notebook.object_id
assert note.notebook_id == self.notebook.trash_id
assert note.user_id == self.user.object_id
result = self.http_post( "/notebooks/load_note/", dict(
notebook_id = self.notebook.trash_id,
note_id = self.note2.object_id,
), session_id = self.session_id )
note2 = result.get( "note" )
assert note2
assert note2.object_id == self.note2.object_id
assert note2.deleted_from_id == self.notebook.object_id
assert note2.notebook_id == self.notebook.trash_id
assert note2.user_id == self.user.object_id
def test_delete_all_notes_from_trash( self ):
self.login()
@ -1909,6 +1972,7 @@ class Test_notebooks( Test_controller ):
notebook = result[ "notebook" ]
assert notebook.name == new_name
assert notebook.user_id == self.user.object_id
def test_rename_without_login( self ):
new_name = u"renamed notebook"
@ -1971,6 +2035,7 @@ class Test_notebooks( Test_controller ):
assert notebook.read_write == True
assert notebook.owner == True
assert notebook.trash_id
assert notebook.user_id == self.user.object_id
def test_delete_with_multiple_notebooks( self ):
# create a second notebook, which we should be redirected to after the first notebook is deleted
@ -2009,6 +2074,7 @@ class Test_notebooks( Test_controller ):
notebook = result[ "notebook" ]
assert notebook.deleted == True
assert notebook.user_id == self.user.object_id
def test_contents_after_delete_twice( self ):
self.login()
@ -2028,6 +2094,7 @@ class Test_notebooks( Test_controller ):
notebook = result[ "notebook" ]
assert notebook.deleted == True
assert notebook.user_id == self.user.object_id
def test_delete_without_login( self ):
result = self.http_post( "/notebooks/delete", dict(
@ -2136,6 +2203,7 @@ class Test_notebooks( Test_controller ):
assert notebook.read_write == True
assert notebook.owner == True
assert notebook.trash_id
assert notebook.user_id == self.user.object_id
def test_contents_after_undelete( self ):
self.login()
@ -2155,6 +2223,7 @@ class Test_notebooks( Test_controller ):
notebook = result[ "notebook" ]
assert notebook.deleted == False
assert notebook.user_id == self.user.object_id
def test_undelete_without_login( self ):
self.http_post( "/notebooks/delete", dict(
@ -2194,6 +2263,7 @@ class Test_notebooks( Test_controller ):
assert notebook.read_write == True
assert notebook.owner == True
assert notebook.trash_id
assert notebook.user_id == self.user.object_id
def test_recent_notes( self ):
result = cherrypy.root.notebooks.load_recent_notes(

View File

@ -115,6 +115,10 @@ class Note( Persistent ):
self.__rank = rank
self.update_revision()
def __set_user_id( self, user_id ):
self.__user_id = user_id
self.update_revision()
@staticmethod
def sql_load( object_id, revision = None ):
if revision:
@ -170,5 +174,5 @@ class Note( Persistent ):
startup = property( lambda self: self.__startup, __set_startup )
deleted_from_id = property( lambda self: self.__deleted_from_id, __set_deleted_from_id )
rank = property( lambda self: self.__rank, __set_rank )
user_id = property( lambda self: self.__user_id )
user_id = property( lambda self: self.__user_id, __set_user_id )
creation = property( lambda self: self.__creation )

View File

@ -228,9 +228,13 @@ class Notebook( Persistent ):
self.__deleted = deleted
self.update_revision()
def __set_user_id( self, user_id ):
self.__user_id = user_id
self.update_revision()
name = property( lambda self: self.__name, __set_name )
trash_id = property( lambda self: self.__trash_id )
read_write = property( lambda self: self.__read_write, __set_read_write )
owner = property( lambda self: self.__owner, __set_owner )
deleted = property( lambda self: self.__deleted, __set_deleted )
user_id = property( lambda self: self.__user_id )
user_id = property( lambda self: self.__user_id, __set_user_id )

11
model/delta/1.1.0.sql Normal file
View File

@ -0,0 +1,11 @@
update notebook set user_id = (
select user_notebook.user_id
from user_notebook
where notebook_id = notebook.id and read_write = 't' and owner = 't'
limit 1
);
update note set user_id = (
select notebook_current.user_id
from notebook_current
where note.notebook_id = notebook_current.id
);

View File

@ -132,6 +132,13 @@ class Test_note( object ):
assert self.note.revision > previous_revision
assert self.note.rank == 5
def test_set_user_id( self ):
previous_revision = self.note.revision
self.note.user_id = u"5"
assert self.note.revision > previous_revision
assert self.note.user_id == u"5"
def test_to_dict( self ):
d = self.note.to_dict()

View File

@ -56,6 +56,13 @@ class Test_notebook( object ):
assert self.notebook.deleted == True
assert self.notebook.revision > previous_revision
def test_set_user_id( self ):
previous_revision = self.notebook.revision
self.notebook.user_id = u"5"
assert self.notebook.user_id == u"5"
assert self.notebook.revision > previous_revision
def test_to_dict( self ):
d = self.notebook.to_dict()