From 75c79925575e418e82083d6b1f22279c71d71a8f Mon Sep 17 00:00:00 2001 From: Chinmay Pai Date: Fri, 21 Sep 2018 13:40:25 +0530 Subject: [PATCH] file-api: add new test, fix minor bugs and code indentation Signed-off-by: Chinmay Pai --- frappe/client.py | 12 +++- frappe/core/doctype/communication/email.py | 7 ++- frappe/core/doctype/data_import/importer.py | 19 +++++-- frappe/core/doctype/file/file.py | 37 +++++++----- frappe/core/doctype/file/test_file.py | 56 ++++++++++--------- .../prepared_report/prepared_report.py | 10 +++- frappe/desk/page/setup_wizard/setup_wizard.py | 10 +++- frappe/email/receive.py | 6 +- frappe/handler.py | 4 +- .../gsuite_templates/gsuite_templates.py | 9 ++- frappe/model/document.py | 10 +++- frappe/twofactor.py | 8 ++- frappe/website/doctype/web_form/web_form.py | 10 +++- 13 files changed, 127 insertions(+), 71 deletions(-) diff --git a/frappe/client.py b/frappe/client.py index 9cb7735a95..d863419081 100644 --- a/frappe/client.py +++ b/frappe/client.py @@ -350,9 +350,15 @@ def attach_file(filename=None, filedata=None, doctype=None, docname=None, folder if not doc.has_permission(): frappe.throw(_("Not permitted"), frappe.PermissionError) - _file = frappe.get_doc({"doctype": "File", "file_name": filename, "attached_to_doctype": doctype, - "attached_to_name": docname, "attached_to_field": docfield, - "folder": folder, "is_private": is_private, "content": filedata, + _file = frappe.get_doc({ + "doctype": "File", + "file_name": filename, + "attached_to_doctype": doctype, + "attached_to_name": docname, + "attached_to_field": docfield, + "folder": folder, + "is_private": is_private, + "content": filedata, "decode": decode_base64}) _file.save() diff --git a/frappe/core/doctype/communication/email.py b/frappe/core/doctype/communication/email.py index 9faf329281..26a1030d00 100755 --- a/frappe/core/doctype/communication/email.py +++ b/frappe/core/doctype/communication/email.py @@ -404,8 +404,11 @@ def add_attachments(name, attachments): ["file_name", "file_url", "is_private"], as_dict=1) # save attachments to new doc - _file = frappe.get_doc("File", {"file_url": attach.file_url, - "attached_to_doctype": "Communication", "attached_to_name": name, + _file = frappe.get_doc({ + "doctype": "File", + "file_url": attach.file_url, + "attached_to_doctype": "Communication", + "attached_to_name": name, "folder": "Home/Attachments"}) _file.save() diff --git a/frappe/core/doctype/data_import/importer.py b/frappe/core/doctype/data_import/importer.py index 43d4ada865..c65e2fd921 100644 --- a/frappe/core/doctype/data_import/importer.py +++ b/frappe/core/doctype/data_import/importer.py @@ -264,8 +264,13 @@ def upload(rows = None, submit_after_import=None, ignore_encoding_errors=False, # file is already attached return - _file = frappe.get_doc("File", {"file_url": file_url, "attached_to_name": docname, - "attached_to_doctype": doctype, "attached_to_field": 0, "folder": "Home/Attachments"}) + _file = frappe.get_doc({ + "doctype": "File", + "file_url": file_url, + "attached_to_name": docname, + "attached_to_doctype": doctype, + "attached_to_field": 0, + "folder": "Home/Attachments"}) _file.save() @@ -465,9 +470,13 @@ def upload(rows = None, submit_after_import=None, ignore_encoding_errors=False, else: from frappe.utils.csvutils import to_csv file_data = to_csv(data_rows_with_error) - _file = frappe.get_doc({"doctype": "File", "file_name": file_name, - "attached_to_doctype": "Data Import", "attached_to_name": data_import_doc.name, - "folder": "Home/Attachments", "content": file_data}) + _file = frappe.get_doc({ + "doctype": "File", + "file_name": file_name, + "attached_to_doctype": "Data Import", + "attached_to_name": data_import_doc.name, + "folder": "Home/Attachments", + "content": file_data}) _file.save() data_import_doc.error_file = _file.file_url diff --git a/frappe/core/doctype/file/file.py b/frappe/core/doctype/file/file.py index 0f9254a9a2..7a45aed5d2 100755 --- a/frappe/core/doctype/file/file.py +++ b/frappe/core/doctype/file/file.py @@ -85,7 +85,8 @@ class File(NestedSet): self.validate_folder() if not self.flags.ignore_file_validate: - self.validate_file() + if not self.is_folder: + self.validate_file() self.generate_content_hash() self.set_folder_size() @@ -121,7 +122,8 @@ class File(NestedSet): break self.attached_to_field = field_name if self.attached_to_field: - frappe.db.set_value(self.attached_to_doctype, self.attached_to_name, self.attached_to_field, self.file_url) + frappe.db.set_value(self.attached_to_doctype, self.attached_to_name, + self.attached_to_field, self.file_url) def set_folder_size(self): @@ -180,7 +182,8 @@ class File(NestedSet): self.attached_to_name)) if len(n_records) > 0: self.duplicate_entry = n_records[0][0] - frappe.throw(frappe._("Same file has already been attached to the record"), frappe.DuplicateEntryError) + frappe.throw(frappe._("Same file has already been attached to the record"), + frappe.DuplicateEntryError) def generate_content_hash(self): if self.content_hash or not self.file_url: @@ -261,7 +264,9 @@ class File(NestedSet): if not self.flags.ignore_permissions and \ not frappe.has_permission(self.attached_to_doctype, "write", self.attached_to_name): - frappe.throw(frappe._("Cannot delete file as it belongs to {0} {1} for which you do not have permissions").format(self.attached_to_doctype, self.attached_to_name), + frappe.throw(frappe._( + "Cannot delete file as it belongs to {0} {1} for which you do not have permissions").format( + self.attached_to_doctype, self.attached_to_name), frappe.PermissionError) except frappe.DoesNotExistError: pass @@ -551,6 +556,7 @@ class File(NestedSet): def on_doctype_update(): frappe.db.add_index("File", ["attached_to_doctype", "attached_to_name"]) + frappe.db.add_index("File", ["lft", "rgt"]) def make_home_folder(): home = frappe.get_doc({ @@ -735,8 +741,10 @@ def remove_all(dt, dn, from_delete=False): def remove_file_by_url(file_url, doctype=None, name=None): if doctype and name: - fid = frappe.db.get_value("File", {"file_url": file_url, - "attached_to_doctype": doctype, "attached_to_name": name}) + fid = frappe.db.get_value("File", { + "file_url": file_url, + "attached_to_doctype": doctype, + "attached_to_name": name}) else: fid = frappe.db.get_value("File", {"file_url": file_url}) @@ -810,10 +818,15 @@ def extract_images_from_html(doc, content): doctype = doc.parenttype if doc.parent else doc.doctype name = doc.parent or doc.name - # TODO fix this - _file = frappe.get_doc({"doctype": "File", "file_name": filename, - "attached_to_doctype": doctype, "attached_to_name": name}) - file_url = _file.save_file(content=content, decode=True).file_url + _file = frappe.get_doc({ + "doctype": "File", + "file_name": filename, + "attached_to_doctype": doctype, + "attached_to_name": name, + "content": content, + "decode": True}) + _file.save() + file_url = _file.file_url if not frappe.flags.has_dataurl: frappe.flags.has_dataurl = True @@ -871,7 +884,3 @@ def validate_filename(filename): timestamp = now_datetime().strftime(" %Y-%m-%d %H:%M:%S") fname = get_file_name(filename, timestamp) return fname - - -def on_doctype_update(): - frappe.db.add_index("File", ["lft", "rgt"]) diff --git a/frappe/core/doctype/file/test_file.py b/frappe/core/doctype/file/test_file.py index e2fd89d943..b082bf250b 100644 --- a/frappe/core/doctype/file/test_file.py +++ b/frappe/core/doctype/file/test_file.py @@ -4,9 +4,11 @@ from __future__ import unicode_literals import frappe +import os import unittest from frappe import _ -from frappe.core.doctype.file.file import move_file, get_files_path +from frappe.core.doctype.file.file import move_file +from frappe.utils import get_files_path # test_records = frappe.get_test_records('File') test_content1 = 'Hello' @@ -24,17 +26,18 @@ class TestSimpleFile(unittest.TestCase): def setUp(self): self.attached_to_doctype, self.attached_to_docname = make_test_doc() self.test_content = test_content1 - _file = frappe.get_doc({"doctype": "File", + _file = frappe.get_doc({ + "doctype": "File", "file_name": "test1.txt", "attached_to_doctype": self.attached_to_doctype, "attached_to_name": self.attached_to_docname, "content": self.test_content}) _file.save() - self.saved_file_name = _file.file_name + self.saved_file_url = _file.file_url def test_save(self): - _file = frappe.get_doc("File", {"file_name": self.saved_file_name}) + _file = frappe.get_doc("File", {"file_url": self.saved_file_url}) content = _file.get_content() self.assertEqual(content, self.test_content) @@ -49,26 +52,28 @@ class TestSameFileName(unittest.TestCase): self.attached_to_doctype, self.attached_to_docname = make_test_doc() self.test_content1 = test_content1 self.test_content2 = test_content2 - _file1 = frappe.get_doc({"doctype": "File", + _file1 = frappe.get_doc({ + "doctype": "File", "file_name": "testing.txt", "attached_to_doctype": self.attached_to_doctype, "attached_to_name": self.attached_to_docname, "content": self.test_content1}) - _file2 = frappe.get_doc({"doctype": "File", + _file1.save() + _file2 = frappe.get_doc({ + "doctype": "File", "file_name": "testing.txt", "attached_to_doctype": self.attached_to_doctype, "attached_to_name": self.attached_to_docname, "content": self.test_content2}) - _file1.save() _file2.save() - self.saved_filename1 = _file1.file_name - self.saved_filename2 = _file2.file_name + self.saved_file_url1 = _file1.file_url + self.saved_file_url2 = _file2.file_url def test_saved_content(self): - _file = frappe.get_doc("File", {"file_name": self.saved_filename1}) + _file = frappe.get_doc("File", {"file_url": self.saved_file_url1}) content1 = _file.get_content() self.assertEqual(content1, self.test_content1) - _file = frappe.get_doc("File", {"file_name": self.saved_filename2}) + _file = frappe.get_doc("File", {"file_url": self.saved_file_url2}) content2 = _file.get_content() self.assertEqual(content2, self.test_content2) @@ -86,30 +91,25 @@ class TestSameContent(unittest.TestCase): self.test_content2 = test_content1 self.orig_filename = 'hello.txt' self.dup_filename = 'hello2.txt' - _file1 = frappe.get_doc({"doctype": "File", + _file1 = frappe.get_doc({ + "doctype": "File", "file_name": self.orig_filename, "attached_to_doctype": self.attached_to_doctype1, "attached_to_name": self.attached_to_docname1, "content": self.test_content1}) _file1.save() - _file2 = frappe.get_doc({"doctype": "File", + _file2 = frappe.get_doc({ + "doctype": "File", "file_name": self.dup_filename, "attached_to_doctype": self.attached_to_doctype2, "attached_to_name": self.attached_to_docname2, "content": self.test_content2}) - + _file2.save() - self.saved_filename1 = _file1.file_name - self.saved_filename2 = _file2.file_name def test_saved_content(self): - pass - # _file1 = frappe.get_doc("File", {"file_name": self.saved_filename1}) - # filename1 = _file1.file_name - # _file2 = frappe.get_doc("File", {"file_name": self.saved_filename2}) - # filename2 = _file2.file_name - # self.assertFalse(os.path.exists(get_files_path(self.dup_filename))) + self.assertFalse(os.path.exists(get_files_path(self.dup_filename))) def tearDown(self): # File gets deleted on rollback, so blank @@ -133,7 +133,8 @@ class TestFile(unittest.TestCase): frappe.delete_doc("File", f[0]) def upload_file(self): - _file = frappe.get_doc({"doctype": "File", + _file = frappe.get_doc({ + "doctype": "File", "file_name": "file_copy.txt", "attached_to_name": "", "attached_to_doctype": "", @@ -174,7 +175,8 @@ class TestFile(unittest.TestCase): def test_folder_copy(self): folder = self.get_folder("Test Folder 2", "Home") folder = self.get_folder("Test Folder 3", "Home/Test Folder 2") - _file = frappe.get_doc({"doctype": "File", + _file = frappe.get_doc({ + "doctype": "File", "file_name": "folder_copy.txt", "attached_to_name": "", "attached_to_doctype": "", @@ -210,7 +212,8 @@ class TestFile(unittest.TestCase): self.assertEqual(frappe.db.get_value("File", _("Home/Test Folder 1"), "file_size"), 0) folder = self.get_folder("Test Folder 3", "Home/Test Folder 1") - _file = frappe.get_doc({"doctype": "File", + _file = frappe.get_doc({ + "doctype": "File", "file_name": "folder_copy.txt", "attached_to_name": "", "attached_to_doctype": "", @@ -239,7 +242,8 @@ class TestFile(unittest.TestCase): # Rebuild the frappe.local.conf to take up the changes from site_config frappe.local.conf = _dict(frappe.get_site_config()) - _file = frappe.get_doc({"doctype": "File", + _file = frappe.get_doc({ + "doctype": "File", "file_name": "_test_max_space.txt", "attached_to_name": "", "attached_to_doctype": "", diff --git a/frappe/core/doctype/prepared_report/prepared_report.py b/frappe/core/doctype/prepared_report/prepared_report.py index ec4b883f78..78afb3c183 100644 --- a/frappe/core/doctype/prepared_report/prepared_report.py +++ b/frappe/core/doctype/prepared_report/prepared_report.py @@ -61,9 +61,13 @@ def create_csv_file(columns, data, dt, dn): csv_filename = '{0}.csv'.format(frappe.utils.data.format_datetime(frappe.utils.now(), "Y-m-d-H:M")) rows = [tuple(columns)] + data encoded = base64.b64encode(frappe.safe_encode(to_csv(rows))) - # Call save_file function to upload and attach the file - _file = frappe.get_doc({"doctype": "File", "file_name": csv_filename, - "attached_to_doctype": dt, "attached_to_name": dn, "content": encoded, + # Call save() file function to upload and attach the file + _file = frappe.get_doc({ + "doctype": "File", + "file_name": csv_filename, + "attached_to_doctype": dt, + "attached_to_name": dn, + "content": encoded, "decode": True}) _file.save() diff --git a/frappe/desk/page/setup_wizard/setup_wizard.py b/frappe/desk/page/setup_wizard/setup_wizard.py index 1bf04893f5..82f44ee249 100755 --- a/frappe/desk/page/setup_wizard/setup_wizard.py +++ b/frappe/desk/page/setup_wizard/setup_wizard.py @@ -186,9 +186,13 @@ def update_user_name(args): attach_user = args.get("attach_user").split(",") if len(attach_user)==3: filename, filetype, content = attach_user - _file = frappe.get_doc({"doctype": "File", "file_name": filename, - "attached_to_doctype": "User", "attached_to_docname": args.get("name"), - "content": content, "decode": True}) + _file = frappe.get_doc({ + "doctype": "File", + "file_name": filename, + "attached_to_doctype": "User", + "attached_to_name": args.get("name"), + "content": content, + "decode": True}) _file.save() fileurl = _file.file_url frappe.db.set_value("User", args.get("name"), "user_image", fileurl) diff --git a/frappe/email/receive.py b/frappe/email/receive.py index 2b977cdfa8..ffee8dc17a 100644 --- a/frappe/email/receive.py +++ b/frappe/email/receive.py @@ -523,10 +523,12 @@ class Email: for attachment in self.attachments: try: - _file = frappe.get_doc({"doctype": "File", + _file = frappe.get_doc({ + "doctype": "File", "file_name": attachment['fname'], "attached_to_doctype": doc.doctype, - "attached_to_name": doc.name, "is_private": 1, + "attached_to_name": doc.name, + "is_private": 1, "content": attachment['fcontent']}) _file.save() saved_attachments.append(_file) diff --git a/frappe/handler.py b/frappe/handler.py index 642966b1b2..7a040871ad 100755 --- a/frappe/handler.py +++ b/frappe/handler.py @@ -121,9 +121,7 @@ def uploadfile(): "content": frappe.form_dict.filedata, "decode": True }) - ret = ret.save() - # ret = frappe.get_doc({"doctype": "File"}) - # ret = ret.upload() + ret.save() except frappe.DuplicateEntryError: # ignore pass ret = None diff --git a/frappe/integrations/doctype/gsuite_templates/gsuite_templates.py b/frappe/integrations/doctype/gsuite_templates/gsuite_templates.py index bd298004a7..83f379ee29 100644 --- a/frappe/integrations/doctype/gsuite_templates/gsuite_templates.py +++ b/frappe/integrations/doctype/gsuite_templates/gsuite_templates.py @@ -24,8 +24,13 @@ def create_gsuite_doc(doctype, docname, gs_template=None): r = run_gsuite_script('new', filename, templ.template_id, templ.destination_id, json_data) - _file = frappe.get_doc("File", {"file_url": r['url'], "file_name": filename, - "attached_to_doctype": doctype, "attached_to_name": docname, "attached_to_field": True, + _file = frappe.get_doc({ + "doctype": "File", + "file_url": r['url'], + "file_name": filename, + "attached_to_doctype": doctype, + "attached_to_name": docname, + "attached_to_field": True, "folder": "Home/Attachments"}) _file.save() diff --git a/frappe/model/document.py b/frappe/model/document.py index c35b1a91de..08054a0c33 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -319,9 +319,13 @@ class Document(BaseDocument): for attach_item in get_attachments(self.doctype, self.amended_from): #save attachments to new doc - _file = frappe.get_doc("File", {"file_url": attach_item.file_url, - "file_name": attach_item.file_name, "attached_to_name": self.name, - "attached_to_doctype": self.doctype, "folder": "Home/Attachments"}) + _file = frappe.get_doc({ + "doctype": "File", + "file_url": attach_item.file_url, + "file_name": attach_item.file_name, + "attached_to_name": self.name, + "attached_to_doctype": self.doctype, + "folder": "Home/Attachments"}) _file.save() diff --git a/frappe/twofactor.py b/frappe/twofactor.py index dbc138e2c2..4ff721067e 100644 --- a/frappe/twofactor.py +++ b/frappe/twofactor.py @@ -335,8 +335,12 @@ def qrcode_as_png(user, totp_uri): '''Save temporary Qrcode to server.''' folder = create_barcode_folder() png_file_name = '{}.png'.format(frappe.generate_hash(length=20)) - _file = frappe.get_doc({"doctype": "File", "file_name": png_file_name, - "attached_to_doctype": 'User', "attached_to_name": user, "folder": folder, + _file = frappe.get_doc({ + "doctype": "File", + "file_name": png_file_name, + "attached_to_doctype": 'User', + "attached_to_name": user, + "folder": folder, "content": png_file_name}) _file.save() frappe.db.commit() diff --git a/frappe/website/doctype/web_form/web_form.py b/frappe/website/doctype/web_form/web_form.py index 77f20b8b77..c2629f3a92 100644 --- a/frappe/website/doctype/web_form/web_form.py +++ b/frappe/website/doctype/web_form/web_form.py @@ -420,9 +420,13 @@ def accept(web_form, data, for_payment=False): # save new file filename, dataurl = filedata.split(',', 1) - _file = frappe.get_doc({"doctype": "File", "file_name": filename, - "attached_to_doctype": doc.doctype, "attached_to_name": doc.name, - "content": dataurl, "decode": True}) + _file = frappe.get_doc({ + "doctype": "File", + "file_name": filename, + "attached_to_doctype": doc.doctype, + "attached_to_name": doc.name, + "content": dataurl, + "decode": True}) _file.save() # update values