fix: Copy attached_to_field and folder when amending documents
This commit is contained in:
parent
9dcbd870c4
commit
b781fa4ee3
3 changed files with 74 additions and 2 deletions
|
|
@ -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})
|
||||
|
|
|
|||
|
|
@ -182,7 +182,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},
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue