Merge pull request #1349 from rmehta/rename-tool
[feature] rename via console
This commit is contained in:
commit
2f2f73b293
2 changed files with 57 additions and 0 deletions
|
|
@ -565,6 +565,27 @@ def import_csv(context, path, only_insert=False, submit_after_import=False, igno
|
|||
|
||||
frappe.destroy()
|
||||
|
||||
@click.command('bulk-rename')
|
||||
@click.argument('doctype')
|
||||
@click.argument('path')
|
||||
@pass_context
|
||||
def _bulk_rename(context, doctype, path):
|
||||
"Import CSV using data import tool"
|
||||
from frappe.model.rename_doc import bulk_rename
|
||||
from frappe.utils.csvutils import read_csv_content
|
||||
|
||||
site = get_single_site(context)
|
||||
|
||||
with open(path, 'r') as csvfile:
|
||||
rows = read_csv_content(csvfile.read())
|
||||
|
||||
frappe.init(site=site)
|
||||
frappe.connect()
|
||||
|
||||
bulk_rename(doctype, rows, via_console = True)
|
||||
|
||||
frappe.destroy()
|
||||
|
||||
# translation
|
||||
@click.command('build-message-files')
|
||||
@pass_context
|
||||
|
|
@ -918,6 +939,7 @@ commands = [
|
|||
export_fixtures,
|
||||
import_doc,
|
||||
import_csv,
|
||||
_bulk_rename,
|
||||
build_message_files,
|
||||
get_untranslated,
|
||||
update_translations,
|
||||
|
|
|
|||
|
|
@ -333,3 +333,38 @@ def rename_dynamic_links(doctype, old, new):
|
|||
|
||||
frappe.db.sql("""update `tab{parent}` set {fieldname}=%s
|
||||
where name=%s""".format(**df), (new, to_change))
|
||||
|
||||
def bulk_rename(doctype, rows=None, via_console = False):
|
||||
"""Bulk rename documents
|
||||
|
||||
:param doctype: DocType to be renamed
|
||||
:param rows: list of documents as `((oldname, newname), ..)`"""
|
||||
if not rows:
|
||||
frappe.throw(_("Please select a valid csv file with data"))
|
||||
|
||||
if not via_console:
|
||||
max_rows = 500
|
||||
if len(rows) > max_rows:
|
||||
frappe.throw(_("Maximum {0} rows allowed").format(max_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(doctype, row[0], row[1]):
|
||||
msg = _("Successful: {0} to {1}").format(row[0], row[1])
|
||||
frappe.db.commit()
|
||||
else:
|
||||
msg = _("Ignored: {0} to {1}").format(row[0], row[1])
|
||||
except Exception, e:
|
||||
msg = _("** Failed: {0} to {1}: {2}").format(row[0], row[1], repr(e))
|
||||
frappe.db.rollback()
|
||||
|
||||
if via_console:
|
||||
print msg
|
||||
else:
|
||||
rename_log.append(msg)
|
||||
|
||||
if not via_console:
|
||||
return rename_log
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue