diff --git a/controller/Files.py b/controller/Files.py index 7c54ca7..bd63757 100644 --- a/controller/Files.py +++ b/controller/Files.py @@ -560,8 +560,8 @@ class Files( object ): @type note: model.Note @param note: note to search for file links - @type ignore_file_links: bool - @param ignore_file_links: if True, delete all files that are/were linked from this note + @type purge_all_links: bool + @param purge_all_links: if True, delete all files that are/were linked from this note """ # load metadata for all files with the given note's note_id files = self.__database.select_many( File, File.sql_load_note_files( note.object_id ) ) diff --git a/controller/test/Test_files.py b/controller/test/Test_files.py index 0f4ec5d..de1865b 100644 --- a/controller/test/Test_files.py +++ b/controller/test/Test_files.py @@ -1,5 +1,6 @@ import time import types +import cherrypy from threading import Thread from StringIO import StringIO from Test_controller import Test_controller @@ -817,7 +818,150 @@ class Test_files( Test_controller ): assert u"access" in result[ u"error" ] def test_purge_unused( self ): - raise NotImplementedError() + self.login() + + self.http_upload( + "/files/upload?file_id=%s" % self.file_id, + dict( + notebook_id = self.notebook.object_id, + note_id = self.note.object_id, + ), + filename = self.filename, + file_data = self.file_data, + content_type = self.content_type, + session_id = self.session_id, + ) + + # the file is not linked to from the note's contents, so this should delete it + cherrypy.root.files.purge_unused( self.note ) + + db_file = self.database.load( File, self.file_id ) + assert db_file is None + assert not Upload_file.exists( self.file_id ) + + def test_purge_unused_empty_link( self ): + self.login() + + self.http_upload( + "/files/upload?file_id=%s" % self.file_id, + dict( + notebook_id = self.notebook.object_id, + note_id = self.note.object_id, + ), + filename = self.filename, + file_data = self.file_data, + content_type = self.content_type, + session_id = self.session_id, + ) + + self.note.contents = '' % self.file_id + self.database.save( self.note ) + + # the file is linked to from the note's contents but the link title is empty, so this should + # delete it + cherrypy.root.files.purge_unused( self.note ) + + db_file = self.database.load( File, self.file_id ) + assert db_file is None + assert not Upload_file.exists( self.file_id ) + + def test_purge_unused_keep_file( self ): + self.login() + + self.http_upload( + "/files/upload?file_id=%s" % self.file_id, + dict( + notebook_id = self.notebook.object_id, + note_id = self.note.object_id, + ), + filename = self.filename, + file_data = self.file_data, + content_type = self.content_type, + session_id = self.session_id, + ) + + self.note.contents = 'file link' % self.file_id + self.database.save( self.note ) + + # the file is linked to from the note's contents, so this should not delete it + cherrypy.root.files.purge_unused( self.note ) + + db_file = self.database.load( File, self.file_id ) + assert db_file + assert db_file.object_id == self.file_id + assert db_file.filename == self.filename + assert Upload_file.exists( self.file_id ) + + def test_purge_unused_all_links( self ): + self.login() + + self.http_upload( + "/files/upload?file_id=%s" % self.file_id, + dict( + notebook_id = self.notebook.object_id, + note_id = self.note.object_id, + ), + filename = self.filename, + file_data = self.file_data, + content_type = self.content_type, + session_id = self.session_id, + ) + + self.note.contents = 'file link' % self.file_id + self.database.save( self.note ) + + # the file is linked to from the note's contents, but because of the purge_all_links flag, it + # should be deleted anyway + cherrypy.root.files.purge_unused( self.note, purge_all_links = True ) + + db_file = self.database.load( File, self.file_id ) + assert db_file is None + assert not Upload_file.exists( self.file_id ) + + def test_purge_unused_multiple_files( self ): + self.login() + + self.http_upload( + "/files/upload?file_id=%s" % self.file_id, + dict( + notebook_id = self.notebook.object_id, + note_id = self.note.object_id, + ), + filename = self.filename, + file_data = self.file_data, + content_type = self.content_type, + session_id = self.session_id, + ) + + self.note.contents = 'file link' % self.file_id + self.database.save( self.note ) + + other_file_id = u"23" + self.http_upload( + "/files/upload?file_id=%s" % other_file_id, + dict( + notebook_id = self.notebook.object_id, + note_id = self.note.object_id, + ), + filename = u"otherfile.png", + file_data = u"whee", + content_type = self.content_type, + session_id = self.session_id, + ) + + # one file is linked from the note's contents but the other is not. the file that is not linked + # should be deleted + cherrypy.root.files.purge_unused( self.note ) + + db_file = self.database.load( File, self.file_id ) + assert db_file + assert db_file.object_id == self.file_id + assert db_file.filename == self.filename + assert Upload_file.exists( self.file_id ) + + other_db_file = self.database.load( File, other_file_id ) + assert other_db_file is None + assert not Upload_file.exists( other_file_id ) def login( self ): result = self.http_post( "/users/login", dict(