Unify merge and merge_and_delete_originals

This commit is contained in:
Dominik Bruhn 2024-06-07 16:34:37 +02:00
parent ef9d1b5188
commit 236213ed2b
6 changed files with 29 additions and 23 deletions

View File

@ -412,14 +412,13 @@ The following methods are supported:
- `"merge": true or false` (defaults to false)
- The `merge` flag determines if the supplied permissions will overwrite all existing permissions (including
removing them) or be merged with existing permissions.
- `merge` and `merge_and_delete_originals`
- `merge`
- No additional `parameters` required.
- 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.
- As the name implies, `merge_and_delete_originals` deletes the original
documents after merging. This requires the calling user being the owner of
all documents that are merged.
- `"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]"`

View File

@ -813,13 +813,11 @@ 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,
mergeDialog.deleteOriginals ? 'merge_and_delete_originals' : 'merge',
args,
mergeDialog.documentIDs
)
this.executeBulkOperation(modal, 'merge', args, mergeDialog.documentIDs)
this.toastService.showInfo(
$localize`Merged document will be queued for consumption.`
)

View File

@ -296,13 +296,6 @@ def merge(
return "OK"
def merge_and_delete_originals(
doc_ids: list[int],
metadata_document_id: Optional[int] = None,
):
return merge(doc_ids, metadata_document_id, True)
def split(doc_ids: list[int], pages: list[list[int]]):
logger.info(
f"Attempting to split document {doc_ids[0]} into {len(pages)} documents",

View File

@ -943,7 +943,6 @@ class BulkEditSerializer(
"set_permissions",
"rotate",
"merge",
"merge_and_delete_originals",
"split",
"delete_pages",
],
@ -1000,8 +999,6 @@ class BulkEditSerializer(
return bulk_edit.rotate
elif method == "merge":
return bulk_edit.merge
elif method == "merge_and_delete_originals":
return bulk_edit.merge_and_delete_originals
elif method == "split":
return bulk_edit.split
elif method == "delete_pages":
@ -1142,6 +1139,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"]
@ -1174,6 +1178,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

View File

@ -514,7 +514,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
"""
doc_ids = [self.doc1_delete_after_merge.id, self.doc2_delete_after_merge.id]
result = bulk_edit.merge_and_delete_originals(doc_ids)
result = bulk_edit.merge(doc_ids, delete_originals=True)
self.assertEqual(result, "OK")
expected_filename = (

View File

@ -965,14 +965,17 @@ 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,
bulk_edit.merge_and_delete_originals,
]
else all(
has_perms_owner_aware(user, "change_document", doc)
@ -980,6 +983,13 @@ class BulkEditView(PassUserMixin):
)
)
if (
method == bulk_edit.merge
and parameters["delete_originals"]
and not user_is_owner_of_all_documents
):
has_perms = False
if not has_perms:
return HttpResponseForbidden("Insufficient permissions")