Compare commits

...
Author SHA1 Message Date
jonaswinkler 6e332674a4 bugfix, more tests 2020-12-06 02:10:14 +01:00
jonaswinkler 822c8ac24f Merge branch 'dev' into feature-better-exporter 2020-12-06 02:00:29 +01:00
jonaswinkler e0d155e780 exporter respects filename format 2020-12-05 12:52:34 +01:00
3 changed files with 34 additions and 12 deletions

No files matched your search

@@ -3,13 +3,16 @@ import os
import shutil
import time
from django.conf import settings
from django.core import serializers
from django.core.management.base import BaseCommand, CommandError
from django.utils.text import slugify
from documents.models import Document, Correspondent, Tag, DocumentType
from documents.settings import EXPORTER_FILE_NAME, EXPORTER_THUMBNAIL_NAME, \
EXPORTER_ARCHIVE_NAME
from paperless.db import GnuPG
from ...file_handling import generate_filename
from ...mixins import Renderable
@@ -54,31 +57,46 @@ class Command(Renderable, BaseCommand):
document = document_map[document_dict["pk"]]
unique_filename = f"{document.pk:07}_{document.file_name}"
file_target = os.path.join(self.target, unique_filename)
if settings.PAPERLESS_FILENAME_FORMAT:
# set storagy type temporarily for filename generation
storage_type_actual = document.storage_type
thumbnail_name = unique_filename + "-thumbnail.png"
document.storage_type = Document.STORAGE_TYPE_UNENCRYPTED
name = generate_filename(document)
document.storage_type = storage_type_actual
else:
name = f"{slugify(str(document))}_{document.id}{document.file_type}"
original_name = os.path.join("originals", name)
original_target = os.path.join(self.target, original_name)
os.makedirs(os.path.dirname(original_target), exist_ok=True)
thumbnail_name = os.path.splitext(name)[0] + ".png"
thumbnail_name = os.path.join("thumbnails", thumbnail_name)
thumbnail_target = os.path.join(self.target, thumbnail_name)
os.makedirs(os.path.dirname(thumbnail_target), exist_ok=True)
document_dict[EXPORTER_FILE_NAME] = unique_filename
document_dict[EXPORTER_FILE_NAME] = original_name
document_dict[EXPORTER_THUMBNAIL_NAME] = thumbnail_name
if os.path.exists(document.archive_path):
archive_name = \
f"{document.pk:07}_archive_{document.archive_file_name}"
archive_name = os.path.splitext(name)[0] + ".pdf"
archive_name = os.path.join("archive", archive_name)
archive_target = os.path.join(self.target, archive_name)
os.makedirs(os.path.dirname(archive_target), exist_ok=True)
document_dict[EXPORTER_ARCHIVE_NAME] = archive_name
else:
archive_name = None
archive_target = None
print(f"Exporting: {file_target}")
print(f"Exporting: {document}")
t = int(time.mktime(document.created.timetuple()))
if document.storage_type == Document.STORAGE_TYPE_GPG:
with open(file_target, "wb") as f:
with open(original_target, "wb") as f:
f.write(GnuPG.decrypted(document.source_file))
os.utime(file_target, times=(t, t))
os.utime(original_target, times=(t, t))
with open(thumbnail_target, "wb") as f:
f.write(GnuPG.decrypted(document.thumbnail_file))
@@ -90,7 +108,7 @@ class Command(Renderable, BaseCommand):
os.utime(archive_target, times=(t, t))
else:
shutil.copy(document.source_path, file_target)
shutil.copy(document.source_path, original_target)
shutil.copy(document.thumbnail_path, thumbnail_target)
if archive_target:
@@ -69,3 +69,9 @@ class TestExportImport(DirectoriesMixin, TestCase):
call_command('document_exporter', target)
Document.objects.create(checksum="AAAAAAAAAAAAAAAAA", title="wow", filename="0000004.pdf", id=3, mime_type="application/pdf")
self.assertRaises(FileNotFoundError, call_command, 'document_exporter', target)
@override_settings(
PAPERLESS_FILENAME_FORMAT="{correspondent}/{year}/{title}"
)
def test_export_with_filename_format(self):
self.test_exporter()
-2
View File
@@ -1,5 +1,3 @@
from datetime import datetime
from django.test import TestCase
from django.utils import timezone