A community-supported supercharged document management system: scan, index and archive all your documents
Python aiangulararchivingdjangodmsdocument-managementdocument-management-systemllmmachine-learningocroptical-character-recognitionpdf
Change: enable auditlog by default, fix import / export (#6267)
shamoon0f8b2e69c930
diff --git a/docs/configuration.md b/docs/configuration.mdindex 981d27b27..1b5cc510d 100644--- a/docs/configuration.md+++ b/docs/configuration.md@@ -1311,11 +1311,9 @@ assigns or creates tags if a properly formatted barcode is detected.#### [`PAPERLESS_AUDIT_LOG_ENABLED=<bool>`](#PAPERLESS_AUDIT_LOG_ENABLED) {#PAPERLESS_AUDIT_LOG_ENABLED}-: Enables an audit trail for documents, document types, correspondents, and tags. Log entries can be viewed in the Django backend only.+: Enables the audit trail for documents, document types, correspondents, and tags.- !!! warning-- Once enabled cannot be disabled+ Defaults to true.## Collate Double-Sided Documents {#collate}diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.pyindex faa42ba31..b02fd686b 100644--- a/src/documents/management/commands/document_exporter.py+++ b/src/documents/management/commands/document_exporter.py@@ -9,6 +9,9 @@ from typing import Optionalimport tqdmfrom django.conf import settings++if settings.AUDIT_LOG_ENABLED:+ from auditlog.models import LogEntryfrom django.contrib.auth.models import Groupfrom django.contrib.auth.models import Permissionfrom django.contrib.auth.models import User@@ -307,6 +310,11 @@ class Command(BaseCommand):serializers.serialize("json", ApplicationConfiguration.objects.all()),)+ if settings.AUDIT_LOG_ENABLED:+ manifest += json.loads(+ serializers.serialize("json", LogEntry.objects.all()),+ )+# These are treated specially and included in the per-document manifest# if that setting is enabled. Otherwise, they are just exported to the bulk# manifestdiff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.pyindex 88ac2b91a..dc0ac36fd 100644--- a/src/documents/management/commands/document_importer.py+++ b/src/documents/management/commands/document_importer.py@@ -21,7 +21,13 @@ from django.db.models.signals import post_savefrom filelock import FileLockfrom documents.file_handling import create_source_path_directory+from documents.models import Correspondent+from documents.models import CustomField+from documents.models import CustomFieldInstancefrom documents.models import Document+from documents.models import DocumentType+from documents.models import Note+from documents.models import Tagfrom documents.parsers import run_convertfrom documents.settings import EXPORTER_ARCHIVE_NAMEfrom documents.settings import EXPORTER_FILE_NAME@@ -30,6 +36,9 @@ from documents.signals.handlers import update_filename_and_move_filesfrom documents.utils import copy_file_with_basic_statsfrom paperless import version+if settings.AUDIT_LOG_ENABLED:+ from auditlog.registry import auditlog+@contextmanagerdef disable_signal(sig, receiver, sender):@@ -151,6 +160,15 @@ class Command(BaseCommand):receiver=update_filename_and_move_files,sender=Document.tags.through,):+ if settings.AUDIT_LOG_ENABLED:+ auditlog.unregister(Document)+ auditlog.unregister(Correspondent)+ auditlog.unregister(Tag)+ auditlog.unregister(DocumentType)+ auditlog.unregister(Note)+ auditlog.unregister(CustomField)+ auditlog.unregister(CustomFieldInstance)+# Fill up the database with whatever is in the manifesttry:with transaction.atomic():diff --git a/src/documents/tests/test_management_exporter.py b/src/documents/tests/test_management_exporter.pyindex 226b89694..b95d07dec 100644--- a/src/documents/tests/test_management_exporter.py+++ b/src/documents/tests/test_management_exporter.py@@ -780,3 +780,17 @@ class TestExportImport(DirectoriesMixin, FileSystemAssertsMixin, TestCase):self.assertEqual(ContentType.objects.count(), num_content_type_objects)self.assertEqual(Permission.objects.count(), num_permission_objects + 1)++ def test_exporter_with_auditlog_disabled(self):+ shutil.rmtree(os.path.join(self.dirs.media_dir, "documents"))+ shutil.copytree(+ os.path.join(os.path.dirname(__file__), "samples", "documents"),+ os.path.join(self.dirs.media_dir, "documents"),+ )++ with override_settings(+ AUDIT_LOG_ENABLED=False,+ ):+ manifest = self._do_export(use_filename_format=True)+ for obj in manifest:+ self.assertNotEqual(obj["model"], "auditlog.logentry")diff --git a/src/paperless/checks.py b/src/paperless/checks.pyindex 0bfdce2cd..cbc8da5cf 100644--- a/src/paperless/checks.py+++ b/src/paperless/checks.py@@ -5,7 +5,6 @@ import shutilimport statfrom django.conf import settings-from django.core.checks import Criticalfrom django.core.checks import Errorfrom django.core.checks import Warningfrom django.core.checks import register@@ -205,13 +204,10 @@ def audit_log_check(app_configs, **kwargs):all_tables = db_conn.introspection.table_names()result = []- if ("auditlog_logentry" in all_tables) and not (settings.AUDIT_LOG_ENABLED):+ if ("auditlog_logentry" in all_tables) and not settings.AUDIT_LOG_ENABLED:result.append(- Critical(- (- "auditlog table was found but PAPERLESS_AUDIT_LOG_ENABLED"- " is not active. This setting cannot be disabled after enabling"- ),+ Warning(+ ("auditlog table was found but audit log is disabled."),),)diff --git a/src/paperless/settings.py b/src/paperless/settings.pyindex a583ef406..64af7c9b7 100644--- a/src/paperless/settings.py+++ b/src/paperless/settings.py@@ -1045,7 +1045,7 @@ TIKA_GOTENBERG_ENDPOINT = os.getenv(if TIKA_ENABLED:INSTALLED_APPS.append("paperless_tika.apps.PaperlessTikaConfig")-AUDIT_LOG_ENABLED = __get_boolean("PAPERLESS_AUDIT_LOG_ENABLED", "NO")+AUDIT_LOG_ENABLED = __get_boolean("PAPERLESS_AUDIT_LOG_ENABLED", "true")if AUDIT_LOG_ENABLED:INSTALLED_APPS.append("auditlog")MIDDLEWARE.append("auditlog.middleware.AuditlogMiddleware")diff --git a/src/paperless/tests/test_checks.py b/src/paperless/tests/test_checks.pyindex a6879cdbf..d2ea9102b 100644--- a/src/paperless/tests/test_checks.py+++ b/src/paperless/tests/test_checks.py@@ -259,9 +259,6 @@ class TestAuditLogChecks(TestCase):msg = msgs[0]self.assertIn(- (- "auditlog table was found but PAPERLESS_AUDIT_LOG_ENABLED"- " is not active."- ),+ ("auditlog table was found but audit log is disabled."),msg.msg,)