bulk rename

This commit is contained in:
Ravi Dey 2011-07-07 16:37:03 +05:30
parent 8982272645
commit 8cfa84a646
3 changed files with 183 additions and 0 deletions

View File

@ -0,0 +1,84 @@
# Please edit this list and import only required elements
import webnotes
from webnotes.utils import add_days, add_months, add_years, cint, cstr, date_diff, default_fields, flt, fmt_money, formatdate, generate_hash, getTraceback, get_defaults, get_first_day, get_last_day, getdate, has_common, month_name, now, nowdate, replace_newlines, sendmail, set_default, str_esc_quote, user_format, validate_email_add
from webnotes.model import db_exists
from webnotes.model.doc import Document, addchild, removechild, getchildren, make_autoname, SuperDocType
from webnotes.model.doclist import getlist, copy_doclist
from webnotes.model.code import get_obj, get_server_obj, run_server_obj, updatedb, check_syntax
from webnotes import session, form, is_testing, msgprint, errprint
set = webnotes.conn.set
sql = webnotes.conn.sql
get_value = webnotes.conn.get_value
in_transaction = webnotes.conn.in_transaction
convert_to_lists = webnotes.conn.convert_to_lists
# -----------------------------------------------------------------------------------------
class DocType:
def __init__(self, d, dl):
self.doc, self.doclist = d, dl
# bulk rename
def do_rename(self):
import csv
data = csv.reader(self.get_csv_data().splitlines())
updated = 0
msgprint(self.doc.rename_doctype)
if self.doc.rename_doctype == 'Account':
for line in data:
if len(line)==2:
rec = sql("select tc.abbr, ta.name from `tabAccount` ta, `tabCompany` tc where ta.company = tc.name and ta.account_name = %s", line[0], as_dict=1)
if rec:
new_name = line[1] + ' - ' + rec[0]['abbr']
webnotes.conn.begin()
webnotes.model.rename(self.doc.rename_doctype, rec[0]['name'], new_name)
sql("update `tabAccount` set account_name = '%s' where name = '%s'" %(line[1],new_name))
msgprint(new_name)
webnotes.conn.commit()
updated += 1
else:
msgprint("[Ignored] Incorrect format: %s" % str(line))
else:
for line in data:
if len(line)==2:
# call on_rename method if exists
obj = get_obj(self.doc.rename_doctype, line[0])
if hasattr(obj, 'on_rename'):
obj.on_rename(line[1],line[0])
# rename the document
webnotes.model.rename(self.doc.rename_doctype, line[0], line[1])
sql("commit")
sql("start transaction")
updated += 1
else:
msgprint("[Ignored] Incorrect format: %s" % str(line))
msgprint("<b>%s</b> items updated" % updated)
# Update CSV data
def get_csv_data(self):
if not self.doc.file_list:
msgprint("File not attached!")
raise Exception
fid = self.doc.file_list.split(',')[1]
from webnotes.utils import file_manager
fn, content = file_manager.get_file(fid)
if not type(content) == str:
content = content.tostring()
return content

View File

@ -0,0 +1,99 @@
# DocType, Bulk Rename Tool
[
# These values are common in all dictionaries
{
'creation': '2011-07-07 16:14:45',
'docstatus': 0,
'modified': '2011-07-07 16:16:16',
'modified_by': 'Administrator',
'owner': 'Administrator'
},
# These values are common for all DocType
{
'_last_update': '1310035501',
'allow_attach': 1,
'autoname': 'field:rename_doctype',
'colour': 'White:FFF',
'doctype': 'DocType',
'module': 'Utilities',
'name': '__common__',
'section_style': 'Simple',
'show_in_menu': 0,
'version': 4
},
# These values are common for all DocField
{
'doctype': 'DocField',
'name': '__common__',
'parent': 'Bulk Rename Tool',
'parentfield': 'fields',
'parenttype': 'DocType',
'permlevel': 0
},
# These values are common for all DocPerm
{
'doctype': 'DocPerm',
'name': '__common__',
'parent': 'Bulk Rename Tool',
'parentfield': 'permissions',
'parenttype': 'DocType',
'read': 1,
'role': 'System Manager'
},
# DocType, Bulk Rename Tool
{
'doctype': 'DocType',
'name': 'Bulk Rename Tool'
},
# DocPerm
{
'create': 1,
'doctype': 'DocPerm',
'idx': 1,
'permlevel': 0,
'submit': 1,
'write': 1
},
# DocPerm
{
'doctype': 'DocPerm',
'idx': 2,
'permlevel': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'rename_doctype',
'fieldtype': 'Data',
'idx': 1,
'label': 'Rename DocType',
'options': 'Suggest',
'reqd': 1
},
# DocField
{
'doctype': 'DocField',
'fieldname': 'file_list',
'fieldtype': 'Text',
'idx': 2,
'label': 'File List'
},
# DocField
{
'doctype': 'DocField',
'fieldtype': 'Button',
'idx': 3,
'label': 'Rename',
'options': 'do_rename'
}
]