From 4731fb999cd07c22f959a797be26151be7f3c298 Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Tue, 26 Mar 2013 12:47:19 +0530 Subject: [PATCH] [feature] [fix] utilities/rename tool upgraded to upload multiple items --- .../upload_attendance/upload_attendance.js | 12 +--- setup/page/setup/setup.js | 6 ++ .../stock_reconciliation.js | 9 ++- utilities/doctype/rename_tool/__init__.py | 0 utilities/doctype/rename_tool/rename_tool.js | 47 +++++++++++++ utilities/doctype/rename_tool/rename_tool.py | 49 +++++++++++++ utilities/doctype/rename_tool/rename_tool.txt | 69 +++++++++++++++++++ 7 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 utilities/doctype/rename_tool/__init__.py create mode 100644 utilities/doctype/rename_tool/rename_tool.js create mode 100644 utilities/doctype/rename_tool/rename_tool.py create mode 100644 utilities/doctype/rename_tool/rename_tool.txt diff --git a/hr/doctype/upload_attendance/upload_attendance.js b/hr/doctype/upload_attendance/upload_attendance.js index 0c1d7b2574..35a00ed69d 100644 --- a/hr/doctype/upload_attendance/upload_attendance.js +++ b/hr/doctype/upload_attendance/upload_attendance.js @@ -44,20 +44,17 @@ erpnext.hr.AttendanceControlPanel = wn.ui.form.Controller.extend({ show_upload: function() { var me = this; var $wrapper = $(cur_frm.fields_dict.upload_html.wrapper).empty(); - var upload_area = $('
').appendTo($wrapper); // upload wn.upload.make({ - parent: $('#dit-upload-area'), + parent: $wrapper, args: { method: 'hr.doctype.upload_attendance.upload_attendance.upload' }, sample_url: "e.g. http://example.com/somefile.csv", callback: function(r) { var $log_wrapper = $(cur_frm.fields_dict.import_log.wrapper).empty(); - var log_area = $('
').appendTo($log_wrapper); - $wrapper.find(".dit-progress-area").toggle(false); if(!r.messages) r.messages = []; // replace links if error has occured if(r.exc || r.error) { @@ -81,7 +78,7 @@ erpnext.hr.AttendanceControlPanel = wn.ui.form.Controller.extend({ console.log(r.messages); $.each(r.messages, function(i, v) { - var $p = $('

').html(v).appendTo('#dit-output'); + var $p = $('

').html(v).appendTo($log_wrapper); if(v.substr(0,5)=='Error') { $p.css('color', 'red'); } else if(v.substr(0,8)=='Inserted') { @@ -96,11 +93,8 @@ erpnext.hr.AttendanceControlPanel = wn.ui.form.Controller.extend({ }); // rename button - $('#dit-upload-area form input[type="submit"]') + $wrapper.find('form input[type="submit"]') .attr('value', 'Upload and Import') - .click(function() { - $wrapper.find(".dit-progress-area").toggle(true); - }); } }) diff --git a/setup/page/setup/setup.js b/setup/page/setup/setup.js index a9df4592f9..e59a18ab99 100644 --- a/setup/page/setup/setup.js +++ b/setup/page/setup/setup.js @@ -76,6 +76,12 @@ wn.module_page["Setup"] = [ label: wn._("Manage numbering series"), "description":wn._("Set multiple numbering series for transactions") }, + { + "route":"Form/Rename Tool", + doctype: "Rename Tool", + label: wn._("Rename Tool"), + "description":wn._("Rename multiple items in one go") + }, ] }, { diff --git a/stock/doctype/stock_reconciliation/stock_reconciliation.js b/stock/doctype/stock_reconciliation/stock_reconciliation.js index 372166eaac..9229e14df8 100644 --- a/stock/doctype/stock_reconciliation/stock_reconciliation.js +++ b/stock/doctype/stock_reconciliation/stock_reconciliation.js @@ -101,22 +101,25 @@ erpnext.stock.StockReconciliation = erpnext.stock.StockController.extend({ show_upload: function() { var me = this; var $wrapper = $(cur_frm.fields_dict.upload_html.wrapper).empty(); - var upload_area = $('

').appendTo($wrapper); // upload wn.upload.make({ - parent: $('#dit-upload-area'), + parent: $wrapper, args: { method: 'stock.doctype.stock_reconciliation.stock_reconciliation.upload' }, sample_url: "e.g. http://example.com/somefile.csv", callback: function(r) { - $wrapper.find(".dit-progress-area").toggle(false); me.frm.set_value("reconciliation_json", JSON.stringify(r)); me.show_reconciliation_data(); me.frm.save(); } }); + + // rename button + $wrapper.find('form input[type="submit"]') + .attr('value', 'Upload') + }, show_download_reconciliation_data: function() { diff --git a/utilities/doctype/rename_tool/__init__.py b/utilities/doctype/rename_tool/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/utilities/doctype/rename_tool/rename_tool.js b/utilities/doctype/rename_tool/rename_tool.js new file mode 100644 index 0000000000..9c93622185 --- /dev/null +++ b/utilities/doctype/rename_tool/rename_tool.js @@ -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("
" + + wn._("Upload a .csv file with two columns: the old name and the new name. Max 500 rows.") + + "
"); + 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("
"); + $.each(r, function(i, v) { + $("
" + v + "
").appendTo($log); + }); + } + }); + + // rename button + $wrapper.find('form input[type="submit"]') + .click(function() { + $log.html("Working..."); + }) + .addClass("btn-info") + .attr('value', 'Upload and Rename') + +} \ No newline at end of file diff --git a/utilities/doctype/rename_tool/rename_tool.py b/utilities/doctype/rename_tool/rename_tool.py new file mode 100644 index 0000000000..2e368ced70 --- /dev/null +++ b/utilities/doctype/rename_tool/rename_tool.py @@ -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("" + \ + _("Failed: ") + row[0] + " -> " + row[1] + "") + rename_log.append("" + repr(e) + "") + + return rename_log \ No newline at end of file diff --git a/utilities/doctype/rename_tool/rename_tool.txt b/utilities/doctype/rename_tool/rename_tool.txt new file mode 100644 index 0000000000..20c2561556 --- /dev/null +++ b/utilities/doctype/rename_tool/rename_tool.txt @@ -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" + } +] \ No newline at end of file