Skip to content

Commit

Permalink
Merge pull request #1349 from rmehta/rename-tool
Browse files Browse the repository at this point in the history
[feature] rename via console
  • Loading branch information
anandpdoshi committed Oct 21, 2015
2 parents b2aa8f8 + 879dc6f commit 2f2f73b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions frappe/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -918,6 +939,7 @@ def get_version(context):
export_fixtures,
import_doc,
import_csv,
_bulk_rename,
build_message_files,
get_untranslated,
update_translations,
Expand Down
35 changes: 35 additions & 0 deletions frappe/model/rename_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2f2f73b

Please sign in to comment.