[feature] [fix] utilities/rename tool upgraded to upload multiple items

This commit is contained in:
Rushabh Mehta
2013-03-26 12:47:19 +05:30
parent 74506e96d3
commit 4731fb999c
7 changed files with 180 additions and 12 deletions

View File

@@ -0,0 +1,47 @@
cur_frm.cscript.refresh = function(doc) {
wn.call({
method:"utilities.doctype.rename_tool.rename_tool.get_doctypes",
callback: function(r) {
cur_frm.set_df_property("select_doctype", "options", r.message);
cur_frm.cscript.setup_upload();
}
});
}
cur_frm.cscript.select_doctype = function() {
cur_frm.cscript.setup_upload();
}
cur_frm.cscript.setup_upload = function() {
var me = this;
var $wrapper = $(cur_frm.fields_dict.upload_html.wrapper).empty()
.html("<hr><div class='alert'>" +
wn._("Upload a .csv file with two columns: the old name and the new name. Max 500 rows.")
+ "</div>");
var $log = $(cur_frm.fields_dict.rename_log.wrapper).empty();
// upload
wn.upload.make({
parent: $wrapper,
args: {
method: 'utilities.doctype.rename_tool.rename_tool.upload',
select_doctype: cur_frm.doc.select_doctype
},
sample_url: "e.g. http://example.com/somefile.csv",
callback: function(r) {
$log.empty().html("<hr>");
$.each(r, function(i, v) {
$("<div>" + v + "</div>").appendTo($log);
});
}
});
// rename button
$wrapper.find('form input[type="submit"]')
.click(function() {
$log.html("Working...");
})
.addClass("btn-info")
.attr('value', 'Upload and Rename')
}

View File

@@ -0,0 +1,49 @@
# For license information, please see license.txt
from __future__ import unicode_literals
import webnotes
from webnotes import _
class DocType:
def __init__(self, d, dl):
self.doc, self.doclist = d, dl
@webnotes.whitelist()
def get_doctypes():
return webnotes.conn.sql_list("""select name from tabDocType
where ifnull(allow_rename,0)=1 and module!='Core' order by name""")
@webnotes.whitelist(allow_roles=["System Manager"])
def upload(select_doctype=None, rows=None):
from webnotes.utils.datautils import read_csv_content_from_uploaded_file
from webnotes.modules import scrub
from webnotes.model.rename_doc import rename_doc
if not select_doctype:
select_doctype = webnotes.form_dict.select_doctype
if not rows:
rows = read_csv_content_from_uploaded_file()
if not rows:
webnotes.msgprint(_("Please select a valid csv file with data."))
raise Exception
if len(rows) > 500:
webnotes.msgprint(_("Max 500 rows only."))
raise Exception
rename_log = []
for row in rows:
if len(row) > 2:
try:
if rename_doc(select_doctype, row[0], row[1]):
rename_log.append(_("Successful: ") + row[0] + " -> " + row[1])
webnotes.conn.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>")
return rename_log

View File

@@ -0,0 +1,69 @@
[
{
"creation": "2012-12-03 10:25:59",
"docstatus": 0,
"modified": "2013-03-26 12:46:07",
"modified_by": "Administrator",
"owner": "Administrator"
},
{
"allow_attach": 0,
"allow_email": 1,
"allow_print": 1,
"doctype": "DocType",
"hide_heading": 0,
"hide_toolbar": 1,
"issingle": 1,
"max_attachments": 1,
"module": "Utilities",
"name": "__common__"
},
{
"doctype": "DocField",
"name": "__common__",
"parent": "Rename Tool",
"parentfield": "fields",
"parenttype": "DocType",
"permlevel": 0
},
{
"create": 1,
"doctype": "DocPerm",
"name": "__common__",
"parent": "Rename Tool",
"parentfield": "permissions",
"parenttype": "DocType",
"permlevel": 0,
"read": 1,
"report": 0,
"role": "System Manager",
"submit": 0,
"write": 1
},
{
"doctype": "DocType",
"name": "Rename Tool"
},
{
"description": "Type of document to rename.",
"doctype": "DocField",
"fieldname": "select_doctype",
"fieldtype": "Select",
"label": "Select DocType"
},
{
"doctype": "DocField",
"fieldname": "upload_html",
"fieldtype": "HTML",
"label": "Upload HTML"
},
{
"doctype": "DocField",
"fieldname": "rename_log",
"fieldtype": "HTML",
"label": "Rename Log"
},
{
"doctype": "DocPerm"
}
]