mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	fix conditional in separate_pages
add additional test for separate_pages Signed-off-by: Florian Brandes <florian.brandes@posteo.de>
This commit is contained in:
		| @@ -137,23 +137,26 @@ def separate_pages(filepath: str, pages_to_split_on: List[int]) -> List[str]: | ||||
|             dst.save(out) | ||||
|         document_paths = [savepath] | ||||
|  | ||||
|     for count, page_number in enumerate(pages_to_split_on): | ||||
|         logger.debug(f"Count: {str(count)} page_number: {str(page_number)}") | ||||
|         dst = Pdf.new() | ||||
|         try: | ||||
|             next_page = pages_to_split_on[count + 1] | ||||
|         except IndexError: | ||||
|             next_page = len(pdf.pages) | ||||
|         # skip the first page_number. This contains the barcode page | ||||
|         for page in range(page_number + 1, next_page): | ||||
|             logger.debug(f"page_number: {str(page_number)} next_page: {str(next_page)}") | ||||
|             dst.pages.append(pdf.pages[page]) | ||||
|         output_filename = "{}_document_{}.pdf".format(fname, str(count + 1)) | ||||
|         logger.debug(f"pdf no:{str(count)} has {str(len(dst.pages))} pages") | ||||
|         savepath = os.path.join(tempdir, output_filename) | ||||
|         with open(savepath, "wb") as out: | ||||
|             dst.save(out) | ||||
|         document_paths.append(savepath) | ||||
|         # iterate through the rest of the document | ||||
|         for count, page_number in enumerate(pages_to_split_on): | ||||
|             logger.debug(f"Count: {str(count)} page_number: {str(page_number)}") | ||||
|             dst = Pdf.new() | ||||
|             try: | ||||
|                 next_page = pages_to_split_on[count + 1] | ||||
|             except IndexError: | ||||
|                 next_page = len(pdf.pages) | ||||
|             # skip the first page_number. This contains the barcode page | ||||
|             for page in range(page_number + 1, next_page): | ||||
|                 logger.debug( | ||||
|                     f"page_number: {str(page_number)} next_page: {str(next_page)}", | ||||
|                 ) | ||||
|                 dst.pages.append(pdf.pages[page]) | ||||
|             output_filename = "{}_document_{}.pdf".format(fname, str(count + 1)) | ||||
|             logger.debug(f"pdf no:{str(count)} has {str(len(dst.pages))} pages") | ||||
|             savepath = os.path.join(tempdir, output_filename) | ||||
|             with open(savepath, "wb") as out: | ||||
|                 dst.save(out) | ||||
|             document_paths.append(savepath) | ||||
|     logger.debug(f"Temp files are {str(document_paths)}") | ||||
|     return document_paths | ||||
|  | ||||
|   | ||||
| @@ -312,6 +312,23 @@ class TestTasks(DirectoriesMixin, TestCase): | ||||
|         pages = tasks.separate_pages(test_file, [1]) | ||||
|         self.assertEqual(len(pages), 2) | ||||
|  | ||||
|     def test_separate_pages_no_list(self): | ||||
|         test_file = os.path.join( | ||||
|             os.path.dirname(__file__), | ||||
|             "samples", | ||||
|             "barcodes", | ||||
|             "patch-code-t-middle.pdf", | ||||
|         ) | ||||
|         with self.assertLogs("paperless.tasks", level="WARNING") as cm: | ||||
|             pages = tasks.separate_pages(test_file, []) | ||||
|             self.assertEqual(pages, []) | ||||
|             self.assertEqual( | ||||
|                 cm.output, | ||||
|                 [ | ||||
|                     f"WARNING:paperless.tasks:No pages to split on!", | ||||
|                 ], | ||||
|             ) | ||||
|  | ||||
|     def test_save_to_dir(self): | ||||
|         test_file = os.path.join( | ||||
|             os.path.dirname(__file__), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Florian Brandes
					Florian Brandes