Support update vs create

This commit is contained in:
shamoon
2025-07-02 12:33:36 -07:00
parent 67624e539b
commit f9a5cfa1bf
7 changed files with 96 additions and 22 deletions

View File

@@ -502,6 +502,8 @@ def edit_pdf(
operations: list[dict],
*,
delete_original: bool = False,
update_document: bool = False,
include_metadata: bool = True,
user: User | None = None,
) -> Literal["OK"]:
"""
@@ -533,9 +535,25 @@ def edit_pdf(
if op.get("rotate"):
dst.pages[-1].rotate(op["rotate"], relative=True)
if update_document:
if len(pdf_docs) != 1:
logger.error(
"Update requested but multiple output documents specified",
)
return "ERROR"
pdf = pdf_docs[0]
pdf.remove_unreferenced_resources()
pdf.save(doc.source_path)
doc.checksum = hashlib.md5(doc.source_path.read_bytes()).hexdigest()
doc.page_count = len(pdf.pages)
doc.save()
update_document_content_maybe_archive_file.delay(document_id=doc.id)
else:
consume_tasks = []
overrides: DocumentMetadataOverrides = (
overrides = (
DocumentMetadataOverrides().from_document(doc)
if include_metadata
else DocumentMetadataOverrides()
)
if user is not None:
overrides.owner_id = user.id
@@ -564,6 +582,7 @@ def edit_pdf(
except Exception as e:
logger.exception(f"Error editing document {doc.id}: {e}")
return "ERROR"
return "OK"

View File

@@ -1537,11 +1537,23 @@ class BulkEditSerializer(
raise serializers.ValidationError("rotate must be an integer")
if "doc" in op and not isinstance(op["doc"], int):
raise serializers.ValidationError("doc must be an integer")
if "delete_original" in parameters:
if not isinstance(parameters["delete_original"], bool):
raise serializers.ValidationError("delete_original must be a boolean")
if "update_document" in parameters:
if not isinstance(parameters["update_document"], bool):
raise serializers.ValidationError("update_document must be a boolean")
else:
parameters["delete_original"] = False
parameters["update_document"] = False
if "include_metadata" in parameters:
if not isinstance(parameters["include_metadata"], bool):
raise serializers.ValidationError("include_metadata must be a boolean")
else:
parameters["include_metadata"] = True
if parameters["update_document"]:
max_idx = max(op.get("doc", 0) for op in parameters["operations"])
if max_idx > 0:
raise serializers.ValidationError(
"update_document only allowed with a single output document",
)
def validate(self, attrs):
method = attrs["method"]

View File

@@ -1375,17 +1375,21 @@ class BulkEditView(PassUserMixin):
method in [bulk_edit.merge, bulk_edit.split]
and parameters["delete_originals"]
)
or (method == bulk_edit.edit_pdf and parameters["delete_original"])
or (method == bulk_edit.edit_pdf and parameters["update_document"])
):
has_perms = user_is_owner_of_all_documents
# check global add permissions for methods that create documents
if (
has_perms
and method in [bulk_edit.split, bulk_edit.merge, bulk_edit.edit_pdf]
and not user.has_perm(
"documents.add_document",
and (
method in [bulk_edit.split, bulk_edit.merge]
or (
method == bulk_edit.edit_pdf
and not parameters["update_document"]
)
)
and not user.has_perm("documents.add_document")
):
has_perms = False
@@ -1398,7 +1402,6 @@ class BulkEditView(PassUserMixin):
method in [bulk_edit.merge, bulk_edit.split]
and parameters["delete_originals"]
)
or (method == bulk_edit.edit_pdf and parameters["delete_original"])
)
and not user.has_perm("documents.delete_document")
):