mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Merge pull request #140 from danielquinn/issue/131
Fix for #131: delete files on document.delete
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
			
		||||
from django.apps import AppConfig
 | 
			
		||||
from django.db.models.signals import post_delete
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DocumentsConfig(AppConfig):
 | 
			
		||||
@@ -10,8 +11,12 @@ class DocumentsConfig(AppConfig):
 | 
			
		||||
        from .signals import document_consumption_started
 | 
			
		||||
        from .signals import document_consumption_finished
 | 
			
		||||
        from .signals.handlers import (
 | 
			
		||||
            set_correspondent, set_tags, run_pre_consume_script,
 | 
			
		||||
            run_post_consume_script)
 | 
			
		||||
            set_correspondent,
 | 
			
		||||
            set_tags,
 | 
			
		||||
            run_pre_consume_script,
 | 
			
		||||
            run_post_consume_script,
 | 
			
		||||
            cleanup_document_deletion
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        document_consumption_started.connect(run_pre_consume_script)
 | 
			
		||||
 | 
			
		||||
@@ -19,4 +24,6 @@ class DocumentsConfig(AppConfig):
 | 
			
		||||
        document_consumption_finished.connect(set_correspondent)
 | 
			
		||||
        document_consumption_finished.connect(run_post_consume_script)
 | 
			
		||||
 | 
			
		||||
        post_delete.connect(cleanup_document_deletion)
 | 
			
		||||
 | 
			
		||||
        AppConfig.ready(self)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from subprocess import Popen
 | 
			
		||||
 | 
			
		||||
@@ -62,10 +63,7 @@ def run_pre_consume_script(sender, filename, **kwargs):
 | 
			
		||||
    if not settings.PRE_CONSUME_SCRIPT:
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    Popen((
 | 
			
		||||
        settings.PRE_CONSUME_SCRIPT,
 | 
			
		||||
        filename
 | 
			
		||||
    )).wait()
 | 
			
		||||
    Popen((settings.PRE_CONSUME_SCRIPT, filename)).wait()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def run_post_consume_script(sender, document, **kwargs):
 | 
			
		||||
@@ -84,3 +82,10 @@ def run_post_consume_script(sender, document, **kwargs):
 | 
			
		||||
        str(document.correspondent),
 | 
			
		||||
        str(",".join(document.tags.all().values_list("slug", flat=True)))
 | 
			
		||||
    )).wait()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def cleanup_document_deletion(sender, instance, using, **kwargs):
 | 
			
		||||
    try:
 | 
			
		||||
        os.unlink(instance.source_path)
 | 
			
		||||
    except FileNotFoundError:
 | 
			
		||||
        pass  # The file's already gone, so we're cool with it.
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
 | 
			
		||||
from ..models import Document, FileInfo
 | 
			
		||||
from ..models import FileInfo
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestAttachment(TestCase):
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								src/documents/tests/test_document_model.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/documents/tests/test_document_model.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
from unittest import mock
 | 
			
		||||
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
 | 
			
		||||
from ..models import Document, Correspondent
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestDocument(TestCase):
 | 
			
		||||
 | 
			
		||||
    def test_file_deletion(self):
 | 
			
		||||
        document = Document.objects.create(
 | 
			
		||||
            correspondent=Correspondent.objects.create(name="Test0"),
 | 
			
		||||
            title="Title",
 | 
			
		||||
            content="content",
 | 
			
		||||
            checksum="checksum",
 | 
			
		||||
        )
 | 
			
		||||
        file_path = document.source_path
 | 
			
		||||
        with mock.patch("documents.signals.handlers.os.unlink") as mock_unlink:
 | 
			
		||||
            document.delete()
 | 
			
		||||
            mock_unlink.assert_called_with(file_path)
 | 
			
		||||
		Reference in New Issue
	
	Block a user