mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Fix: made try_delete_empty_directories and delete_all_empty_subdirectories staticmethods
This commit is contained in:
		@@ -428,44 +428,46 @@ class Document(models.Model):
 | 
				
			|||||||
            self.filename = filename
 | 
					            self.filename = filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def try_delete_empty_directories(directory):
 | 
					    @staticmethod
 | 
				
			||||||
    # Go up in the directory hierarchy and try to delete all directories
 | 
					    def try_delete_empty_directories(directory):
 | 
				
			||||||
    directory = os.path.normpath(directory)
 | 
					        # Go up in the directory hierarchy and try to delete all directories
 | 
				
			||||||
    root = os.path.normpath(Document.filename_to_path(""))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    while directory != root:
 | 
					 | 
				
			||||||
        # Try to delete the current directory
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            os.rmdir(directory)
 | 
					 | 
				
			||||||
        except os.error:
 | 
					 | 
				
			||||||
            # Directory not empty, no need to go further up
 | 
					 | 
				
			||||||
            return
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Cut off actual directory and go one level up
 | 
					 | 
				
			||||||
        directory, _ = os.path.split(directory)
 | 
					 | 
				
			||||||
        directory = os.path.normpath(directory)
 | 
					        directory = os.path.normpath(directory)
 | 
				
			||||||
 | 
					        root = os.path.normpath(Document.filename_to_path(""))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        while directory != root:
 | 
				
			||||||
 | 
					            # Try to delete the current directory
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                os.rmdir(directory)
 | 
				
			||||||
 | 
					            except os.error:
 | 
				
			||||||
 | 
					                # Directory not empty, no need to go further up
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Cut off actual directory and go one level up
 | 
				
			||||||
 | 
					            directory, _ = os.path.split(directory)
 | 
				
			||||||
 | 
					            directory = os.path.normpath(directory)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def delete_all_empty_subdirectories(directory):
 | 
					    @staticmethod
 | 
				
			||||||
    # Go through all folders and try to delete all directories
 | 
					    def delete_all_empty_subdirectories(directory):
 | 
				
			||||||
    root = os.path.normpath(Document.filename_to_path(directory))
 | 
					        # Go through all folders and try to delete all directories
 | 
				
			||||||
 | 
					        root = os.path.normpath(Document.filename_to_path(directory))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for filename in os.listdir(root):
 | 
					        for filename in os.listdir(root):
 | 
				
			||||||
        fullname = os.path.join(directory, filename)
 | 
					            fullname = os.path.join(directory, filename)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not os.path.isdir(Document.filename_to_path(fullname)):
 | 
					            if not os.path.isdir(Document.filename_to_path(fullname)):
 | 
				
			||||||
            continue
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Try to delete the directory
 | 
					            # Go into subdirectory to see, if there is more to delete
 | 
				
			||||||
        try:
 | 
					            Document.delete_all_empty_subdirectories(os.path.join(directory, filename))
 | 
				
			||||||
            os.rmdir(Document.filename_to_path(fullname))
 | 
					 | 
				
			||||||
            continue
 | 
					 | 
				
			||||||
        except os.error:
 | 
					 | 
				
			||||||
            # Directory not empty, no need to go further up
 | 
					 | 
				
			||||||
            continue
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Go into subdirectory to see, if there is more to delete
 | 
					            # Try to delete the directory
 | 
				
			||||||
        delete_all_empty_subdirectories(os.path.join(directory, filename))
 | 
					            try:
 | 
				
			||||||
 | 
					                os.rmdir(Document.filename_to_path(fullname))
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            except os.error:
 | 
				
			||||||
 | 
					                # Directory not empty, no need to go further up
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@receiver(models.signals.m2m_changed, sender=Document.tags.through)
 | 
					@receiver(models.signals.m2m_changed, sender=Document.tags.through)
 | 
				
			||||||
@@ -502,7 +504,7 @@ def update_filename(sender, instance, **kwargs):
 | 
				
			|||||||
    # Delete empty directory
 | 
					    # Delete empty directory
 | 
				
			||||||
    old_dir = os.path.dirname(instance.filename)
 | 
					    old_dir = os.path.dirname(instance.filename)
 | 
				
			||||||
    old_path = instance.filename_to_path(old_dir)
 | 
					    old_path = instance.filename_to_path(old_dir)
 | 
				
			||||||
    try_delete_empty_directories(old_path)
 | 
					    Document.try_delete_empty_directories(old_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    instance.filename = new_filename
 | 
					    instance.filename = new_filename
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -530,7 +532,7 @@ def delete_files(sender, instance, **kwargs):
 | 
				
			|||||||
    # And remove the directory (if applicable)
 | 
					    # And remove the directory (if applicable)
 | 
				
			||||||
    old_dir = os.path.dirname(instance.filename)
 | 
					    old_dir = os.path.dirname(instance.filename)
 | 
				
			||||||
    old_path = instance.filename_to_path(old_dir)
 | 
					    old_path = instance.filename_to_path(old_dir)
 | 
				
			||||||
    try_delete_empty_directories(old_path)
 | 
					    Document.try_delete_empty_directories(old_path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Log(models.Model):
 | 
					class Log(models.Model):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user