Date: Tue, 4 Mar 2014 00:26:17 +0530
Subject: [PATCH 04/15] show correct name for file data link
---
frappe/public/js/frappe/form/attachments.js | 3 +++
1 file changed, 3 insertions(+)
diff --git a/frappe/public/js/frappe/form/attachments.js b/frappe/public/js/frappe/form/attachments.js
index d6637f5158..1d9f7248ec 100644
--- a/frappe/public/js/frappe/form/attachments.js
+++ b/frappe/public/js/frappe/form/attachments.js
@@ -63,6 +63,9 @@ frappe.ui.form.Attachments = Class.extend({
var file_name = attachment.file_name;
var file_url = attachment.file_url;
var fileid = attachment.name;
+ if (!file_name) {
+ file_name = file_url;
+ }
var me = this;
var $attach = $(repl('\
From 5103366eb9e824209b3ecc4cde2af742122e99e6 Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Tue, 4 Mar 2014 00:27:07 +0530
Subject: [PATCH 05/15] add filemanager patch
---
frappe/patches.txt | 1 +
frappe/patches/4_0/file_manager_hooks.py | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+)
create mode 100644 frappe/patches/4_0/file_manager_hooks.py
diff --git a/frappe/patches.txt b/frappe/patches.txt
index bcb5b9efa3..ffc14b4433 100644
--- a/frappe/patches.txt
+++ b/frappe/patches.txt
@@ -24,3 +24,4 @@ frappe.patches.4_0.remove_old_parent
frappe.patches.4_0.rename_profile_to_user
frappe.patches.4_0.update_datetime
frappe.patches.4_0.deprecate_control_panel
+frappe.patches.4_0.file_manager_hooks
diff --git a/frappe/patches/4_0/file_manager_hooks.py b/frappe/patches/4_0/file_manager_hooks.py
new file mode 100644
index 0000000000..080db3241f
--- /dev/null
+++ b/frappe/patches/4_0/file_manager_hooks.py
@@ -0,0 +1,22 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# MIT License. See license.txt
+
+from __future__ import unicode_literals
+
+import frappe
+import os
+from frappe.utils import get_files_path
+
+def execute():
+ for name, file_name, file_url in frappe.db.sql(
+ """select name, file_name, file_url from `tabFile Data`
+ where file_name is not null"""):
+ b = frappe.bean('File Data', name)
+ old_file_name = b.doc.file_name
+ b.doc.file_name = os.path.basename(old_file_name)
+ if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
+ b.doc.file_url = os.path.normpath('/' + old_file_name)
+ else:
+ b.doc.file_url = os.path.normpath('/files/' + old_file_name)
+ b.save()
+
From e30dfd4ed07e4b42b9176c93eec7617d6f9e7faa Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Wed, 5 Mar 2014 16:59:45 +0530
Subject: [PATCH 06/15] file deduplication
---
frappe/core/doctype/file_data/file_data.py | 4 ++--
frappe/hooks.txt | 2 ++
frappe/patches/4_0/file_manager_hooks.py | 3 +++
frappe/utils/file_manager.py | 15 ++++++++++++---
4 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/frappe/core/doctype/file_data/file_data.py b/frappe/core/doctype/file_data/file_data.py
index 91fb3cb6fd..9e8bd0c156 100644
--- a/frappe/core/doctype/file_data/file_data.py
+++ b/frappe/core/doctype/file_data/file_data.py
@@ -44,8 +44,8 @@ class FileData(Document):
pass
# if file not attached to any other record, delete it
- if self.file_name and not frappe.db.count("File Data",
- {"file_name": self.file_name, "name": ["!=", self.name]}):
+ if self.doc.file_name and not frappe.db.count("File Data",
+ {"content_hash": self.content_hash, "name": ["!=", self.name]}):
delete_file_data_content(self)
def on_rollback(self):
diff --git a/frappe/hooks.txt b/frappe/hooks.txt
index c3aff8d520..dbc5a5b071 100644
--- a/frappe/hooks.txt
+++ b/frappe/hooks.txt
@@ -48,3 +48,5 @@ doc_event:Website Route Permission:on_update = frappe.templates.generators.websi
doc_event:*:on_update = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications
doc_event:*:on_cancel = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications
doc_event:*:on_trash = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications
+
+write_file_keys = file_url
diff --git a/frappe/patches/4_0/file_manager_hooks.py b/frappe/patches/4_0/file_manager_hooks.py
index 080db3241f..f5e83ec69b 100644
--- a/frappe/patches/4_0/file_manager_hooks.py
+++ b/frappe/patches/4_0/file_manager_hooks.py
@@ -6,6 +6,7 @@ from __future__ import unicode_literals
import frappe
import os
from frappe.utils import get_files_path
+from frappe.utils.filemanager import get_content_hash, get_file
def execute():
for name, file_name, file_url in frappe.db.sql(
@@ -18,5 +19,7 @@ def execute():
b.doc.file_url = os.path.normpath('/' + old_file_name)
else:
b.doc.file_url = os.path.normpath('/files/' + old_file_name)
+ _file_name, content = get_file(file_name)
+ b.doc.content_hash = get_content_hash(content)
b.save()
diff --git a/frappe/utils/file_manager.py b/frappe/utils/file_manager.py
index 3557d7ea90..c4a8d78647 100644
--- a/frappe/utils/file_manager.py
+++ b/frappe/utils/file_manager.py
@@ -107,14 +107,17 @@ def save_file(fname, content, dt, dn, decode=False):
method = get_hook_method('write_file', fallback=save_file_on_filesystem)
- file_data = method(fname, content, content_type=content_type)
- file_data = copy(file_data)
+ file_data = get_file_data_from_hash(content_hash)
+ if not file_data:
+ file_data = method(fname, content, content_type=content_type)
+ file_data = copy(file_data)
file_data.update({
"doctype": "File Data",
"attached_to_doctype": dt,
"attached_to_name": dn,
"file_size": file_size,
- "file_hash": content_hash
+ "file_hash": content_hash,
+ "file_name": fname
})
f = frappe.bean(file_data)
@@ -125,6 +128,12 @@ def save_file(fname, content, dt, dn, decode=False):
return frappe.doc("File Data", f.doc.duplicate_entry)
return f.doc
+
+def get_file_data_from_hash(content_hash):
+ for name in frappe.db.sql_list("select name from `tabFile Data` where content_hash='{}'".format(content_hash)):
+ b = frappe.bean('File Data', name)
+ return {k:b.doc.fields[k] for k in frappe.get_hooks()['write_file_keys']}
+ return False
def save_file_on_filesystem(fname, content, content_type=None):
import filecmp
From 0791292d34e98647010088006d240571c7015afd Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Mon, 7 Apr 2014 13:01:50 +0530
Subject: [PATCH 07/15] schema changes for file manager hooks
---
frappe/core/doctype/file_data/file_data.json | 201 ++++++++++++++++++-
1 file changed, 192 insertions(+), 9 deletions(-)
diff --git a/frappe/core/doctype/file_data/file_data.json b/frappe/core/doctype/file_data/file_data.json
index d416ebbb3f..a3b056ca32 100644
--- a/frappe/core/doctype/file_data/file_data.json
+++ b/frappe/core/doctype/file_data/file_data.json
@@ -1,72 +1,255 @@
{
+ "_last_update": null,
+ "_user_tags": null,
+ "allow_attach": null,
+ "allow_copy": null,
+ "allow_email": null,
+ "allow_import": null,
+ "allow_print": null,
+ "allow_rename": null,
+ "allow_trash": null,
"autoname": "File.######",
- "creation": "2012-12-12 11:19:22.000000",
+ "change_log": null,
+ "client_script": null,
+ "client_script_core": null,
+ "client_string": null,
+ "colour": null,
+ "creation": "2012-12-12 11:19:22",
+ "custom": null,
+ "default_print_format": null,
+ "description": null,
"docstatus": 0,
"doctype": "DocType",
+ "document_type": null,
+ "dt_template": null,
"fields": [
{
+ "allow_on_submit": null,
+ "default": null,
+ "depends_on": null,
+ "description": null,
"fieldname": "file_name",
"fieldtype": "Data",
+ "hidden": null,
+ "ignore_restrictions": null,
+ "in_filter": null,
+ "in_list_view": null,
"label": "File Name",
+ "no_column": null,
+ "no_copy": null,
"oldfieldname": "file_name",
"oldfieldtype": "Data",
+ "options": null,
"permlevel": 0,
- "read_only": 1
+ "print_hide": null,
+ "print_width": null,
+ "read_only": 1,
+ "report_hide": null,
+ "reqd": null,
+ "search_index": null,
+ "set_only_once": null,
+ "trigger": null,
+ "width": null
},
{
+ "allow_on_submit": null,
+ "default": null,
+ "depends_on": null,
+ "description": null,
"fieldname": "file_url",
"fieldtype": "Data",
+ "hidden": null,
+ "ignore_restrictions": null,
+ "in_filter": null,
"in_list_view": 1,
"label": "File URL",
+ "no_column": null,
+ "no_copy": null,
+ "oldfieldname": null,
+ "oldfieldtype": null,
+ "options": null,
"permlevel": 0,
- "read_only": 1
+ "print_hide": null,
+ "print_width": null,
+ "read_only": 1,
+ "report_hide": null,
+ "reqd": null,
+ "search_index": null,
+ "set_only_once": null,
+ "trigger": null,
+ "width": null
},
{
+ "allow_on_submit": null,
+ "default": null,
+ "depends_on": null,
+ "description": null,
"fieldname": "attached_to_doctype",
"fieldtype": "Link",
+ "hidden": null,
+ "ignore_restrictions": null,
+ "in_filter": null,
"in_list_view": 1,
"label": "Attached To DocType",
+ "no_column": null,
+ "no_copy": null,
+ "oldfieldname": null,
+ "oldfieldtype": null,
"options": "DocType",
"permlevel": 0,
+ "print_hide": null,
+ "print_width": null,
"read_only": 1,
- "search_index": 1
+ "report_hide": null,
+ "reqd": null,
+ "search_index": 1,
+ "set_only_once": null,
+ "trigger": null,
+ "width": null
},
{
+ "allow_on_submit": null,
+ "default": null,
+ "depends_on": null,
+ "description": null,
"fieldname": "attached_to_name",
"fieldtype": "Data",
+ "hidden": null,
+ "ignore_restrictions": null,
+ "in_filter": null,
"in_list_view": 1,
"label": "Attached To Name",
+ "no_column": null,
+ "no_copy": null,
+ "oldfieldname": null,
+ "oldfieldtype": null,
+ "options": null,
"permlevel": 0,
+ "print_hide": null,
+ "print_width": null,
"read_only": 1,
- "search_index": 1
+ "report_hide": null,
+ "reqd": null,
+ "search_index": 1,
+ "set_only_once": null,
+ "trigger": null,
+ "width": null
},
{
+ "allow_on_submit": null,
+ "default": null,
+ "depends_on": null,
+ "description": null,
"fieldname": "file_size",
"fieldtype": "Int",
+ "hidden": null,
+ "ignore_restrictions": null,
+ "in_filter": null,
"in_list_view": 1,
"label": "File Size",
+ "no_column": null,
+ "no_copy": null,
+ "oldfieldname": null,
+ "oldfieldtype": null,
+ "options": null,
"permlevel": 0,
- "read_only": 1
+ "print_hide": null,
+ "print_width": null,
+ "read_only": 1,
+ "report_hide": null,
+ "reqd": null,
+ "search_index": null,
+ "set_only_once": null,
+ "trigger": null,
+ "width": null
+ },
+ {
+ "allow_on_submit": null,
+ "default": null,
+ "depends_on": null,
+ "description": null,
+ "fieldname": "file_hash",
+ "fieldtype": "Data",
+ "hidden": null,
+ "ignore_restrictions": null,
+ "in_filter": null,
+ "in_list_view": null,
+ "label": "File Hash",
+ "no_column": null,
+ "no_copy": null,
+ "oldfieldname": null,
+ "oldfieldtype": null,
+ "options": null,
+ "permlevel": 0,
+ "print_hide": null,
+ "print_width": null,
+ "read_only": null,
+ "report_hide": null,
+ "reqd": null,
+ "search_index": 1,
+ "set_only_once": null,
+ "trigger": null,
+ "width": null
}
],
+ "hide_heading": null,
+ "hide_toolbar": null,
"icon": "icon-file",
"idx": 1,
- "modified": "2014-01-20 17:48:46.000000",
+ "in_create": null,
+ "in_dialog": null,
+ "is_submittable": null,
+ "is_transaction_doc": null,
+ "issingle": null,
+ "istable": null,
+ "max_attachments": null,
+ "menu_index": null,
+ "modified": "2014-04-07 13:00:57.412982",
"modified_by": "Administrator",
"module": "Core",
"name": "File Data",
+ "name_case": null,
"owner": "Administrator",
+ "parent": null,
+ "parent_node": null,
+ "parentfield": null,
+ "parenttype": null,
"permissions": [
{
- "cancel": 1,
+ "amend": null,
+ "cancel": 0,
+ "create": null,
"delete": 1,
"email": 1,
+ "export": null,
+ "import": null,
+ "match": null,
"permlevel": 0,
"print": 1,
"read": 1,
+ "report": null,
+ "restrict": null,
+ "restricted": null,
"role": "System Manager",
+ "submit": null,
"write": 1
}
],
- "read_only": 0
+ "plugin": null,
+ "print_outline": null,
+ "read_only": 0,
+ "read_only_onload": null,
+ "search_fields": null,
+ "section_style": null,
+ "server_code": null,
+ "server_code_compiled": null,
+ "server_code_core": null,
+ "server_code_error": null,
+ "show_in_menu": null,
+ "smallicon": null,
+ "subject": null,
+ "tag_fields": null,
+ "title_field": null,
+ "use_template": null,
+ "version": null
}
\ No newline at end of file
From 66c7de1799597a83f170ffc38bad13aaf7849732 Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Mon, 7 Apr 2014 16:58:53 +0530
Subject: [PATCH 08/15] fixes for new model
---
frappe/core/doctype/file_data/file_data.json | 6 +++---
frappe/core/doctype/file_data/file_data.py | 4 ++--
frappe/hooks.txt | 1 +
frappe/patches/4_0/file_manager_hooks.py | 12 ++++++------
frappe/utils/file_manager.py | 17 ++++++++---------
5 files changed, 20 insertions(+), 20 deletions(-)
diff --git a/frappe/core/doctype/file_data/file_data.json b/frappe/core/doctype/file_data/file_data.json
index a3b056ca32..3feee0c22f 100644
--- a/frappe/core/doctype/file_data/file_data.json
+++ b/frappe/core/doctype/file_data/file_data.json
@@ -168,13 +168,13 @@
"default": null,
"depends_on": null,
"description": null,
- "fieldname": "file_hash",
+ "fieldname": "content_hash",
"fieldtype": "Data",
"hidden": null,
"ignore_restrictions": null,
"in_filter": null,
"in_list_view": null,
- "label": "File Hash",
+ "label": "Content Hash",
"no_column": null,
"no_copy": null,
"oldfieldname": null,
@@ -204,7 +204,7 @@
"istable": null,
"max_attachments": null,
"menu_index": null,
- "modified": "2014-04-07 13:00:57.412982",
+ "modified": "2014-04-07 13:05:47.684017",
"modified_by": "Administrator",
"module": "Core",
"name": "File Data",
diff --git a/frappe/core/doctype/file_data/file_data.py b/frappe/core/doctype/file_data/file_data.py
index 9e8bd0c156..ec8fd70d06 100644
--- a/frappe/core/doctype/file_data/file_data.py
+++ b/frappe/core/doctype/file_data/file_data.py
@@ -35,7 +35,7 @@ class FileData(Document):
if self.attached_to_name:
# check persmission
try:
- if not self.ignore_permissions and \
+ if not getattr(self, 'ignore_permissions', False) and \
not frappe.has_permission(self.attached_to_doctype, "write", self.attached_to_name):
frappe.msgprint(frappe._("No permission to write / remove."), raise_exception=True)
@@ -44,7 +44,7 @@ class FileData(Document):
pass
# if file not attached to any other record, delete it
- if self.doc.file_name and not frappe.db.count("File Data",
+ if self.file_name and not frappe.db.count("File Data",
{"content_hash": self.content_hash, "name": ["!=", self.name]}):
delete_file_data_content(self)
diff --git a/frappe/hooks.txt b/frappe/hooks.txt
index dbc5a5b071..5feb017e48 100644
--- a/frappe/hooks.txt
+++ b/frappe/hooks.txt
@@ -50,3 +50,4 @@ doc_event:*:on_cancel = frappe.core.doctype.notification_count.notification_coun
doc_event:*:on_trash = frappe.core.doctype.notification_count.notification_count.clear_doctype_notifications
write_file_keys = file_url
+write_file_keys = file_name
diff --git a/frappe/patches/4_0/file_manager_hooks.py b/frappe/patches/4_0/file_manager_hooks.py
index f5e83ec69b..7413d4a526 100644
--- a/frappe/patches/4_0/file_manager_hooks.py
+++ b/frappe/patches/4_0/file_manager_hooks.py
@@ -12,14 +12,14 @@ def execute():
for name, file_name, file_url in frappe.db.sql(
"""select name, file_name, file_url from `tabFile Data`
where file_name is not null"""):
- b = frappe.bean('File Data', name)
- old_file_name = b.doc.file_name
- b.doc.file_name = os.path.basename(old_file_name)
+ b = frappe.get_doc('File Data', name)
+ old_file_name = b.file_name
+ b.file_name = os.path.basename(old_file_name)
if old_file_name.startswith('files/') or old_file_name.startswith('/files/'):
- b.doc.file_url = os.path.normpath('/' + old_file_name)
+ b.file_url = os.path.normpath('/' + old_file_name)
else:
- b.doc.file_url = os.path.normpath('/files/' + old_file_name)
+ b.file_url = os.path.normpath('/files/' + old_file_name)
_file_name, content = get_file(file_name)
- b.doc.content_hash = get_content_hash(content)
+ b.content_hash = get_content_hash(content)
b.save()
diff --git a/frappe/utils/file_manager.py b/frappe/utils/file_manager.py
index c4a8d78647..06dea9ee33 100644
--- a/frappe/utils/file_manager.py
+++ b/frappe/utils/file_manager.py
@@ -83,6 +83,7 @@ def extract_images_from_html(doc, fieldname):
data = match.group(1)
headers, content = data.split(",")
filename = headers.split("filename=")[-1]
+ # TODO fix this
file_url = save_file(filename, content, doc.doctype, doc.name, decode=True).get("file_url")
if not frappe.flags.has_dataurl:
frappe.flags.has_dataurl = True
@@ -116,23 +117,21 @@ def save_file(fname, content, dt, dn, decode=False):
"attached_to_doctype": dt,
"attached_to_name": dn,
"file_size": file_size,
- "file_hash": content_hash,
- "file_name": fname
+ "content_hash": content_hash,
})
- f = frappe.bean(file_data)
+ f = frappe.get_doc(file_data)
f.ignore_permissions = True
try:
f.insert();
except frappe.DuplicateEntryError:
- return frappe.doc("File Data", f.doc.duplicate_entry)
-
- return f.doc
+ return frappe.get_doc("File Data", f.duplicate_entry)
+ return f
def get_file_data_from_hash(content_hash):
for name in frappe.db.sql_list("select name from `tabFile Data` where content_hash='{}'".format(content_hash)):
- b = frappe.bean('File Data', name)
- return {k:b.doc.fields[k] for k in frappe.get_hooks()['write_file_keys']}
+ b = frappe.get_doc('File Data', name)
+ return {k:b.get(k) for k in frappe.get_hooks()['write_file_keys']}
return False
def save_file_on_filesystem(fname, content, content_type=None):
@@ -141,7 +140,7 @@ def save_file_on_filesystem(fname, content, content_type=None):
fpath = write_file(content, get_files_path(), fname)
path = os.path.relpath(fpath, public_path)
return {
- 'file_name': path,
+ 'file_name': os.path.basename(path),
'file_url': '/' + path
}
From 0bd269448a7cee956af314e5cd6943253c9b8cdb Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Tue, 8 Apr 2014 17:44:43 +0530
Subject: [PATCH 09/15] fix rename_doc for DocType
---
frappe/model/rename_doc.py | 9 +--------
1 file changed, 1 insertion(+), 8 deletions(-)
diff --git a/frappe/model/rename_doc.py b/frappe/model/rename_doc.py
index 22763351f9..b177708681 100644
--- a/frappe/model/rename_doc.py
+++ b/frappe/model/rename_doc.py
@@ -114,21 +114,14 @@ def update_child_docs(old, new, meta):
% (df.options, '%s', '%s'), (new, old))
def update_link_field_values(link_fields, old, new, doctype):
- update_list = []
-
- # update values
for field in link_fields:
- # if already updated, do not do it again
- if [field['parent'], field['fieldname']] in update_list:
- continue
- update_list.append([field['parent'], field['fieldname']])
if field['issingle']:
frappe.db.sql("""\
update `tabSingles` set value=%s
where doctype=%s and field=%s and value=%s""",
(new, field['parent'], field['fieldname'], old))
else:
- if doctype!='DocType' and field['parent']!=new:
+ if field['parent']!=new:
frappe.db.sql("""\
update `tab%s` set `%s`=%s
where `%s`=%s""" \
From 631de3881fc689905d05abdd5080b44e494adae2 Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Tue, 8 Apr 2014 19:10:05 +0530
Subject: [PATCH 10/15] add File Data columns to list view
---
frappe/core/doctype/file_data/file_data.json | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/frappe/core/doctype/file_data/file_data.json b/frappe/core/doctype/file_data/file_data.json
index 3feee0c22f..cb42e57ad1 100644
--- a/frappe/core/doctype/file_data/file_data.json
+++ b/frappe/core/doctype/file_data/file_data.json
@@ -33,7 +33,7 @@
"hidden": null,
"ignore_restrictions": null,
"in_filter": null,
- "in_list_view": null,
+ "in_list_view": 1,
"label": "File Name",
"no_column": null,
"no_copy": null,
@@ -204,7 +204,7 @@
"istable": null,
"max_attachments": null,
"menu_index": null,
- "modified": "2014-04-07 13:05:47.684017",
+ "modified": "2014-04-07 17:01:13.295614",
"modified_by": "Administrator",
"module": "Core",
"name": "File Data",
From 6fd5f02015240a29c1292a2596c89b695cce39bf Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Tue, 8 Apr 2014 19:10:47 +0530
Subject: [PATCH 11/15] check duplicate assignment using content_hash
---
frappe/core/doctype/file_data/file_data.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/frappe/core/doctype/file_data/file_data.py b/frappe/core/doctype/file_data/file_data.py
index ec8fd70d06..a457a3da5b 100644
--- a/frappe/core/doctype/file_data/file_data.py
+++ b/frappe/core/doctype/file_data/file_data.py
@@ -20,10 +20,10 @@ class FileData(Document):
def on_update(self):
# check duplicate assignement
n_records = frappe.db.sql("""select name from `tabFile Data`
- where file_name=%s
+ where content_hash=%s
and name!=%s
and attached_to_doctype=%s
- and attached_to_name=%s""", (self.file_name, self.name, self.attached_to_doctype,
+ and attached_to_name=%s""", (self.content_hash, self.name, self.attached_to_doctype,
self.attached_to_name))
if len(n_records) > 0:
self.duplicate_entry = n_records[0][0]
From d1a54091dad52f4ca0388fa693ed991db6008787 Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Tue, 8 Apr 2014 19:11:50 +0530
Subject: [PATCH 12/15] fix file_manager hooks patch
---
frappe/patches/4_0/file_manager_hooks.py | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/frappe/patches/4_0/file_manager_hooks.py b/frappe/patches/4_0/file_manager_hooks.py
index 7413d4a526..5c72e62da3 100644
--- a/frappe/patches/4_0/file_manager_hooks.py
+++ b/frappe/patches/4_0/file_manager_hooks.py
@@ -6,9 +6,11 @@ from __future__ import unicode_literals
import frappe
import os
from frappe.utils import get_files_path
-from frappe.utils.filemanager import get_content_hash, get_file
+from frappe.utils.file_manager import get_content_hash, get_file
+
def execute():
+ frappe.reload_doc('core', 'doctype', 'file_data')
for name, file_name, file_url in frappe.db.sql(
"""select name, file_name, file_url from `tabFile Data`
where file_name is not null"""):
@@ -19,7 +21,7 @@ def execute():
b.file_url = os.path.normpath('/' + old_file_name)
else:
b.file_url = os.path.normpath('/files/' + old_file_name)
- _file_name, content = get_file(file_name)
+ _file_name, content = get_file(name)
b.content_hash = get_content_hash(content)
b.save()
From 5e6319530cff9813410a47b8735051a409304837 Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Wed, 9 Apr 2014 12:48:48 +0530
Subject: [PATCH 13/15] change patch order of rename profile to user
---
frappe/patches.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/frappe/patches.txt b/frappe/patches.txt
index ffc14b4433..b1ac2d88f6 100644
--- a/frappe/patches.txt
+++ b/frappe/patches.txt
@@ -16,12 +16,12 @@ frappe.patches.4_0.website_sitemap_hierarchy
frappe.patches.4_0.webnotes_to_frappe
execute:frappe.reset_perms("Module Def")
frappe.patches.4_0.rename_sitemap_to_route
+frappe.patches.4_0.rename_profile_to_user
frappe.patches.4_0.set_website_route_idx
execute:import frappe.installer;frappe.installer.make_site_dirs() #2014-02-19
frappe.patches.4_0.private_backups
frappe.patches.4_0.set_module_in_report
frappe.patches.4_0.remove_old_parent
-frappe.patches.4_0.rename_profile_to_user
frappe.patches.4_0.update_datetime
frappe.patches.4_0.deprecate_control_panel
frappe.patches.4_0.file_manager_hooks
From 4a39f50106bc5c341a48df2d70d08317bf3af32c Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Wed, 9 Apr 2014 12:49:33 +0530
Subject: [PATCH 14/15] rename field only if it exists in profile to user patch
---
frappe/patches/4_0/rename_profile_to_user.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/frappe/patches/4_0/rename_profile_to_user.py b/frappe/patches/4_0/rename_profile_to_user.py
index 6c2b984429..66b8752da6 100644
--- a/frappe/patches/4_0/rename_profile_to_user.py
+++ b/frappe/patches/4_0/rename_profile_to_user.py
@@ -1,6 +1,7 @@
import frappe
from frappe.model import rename_field
+from frappe.model.meta import get_table_columns
def execute():
tables = frappe.db.sql_list("show tables")
@@ -9,6 +10,7 @@ def execute():
if frappe.db.exists("DocType", "Website Route Permission"):
frappe.reload_doc("website", "doctype", "website_route_permission")
- rename_field("Website Route Permission", "profile", "user")
+ if "profile" in get_table_columns("Website Route Permission"):
+ rename_field("Website Route Permission", "profile", "user")
frappe.reload_doc("website", "doctype", "blogger")
rename_field("Blogger", "profile", "user")
From bad85591acf4cade294d5c4bafd82b5a1f9ec966 Mon Sep 17 00:00:00 2001
From: Pratik Vyas
Date: Wed, 9 Apr 2014 16:02:32 +0530
Subject: [PATCH 15/15] add filemanager tests
---
frappe/tests/test_filemanager.py | 80 ++++++++++++++++++++++++++++++++
1 file changed, 80 insertions(+)
create mode 100644 frappe/tests/test_filemanager.py
diff --git a/frappe/tests/test_filemanager.py b/frappe/tests/test_filemanager.py
new file mode 100644
index 0000000000..45207d8857
--- /dev/null
+++ b/frappe/tests/test_filemanager.py
@@ -0,0 +1,80 @@
+# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
+# MIT License. See license.txt
+
+import frappe
+import os
+import unittest
+
+from frappe.utils.file_manager import save_file, get_file, get_files_path
+
+test_content1 = 'Hello'
+test_content2 = 'Hello World'
+
+def make_test_doc():
+ d = frappe.new_doc('ToDo')
+ d.description = 'Test'
+ d.save()
+ return d.doctype, d.name
+
+class TestSimpleFile(unittest.TestCase):
+
+ def setUp(self):
+ self.attached_to_doctype, self.attached_to_docname = make_test_doc()
+ self.test_content = test_content1
+ self.saved_file = save_file('hello.txt', self.test_content, self.attached_to_doctype, self.attached_to_docname)
+ self.saved_filename = get_files_path(self.saved_file.file_name)
+
+ def test_save(self):
+ filename, content = get_file(self.saved_file.name)
+ self.assertEqual(content, self.test_content)
+
+ def tearDown(self):
+ # File gets deleted on rollback, so blank
+ pass
+
+
+class TestSameFileName(unittest.TestCase):
+
+ def setUp(self):
+ self.attached_to_doctype, self.attached_to_docname = make_test_doc()
+ self.test_content1 = test_content1
+ self.test_content2 = test_content2
+ self.saved_file1 = save_file('hello.txt', self.test_content1, self.attached_to_doctype, self.attached_to_docname)
+ self.saved_file2 = save_file('hello.txt', self.test_content2, self.attached_to_doctype, self.attached_to_docname)
+ self.saved_filename1 = get_files_path(self.saved_file1.file_name)
+ self.saved_filename2 = get_files_path(self.saved_file2.file_name)
+
+ def test_saved_content(self):
+ filename1, content1 = get_file(self.saved_file1.name)
+ self.assertEqual(content1, self.test_content1)
+ filename2, content2 = get_file(self.saved_file2.name)
+ self.assertEqual(content2, self.test_content2)
+
+ def tearDown(self):
+ # File gets deleted on rollback, so blank
+ pass
+
+
+class TestSameContent(unittest.TestCase):
+
+ def setUp(self):
+ self.attached_to_doctype1, self.attached_to_docname1 = make_test_doc()
+ self.attached_to_doctype2, self.attached_to_docname2 = make_test_doc()
+ self.test_content1 = test_content1
+ self.test_content2 = test_content1
+ self.orig_filename = 'hello.txt'
+ self.dup_filename = 'hello2.txt'
+ self.saved_file1 = save_file(self.orig_filename, self.test_content1, self.attached_to_doctype1, self.attached_to_docname1)
+ self.saved_file2 = save_file(self.dup_filename, self.test_content2, self.attached_to_doctype2, self.attached_to_docname2)
+ self.saved_filename1 = get_files_path(self.saved_file1.file_name)
+ self.saved_filename2 = get_files_path(self.saved_file2.file_name)
+
+ def test_saved_content(self):
+ filename1, content1 = get_file(self.saved_file1.name)
+ filename2, content2 = get_file(self.saved_file2.name)
+ self.assertEqual(filename1, filename2)
+ self.assertFalse(os.path.exists(get_files_path(self.dup_filename)))
+
+ def tearDown(self):
+ # File gets deleted on rollback, so blank
+ pass