disclaimer: I know I can use hooks to move files to specific location. I do so. This is not the case.
I have a script that creates a new file on my server. The file is placed at URL: /files/subfolder/new_file.pdf
. Once the file is created, I wish for it to become accessible through File
doctype, i.e. appear on the File Manager. So I have the following code:
import os
f_url = 'frontend/public/files/subfolder/' + serial_num + '.pdf'
doc = frappe.new_doc('File')
f_name = f_url.split("/")[-1]
doc.file_name = f_name
file_url = "/files/subfolder/" + f_name
doc.file_url = file_url
doc.insert()
However, the file is automatically (and unnecessarily) being copied to the /files/
folderand the file_url
field is changed to '/files/' + serial_num + '.pdf
’.
I can (and do) fix this by the addition of the following code:
name = frappe.db.get_value("File", {"file_name":f_name},'name')
frappe.db.set_value("File", name,'file_url','/files/subfolder/' + f_name)
frappe.db.set_value('Sales', serial_num,'attached_file', file_url)
frappe.db.commit()
os.remove(f_url.replace("/subfolder",""))
I call this a “bug” as the system does not perform as expected:
If I specify:
doc.file_name = f_name
This is the field value I wish to have.