<commit_before>from __future__ import unicode_literals
import webnotes

@webnotes.whitelist()
def rename_doc(doctype, old, new, debug=0, force=False):
	"""
		Renames a doc(dt, old) to doc(dt, new) and 
		updates all linked fields of type "Link" or "Select" with "link:"
	"""
	import webnotes.utils
	import webnotes.model.doctype
	from webnotes.model.code import get_obj

	# get doclist of given doctype
	doclist = webnotes.model.doctype.get(doctype)
	
	if webnotes.conn.exists(doctype, new):
		webnotes.msgprint("%s: %s exists, select a new, new name." % (doctype, new))

	if not webnotes.has_permission(doctype, "write"):
		webnotes.msgprint("You need write permission to rename", raise_exception=1)

	if not force and not doclist[0].allow_rename:
		webnotes.msgprint("%s cannot be renamed" % doctype, raise_exception=1)
	
	# without child fields of table type fields (form=0)
	# call on_rename method if exists
	obj = get_obj(doctype, old)
	if hasattr(obj, 'on_rename'):
		new = obj.on_rename(new, old) or new
		
	# rename the doc
	webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
		% (doctype, '%s', '%s'), (new, old), debug=debug)
	
	update_child_docs(old, new, doclist, debug=debug)
	if debug: webnotes.errprint("executed update_child_docs")
	
	if doctype=='DocType':
		# rename the table or change doctype of singles
		issingle = webnotes.conn.sql("""\
			select ifnull(issingle, 0) from `tabDocType`
			where name=%s""", new)

		if issingle and webnotes.utils.cint(issingle[0][0]) or 0:
			webnotes.conn.sql("""\
				update tabSingles set doctype=%s
				where doctype=%s""", (new, old))
		else:
			webnotes.conn.sql("rename table `tab%s` to `tab%s`" % (old, new))
		if debug: webnotes.errprint("executed rename table")
	
	# update link fields' values
	link_fields = get_link_fields(doctype)
	if debug: webnotes.errprint(link_fields)
	update_link_field_values(link_fields, old, new, debug=debug)
	if debug: webnotes.errprint("executed update_link_field_values")
	
	if doctype=='DocType':
		rename_doctype(doctype, old, new, debug, force)
		
	return new
	
def rename_doctype(doctype, old, new, debug=0, force=False):
	# change options for fieldtype Table
	update_parent_of_fieldtype_table(old, new, debug=debug)
	if debug: webnotes.errprint("executed update_parent_of_fieldtype_table")
	
	# change options where select options are hardcoded i.e. listed
	select_fields = get_select_fields(old, new, debug=debug)
	update_link_field_values(select_fields, old, new, debug=debug)
	if debug: webnotes.errprint("executed update_link_field_values")
	update_select_field_values(old, new, debug=debug)
	if debug: webnotes.errprint("executed update_select_field_values")
	
	# change parenttype for fieldtype Table
	update_parenttype_values(old, new, debug=debug)
	if debug: webnotes.errprint("executed update_parenttype_values")
	
	# update mapper
	rename_mapper(new)
	
def rename_mapper(new):
	for mapper in webnotes.conn.sql("""select name, from_doctype, to_doctype 
			from `tabDocType Mapper` where from_doctype=%s or to_doctype=%s""", (new, new), as_dict=1):
		rename_doc("DocType Mapper", mapper.name, mapper.from_doctype + "-" + mapper.to_doctype, force=True)
		
def update_child_docs(old, new, doclist, debug=0):
	"""
		updates 'parent' field of child documents
	"""
	# generator of a list of child doctypes
	child_doctypes = (d.options for d in doclist 
		if d.doctype=='DocField' and d.fieldtype=='Table')
	
	for child in child_doctypes:
		webnotes.conn.sql("update `tab%s` set parent=%s where parent=%s" \<commit_msg>renamed blog to blog_post and refresh issues<commit_after>from __future__ import unicode_literals
import webnotes

@webnotes.whitelist()
def rename_doc(doctype, old, new, debug=0, force=False):
	"""
		Renames a doc(dt, old) to doc(dt, new) and 
		updates all linked fields of type "Link" or "Select" with "link:"
	"""
	import webnotes.utils
	import webnotes.model.doctype
	from webnotes.model.code import get_obj

	# get doclist of given doctype
	doclist = webnotes.model.doctype.get(doctype)
	
	if webnotes.conn.exists(doctype, new):
		webnotes.msgprint("%s: %s exists, select a new, new name." % (doctype, new))

	if not webnotes.has_permission(doctype, "write"):
		webnotes.msgprint("You need write permission to rename", raise_exception=1)

	if not force and not doclist[0].allow_rename:
		webnotes.msgprint("%s cannot be renamed" % doctype, raise_exception=1)
	
	# without child fields of table type fields (form=0)
	# call on_rename method if exists
	obj = get_obj(doctype, old)
	if hasattr(obj, 'on_rename'):
		new = obj.on_rename(new, old) or new
		
	# rename the doc
	webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
		% (doctype, '%s', '%s'), (new, old), debug=debug)
	
	update_child_docs(old, new, doclist, debug=debug)
	if debug: webnotes.errprint("executed update_child_docs")
	
	if doctype=='DocType':
		# rename the table or change doctype of singles
		issingle = webnotes.conn.sql("""\
			select ifnull(issingle, 0) from `tabDocType`
			where name=%s""", new)

		if issingle and webnotes.utils.cint(issingle[0][0]) or 0:
			webnotes.conn.sql("""\
				update tabSingles set doctype=%s
				where doctype=%s""", (new, old))
		else:
			webnotes.conn.sql("rename table `tab%s` to `tab%s`" % (old, new))
		if debug: webnotes.errprint("executed rename table")
	
	# update link fields' values
	link_fields = get_link_fields(doctype)
	if debug: webnotes.errprint(link_fields)
	update_link_field_values(link_fields, old, new, debug=debug)
	if debug: webnotes.errprint("executed update_link_field_values")
	
	if doctype=='DocType':
		rename_doctype(doctype, old, new, debug, force)
		
	return new
	
def rename_doctype(doctype, old, new, debug=0, force=False):
	# change options for fieldtype Table
	update_parent_of_fieldtype_table(old, new, debug=debug)
	if debug: webnotes.errprint("executed update_parent_of_fieldtype_table")
	
	# change options where select options are hardcoded i.e. listed
	select_fields = get_select_fields(old, new, debug=debug)
	update_link_field_values(select_fields, old, new, debug=debug)
	if debug: webnotes.errprint("executed update_link_field_values")
	update_select_field_values(old, new, debug=debug)
	if debug: webnotes.errprint("executed update_select_field_values")
	
	# change parenttype for fieldtype Table
	update_parenttype_values(old, new, debug=debug)
	if debug: webnotes.errprint("executed update_parenttype_values")
	
	# rename comments
	webnotes.conn.sql("""update tabComment set comment_doctype=%s where comment_doctype=%s""",
		(new, old))
	
	# update mapper
	rename_mapper(new)
	
def rename_mapper(new):
	for mapper in webnotes.conn.sql("""select name, from_doctype, to_doctype 
			from `tabDocType Mapper` where from_doctype=%s or to_doctype=%s""", (new, new), as_dict=1):
		rename_doc("DocType Mapper", mapper.name, mapper.from_doctype + "-" + mapper.to_doctype, force=True)
		
def update_child_docs(old, new, doclist, debug=0):
	"""
		updates 'parent' field of child documents
	"""
	# generator of a list of child doctypes
	child_doctypes = (d.options for d in doclist 
		if d.doctype=='DocField' and d.fieldtype=='Table')
	
	for child in child_doctypes:
		webnotes.conn.sql("update `tab%s` set parent=%s where parent=%s" \
{'fraction_non_alphanumeric': 0.11991701244813278, 'fraction_numerical': 0.0033195020746887966, 'mean_word_length': 5.365316901408451, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1657400', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2489, 'n_words': 674}