<commit_before>from __future__ import unicode_literals
import webnotes
from webnotes import _
from webnotes.utils import cint
import webnotes.model.doctype
from webnotes.model.doc import validate_name

@webnotes.whitelist()
def rename_doc(doctype, old, new, force=False, merge=False):
	"""
		Renames a doc(dt, old) to doc(dt, new) and 
		updates all linked fields of type "Link" or "Select" with "link:"
	"""
	if not webnotes.conn.exists(doctype, old):
		return
	
	force = cint(force)
	merge = cint(merge)
	
	# get doclist of given doctype
	doclist = webnotes.model.doctype.get(doctype)
	
	# call on_rename
	obj = webnotes.get_obj(doctype, old)
	if hasattr(obj, 'on_rename'):
		new = obj.on_rename(new, old, merge) or new
		
	new = validate_rename(doctype, new, doclist, merge, force)
		
	if not merge:
		rename_parent_and_child(doctype, old, new, doclist)
			
	# update link fields' values
	link_fields = get_link_fields(doctype)
	update_link_field_values(link_fields, old, new)
	
	if doctype=='DocType':
		rename_doctype(doctype, old, new, force)
	
	update_attachments(doctype, old, new)
	
	if merge:
		webnotes.delete_doc(doctype, old)
		
	return new

def update_attachments(doctype, old, new):
	try:
		webnotes.conn.sql("""update `tabFile Data` set attached_to_name=%s
			where attached_to_name=%s and attached_to_doctype=%s""", (new, old, doctype))
	except Exception, e:
		if e.args[0]!=1054: # in patch?
			raise e 

def rename_parent_and_child(doctype, old, new, doclist):
	# rename the doc
	webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
		% (doctype, '%s', '%s'), (new, old))

	update_child_docs(old, new, doclist)

def validate_rename(doctype, new, doclist, merge, force):
	exists = webnotes.conn.exists(doctype, new)

	if merge and not exists:
		webnotes.msgprint("%s: %s does not exist, select a new target to merge." % (doctype, new), raise_exception=1)
	
	if (not merge) and exists:
		webnotes.msgprint("%s: %s exists, select a new, new name." % (doctype, new), raise_exception=1)

	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)
	
	# validate naming like it's done in doc.py
	new = validate_name(doctype, new, merge=merge)

	return new

def rename_doctype(doctype, old, new, force=False):
	# change options for fieldtype Table
	update_parent_of_fieldtype_table(old, new)
	
	# change options where select options are hardcoded i.e. listed
	select_fields = get_select_fields(old, new)
	update_link_field_values(select_fields, old, new)
	update_select_field_values(old, new)
	
	# change parenttype for fieldtype Table
	update_parenttype_values(old, new)
	
	# 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)
		<commit_msg>[rename doc] [fix] rename mapper only if it doesn't exist<commit_after>from __future__ import unicode_literals
import webnotes
from webnotes import _
from webnotes.utils import cint
import webnotes.model.doctype
from webnotes.model.doc import validate_name

@webnotes.whitelist()
def rename_doc(doctype, old, new, force=False, merge=False):
	"""
		Renames a doc(dt, old) to doc(dt, new) and 
		updates all linked fields of type "Link" or "Select" with "link:"
	"""
	if not webnotes.conn.exists(doctype, old):
		return
	
	force = cint(force)
	merge = cint(merge)
	
	# get doclist of given doctype
	doclist = webnotes.model.doctype.get(doctype)
	
	# call on_rename
	obj = webnotes.get_obj(doctype, old)
	if hasattr(obj, 'on_rename'):
		new = obj.on_rename(new, old, merge) or new
		
	new = validate_rename(doctype, new, doclist, merge, force)
		
	if not merge:
		rename_parent_and_child(doctype, old, new, doclist)
			
	# update link fields' values
	link_fields = get_link_fields(doctype)
	update_link_field_values(link_fields, old, new)
	
	if doctype=='DocType':
		rename_doctype(doctype, old, new, force)
	
	update_attachments(doctype, old, new)
	
	if merge:
		webnotes.delete_doc(doctype, old)
		
	return new

def update_attachments(doctype, old, new):
	try:
		webnotes.conn.sql("""update `tabFile Data` set attached_to_name=%s
			where attached_to_name=%s and attached_to_doctype=%s""", (new, old, doctype))
	except Exception, e:
		if e.args[0]!=1054: # in patch?
			raise e 

def rename_parent_and_child(doctype, old, new, doclist):
	# rename the doc
	webnotes.conn.sql("update `tab%s` set name=%s where name=%s" \
		% (doctype, '%s', '%s'), (new, old))

	update_child_docs(old, new, doclist)

def validate_rename(doctype, new, doclist, merge, force):
	exists = webnotes.conn.exists(doctype, new)

	if merge and not exists:
		webnotes.msgprint("%s: %s does not exist, select a new target to merge." % (doctype, new), raise_exception=1)
	
	if (not merge) and exists:
		webnotes.msgprint("%s: %s exists, select a new, new name." % (doctype, new), raise_exception=1)

	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)
	
	# validate naming like it's done in doc.py
	new = validate_name(doctype, new, merge=merge)

	return new

def rename_doctype(doctype, old, new, force=False):
	# change options for fieldtype Table
	update_parent_of_fieldtype_table(old, new)
	
	# change options where select options are hardcoded i.e. listed
	select_fields = get_select_fields(old, new)
	update_link_field_values(select_fields, old, new)
	update_select_field_values(old, new)
	
	# change parenttype for fieldtype Table
	update_parenttype_values(old, new)
	
	# 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):
		if not webnotes.conn.exists("DocType Mapper", mapper.from_doctype + "-" + mapper.to_doctype):
			rename_doc("DocType Mapper", mapper.name, mapper.from_doctype + "-" + mapper.to_doctype, force=True)
		
{'fraction_non_alphanumeric': 0.11639418029098544, 'fraction_numerical': 0.0032998350082495873, 'mean_word_length': 5.16836262719704, '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': '6788189', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2325, 'n_words': 645}