mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
fixes #165
This commit is contained in:
parent
665863e395
commit
e75534c0f2
@ -1,18 +1,29 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
from django.db.models.signals import post_save, m2m_changed
|
||||||
from filelock import FileLock
|
from filelock import FileLock
|
||||||
|
|
||||||
from documents.models import Document
|
from documents.models import Document
|
||||||
from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \
|
from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \
|
||||||
EXPORTER_ARCHIVE_NAME
|
EXPORTER_ARCHIVE_NAME
|
||||||
from ...file_handling import create_source_path_directory, \
|
from ...file_handling import create_source_path_directory
|
||||||
generate_unique_filename
|
|
||||||
from ...mixins import Renderable
|
from ...mixins import Renderable
|
||||||
|
from ...signals.handlers import update_filename_and_move_files
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def disable_signal(sig, receiver, sender):
|
||||||
|
try:
|
||||||
|
sig.disconnect(receiver=receiver, sender=sender)
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
sig.connect(receiver=receiver, sender=sender)
|
||||||
|
|
||||||
|
|
||||||
class Command(Renderable, BaseCommand):
|
class Command(Renderable, BaseCommand):
|
||||||
@ -47,11 +58,16 @@ class Command(Renderable, BaseCommand):
|
|||||||
self.manifest = json.load(f)
|
self.manifest = json.load(f)
|
||||||
|
|
||||||
self._check_manifest()
|
self._check_manifest()
|
||||||
|
with disable_signal(post_save,
|
||||||
|
receiver=update_filename_and_move_files,
|
||||||
|
sender=Document):
|
||||||
|
with disable_signal(m2m_changed,
|
||||||
|
receiver=update_filename_and_move_files,
|
||||||
|
sender=Document.tags.through):
|
||||||
|
# Fill up the database with whatever is in the manifest
|
||||||
|
call_command("loaddata", manifest_path)
|
||||||
|
|
||||||
# Fill up the database with whatever is in the manifest
|
self._import_files_from_manifest()
|
||||||
call_command("loaddata", manifest_path)
|
|
||||||
|
|
||||||
self._import_files_from_manifest()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _check_manifest_exists(path):
|
def _check_manifest_exists(path):
|
||||||
@ -117,9 +133,6 @@ class Command(Renderable, BaseCommand):
|
|||||||
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
|
||||||
|
|
||||||
with FileLock(settings.MEDIA_LOCK):
|
with FileLock(settings.MEDIA_LOCK):
|
||||||
document.filename = generate_unique_filename(
|
|
||||||
document, settings.ORIGINALS_DIR)
|
|
||||||
|
|
||||||
if os.path.isfile(document.source_path):
|
if os.path.isfile(document.source_path):
|
||||||
raise FileExistsError(document.source_path)
|
raise FileExistsError(document.source_path)
|
||||||
|
|
||||||
|
@ -24,11 +24,17 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
|||||||
|
|
||||||
file = os.path.join(self.dirs.originals_dir, "0000001.pdf")
|
file = os.path.join(self.dirs.originals_dir, "0000001.pdf")
|
||||||
|
|
||||||
Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow", filename="0000001.pdf", mime_type="application/pdf")
|
d1 = Document.objects.create(content="Content", checksum="42995833e01aea9b3edee44bbfdd7ce1", archive_checksum="62acb0bcbfbcaa62ca6ad3668e4e404b", title="wow", filename="0000001.pdf", mime_type="application/pdf")
|
||||||
Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
|
d2 = Document.objects.create(content="Content", checksum="9c9691e51741c1f4f41a20896af31770", title="wow", filename="0000002.pdf.gpg", mime_type="application/pdf", storage_type=Document.STORAGE_TYPE_GPG)
|
||||||
Tag.objects.create(name="t")
|
t1 = Tag.objects.create(name="t")
|
||||||
DocumentType.objects.create(name="dt")
|
dt1 = DocumentType.objects.create(name="dt")
|
||||||
Correspondent.objects.create(name="c")
|
c1 = Correspondent.objects.create(name="c")
|
||||||
|
|
||||||
|
d1.tags.add(t1)
|
||||||
|
d1.correspondents = c1
|
||||||
|
d1.document_type = dt1
|
||||||
|
d1.save()
|
||||||
|
d2.save()
|
||||||
|
|
||||||
target = tempfile.mkdtemp()
|
target = tempfile.mkdtemp()
|
||||||
self.addCleanup(shutil.rmtree, target)
|
self.addCleanup(shutil.rmtree, target)
|
||||||
@ -59,11 +65,25 @@ class TestExportImport(DirectoriesMixin, TestCase):
|
|||||||
self.assertEqual(checksum, element['fields']['archive_checksum'])
|
self.assertEqual(checksum, element['fields']['archive_checksum'])
|
||||||
|
|
||||||
with paperless_environment() as dirs:
|
with paperless_environment() as dirs:
|
||||||
|
self.assertEqual(Document.objects.count(), 2)
|
||||||
|
Document.objects.all().delete()
|
||||||
|
Correspondent.objects.all().delete()
|
||||||
|
DocumentType.objects.all().delete()
|
||||||
|
Tag.objects.all().delete()
|
||||||
|
self.assertEqual(Document.objects.count(), 0)
|
||||||
|
|
||||||
call_command('document_importer', target)
|
call_command('document_importer', target)
|
||||||
|
self.assertEqual(Document.objects.count(), 2)
|
||||||
messages = check_sanity()
|
messages = check_sanity()
|
||||||
# everything is alright after the test
|
# everything is alright after the test
|
||||||
self.assertEqual(len(messages), 0, str([str(m) for m in messages]))
|
self.assertEqual(len(messages), 0, str([str(m) for m in messages]))
|
||||||
|
|
||||||
|
@override_settings(
|
||||||
|
PAPERLESS_FILENAME_FORMAT="{title}"
|
||||||
|
)
|
||||||
|
def test_exporter_with_filename_format(self):
|
||||||
|
self.test_exporter()
|
||||||
|
|
||||||
def test_export_missing_files(self):
|
def test_export_missing_files(self):
|
||||||
|
|
||||||
target = tempfile.mkdtemp()
|
target = tempfile.mkdtemp()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user