mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	add exception handler for invalid filename formats.
This commit is contained in:
		@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import logging
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
from collections import defaultdict
 | 
					from collections import defaultdict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -66,6 +67,9 @@ def many_to_dictionary(field):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def generate_filename(document):
 | 
					def generate_filename(document):
 | 
				
			||||||
    # Create filename based on configured format
 | 
					    # Create filename based on configured format
 | 
				
			||||||
 | 
					    path = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
        if settings.PAPERLESS_FILENAME_FORMAT is not None:
 | 
					        if settings.PAPERLESS_FILENAME_FORMAT is not None:
 | 
				
			||||||
            tags = defaultdict(lambda: slugify(None),
 | 
					            tags = defaultdict(lambda: slugify(None),
 | 
				
			||||||
                               many_to_dictionary(document.tags))
 | 
					                               many_to_dictionary(document.tags))
 | 
				
			||||||
@@ -82,8 +86,8 @@ def generate_filename(document):
 | 
				
			|||||||
                added_day=document.added.day if document.added else "none",
 | 
					                added_day=document.added.day if document.added else "none",
 | 
				
			||||||
                tags=tags,
 | 
					                tags=tags,
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
    else:
 | 
					    except (ValueError, KeyError, IndexError) as e:
 | 
				
			||||||
        path = ""
 | 
					        logging.getLogger(__name__).warning("Invalid PAPERLESS_FILENAME_FORMAT: {}, falling back to default,".format(settings.PAPERLESS_FILENAME_FORMAT))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Always append the primary key to guarantee uniqueness of filename
 | 
					    # Always append the primary key to guarantee uniqueness of filename
 | 
				
			||||||
    if len(path) > 0:
 | 
					    if len(path) > 0:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
from django.dispatch import Signal
 | 
					from django.dispatch import Signal
 | 
				
			||||||
 | 
					
 | 
				
			||||||
document_consumption_started = Signal(providing_args=["filename"])
 | 
					document_consumption_started = Signal()
 | 
				
			||||||
document_consumption_finished = Signal(providing_args=["document"])
 | 
					document_consumption_finished = Signal()
 | 
				
			||||||
document_consumer_declaration = Signal(providing_args=[])
 | 
					document_consumer_declaration = Signal()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -330,3 +330,21 @@ class TestDate(TestCase):
 | 
				
			|||||||
            os.path.join(tmp, "notempty", "file")), True)
 | 
					            os.path.join(tmp, "notempty", "file")), True)
 | 
				
			||||||
        self.assertEqual(os.path.isdir(
 | 
					        self.assertEqual(os.path.isdir(
 | 
				
			||||||
            os.path.join(tmp, "notempty", "empty")), False)
 | 
					            os.path.join(tmp, "notempty", "empty")), False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @override_settings(PAPERLESS_FILENAME_FORMAT="{created/[title]")
 | 
				
			||||||
 | 
					    def test_invalid_format(self):
 | 
				
			||||||
 | 
					        document = Document()
 | 
				
			||||||
 | 
					        document.pk = 1
 | 
				
			||||||
 | 
					        document.file_type = "pdf"
 | 
				
			||||||
 | 
					        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(generate_filename(document), "0000001.pdf")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @override_settings(PAPERLESS_FILENAME_FORMAT="{created__year}")
 | 
				
			||||||
 | 
					    def test_invalid_format_key(self):
 | 
				
			||||||
 | 
					        document = Document()
 | 
				
			||||||
 | 
					        document.pk = 1
 | 
				
			||||||
 | 
					        document.file_type = "pdf"
 | 
				
			||||||
 | 
					        document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(generate_filename(document), "0000001.pdf")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user