Coverage for documents/management/commands/document_archiver.py: 0%

28 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 09:07 +0000

1import logging 

2 

3from django.conf import settings 

4 

5from documents.management.commands.base import PaperlessCommand 

6from documents.models import Document 

7from documents.tasks import update_document_content_maybe_archive_file 

8 

9logger = logging.getLogger("paperless.management.archiver") 

10 

11 

12class Command(PaperlessCommand): 

13 help = ( 

14 "Using the current classification model, assigns correspondents, tags " 

15 "and document types to all documents, effectively allowing you to " 

16 "back-tag all previously indexed documents with metadata created (or " 

17 "modified) after their initial import." 

18 ) 

19 

20 supports_progress_bar = True 

21 supports_multiprocessing = True 

22 

23 def add_arguments(self, parser): 

24 super().add_arguments(parser) 

25 parser.add_argument( 

26 "-f", 

27 "--overwrite", 

28 default=False, 

29 action="store_true", 

30 help=( 

31 "Recreates the archived document for documents that already " 

32 "have an archived version." 

33 ), 

34 ) 

35 parser.add_argument( 

36 "-d", 

37 "--document", 

38 default=None, 

39 type=int, 

40 required=False, 

41 help=( 

42 "Specify the ID of a document, and this command will only " 

43 "run on this specific document." 

44 ), 

45 ) 

46 

47 def handle(self, *args, **options): 

48 settings.SCRATCH_DIR.mkdir(parents=True, exist_ok=True) 

49 

50 overwrite = options["overwrite"] 

51 

52 if options["document"]: 

53 documents = Document.objects.filter(pk=options["document"]) 

54 else: 

55 documents = Document.objects.all() 

56 

57 document_ids = [ 

58 doc.id for doc in documents if overwrite or not doc.has_archive_version 

59 ] 

60 

61 try: 

62 logging.getLogger().handlers[0].level = logging.ERROR 

63 

64 for result in self.process_parallel( 

65 update_document_content_maybe_archive_file, 

66 document_ids, 

67 description="Archiving...", 

68 ): 

69 if result.error: 

70 self.console.print( 

71 f"[red]Failed document {result.item}: {result.error}[/red]", 

72 ) 

73 except KeyboardInterrupt: # pragma: no cover 

74 self.console.print("[yellow]Aborting...[/yellow]")