# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

import webnotes

def execute():
	webnotes.reload_doc("core", "doctype", "custom_field")
	
	cf_doclist = webnotes.get_doctype("Custom Field")

	delete_list = []
	for d in webnotes.conn.sql("""select cf.name as cf_name, ps.property, 
			ps.value, ps.name as ps_name
		from `tabProperty Setter` ps, `tabCustom Field` cf
		where ps.doctype_or_field = 'DocField' and ps.property != 'previous_field'
		and ps.doc_type=cf.dt and ps.field_name=cf.fieldname""", as_dict=1):
			if cf_doclist.get_field(d.property):
				webnotes.conn.sql("""update `tabCustom Field` 
					set `%s`=%s where name=%s""" % (d.property, '%s', '%s'), (d.value, d.cf_name))
				
				delete_list.append(d.ps_name)
	
	if delete_list:
		webnotes.conn.sql("""delete from `tabProperty Setter` where name in (%s)""" % 
			', '.join(['%s']*len(delete_list)), tuple(delete_list))
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

import webnotes
from webnotes.model.meta import get_field

def execute():
	webnotes.reload_doc("core", "doctype", "custom_field")
	
	custom_fields = {}
	for cf in webnotes.conn.sql("""select dt, fieldname from `tabCustom Field`""", as_dict=1):
		custom_fields.setdefault(cf.dt, []).append(cf.fieldname)
		
	delete_list = []
	for ps in webnotes.conn.sql("""select * from `tabProperty Setter`""", as_dict=1):
		if ps.field_name in custom_fields.get(ps.doc_type, []):
			
			if ps.property == "previous_field":
				property_name = "insert_after"
				
				field_meta = get_field(ps.doc_type, ps.value)
				property_value = field_meta.label if field_meta else ""
			else:
				property_name = ps.property
				property_value  =ps.value
	
			webnotes.conn.sql("""update `tabCustom Field` 
				set %s=%s where dt=%s and fieldname=%s""" % (property_name,	'%s', '%s', '%s'), 
				(property_value, ps.doc_type, ps.field_name))
				
			delete_list.append(ps.name)
			
	if delete_list:
		webnotes.conn.sql("""delete from `tabProperty Setter` where name in (%s)""" % 
			', '.join(['%s']*len(delete_list)), tuple(delete_list))
--- 
+++ 
@@ -2,34 +2,24 @@
 # License: GNU General Public License v3. See license.txt
 
 import webnotes
-from webnotes.model.meta import get_field
 
 def execute():
 	webnotes.reload_doc("core", "doctype", "custom_field")
 	
-	custom_fields = {}
-	for cf in webnotes.conn.sql("""select dt, fieldname from `tabCustom Field`""", as_dict=1):
-		custom_fields.setdefault(cf.dt, []).append(cf.fieldname)
-		
+	cf_doclist = webnotes.get_doctype("Custom Field")
+
 	delete_list = []
-	for ps in webnotes.conn.sql("""select * from `tabProperty Setter`""", as_dict=1):
-		if ps.field_name in custom_fields.get(ps.doc_type, []):
-			
-			if ps.property == "previous_field":
-				property_name = "insert_after"
+	for d in webnotes.conn.sql("""select cf.name as cf_name, ps.property, 
+			ps.value, ps.name as ps_name
+		from `tabProperty Setter` ps, `tabCustom Field` cf
+		where ps.doctype_or_field = 'DocField' and ps.property != 'previous_field'
+		and ps.doc_type=cf.dt and ps.field_name=cf.fieldname""", as_dict=1):
+			if cf_doclist.get_field(d.property):
+				webnotes.conn.sql("""update `tabCustom Field` 
+					set `%s`=%s where name=%s""" % (d.property, '%s', '%s'), (d.value, d.cf_name))
 				
-				field_meta = get_field(ps.doc_type, ps.value)
-				property_value = field_meta.label if field_meta else ""
-			else:
-				property_name = ps.property
-				property_value  =ps.value
+				delete_list.append(d.ps_name)
 	
-			webnotes.conn.sql("""update `tabCustom Field` 
-				set %s=%s where dt=%s and fieldname=%s""" % (property_name,	'%s', '%s', '%s'), 
-				(property_value, ps.doc_type, ps.field_name))
-				
-			delete_list.append(ps.name)
-			
 	if delete_list:
 		webnotes.conn.sql("""delete from `tabProperty Setter` where name in (%s)""" % 
 			', '.join(['%s']*len(delete_list)), tuple(delete_list))
Delete Property Setters for Custom Fields, and set them inside Custom Field
