Merge pull request #37180 from frappe/32475-attached_to_field-and-folder-fields-not-copied-by-copy_attachments_from_amended_from-function

This commit is contained in:
Suraj Shetty 2026-02-24 15:54:40 +05:30 committed by GitHub
commit 0c9a55f4e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 74 additions and 2 deletions

View file

@ -641,6 +641,77 @@ class TestAttachment(IntegrationTestCase):
self.assertTrue(exists)
class TestCopyAttachmentsFromAmendedFrom(IntegrationTestCase):
"""Test that attached_to_field and folder are copied when amending a document."""
@classmethod
def setUpClass(cls):
super().setUpClass()
from frappe.core.doctype.doctype.test_doctype import new_doctype
cls.test_doctype = "Test Amendable Attachment"
new_doctype(
cls.test_doctype,
is_submittable=1,
fields=[
{"label": "Title", "fieldname": "title", "fieldtype": "Data"},
{"label": "Attachment", "fieldname": "attachment", "fieldtype": "Attach"},
],
).insert(ignore_if_duplicate=True)
@classmethod
def tearDownClass(cls):
frappe.db.rollback()
frappe.delete_doc_if_exists("DocType", cls.test_doctype)
def test_attached_to_field_and_folder_copied_on_amend(self):
# Create custom folder
custom_folder = frappe.get_doc(
{"doctype": "File", "file_name": "Test Amend Folder", "is_folder": 1, "folder": "Home"}
).insert()
# Create original document and attach file with attached_to_field and custom folder
doc = frappe.get_doc(doctype=self.test_doctype, title="Original").insert()
file = frappe.get_doc(
{
"doctype": "File",
"file_name": "amend_test_attach.txt",
"content": "Test Content",
"attached_to_doctype": self.test_doctype,
"attached_to_name": doc.name,
"attached_to_field": "attachment",
"folder": custom_folder.name,
}
).insert()
doc.attachment = file.file_url
doc.save()
# Submit and cancel
doc.submit()
doc.cancel()
# Amend document
amended_doc = frappe.copy_doc(doc)
amended_doc.docstatus = 0
amended_doc.amended_from = doc.name
amended_doc.save()
# Verify copied file has attached_to_field and folder from original
copied_files = frappe.get_all(
"File",
filters={
"attached_to_doctype": self.test_doctype,
"attached_to_name": amended_doc.name,
"file_name": "amend_test_attach.txt",
},
fields=["name", "attached_to_field", "folder"],
)
self.assertEqual(len(copied_files), 1, "Exactly one file should be copied to amended doc")
self.assertEqual(copied_files[0].attached_to_field, "attachment")
self.assertEqual(copied_files[0].folder, custom_folder.name)
class TestAttachmentsAccess(IntegrationTestCase):
def setUp(self) -> None:
frappe.db.delete("File", {"is_folder": 0})

View file

@ -185,7 +185,7 @@ def get_milestones(doctype, name):
def get_attachments(dt, dn):
return frappe.get_all(
"File",
fields=["name", "file_name", "file_url", "is_private"],
fields=["name", "file_name", "file_url", "is_private", "attached_to_field", "folder"],
filters={"attached_to_name": str(dn), "attached_to_doctype": dt},
)

View file

@ -598,7 +598,8 @@ class Document(BaseDocument):
"file_name": attach_item.file_name,
"attached_to_name": self.name,
"attached_to_doctype": self.doctype,
"folder": "Home/Attachments",
"attached_to_field": attach_item.attached_to_field,
"folder": attach_item.folder or "Home/Attachments",
"is_private": attach_item.is_private,
}
)