api.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import json
  2. import frappe
  3. from frappe.utils.file_manager import save_url
  4. @frappe.whitelist()
  5. def attach_all_docs(document, method=None):
  6. """This function attaches drawings to the purchase order based on the items being ordered"""
  7. document = json.loads(document)
  8. count = 0
  9. for item_doc in document["items"]:
  10. item = frappe.get_doc("Item",item_doc["item_code"])
  11. attachments = []
  12. # Get the path for the attachments
  13. if item.item_attachment_1:
  14. attachments.append(item.item_attachment_1)
  15. if item.item_attachment_2:
  16. attachments.append(item.item_attachment_2)
  17. if item.item_attachment_3:
  18. attachments.append(item.item_attachment_3)
  19. if item.item_attachment_4:
  20. attachments.append(item.item_attachment_4)
  21. for attach in attachments:
  22. count = count + 1
  23. save_url(attach, attach.split("/")[-1], document["doctype"], document["name"], "Home/Attachments", True)
  24. frappe.msgprint("Attached {0} files".format(count))