Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Enhancement: support delete originals after split / merge (#6935)

Dominik Bruhnd1ac15baa94f
diff --git a/docs/api.md b/docs/api.md
index c38018f71..94ece85ab 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -417,9 +417,14 @@ The following methods are supported:
- The ordering of the merged document is determined by the list of IDs.
- Optional `parameters`:
- `"metadata_document_id": DOC_ID` apply metadata (tags, correspondent, etc.) from this document to the merged document.
+ - `"delete_originals": true` to delete the original documents. This requires the calling user being the owner of
+ all documents that are merged.
- `split`
- Requires `parameters`:
- `"pages": [..]` The list should be a list of pages and/or a ranges, separated by commas e.g. `"[1,2-3,4,5-7]"`
+ - Optional `parameters`:
+ - `"delete_originals": true` to delete the original document after consumption. This requires the calling user being the owner of
+ the document.
- The split operation only accepts a single document.
- `rotate`
- Requires `parameters`:
diff --git a/src-ui/messages.xlf b/src-ui/messages.xlf
index 2cdb7a78a..56cfa9ae4 100644
--- a/src-ui/messages.xlf
+++ b/src-ui/messages.xlf
@@ -2302,11 +2302,11 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1151</context>
+ <context context-type="linenumber">1152</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1192</context>
+ <context context-type="linenumber">1193</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
@@ -2970,11 +2970,18 @@
<context context-type="linenumber">24</context>
</context-group>
</trans-unit>
+ <trans-unit id="5612366187076076264" datatype="html">
+ <source>Delete original documents after successful merge</source>
+ <context-group purpose="location">
+ <context context-type="sourcefile">src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.html</context>
+ <context context-type="linenumber">32</context>
+ </context-group>
+ </trans-unit>
<trans-unit id="5138283234724909648" datatype="html">
<source>Note that only PDFs will be included.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.html</context>
- <context context-type="linenumber">30</context>
+ <context context-type="linenumber">34</context>
</context-group>
</trans-unit>
<trans-unit id="8157388568390631653" datatype="html">
@@ -2991,6 +2998,13 @@
<context context-type="linenumber">28</context>
</context-group>
</trans-unit>
+ <trans-unit id="492847770415850840" datatype="html">
+ <source>Delete original document after successful split</source>
+ <context-group purpose="location">
+ <context context-type="sourcefile">src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.html</context>
+ <context context-type="linenumber">49</context>
+ </context-group>
+ </trans-unit>
<trans-unit id="2509141182388535183" datatype="html">
<source>View</source>
<context-group purpose="location">
@@ -5475,7 +5489,7 @@
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1169</context>
+ <context context-type="linenumber">1170</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/guards/dirty-saved-view.guard.ts</context>
@@ -5957,21 +5971,21 @@
<source>Split operation will begin in the background.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1128</context>
+ <context context-type="linenumber">1129</context>
</context-group>
</trans-unit>
<trans-unit id="3235014591864339926" datatype="html">
<source>Error executing split operation</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1137</context>
+ <context context-type="linenumber">1138</context>
</context-group>
</trans-unit>
<trans-unit id="6555329262222566158" datatype="html">
<source>Rotate confirm</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1149</context>
+ <context context-type="linenumber">1150</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
@@ -5982,49 +5996,49 @@
<source>This operation will permanently rotate the original version of the current document.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1150</context>
+ <context context-type="linenumber">1151</context>
</context-group>
</trans-unit>
<trans-unit id="4069543875319587651" datatype="html">
<source>Rotation will begin in the background. Close and re-open the document after the operation has completed to see the changes.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1166</context>
+ <context context-type="linenumber">1167</context>
</context-group>
</trans-unit>
<trans-unit id="2962674215361798818" datatype="html">
<source>Error executing rotate operation</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1178</context>
+ <context context-type="linenumber">1179</context>
</context-group>
</trans-unit>
<trans-unit id="3539261415918606512" datatype="html">
<source>Delete pages confirm</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1190</context>
+ <context context-type="linenumber">1191</context>
</context-group>
</trans-unit>
<trans-unit id="5854352498125813866" datatype="html">
<source>This operation will permanently delete the selected pages from the original document.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1191</context>
+ <context context-type="linenumber">1192</context>
</context-group>
</trans-unit>
<trans-unit id="8617528702531167646" datatype="html">
<source>Delete pages operation will begin in the background. Close and re-open or reload this document after the operation has completed to see the changes.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1206</context>
+ <context context-type="linenumber">1207</context>
</context-group>
</trans-unit>
<trans-unit id="1249139200486584973" datatype="html">
<source>Error executing delete pages operation</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-detail/document-detail.component.ts</context>
- <context context-type="linenumber">1215</context>
+ <context context-type="linenumber">1216</context>
</context-group>
</trans-unit>
<trans-unit id="4958946940233632319" datatype="html">
@@ -6417,7 +6431,7 @@
<source>Merged document will be queued for consumption.</source>
<context-group purpose="location">
<context context-type="sourcefile">src/app/components/document-list/bulk-editor/bulk-editor.component.ts</context>
- <context context-type="linenumber">819</context>
+ <context context-type="linenumber">822</context>
</context-group>
</trans-unit>
<trans-unit id="2784168796433474565" datatype="html">
diff --git a/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.html b/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.html
index 0da291c94..576861ff2 100644
--- a/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.html
+++ b/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.html
@@ -27,6 +27,10 @@
}
</select>
</div>
+ <div class="form-check form-switch mt-4">
+ <input class="form-check-input" type="checkbox" role="switch" id="deleteOriginalsSwitch" [(ngModel)]="deleteOriginals" [disabled]="!userOwnsAllDocuments">
+ <label class="form-check-label" for="deleteOriginalsSwitch" i18n>Delete original documents after successful merge</label>
+ </div>
<p class="small text-muted fst-italic mt-4" i18n>Note that only PDFs will be included.</p>
</div>
<div class="modal-footer">
diff --git a/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.ts b/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.ts
index 7e64a4f84..1530620e1 100644
--- a/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.ts
+++ b/src-ui/src/app/components/common/confirm-dialog/merge-confirm-dialog/merge-confirm-dialog.component.ts
@@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core'
import { ConfirmDialogComponent } from '../confirm-dialog.component'
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { DocumentService } from 'src/app/services/rest/document.service'
+import { PermissionsService } from 'src/app/services/permissions.service'
import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop'
import { Subject, takeUntil } from 'rxjs'
import { Document } from 'src/app/data/document'
@@ -16,6 +17,7 @@ export class MergeConfirmDialogComponent
implements OnInit
{
public documentIDs: number[] = []
+ public deleteOriginals: boolean = false
private _documents: Document[] = []
get documents(): Document[] {
return this._documents
@@ -27,7 +29,8 @@ export class MergeConfirmDialogComponent
constructor(
activeModal: NgbActiveModal,
- private documentService: DocumentService
+ private documentService: DocumentService,
+ private permissionService: PermissionsService
) {
super(activeModal)
}
@@ -48,4 +51,10 @@ export class MergeConfirmDialogComponent
getDocument(documentID: number): Document {
return this.documents.find((d) => d.id === documentID)
}
+
+ get userOwnsAllDocuments(): boolean {
+ return this.documents.every((d) =>
+ this.permissionService.currentUserOwnsObject(d)
+ )
+ }
}
diff --git a/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.html b/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.html
index d73e97817..411f5ab79 100644
--- a/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.html
+++ b/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.html
@@ -44,6 +44,10 @@
</ul>
</div>
</div>
+ <div class="form-check form-switch mt-4">
+ <input class="form-check-input" type="checkbox" role="switch" id="deleteOriginalSwitch" [(ngModel)]="deleteOriginal" [disabled]="!userOwnsDocument">
+ <label class="form-check-label" for="deleteOriginalSwitch" i18n>Delete original document after successful split</label>
+ </div>
</div>
<div class="modal-footer">
<button type="button" class="btn" [class]="cancelBtnClass" (click)="cancel()" [disabled]="!buttonsEnabled">
diff --git a/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.spec.ts b/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.spec.ts
index 27f458537..c010702fa 100644
--- a/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.spec.ts
+++ b/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.spec.ts
@@ -7,6 +7,7 @@ import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { NgxBootstrapIconsModule, allIcons } from 'ngx-bootstrap-icons'
import { DocumentService } from 'src/app/services/rest/document.service'
import { PdfViewerModule } from 'ng2-pdf-viewer'
+import { of } from 'rxjs'
describe('SplitConfirmDialogComponent', () => {
let component: SplitConfirmDialogComponent
@@ -32,6 +33,14 @@ describe('SplitConfirmDialogComponent', () => {
fixture.detectChanges()
})
+ it('should load document on init', () => {
+ const getSpy = jest.spyOn(documentService, 'get')
+ component.documentID = 1
+ getSpy.mockReturnValue(of({ id: 1 } as any))
+ component.ngOnInit()
+ expect(documentService.get).toHaveBeenCalledWith(1)
+ })
+
it('should update pagesString when pages are added', () => {
component.totalPages = 5
component.page = 2
diff --git a/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.ts b/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.ts
index 47261afa2..266da00ab 100644
--- a/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.ts
+++ b/src-ui/src/app/components/common/confirm-dialog/split-confirm-dialog/split-confirm-dialog.component.ts
@@ -1,7 +1,9 @@
-import { Component } from '@angular/core'
+import { Component, OnInit } from '@angular/core'
import { ConfirmDialogComponent } from '../confirm-dialog.component'
+import { Document } from 'src/app/data/document'
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
import { DocumentService } from 'src/app/services/rest/document.service'
+import { PermissionsService } from 'src/app/services/permissions.service'
import { PDFDocumentProxy } from 'ng2-pdf-viewer'
@Component({
@@ -9,7 +11,10 @@ import { PDFDocumentProxy } from 'ng2-pdf-viewer'
templateUrl: './split-confirm-dialog.component.html',
styleUrl: './split-confirm-dialog.component.scss',
})
-export class SplitConfirmDialogComponent extends ConfirmDialogComponent {
+export class SplitConfirmDialogComponent
+ extends ConfirmDialogComponent
+ implements OnInit
+{
public get pagesString(): string {
let pagesStr = ''
@@ -32,8 +37,10 @@ export class SplitConfirmDialogComponent extends ConfirmDialogComponent {
private pages: Set<number> = new Set()
public documentID: number
+ private document: Document
public page: number = 1
public totalPages: number
+ public deleteOriginal: boolean = false
public get pdfSrc(): string {
return this.documentService.getPreviewUrl(this.documentID)
@@ -41,12 +48,19 @@ export class SplitConfirmDialogComponent extends ConfirmDialogComponent {
constructor(
activeModal: NgbActiveModal,
- private documentService: DocumentService
+ private documentService: DocumentService,
+ private permissionService: PermissionsService
) {
super(activeModal)
this.confirmButtonEnabled = this.pages.size > 0
}
+ ngOnInit(): void {
+ this.documentService.get(this.documentID).subscribe((r) => {
+ this.document = r
+ })
+ }
+
pdfPreviewLoaded(pdf: PDFDocumentProxy) {
this.totalPages = pdf.numPages
}
@@ -63,4 +77,8 @@ export class SplitConfirmDialogComponent extends ConfirmDialogComponent {
this.pages.delete(page)
this.confirmButtonEnabled = this.pages.size > 0
}
+
+ get userOwnsDocument(): boolean {
+ return this.permissionService.currentUserOwnsObject(this.document)
+ }
}
diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts
index 0194b9544..0a6eb3a5f 100644
--- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts
+++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts
@@ -1099,7 +1099,7 @@ describe('DocumentDetailComponent', () => {
expect(req.request.body).toEqual({
documents: [doc.id],
method: 'split',
- parameters: { pages: '1-2,3-5' },
+ parameters: { pages: '1-2,3-5', delete_originals: false },
})
req.error(new ProgressEvent('failed'))
modal.componentInstance.confirm()
diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts
index ff671efe0..339d35587 100644
--- a/src-ui/src/app/components/document-detail/document-detail.component.ts
+++ b/src-ui/src/app/components/document-detail/document-detail.component.ts
@@ -1120,6 +1120,7 @@ export class DocumentDetailComponent
this.documentsService
.bulkEdit([this.document.id], 'split', {
pages: modal.componentInstance.pagesString,
+ delete_originals: modal.componentInstance.deleteOriginal,
})
.pipe(first(), takeUntil(this.unsubscribeNotifier))
.subscribe({
diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts
index 8587095c1..cbc8cc4bf 100644
--- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts
+++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.spec.ts
@@ -1056,6 +1056,25 @@ describe('BulkEditorComponent', () => {
httpTestingController.match(
`${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id`
) // listAllFilteredIds
+
+ // Test with Delete Originals enabled
+ modal.componentInstance.deleteOriginals = true
+ modal.componentInstance.confirm()
+ req = httpTestingController.expectOne(
+ `${environment.apiBaseUrl}documents/bulk_edit/`
+ )
+ req.flush(true)
+ expect(req.request.body).toEqual({
+ documents: [3, 4],
+ method: 'merge',
+ parameters: { metadata_document_id: 3, delete_originals: true },
+ })
+ httpTestingController.match(
+ `${environment.apiBaseUrl}documents/?page=1&page_size=50&ordering=-created&truncate_content=true`
+ ) // list reload
+ httpTestingController.match(
+ `${environment.apiBaseUrl}documents/?page=1&page_size=100000&fields=id`
+ ) // listAllFilteredIds
})
it('should support bulk download with archive, originals or both and file formatting', () => {
diff --git a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts
index df3bf88c0..a03e27549 100644
--- a/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts
+++ b/src-ui/src/app/components/document-list/bulk-editor/bulk-editor.component.ts
@@ -813,6 +813,9 @@ export class BulkEditorComponent
if (mergeDialog.metadataDocumentID > -1) {
args['metadata_document_id'] = mergeDialog.metadataDocumentID
}
+ if (mergeDialog.deleteOriginals) {
+ args['delete_originals'] = true
+ }
mergeDialog.buttonsEnabled = false
this.executeBulkOperation(modal, 'merge', args, mergeDialog.documentIDs)
this.toastService.showInfo(
diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py
index 9e0fc242a..7733a8d68 100644
--- a/src/documents/bulk_edit.py
+++ b/src/documents/bulk_edit.py
@@ -4,7 +4,10 @@ import logging
import os
from typing import Optional
+from celery import chain
from celery import chord
+from celery import group
+from celery import shared_task
from django.conf import settings
from django.db.models import Q
@@ -153,6 +156,7 @@ def modify_custom_fields(doc_ids: list[int], add_custom_fields, remove_custom_fi
return "OK"
+@shared_task
def delete(doc_ids: list[int]):
Document.objects.filter(id__in=doc_ids).delete()
@@ -234,7 +238,11 @@ def rotate(doc_ids: list[int], degrees: int):
return "OK"
-def merge(doc_ids: list[int], metadata_document_id: Optional[int] = None):
+def merge(
+ doc_ids: list[int],
+ metadata_document_id: Optional[int] = None,
+ delete_originals: bool = False,
+):
logger.info(
f"Attempting to merge {len(doc_ids)} documents into a single document.",
)
@@ -277,7 +285,8 @@ def merge(doc_ids: list[int], metadata_document_id: Optional[int] = None):
overrides = DocumentMetadataOverrides()
logger.info("Adding merged document to the task queue.")
- consume_file.delay(
+
+ consume_task = consume_file.s(
ConsumableDocument(
source=DocumentSource.ConsumeFolder,
original_file=filepath,
@@ -285,16 +294,26 @@ def merge(doc_ids: list[int], metadata_document_id: Optional[int] = None):
overrides,
)
+ if delete_originals:
+ logger.info(
+ "Queueing removal of original documents after consumption of merged document",
+ )
+ chain(consume_task, delete.si(affected_docs)).delay()
+ else:
+ consume_task.delay()
+
return "OK"
-def split(doc_ids: list[int], pages: list[list[int]]):
+def split(doc_ids: list[int], pages: list[list[int]], delete_originals: bool = False):
logger.info(
f"Attempting to split document {doc_ids[0]} into {len(pages)} documents",
)
doc = Document.objects.get(id=doc_ids[0])
import pikepdf
+ consume_tasks = []
+
try:
with pikepdf.open(doc.source_path) as pdf:
for idx, split_doc in enumerate(pages):
@@ -314,13 +333,24 @@ def split(doc_ids: list[int], pages: list[list[int]]):
logger.info(
f"Adding split document with pages {split_doc} to the task queue.",
)
- consume_file.delay(
- ConsumableDocument(
- source=DocumentSource.ConsumeFolder,
- original_file=filepath,
+ consume_tasks.append(
+ consume_file.s(
+ ConsumableDocument(
+ source=DocumentSource.ConsumeFolder,
+ original_file=filepath,
+ ),
+ overrides,
),
- overrides,
)
+
+ if delete_originals:
+ logger.info(
+ "Queueing removal of original document after consumption of the split documents",
+ )
+ chord(header=consume_tasks, body=delete.si([doc.id])).delay()
+ else:
+ group(consume_tasks).delay()
+
except Exception as e:
logger.exception(f"Error splitting document {doc.id}: {e}")
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index 5213d4ca0..426527183 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -1131,6 +1131,12 @@ class BulkEditSerializer(
except ValueError:
raise serializers.ValidationError("invalid pages specified")
+ if "delete_originals" in parameters:
+ if not isinstance(parameters["delete_originals"], bool):
+ raise serializers.ValidationError("delete_originals must be a boolean")
+ else:
+ parameters["delete_originals"] = False
+
def _validate_parameters_delete_pages(self, parameters):
if "pages" not in parameters:
raise serializers.ValidationError("pages not specified")
@@ -1139,6 +1145,13 @@ class BulkEditSerializer(
if not all(isinstance(i, int) for i in parameters["pages"]):
raise serializers.ValidationError("pages must be a list of integers")
+ def _validate_parameters_merge(self, parameters):
+ if "delete_originals" in parameters:
+ if not isinstance(parameters["delete_originals"], bool):
+ raise serializers.ValidationError("delete_originals must be a boolean")
+ else:
+ parameters["delete_originals"] = False
+
def validate(self, attrs):
method = attrs["method"]
parameters = attrs["parameters"]
@@ -1171,6 +1184,8 @@ class BulkEditSerializer(
"Delete pages method only supports one document",
)
self._validate_parameters_delete_pages(parameters)
+ elif method == bulk_edit.merge:
+ self._validate_parameters_merge(parameters)
return attrs
diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py
index 7078aca12..ebef6a329 100644
--- a/src/documents/tests/test_api_bulk_edit.py
+++ b/src/documents/tests/test_api_bulk_edit.py
@@ -994,6 +994,82 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id])
self.assertEqual(kwargs["metadata_document_id"], self.doc3.id)
+ @mock.patch("documents.serialisers.bulk_edit.merge")
+ def test_merge_and_delete_insufficient_permissions(self, m):
+ self.doc1.owner = User.objects.get(username="temp_admin")
+ self.doc1.save()
+ user1 = User.objects.create(username="user1")
+ self.client.force_authenticate(user=user1)
+
+ m.return_value = "OK"
+
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc1.id, self.doc2.id],
+ "method": "merge",
+ "parameters": {
+ "metadata_document_id": self.doc2.id,
+ "delete_originals": True,
+ },
+ },
+ ),
+ content_type="application/json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
+ m.assert_not_called()
+ self.assertEqual(response.content, b"Insufficient permissions")
+
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc2.id, self.doc3.id],
+ "method": "merge",
+ "parameters": {
+ "metadata_document_id": self.doc2.id,
+ "delete_originals": True,
+ },
+ },
+ ),
+ content_type="application/json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ m.assert_called_once()
+
+ @mock.patch("documents.serialisers.bulk_edit.merge")
+ def test_merge_invalid_parameters(self, m):
+ """
+ GIVEN:
+ - API data for merging documents is called
+ - The parameters are invalid
+ WHEN:
+ - API is called
+ THEN:
+ - The API fails with a correct error code
+ """
+ m.return_value = "OK"
+
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc1.id, self.doc2.id],
+ "method": "merge",
+ "parameters": {
+ "delete_originals": "not_boolean",
+ },
+ },
+ ),
+ content_type="application/json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ m.assert_not_called()
+
@mock.patch("documents.serialisers.bulk_edit.split")
def test_split(self, m):
m.return_value = "OK"
@@ -1066,6 +1142,24 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn(b"Split method only supports one document", response.content)
+ response = self.client.post(
+ "/api/documents/bulk_edit/",
+ json.dumps(
+ {
+ "documents": [self.doc2.id],
+ "method": "split",
+ "parameters": {
+ "pages": "1",
+ "delete_originals": "notabool",
+ }, # not a bool
+ },
+ ),
+ content_type="application/json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(b"delete_originals must be a boolean", response.content)
+
@mock.patch("documents.serialisers.bulk_edit.delete_pages")
def test_delete_pages(self, m):
m.return_value = "OK"
diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py
index 16579c887..37ebc35fc 100644
--- a/src/documents/tests/test_bulk_edit.py
+++ b/src/documents/tests/test_bulk_edit.py
@@ -410,7 +410,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
mime_type="image/jpeg",
)
- @mock.patch("documents.tasks.consume_file.delay")
+ @mock.patch("documents.tasks.consume_file.s")
def test_merge(self, mock_consume_file):
"""
GIVEN:
@@ -444,6 +444,50 @@ class TestPDFActions(DirectoriesMixin, TestCase):
self.assertEqual(result, "OK")
+ @mock.patch("documents.bulk_edit.delete.si")
+ @mock.patch("documents.tasks.consume_file.s")
+ @mock.patch("documents.bulk_edit.chain")
+ def test_merge_and_delete_originals(
+ self,
+ mock_chain,
+ mock_consume_file,
+ mock_delete_documents,
+ ):
+ """
+ GIVEN:
+ - Existing documents
+ WHEN:
+ - Merge action with deleting documents is called with 3 documents
+ THEN:
+ - Consume file task should be called
+ - Document deletion task should be called
+ """
+ doc_ids = [self.doc1.id, self.doc2.id, self.doc3.id]
+
+ result = bulk_edit.merge(doc_ids, delete_originals=True)
+ self.assertEqual(result, "OK")
+
+ expected_filename = (
+ f"{'_'.join([str(doc_id) for doc_id in doc_ids])[:100]}_merged.pdf"
+ )
+
+ mock_consume_file.assert_called()
+ mock_delete_documents.assert_called()
+ mock_chain.assert_called_once()
+
+ consume_file_args, _ = mock_consume_file.call_args
+ self.assertEqual(
+ Path(consume_file_args[0].original_file).name,
+ expected_filename,
+ )
+ self.assertEqual(consume_file_args[1].title, None)
+
+ delete_documents_args, _ = mock_delete_documents.call_args
+ self.assertEqual(
+ delete_documents_args[0],
+ doc_ids,
+ )
+
@mock.patch("documents.tasks.consume_file.delay")
@mock.patch("pikepdf.open")
def test_merge_with_errors(self, mock_open_pdf, mock_consume_file):
@@ -469,7 +513,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
mock_consume_file.assert_not_called()
- @mock.patch("documents.tasks.consume_file.delay")
+ @mock.patch("documents.tasks.consume_file.s")
def test_split(self, mock_consume_file):
"""
GIVEN:
@@ -488,6 +532,44 @@ class TestPDFActions(DirectoriesMixin, TestCase):
self.assertEqual(result, "OK")
+ @mock.patch("documents.bulk_edit.delete.si")
+ @mock.patch("documents.tasks.consume_file.s")
+ @mock.patch("documents.bulk_edit.chord")
+ def test_split_and_delete_originals(
+ self,
+ mock_chord,
+ mock_consume_file,
+ mock_delete_documents,
+ ):
+ """
+ GIVEN:
+ - Existing documents
+ WHEN:
+ - Split action with deleting documents is called with 1 document and 2 page groups
+ - delete_originals is set to True
+ THEN:
+ - Consume file should be called twice
+ - Document deletion task should be called
+ """
+ doc_ids = [self.doc2.id]
+ pages = [[1, 2], [3]]
+
+ result = bulk_edit.split(doc_ids, pages, delete_originals=True)
+ self.assertEqual(result, "OK")
+
+ self.assertEqual(mock_consume_file.call_count, 2)
+ consume_file_args, _ = mock_consume_file.call_args
+ self.assertEqual(consume_file_args[1].title, "B (split 2)")
+
+ mock_delete_documents.assert_called()
+ mock_chord.assert_called_once()
+
+ delete_documents_args, _ = mock_delete_documents.call_args
+ self.assertEqual(
+ delete_documents_args[0],
+ doc_ids,
+ )
+
@mock.patch("documents.tasks.consume_file.delay")
@mock.patch("pikepdf.Pdf.save")
def test_split_with_errors(self, mock_save_pdf, mock_consume_file):
diff --git a/src/documents/views.py b/src/documents/views.py
index 02023b59f..c0ad3c67a 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -965,16 +965,31 @@ class BulkEditView(PassUserMixin):
document_objs = Document.objects.select_related("owner").filter(
pk__in=documents,
)
+ user_is_owner_of_all_documents = all(
+ (doc.owner == user or doc.owner is None) for doc in document_objs
+ )
+
has_perms = (
- all((doc.owner == user or doc.owner is None) for doc in document_objs)
+ user_is_owner_of_all_documents
if method
- in [bulk_edit.set_permissions, bulk_edit.delete, bulk_edit.rotate]
+ in [
+ bulk_edit.set_permissions,
+ bulk_edit.delete,
+ bulk_edit.rotate,
+ ]
else all(
has_perms_owner_aware(user, "change_document", doc)
for doc in document_objs
)
)
+ if (
+ method in [bulk_edit.merge, bulk_edit.split]
+ and parameters["delete_originals"]
+ and not user_is_owner_of_all_documents
+ ):
+ has_perms = False
+
if not has_perms:
return HttpResponseForbidden("Insufficient permissions")