file-api: add new test, fix minor bugs and code indentation

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
This commit is contained in:
Chinmay Pai 2018-09-21 13:40:25 +05:30
parent 91aff48a19
commit 75c7992557
No known key found for this signature in database
GPG key ID: 75507BE256F40CED
13 changed files with 127 additions and 71 deletions

View file

@ -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()

View file

@ -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()

View file

@ -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

View file

@ -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"])

View file

@ -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": "",

View file

@ -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()

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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()

View file

@ -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()

View file

@ -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()

View file

@ -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