Merge pull request #4207 from rmehta/rename-tool

[feature] rename via console merge with frappe/frappe#1349
This commit is contained in:
Anand Doshi 2015-10-21 14:27:49 +05:30
commit 99ba924303
2 changed files with 4 additions and 28 deletions

View File

@ -21,7 +21,7 @@ frappe.ui.form.on("Rename Tool", {
select_doctype: frm.doc.select_doctype
},
callback: function(r) {
frm.get_field("rename_log").$wrapper.html(r.message);
frm.get_field("rename_log").$wrapper.html(r.message.join("<br>"));
}
});
});

View File

@ -5,9 +5,9 @@
from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.rename_doc import bulk_rename
class RenameTool(Document):
pass
@ -20,37 +20,13 @@ def get_doctypes():
@frappe.whitelist()
def upload(select_doctype=None, rows=None):
from frappe.utils.csvutils import read_csv_content_from_attached_file
from frappe.model.rename_doc import rename_doc
if not select_doctype:
select_doctype = frappe.form_dict.select_doctype
if not frappe.has_permission(select_doctype, "write"):
raise frappe.PermissionError
if not rows:
rows = read_csv_content_from_attached_file(frappe.get_doc("Rename Tool", "Rename Tool"))
if not rows:
frappe.throw(_("Please select a valid csv file with data"))
rows = read_csv_content_from_attached_file(frappe.get_doc("Rename Tool", "Rename Tool"))
max_rows = 500
if len(rows) > max_rows:
frappe.throw(_("Maximum {0} rows allowed").format(max_rows))
return bulk_rename(select_doctype, rows=rows)
rename_log = []
for row in rows:
# if row has some content
if len(row) > 1 and row[0] and row[1]:
try:
if rename_doc(select_doctype, row[0], row[1]):
rename_log.append(_("Successful: ") + row[0] + " -> " + row[1])
frappe.db.commit()
else:
rename_log.append(_("Ignored: ") + row[0] + " -> " + row[1])
except Exception, e:
rename_log.append("<span style='color: RED'>" + \
_("Failed: ") + row[0] + " -> " + row[1] + "</span>")
rename_log.append("<span style='margin-left: 20px;'>" + repr(e) + "</span>")
frappe.db.rollback()
return rename_log