mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Fix: made try_delete_empty_directories and delete_all_empty_subdirectories staticmethods
This commit is contained in:
		| @@ -428,6 +428,7 @@ class Document(models.Model): | |||||||
|             self.filename = filename |             self.filename = filename | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|     def try_delete_empty_directories(directory): |     def try_delete_empty_directories(directory): | ||||||
|         # Go up in the directory hierarchy and try to delete all directories |         # Go up in the directory hierarchy and try to delete all directories | ||||||
|         directory = os.path.normpath(directory) |         directory = os.path.normpath(directory) | ||||||
| @@ -446,6 +447,7 @@ def try_delete_empty_directories(directory): | |||||||
|             directory = os.path.normpath(directory) |             directory = os.path.normpath(directory) | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|     def delete_all_empty_subdirectories(directory): |     def delete_all_empty_subdirectories(directory): | ||||||
|         # Go through all folders and try to delete all directories |         # Go through all folders and try to delete all directories | ||||||
|         root = os.path.normpath(Document.filename_to_path(directory)) |         root = os.path.normpath(Document.filename_to_path(directory)) | ||||||
| @@ -456,6 +458,9 @@ def delete_all_empty_subdirectories(directory): | |||||||
|             if not os.path.isdir(Document.filename_to_path(fullname)): |             if not os.path.isdir(Document.filename_to_path(fullname)): | ||||||
|                 continue |                 continue | ||||||
|  |  | ||||||
|  |             # Go into subdirectory to see, if there is more to delete | ||||||
|  |             Document.delete_all_empty_subdirectories(os.path.join(directory, filename)) | ||||||
|  |  | ||||||
|             # Try to delete the directory |             # Try to delete the directory | ||||||
|             try: |             try: | ||||||
|                 os.rmdir(Document.filename_to_path(fullname)) |                 os.rmdir(Document.filename_to_path(fullname)) | ||||||
| @@ -464,9 +469,6 @@ def delete_all_empty_subdirectories(directory): | |||||||
|                 # Directory not empty, no need to go further up |                 # Directory not empty, no need to go further up | ||||||
|                 continue |                 continue | ||||||
|  |  | ||||||
|         # Go into subdirectory to see, if there is more to delete |  | ||||||
|         delete_all_empty_subdirectories(os.path.join(directory, filename)) |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @receiver(models.signals.m2m_changed, sender=Document.tags.through) | @receiver(models.signals.m2m_changed, sender=Document.tags.through) | ||||||
| @receiver(models.signals.post_save, sender=Document) | @receiver(models.signals.post_save, sender=Document) | ||||||
| @@ -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
	 Wolf-Bastian Poettner
					Wolf-Bastian Poettner