From d04b54140cc33d1b048172062c2b8edb4155eed7 Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Fri, 27 Nov 2020 13:12:13 +0100 Subject: [PATCH] moved consumption dir check into the correct spot --- src/documents/consumer.py | 13 ------------ .../management/commands/document_consumer.py | 11 +++++++++- src/documents/tests/test_consumer.py | 20 ------------------- .../tests/test_management_consumer.py | 11 ++++++++++ 4 files changed, 21 insertions(+), 34 deletions(-) diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 8fed01c30..a7afca89d 100755 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -8,7 +8,6 @@ from django.conf import settings from django.db import transaction from django.utils import timezone -from paperless.db import GnuPG from .classifier import DocumentClassifier, IncompatibleClassifierVersionError from .file_handling import generate_filename, create_source_path_directory from .loggers import LoggingMixin @@ -40,17 +39,6 @@ class Consumer(LoggingMixin): raise ConsumerError("Cannot consume {}: It is not a file".format( self.path)) - def pre_check_consumption_dir(self): - if not settings.CONSUMPTION_DIR: - raise ConsumerError( - "The CONSUMPTION_DIR settings variable does not appear to be " - "set.") - - if not os.path.isdir(settings.CONSUMPTION_DIR): - raise ConsumerError( - "Consumption directory {} does not exist".format( - settings.CONSUMPTION_DIR)) - def pre_check_duplicate(self): with open(self.path, "rb") as f: checksum = hashlib.md5(f.read()).hexdigest() @@ -92,7 +80,6 @@ class Consumer(LoggingMixin): # Make sure that preconditions for consuming the file are met. self.pre_check_file_exists() - self.pre_check_consumption_dir() self.pre_check_directories() self.pre_check_duplicate() diff --git a/src/documents/management/commands/document_consumer.py b/src/documents/management/commands/document_consumer.py index c25d0cfa9..b738f001b 100644 --- a/src/documents/management/commands/document_consumer.py +++ b/src/documents/management/commands/document_consumer.py @@ -3,7 +3,7 @@ import os from time import sleep from django.conf import settings -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandError from django_q.tasks import async_task from watchdog.events import FileSystemEventHandler from watchdog.observers.polling import PollingObserver @@ -95,6 +95,15 @@ class Command(BaseCommand): def handle(self, *args, **options): directory = options["directory"] + if not directory: + raise CommandError( + "CONSUMPTION_DIR does not appear to be set." + ) + + if not os.path.isdir(directory): + raise CommandError( + f"Consumption directory {directory} does not exist") + for entry in os.scandir(directory): _consume(entry.path) diff --git a/src/documents/tests/test_consumer.py b/src/documents/tests/test_consumer.py index 323f5051f..ed835a467 100644 --- a/src/documents/tests/test_consumer.py +++ b/src/documents/tests/test_consumer.py @@ -502,26 +502,6 @@ class TestConsumer(TestCase): self.fail("Should throw exception") - @override_settings(CONSUMPTION_DIR=None) - def testConsumptionDirUnset(self): - try: - self.consumer.try_consume_file(self.get_test_file()) - except ConsumerError as e: - self.assertEqual(str(e), "The CONSUMPTION_DIR settings variable does not appear to be set.") - return - - self.fail("Should throw exception") - - @override_settings(CONSUMPTION_DIR="asd") - def testNoConsumptionDir(self): - try: - self.consumer.try_consume_file(self.get_test_file()) - except ConsumerError as e: - self.assertEqual(str(e), "Consumption directory asd does not exist") - return - - self.fail("Should throw exception") - def testDuplicates(self): self.consumer.try_consume_file(self.get_test_file()) diff --git a/src/documents/tests/test_management_consumer.py b/src/documents/tests/test_management_consumer.py index 33938d450..25b71f563 100644 --- a/src/documents/tests/test_management_consumer.py +++ b/src/documents/tests/test_management_consumer.py @@ -6,6 +6,7 @@ from time import sleep from unittest import mock from django.conf import settings +from django.core.management import call_command, CommandError from django.test import TestCase, override_settings from documents.consumer import ConsumerError @@ -193,3 +194,13 @@ class TestConsumer(TestCase): @override_settings(CONSUMER_POLLING=1) def test_slow_write_incomplete_polling(self): self.test_slow_write_incomplete() + + @override_settings(CONSUMPTION_DIR="does_not_exist") + def test_consumption_directory_invalid(self): + + self.assertRaises(CommandError, call_command, 'document_consumer', '--oneshot') + + @override_settings(CONSUMPTION_DIR="") + def test_consumption_directory_unset(self): + + self.assertRaises(CommandError, call_command, 'document_consumer', '--oneshot')