Merge pull request #11122 from netchampfaris/attach-files-to-doc-on-update

fix: Attach files to the document if they are set programmatically
This commit is contained in:
mergify[bot] 2020-08-24 06:22:08 +00:00 committed by GitHub
commit 6032d91c1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 1 deletions

View file

@ -922,3 +922,40 @@ def update_existing_file_docs(doc):
content_hash=doc.content_hash,
file_name=doc.name
))
def attach_files_to_document(doc, event):
""" Runs on on_update hook of all documents.
Goes through every Attach and Attach Image field and attaches
the file url to the document if it is not already attached.
"""
attach_fields = doc.meta.get(
"fields", {"fieldtype": ["in", ["Attach", "Attach Image"]]}
)
for df in attach_fields:
# this method runs in on_update hook of all documents
# we dont want the update to fail if file cannot be attached for some reason
try:
value = doc.get(df.fieldname)
if not value.startswith(("/files", "/private/files")):
return
if frappe.db.exists("File", {
"file_url": value,
"attached_to_name": doc.name,
"attached_to_doctype": doc.doctype,
"attached_to_field": df.fieldname,
}):
return
frappe.get_doc(
doctype="File",
file_url=value,
attached_to_name=doc.name,
attached_to_doctype=doc.doctype,
attached_to_field=df.fieldname,
folder="Home/Attachments",
).insert()
except Exception:
frappe.log_error(title=_("Error Attaching File"))

View file

@ -328,3 +328,49 @@ class TestFile(unittest.TestCase):
self.assertTrue(os.path.exists(file2.get_full_path()))
class TestAttachment(unittest.TestCase):
test_doctype = 'Test For Attachment'
def setUp(self):
if frappe.db.exists('DocType', self.test_doctype):
return
frappe.get_doc(
doctype='DocType',
name=self.test_doctype,
module='Custom',
custom=1,
fields=[
{'label': 'Title', 'fieldname': 'title', 'fieldtype': 'Data'},
{'label': 'Attachment', 'fieldname': 'attachment', 'fieldtype': 'Attach'},
]
).insert()
def tearDown(self):
frappe.delete_doc('DocType', self.test_doctype)
def test_file_attachment_on_update(self):
doc = frappe.get_doc(
doctype=self.test_doctype,
title='test for attachment on update'
).insert()
file = frappe.get_doc({
'doctype': 'File',
'file_name': 'test_attach.txt',
'content': 'Test Content'
})
file.save()
doc.attachment = file.file_url
doc.save()
exists = frappe.db.exists('File', {
'file_name': 'test_attach.txt',
'file_url': file.file_url,
'attached_to_doctype': self.test_doctype,
'attached_to_name': doc.name,
'attached_to_field': 'attachment'
})
self.assertTrue(exists)

View file

@ -136,7 +136,8 @@ doc_events = {
"frappe.workflow.doctype.workflow_action.workflow_action.process_workflow_actions",
"frappe.automation.doctype.assignment_rule.assignment_rule.apply",
"frappe.automation.doctype.milestone_tracker.milestone_tracker.evaluate_milestone",
"frappe.event_streaming.doctype.event_update_log.event_update_log.notify_consumers"
"frappe.core.doctype.file.file.attach_files_to_document",
"frappe.event_streaming.doctype.event_update_log.event_update_log.notify_consumers",
],
"after_rename": "frappe.desk.notifications.clear_doctype_notifications",
"on_cancel": [

View file

@ -144,6 +144,9 @@ export default {
docname: {
default: null
},
fieldname: {
default: null
},
folder: {
default: 'Home'
},
@ -406,6 +409,10 @@ export default {
form_data.append('docname', this.docname);
}
if (this.fieldname) {
form_data.append('fieldname', this.fieldname);
}
if (this.method) {
form_data.append('method', this.method);
}

View file

@ -7,6 +7,7 @@ export default class FileUploader {
on_success,
doctype,
docname,
fieldname,
files,
folder,
restrictions,
@ -28,6 +29,7 @@ export default class FileUploader {
show_upload_button: !Boolean(this.dialog),
doctype,
docname,
fieldname,
method,
folder,
on_success,

View file

@ -66,6 +66,7 @@ frappe.ui.form.ControlAttach = frappe.ui.form.ControlData.extend({
if (this.frm) {
options.doctype = this.frm.doctype;
options.docname = this.frm.docname;
options.fieldname = this.df.fieldname;
}
if (this.df.options) {