mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-26 01:16:16 +00:00
Format Python code with black
This commit is contained in:
@@ -24,20 +24,14 @@ def archive_path_old(self):
|
||||
else:
|
||||
fname = "{:07}.pdf".format(self.pk)
|
||||
|
||||
return os.path.join(
|
||||
settings.ARCHIVE_DIR,
|
||||
fname
|
||||
)
|
||||
return os.path.join(settings.ARCHIVE_DIR, fname)
|
||||
|
||||
|
||||
def archive_path_new(doc):
|
||||
if doc.archive_filename is not None:
|
||||
return os.path.join(
|
||||
settings.ARCHIVE_DIR,
|
||||
str(doc.archive_filename)
|
||||
)
|
||||
else:
|
||||
return None
|
||||
if doc.archive_filename is not None:
|
||||
return os.path.join(settings.ARCHIVE_DIR, str(doc.archive_filename))
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def source_path(doc):
|
||||
@@ -48,10 +42,7 @@ def source_path(doc):
|
||||
if doc.storage_type == STORAGE_TYPE_GPG:
|
||||
fname += ".gpg" # pragma: no cover
|
||||
|
||||
return os.path.join(
|
||||
settings.ORIGINALS_DIR,
|
||||
fname
|
||||
)
|
||||
return os.path.join(settings.ORIGINALS_DIR, fname)
|
||||
|
||||
|
||||
def thumbnail_path(doc):
|
||||
@@ -59,13 +50,18 @@ def thumbnail_path(doc):
|
||||
if doc.storage_type == STORAGE_TYPE_GPG:
|
||||
file_name += ".gpg"
|
||||
|
||||
return os.path.join(
|
||||
settings.THUMBNAIL_DIR,
|
||||
file_name
|
||||
)
|
||||
return os.path.join(settings.THUMBNAIL_DIR, file_name)
|
||||
|
||||
|
||||
def make_test_document(document_class, title: str, mime_type: str, original: str, original_filename: str, archive: str = None, archive_filename: str = None):
|
||||
def make_test_document(
|
||||
document_class,
|
||||
title: str,
|
||||
mime_type: str,
|
||||
original: str,
|
||||
original_filename: str,
|
||||
archive: str = None,
|
||||
archive_filename: str = None,
|
||||
):
|
||||
doc = document_class()
|
||||
doc.filename = original_filename
|
||||
doc.title = title
|
||||
@@ -96,8 +92,12 @@ def make_test_document(document_class, title: str, mime_type: str, original: str
|
||||
|
||||
simple_jpg = os.path.join(os.path.dirname(__file__), "samples", "simple.jpg")
|
||||
simple_pdf = os.path.join(os.path.dirname(__file__), "samples", "simple.pdf")
|
||||
simple_pdf2 = os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf")
|
||||
simple_pdf3 = os.path.join(os.path.dirname(__file__), "samples", "documents", "originals", "0000003.pdf")
|
||||
simple_pdf2 = os.path.join(
|
||||
os.path.dirname(__file__), "samples", "documents", "originals", "0000002.pdf"
|
||||
)
|
||||
simple_pdf3 = os.path.join(
|
||||
os.path.dirname(__file__), "samples", "documents", "originals", "0000003.pdf"
|
||||
)
|
||||
simple_txt = os.path.join(os.path.dirname(__file__), "samples", "simple.txt")
|
||||
simple_png = os.path.join(os.path.dirname(__file__), "samples", "simple-noalpha.png")
|
||||
simple_png2 = os.path.join(os.path.dirname(__file__), "examples", "no-text.png")
|
||||
@@ -106,26 +106,52 @@ simple_png2 = os.path.join(os.path.dirname(__file__), "examples", "no-text.png")
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="")
|
||||
class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
|
||||
|
||||
migrate_from = '1011_auto_20210101_2340'
|
||||
migrate_to = '1012_fix_archive_files'
|
||||
migrate_from = "1011_auto_20210101_2340"
|
||||
migrate_to = "1012_fix_archive_files"
|
||||
|
||||
def setUpBeforeMigration(self, apps):
|
||||
Document = apps.get_model("documents", "Document")
|
||||
|
||||
self.unrelated = make_test_document(Document, "unrelated", "application/pdf", simple_pdf3, "unrelated.pdf", simple_pdf)
|
||||
self.no_text = make_test_document(Document, "no-text", "image/png", simple_png2, "no-text.png", simple_pdf)
|
||||
self.doc_no_archive = make_test_document(Document, "no_archive", "text/plain", simple_txt, "no_archive.txt")
|
||||
self.clash1 = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf)
|
||||
self.clash2 = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf)
|
||||
self.clash3 = make_test_document(Document, "clash", "image/png", simple_png, "clash.png", simple_pdf)
|
||||
self.clash4 = make_test_document(Document, "clash.png", "application/pdf", simple_pdf2, "clash.png.pdf", simple_pdf2)
|
||||
self.unrelated = make_test_document(
|
||||
Document,
|
||||
"unrelated",
|
||||
"application/pdf",
|
||||
simple_pdf3,
|
||||
"unrelated.pdf",
|
||||
simple_pdf,
|
||||
)
|
||||
self.no_text = make_test_document(
|
||||
Document, "no-text", "image/png", simple_png2, "no-text.png", simple_pdf
|
||||
)
|
||||
self.doc_no_archive = make_test_document(
|
||||
Document, "no_archive", "text/plain", simple_txt, "no_archive.txt"
|
||||
)
|
||||
self.clash1 = make_test_document(
|
||||
Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf
|
||||
)
|
||||
self.clash2 = make_test_document(
|
||||
Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf
|
||||
)
|
||||
self.clash3 = make_test_document(
|
||||
Document, "clash", "image/png", simple_png, "clash.png", simple_pdf
|
||||
)
|
||||
self.clash4 = make_test_document(
|
||||
Document,
|
||||
"clash.png",
|
||||
"application/pdf",
|
||||
simple_pdf2,
|
||||
"clash.png.pdf",
|
||||
simple_pdf2,
|
||||
)
|
||||
|
||||
self.assertEqual(archive_path_old(self.clash1), archive_path_old(self.clash2))
|
||||
self.assertEqual(archive_path_old(self.clash1), archive_path_old(self.clash3))
|
||||
self.assertNotEqual(archive_path_old(self.clash1), archive_path_old(self.clash4))
|
||||
self.assertNotEqual(
|
||||
archive_path_old(self.clash1), archive_path_old(self.clash4)
|
||||
)
|
||||
|
||||
def testArchiveFilesMigrated(self):
|
||||
Document = self.apps.get_model('documents', 'Document')
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
for doc in Document.objects.all():
|
||||
if doc.archive_checksum:
|
||||
@@ -144,31 +170,65 @@ class TestMigrateArchiveFiles(DirectoriesMixin, TestMigrations):
|
||||
archive_checksum = hashlib.md5(f.read()).hexdigest()
|
||||
self.assertEqual(archive_checksum, doc.archive_checksum)
|
||||
|
||||
self.assertEqual(Document.objects.filter(archive_checksum__isnull=False).count(), 6)
|
||||
self.assertEqual(
|
||||
Document.objects.filter(archive_checksum__isnull=False).count(), 6
|
||||
)
|
||||
|
||||
def test_filenames(self):
|
||||
Document = self.apps.get_model('documents', 'Document')
|
||||
self.assertEqual(Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.doc_no_archive.id).archive_filename, None)
|
||||
self.assertEqual(Document.objects.get(id=self.clash1.id).archive_filename, f"{self.clash1.id:07}.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.clash2.id).archive_filename, f"{self.clash2.id:07}.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.clash3.id).archive_filename, f"{self.clash3.id:07}.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf")
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf"
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf"
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.doc_no_archive.id).archive_filename, None
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash1.id).archive_filename,
|
||||
f"{self.clash1.id:07}.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash2.id).archive_filename,
|
||||
f"{self.clash2.id:07}.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash3.id).archive_filename,
|
||||
f"{self.clash3.id:07}.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf"
|
||||
)
|
||||
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
|
||||
class TestMigrateArchiveFilesWithFilenameFormat(TestMigrateArchiveFiles):
|
||||
|
||||
def test_filenames(self):
|
||||
Document = self.apps.get_model('documents', 'Document')
|
||||
self.assertEqual(Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.doc_no_archive.id).archive_filename, None)
|
||||
self.assertEqual(Document.objects.get(id=self.clash1.id).archive_filename, "none/clash.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.clash2.id).archive_filename, "none/clash_01.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.clash3.id).archive_filename, "none/clash_02.pdf")
|
||||
self.assertEqual(Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf")
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.unrelated.id).archive_filename, "unrelated.pdf"
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.no_text.id).archive_filename, "no-text.pdf"
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.doc_no_archive.id).archive_filename, None
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash1.id).archive_filename, "none/clash.pdf"
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash2.id).archive_filename,
|
||||
"none/clash_01.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash3.id).archive_filename,
|
||||
"none/clash_02.pdf",
|
||||
)
|
||||
self.assertEqual(
|
||||
Document.objects.get(id=self.clash4.id).archive_filename, "clash.png.pdf"
|
||||
)
|
||||
|
||||
|
||||
def fake_parse_wrapper(parser, path, mime_type, file_name):
|
||||
@@ -179,34 +239,63 @@ def fake_parse_wrapper(parser, path, mime_type, file_name):
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="")
|
||||
class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
|
||||
migrate_from = '1011_auto_20210101_2340'
|
||||
migrate_to = '1012_fix_archive_files'
|
||||
migrate_from = "1011_auto_20210101_2340"
|
||||
migrate_to = "1012_fix_archive_files"
|
||||
auto_migrate = False
|
||||
|
||||
def test_archive_missing(self):
|
||||
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf)
|
||||
doc = make_test_document(
|
||||
Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf
|
||||
)
|
||||
os.unlink(archive_path_old(doc))
|
||||
|
||||
self.assertRaisesMessage(ValueError, "does not exist at: ", self.performMigration)
|
||||
self.assertRaisesMessage(
|
||||
ValueError, "does not exist at: ", self.performMigration
|
||||
)
|
||||
|
||||
def test_parser_missing(self):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc1 = make_test_document(Document, "document", "invalid/typesss768", simple_png, "document.png", simple_pdf)
|
||||
doc2 = make_test_document(Document, "document", "invalid/typesss768", simple_jpg, "document.jpg", simple_pdf)
|
||||
doc1 = make_test_document(
|
||||
Document,
|
||||
"document",
|
||||
"invalid/typesss768",
|
||||
simple_png,
|
||||
"document.png",
|
||||
simple_pdf,
|
||||
)
|
||||
doc2 = make_test_document(
|
||||
Document,
|
||||
"document",
|
||||
"invalid/typesss768",
|
||||
simple_jpg,
|
||||
"document.jpg",
|
||||
simple_pdf,
|
||||
)
|
||||
|
||||
self.assertRaisesMessage(ValueError, "no parsers are available", self.performMigration)
|
||||
self.assertRaisesMessage(
|
||||
ValueError, "no parsers are available", self.performMigration
|
||||
)
|
||||
|
||||
@mock.patch("documents.migrations.1012_fix_archive_files.parse_wrapper")
|
||||
def test_parser_error(self, m):
|
||||
m.side_effect = ParseError()
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc1 = make_test_document(Document, "document", "image/png", simple_png, "document.png", simple_pdf)
|
||||
doc2 = make_test_document(Document, "document", "application/pdf", simple_jpg, "document.jpg", simple_pdf)
|
||||
doc1 = make_test_document(
|
||||
Document, "document", "image/png", simple_png, "document.png", simple_pdf
|
||||
)
|
||||
doc2 = make_test_document(
|
||||
Document,
|
||||
"document",
|
||||
"application/pdf",
|
||||
simple_jpg,
|
||||
"document.jpg",
|
||||
simple_pdf,
|
||||
)
|
||||
|
||||
self.assertIsNotNone(doc1.archive_checksum)
|
||||
self.assertIsNotNone(doc2.archive_checksum)
|
||||
@@ -217,12 +306,29 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
self.assertEqual(m.call_count, 6)
|
||||
|
||||
self.assertEqual(
|
||||
len(list(filter(lambda log: "Parse error, will try again in 5 seconds" in log, capture.output))),
|
||||
4)
|
||||
len(
|
||||
list(
|
||||
filter(
|
||||
lambda log: "Parse error, will try again in 5 seconds" in log,
|
||||
capture.output,
|
||||
)
|
||||
)
|
||||
),
|
||||
4,
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
len(list(filter(lambda log: "Unable to regenerate archive document for ID:" in log, capture.output))),
|
||||
2)
|
||||
len(
|
||||
list(
|
||||
filter(
|
||||
lambda log: "Unable to regenerate archive document for ID:"
|
||||
in log,
|
||||
capture.output,
|
||||
)
|
||||
)
|
||||
),
|
||||
2,
|
||||
)
|
||||
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
@@ -240,15 +346,33 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc1 = make_test_document(Document, "document", "image/png", simple_png, "document.png", simple_pdf)
|
||||
doc2 = make_test_document(Document, "document", "application/pdf", simple_jpg, "document.jpg", simple_pdf)
|
||||
doc1 = make_test_document(
|
||||
Document, "document", "image/png", simple_png, "document.png", simple_pdf
|
||||
)
|
||||
doc2 = make_test_document(
|
||||
Document,
|
||||
"document",
|
||||
"application/pdf",
|
||||
simple_jpg,
|
||||
"document.jpg",
|
||||
simple_pdf,
|
||||
)
|
||||
|
||||
with self.assertLogs() as capture:
|
||||
self.performMigration()
|
||||
|
||||
self.assertEqual(
|
||||
len(list(filter(lambda log: "Parser did not return an archive document for document" in log, capture.output))),
|
||||
2)
|
||||
len(
|
||||
list(
|
||||
filter(
|
||||
lambda log: "Parser did not return an archive document for document"
|
||||
in log,
|
||||
capture.output,
|
||||
)
|
||||
)
|
||||
),
|
||||
2,
|
||||
)
|
||||
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
@@ -264,19 +388,37 @@ class TestMigrateArchiveFilesErrors(DirectoriesMixin, TestMigrations):
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="")
|
||||
class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):
|
||||
|
||||
migrate_from = '1012_fix_archive_files'
|
||||
migrate_to = '1011_auto_20210101_2340'
|
||||
migrate_from = "1012_fix_archive_files"
|
||||
migrate_to = "1011_auto_20210101_2340"
|
||||
|
||||
def setUpBeforeMigration(self, apps):
|
||||
|
||||
Document = apps.get_model("documents", "Document")
|
||||
|
||||
doc_unrelated = make_test_document(Document, "unrelated", "application/pdf", simple_pdf2, "unrelated.txt", simple_pdf2, "unrelated.pdf")
|
||||
doc_no_archive = make_test_document(Document, "no_archive", "text/plain", simple_txt, "no_archive.txt")
|
||||
clashB = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf, "clash_02.pdf")
|
||||
doc_unrelated = make_test_document(
|
||||
Document,
|
||||
"unrelated",
|
||||
"application/pdf",
|
||||
simple_pdf2,
|
||||
"unrelated.txt",
|
||||
simple_pdf2,
|
||||
"unrelated.pdf",
|
||||
)
|
||||
doc_no_archive = make_test_document(
|
||||
Document, "no_archive", "text/plain", simple_txt, "no_archive.txt"
|
||||
)
|
||||
clashB = make_test_document(
|
||||
Document,
|
||||
"clash",
|
||||
"image/jpeg",
|
||||
simple_jpg,
|
||||
"clash.jpg",
|
||||
simple_pdf,
|
||||
"clash_02.pdf",
|
||||
)
|
||||
|
||||
def testArchiveFilesReverted(self):
|
||||
Document = self.apps.get_model('documents', 'Document')
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
for doc in Document.objects.all():
|
||||
if doc.archive_checksum:
|
||||
@@ -291,35 +433,77 @@ class TestMigrateArchiveFilesBackwards(DirectoriesMixin, TestMigrations):
|
||||
archive_checksum = hashlib.md5(f.read()).hexdigest()
|
||||
self.assertEqual(archive_checksum, doc.archive_checksum)
|
||||
|
||||
self.assertEqual(Document.objects.filter(archive_checksum__isnull=False).count(), 2)
|
||||
self.assertEqual(
|
||||
Document.objects.filter(archive_checksum__isnull=False).count(), 2
|
||||
)
|
||||
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="{correspondent}/{title}")
|
||||
class TestMigrateArchiveFilesBackwardsWithFilenameFormat(TestMigrateArchiveFilesBackwards):
|
||||
class TestMigrateArchiveFilesBackwardsWithFilenameFormat(
|
||||
TestMigrateArchiveFilesBackwards
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
@override_settings(PAPERLESS_FILENAME_FORMAT="")
|
||||
class TestMigrateArchiveFilesBackwardsErrors(DirectoriesMixin, TestMigrations):
|
||||
|
||||
migrate_from = '1012_fix_archive_files'
|
||||
migrate_to = '1011_auto_20210101_2340'
|
||||
migrate_from = "1012_fix_archive_files"
|
||||
migrate_to = "1011_auto_20210101_2340"
|
||||
auto_migrate = False
|
||||
|
||||
def test_filename_clash(self):
|
||||
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
self.clashA = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf, "clash_02.pdf")
|
||||
self.clashB = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf, "clash_01.pdf")
|
||||
self.clashA = make_test_document(
|
||||
Document,
|
||||
"clash",
|
||||
"application/pdf",
|
||||
simple_pdf,
|
||||
"clash.pdf",
|
||||
simple_pdf,
|
||||
"clash_02.pdf",
|
||||
)
|
||||
self.clashB = make_test_document(
|
||||
Document,
|
||||
"clash",
|
||||
"image/jpeg",
|
||||
simple_jpg,
|
||||
"clash.jpg",
|
||||
simple_pdf,
|
||||
"clash_01.pdf",
|
||||
)
|
||||
|
||||
self.assertRaisesMessage(ValueError, "would clash with another archive filename", self.performMigration)
|
||||
self.assertRaisesMessage(
|
||||
ValueError,
|
||||
"would clash with another archive filename",
|
||||
self.performMigration,
|
||||
)
|
||||
|
||||
def test_filename_exists(self):
|
||||
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
self.clashA = make_test_document(Document, "clash", "application/pdf", simple_pdf, "clash.pdf", simple_pdf, "clash.pdf")
|
||||
self.clashB = make_test_document(Document, "clash", "image/jpeg", simple_jpg, "clash.jpg", simple_pdf, "clash_01.pdf")
|
||||
self.clashA = make_test_document(
|
||||
Document,
|
||||
"clash",
|
||||
"application/pdf",
|
||||
simple_pdf,
|
||||
"clash.pdf",
|
||||
simple_pdf,
|
||||
"clash.pdf",
|
||||
)
|
||||
self.clashB = make_test_document(
|
||||
Document,
|
||||
"clash",
|
||||
"image/jpeg",
|
||||
simple_jpg,
|
||||
"clash.jpg",
|
||||
simple_pdf,
|
||||
"clash_01.pdf",
|
||||
)
|
||||
|
||||
self.assertRaisesMessage(ValueError, "file already exists.", self.performMigration)
|
||||
self.assertRaisesMessage(
|
||||
ValueError, "file already exists.", self.performMigration
|
||||
)
|
||||
|
Reference in New Issue
Block a user