Browse Source

rename fields function in model

version-14
Nabin Hait 11 years ago
parent
commit
2add1269c6
2 changed files with 41 additions and 26 deletions
  1. +40
    -0
      webnotes/model/__init__.py
  2. +1
    -26
      webnotes/model/doctype.py

+ 40
- 0
webnotes/model/__init__.py View File

@@ -84,3 +84,43 @@ def delete_fields(args_dict, delete=0):
", ".join(["DROP COLUMN `%s`" % f for f in fields if f in existing_fields])
webnotes.conn.commit()
webnotes.conn.sql(query)

def rename_field(doctype, old_fieldname, new_fieldname):
"""This functions assumes that doctype is already synced"""
doctype_list = webnotes.get_doctype(doctype)
new_field = doctype_list.get_field(new_fieldname)
if not new_field:
print "rename_field: " + (new_fieldname) + " not found in " + doctype
return
if new_field.fieldtype == "Table":
# change parentfield of table mentioned in options
webnotes.conn.sql("""update `tab%s` set parentfield=%s
where parentfield=%s""" % (new_field.options.split("\n")[0], "%s", "%s"),
(new_fieldname, old_fieldname))
elif new_field.fieldtype not in no_value_fields:
if doctype_list[0].issingle:
webnotes.conn.sql("""update `tabSingles` set field=%s
where doctype=%s and field=%s""",
(new_fieldname, doctype, old_fieldname))
else:
webnotes.conn.sql("""update `tab%s` set `%s`=`%s`""" % \
(doctype, new_fieldname, old_fieldname))
webnotes.conn.sql("""update `tabProperty Setter` set field_name = %s
where doc_type=%s and field_name=%s""", (new_fieldname, doctype, old_fieldname))
import json
user_report_cols = webnotes.conn.sql("""select defkey, defvalue from `tabDefaultValue` where
defkey like '_list_settings:%'""")
for key, value in user_report_cols:
new_columns = []
columns_modified = False
for field, field_doctype in json.loads(value):
if field == old_fieldname and field_doctype == doctype:
new_columns.append([field, field_doctype])
columns_modified=True
if columns_modified:
webnotes.conn.sql("""update `tabDefaultValue` set defvalue=%s
where defkey=%s""" % ('%s', '%s'), (json.dumps(new_columns), key))

+ 1
- 26
webnotes/model/doctype.py View File

@@ -404,29 +404,4 @@ class DocTypeDocList(webnotes.model.doclist.DocList):
def get_permissions(self, user=None):
user_roles = webnotes.get_roles(user)
return [p for p in self.get({"doctype": "DocPerm"})
if cint(p.permlevel)==0 and (p.role=="All" or p.role in user_roles)]

def rename_field(doctype, old_fieldname, new_fieldname, lookup_field=None):
"""this function assumes that sync is NOT performed"""
import webnotes.model
doctype_list = get(doctype)
old_field = doctype_list.get_field(lookup_field or old_fieldname)
if not old_field:
print "rename_field: " + (lookup_field or old_fieldname) + " not found."
if old_field.fieldtype == "Table":
# change parentfield of table mentioned in options
webnotes.conn.sql("""update `tab%s` set parentfield=%s
where parentfield=%s""" % (old_field.options.split("\n")[0], "%s", "%s"),
(new_fieldname, old_fieldname))
elif old_field.fieldtype not in webnotes.model.no_value_fields:
# copy
if doctype_list[0].issingle:
webnotes.conn.sql("""update `tabSingles` set field=%s
where doctype=%s and field=%s""",
(new_fieldname, doctype, old_fieldname))
else:
webnotes.conn.sql("""update `tab%s` set `%s`=`%s`""" % \
(doctype, new_fieldname, old_fieldname))
if cint(p.permlevel)==0 and (p.role=="All" or p.role in user_roles)]

Loading…
Cancel
Save