From 1ea0ab3a39f0429acf7ccdcd873f0e330b994db9 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Fri, 25 May 2012 12:20:18 +0530 Subject: [PATCH] refactor rename method --- py/core/doctype/profile/profile.py | 14 ++ py/webnotes/db.py | 4 +- py/webnotes/model/__init__.py | 66 +-------- py/webnotes/model/doctype.py | 8 +- py/webnotes/model/rename.py | 228 +++++++++++++++++++++++++++++ 5 files changed, 251 insertions(+), 69 deletions(-) create mode 100644 py/webnotes/model/rename.py diff --git a/py/core/doctype/profile/profile.py b/py/core/doctype/profile/profile.py index 4362e60c9b..e5edeea545 100644 --- a/py/core/doctype/profile/profile.py +++ b/py/core/doctype/profile/profile.py @@ -59,3 +59,17 @@ class DocType: def get_fullname(self): return (self.doc.first_name or '') + \ (self.doc.first_name and " " or '') + (self.doc.last_name or '') + + def on_rename(self,newdn,olddn): + tables = webnotes.conn.sql("show tables") + for tab in tables: + desc = webnotes.conn.sql("desc `%s`" % tab[0], as_dict=1) + has_fields = [] + for d in desc: + if d.get('Field') in ['owner', 'modified_by']: + has_fields.append(d.get('Field')) + for field in has_fields: + webnotes.conn.sql("""\ + update `%s` set `%s`=%s + where `%s`=%s""" % \ + (tab[0], field, '%s', field, '%s'), (newdn, olddn)) \ No newline at end of file diff --git a/py/webnotes/db.py b/py/webnotes/db.py index 1422739dd4..3bbeba93e5 100644 --- a/py/webnotes/db.py +++ b/py/webnotes/db.py @@ -131,11 +131,11 @@ class Database: # execute try: if values!=(): - if debug: webnotes.msgprint(query % values) + if debug: webnotes.errprint(query % values) self._cursor.execute(query, values) else: - if debug: webnotes.msgprint(query) + if debug: webnotes.errprint(query) self._cursor.execute(query) except Exception, e: # ignore data definition errors diff --git a/py/webnotes/model/__init__.py b/py/webnotes/model/__init__.py index 47b4369467..ff3e5f71ea 100644 --- a/py/webnotes/model/__init__.py +++ b/py/webnotes/model/__init__.py @@ -136,70 +136,10 @@ def get_search_criteria(dt): # Rename Doc #================================================================================= - -def get_link_fields(dt): - """ - Returns linked fields for dt as a tuple of (linked_doctype, linked_field) - """ - return webnotes.conn.sql("select parent, fieldname from tabDocField where \ - parent not like 'old%%' and parent != '0' and \ - ((options = '%s' and fieldtype='Link') or \ - (options = 'link:%s' and fieldtype='Select'))" % (dt, dt)) - -def rename(dt, old, new, is_doctype=0): - """ - Renames a doc(dt, old) to doc(dt, new) and updates all linked fields of type "Link" or "Select" with "link:" - """ - import webnotes.utils - - sql = webnotes.conn.sql - # rename doc - sql("update `tab%s` set name='%s' where name='%s'" % (dt, new, old)) - - # get child docs - ct = sql("select options from tabDocField where parent = '%s' and fieldtype='Table'" % dt) - for c in ct: - sql("update `tab%s` set parent='%s' where parent='%s'" % (c[0], new, old)) +def rename(doctype, old, new, is_doctype=0, debug=1): + import webnotes.model.rename + webnotes.model.rename.rename(doctype, old, new, is_doctype, debug) - # if dt is a child table of another dt - sql("update `tabDocField` set options = '%s' where options = '%s' and fieldtype = 'Table'" % (new, old)) - - # get links (link / select) - ll = get_link_fields(dt) - update_link_fld_values(ll, old, new) - - # doctype - if dt=='DocType': - # update options and values where select options contains old dt - select_flds = sql("select parent, fieldname from `tabDocField` where parent not like 'old%%' and (options like '%%%s%%' or options like '%%%s%%') and options not like 'link:%%' and fieldtype = 'Select' and parent != '%s'" % ('\n' + old, old + '\n', new)) - update_link_fld_values(select_flds, old, new) - - sql("update `tabDocField` set options = replace(options, '%s', '%s') where parent not like 'old%%' and (options like '%%%s%%' or options like '%%%s%%') and options not like 'link:%%' and fieldtype = 'Select' and parent != '%s'" % (old, new, '\n' + old, old + '\n', new)) - - if not is_single_dt(new): - sql("RENAME TABLE `tab%s` TO `tab%s`" % (old, new)) - else: - sql("update tabSingles set doctype = %s where doctype = %s", (new, old)) - - # get child docs (update parenttype) - ct = sql("select options from tabDocField where parent = '%s' and fieldtype='Table'" % new) - for c in ct: - sql("update `tab%s` set parenttype='%s' where parenttype='%s'" % (c[0], new, old)) - - - -def update_link_fld_values(flds, old, new): - for l in flds: - if is_single_dt(l[0]): - webnotes.conn.sql("update `tabSingles` set value='%s' where field='%s' and value = '%s' and doctype = '%s' " % (new, l[1], old, l[0])) - else: - webnotes.conn.sql("update `tab%s` set `%s`='%s' where `%s`='%s'" % (l[0], l[1], new, l[1], old)) - -def is_single_dt(dt): - is_single = webnotes.conn.sql("select issingle from tabDocType where name = %s", dt) - is_single = is_single and webnotes.utils.cint(is_single[0][0]) or 0 - return is_single - #================================================================================= def clear_recycle_bin(): diff --git a/py/webnotes/model/doctype.py b/py/webnotes/model/doctype.py index 92effe37fd..e653340f5f 100644 --- a/py/webnotes/model/doctype.py +++ b/py/webnotes/model/doctype.py @@ -398,23 +398,23 @@ def get_field_property(dt, fieldname, property): else: return field[0][1] -def get_property(dt, property, fn=None): +def get_property(dt, property, fieldname=None): """ get a doctype property, override it from property setter if specified """ - if fn: + if fieldname: prop = webnotes.conn.sql(""" select value from `tabProperty Setter` where doc_type=%s and field_name=%s - and property=%s""", (dt, fn, property)) + and property=%s""", (dt, fieldname, property)) if prop: return prop[0][0] else: val = webnotes.conn.sql("""\ SELECT %s FROM `tabDocField` WHERE parent = %s AND fieldname = %s""" % \ - (property, '%s', '%s'), (dt, fn)) + (property, '%s', '%s'), (dt, fieldname)) if val and val[0][0]: return val[0][0] or '' else: prop = webnotes.conn.sql(""" diff --git a/py/webnotes/model/rename.py b/py/webnotes/model/rename.py new file mode 100644 index 0000000000..6202e1f0fd --- /dev/null +++ b/py/webnotes/model/rename.py @@ -0,0 +1,228 @@ +import webnotes + +def rename(doctype, old, new, is_doctype=0, debug=0): + """ + 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 + + # get doclist of given doctype + # without child fields of table type fields (form=0) + doclist = webnotes.model.doctype.get(doctype, form=0) + + # 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) + + # update link fields' values + link_fields = get_link_fields(doctype) + update_link_field_values(link_fields, old, new, debug=debug) + + if doctype=='DocType': + # change options for fieldtype Table + update_parent_of_fieldtype_table(old, new, debug=debug) + + # 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) + update_select_field_values(old, new, debug=debug) + + # change parenttype for fieldtype Table + update_parenttype_values(old, new, doclist, debug=debug) + + # rename the table or change doctype of singles + if doclist[0].issingle: + 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)) + + +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" \ + % (child, '%s', '%s'), (new, old), debug=debug) + +def update_link_field_values(link_fields, old, new, debug=0): + """ + updates values in tables where current doc is stored as a + link field or select field + """ + update_list = [] + + # update values + for field in link_fields: + # if already updated, do not do it again + if [field['parent'], field['fieldname']] in update_list: + continue + update_list.append([field['parent'], field['fieldname']]) + if field['issingle']: + webnotes.conn.sql("""\ + update `tabSingles` set value=%s + where doctype=%s and field=%s and value=%s""", + (new, field['parent'], field['fieldname'], old), + debug=debug) + else: + webnotes.conn.sql("""\ + update `tab%s` set `%s`=%s + where `%s`=%s""" \ + % (field['parent'], field['fieldname'], '%s', + field['fieldname'], '%s'), + (new, old), + debug=debug) + +def get_link_fields(doctype, debug=0): + # get link fields from tabDocField + link_fields = webnotes.conn.sql("""\ + select parent, fieldname, + (select ifnull(issingle, 0) from tabDocType dt + where dt.name = df.parent) as issingle + from tabDocField df + where + df.parent not like "old%%%%" and df.parent != '0' and + ((df.options=%s and df.fieldtype='Link') or + (df.options='link:%s' and df.fieldtype='Select'))""" \ + % ('%s', doctype), doctype, as_dict=1, debug=debug) + + # get link fields from tabCustom Field + custom_link_fields = webnotes.conn.sql("""\ + select dt as parent, fieldname, + (select ifnull(issingle, 0) from tabDocType dt + where dt.name = df.dt) as issingle + from `tabCustom Field` df + where + df.dt not like "old%%%%" and df.dt != '0' and + ((df.options=%s and df.fieldtype='Link') or + (df.options='link:%s' and df.fieldtype='Select'))""" \ + % ('%s', doctype), doctype, as_dict=1, debug=debug) + + # add custom link fields list to link fields list + link_fields += custom_link_fields + + # remove fields whose options have been changed using property setter + property_setter_link_fields = webnotes.conn.sql("""\ + select ps.doc_type as parent, ps.field_name as fieldname, + (select ifnull(issingle, 0) from tabDocType dt + where dt.name = ps.doc_type) as issingle + from `tabProperty Setter` ps + where + ps.property_type='options' and + ps.field_name is not null and + (ps.value=%s or ps.value='link:%s')""" \ + % ('%s', doctype), doctype, as_dict=1, debug=debug) + + link_fields += property_setter_link_fields + + return link_fields + +def update_parent_of_fieldtype_table(old, new, debug=0): + webnotes.conn.sql("""\ + update `tabDocField` set options=%s + where fieldtype='Table' and options=%s""", (new, old), debug=debug) + + webnotes.conn.sql("""\ + update `tabCustom Field` set options=%s + where fieldtype='Table' and options=%s""", (new, old), debug=debug) + + webnotes.conn.sql("""\ + update `tabProperty Setter` set value=%s + where property='options' and value=%s""", (new, old), debug=debug) + +def get_select_fields(old, new, debug=0): + """ + get select type fields where doctype's name is hardcoded as + new line separated list + """ + # get link fields from tabDocField + select_fields = webnotes.conn.sql("""\ + select parent, fieldname, + (select ifnull(issingle, 0) from tabDocType dt + where dt.name = df.parent) as issingle + from tabDocField df + where + df.parent not like "old%%%%" and df.parent != '0' and + df.parent != %s and df.fieldtype = 'Select' and + df.options not like "link:%%%%" and + (df.options like "%%%%%s%%%%")""" \ + % ('%s', old), new, as_dict=1, debug=debug) + + # get link fields from tabCustom Field + custom_select_fields = webnotes.conn.sql("""\ + select dt as parent, fieldname, + (select ifnull(issingle, 0) from tabDocType dt + where dt.name = df.dt) as issingle + from `tabCustom Field` df + where + df.dt not like "old%%%%" and df.dt != '0' and + df.dt != %s and df.fieldtype = 'Select' and + df.options not like "link:%%%%" and + (df.options like "%%%%%s%%%%")""" \ + % ('%s', old), new, as_dict=1, debug=debug) + + # add custom link fields list to link fields list + select_fields += custom_select_fields + + # remove fields whose options have been changed using property setter + property_setter_select_fields = webnotes.conn.sql("""\ + select ps.doc_type as parent, ps.field_name as fieldname, + (select ifnull(issingle, 0) from tabDocType dt + where dt.name = ps.doc_type) as issingle + from `tabProperty Setter` ps + where + ps.doc_type != %s and + ps.property_type='options' and + ps.field_name is not null and + ps.value not like "link:%%%%" and + (ps.value like "%%%%%s%%%%")""" \ + % ('%s', old), new, as_dict=1, debug=debug) + + select_fields += property_setter_select_fields + + return select_fields + +def update_select_field_values(old, new, debug=0): + webnotes.conn.sql("""\ + update `tabDocField` set options=replace(options, %s, %s) + where + parent != %s and parent not like "old%%%%" and + fieldtype = 'Select' and options not like "link:%%%%" and + (options like "%%%%\\n%s%%%%" or options like "%%%%%s\\n%%%%")""" % \ + ('%s', '%s', '%s', old, old), (old, new, new), debug=debug) + + webnotes.conn.sql("""\ + update `tabCustom Field` set options=replace(options, %s, %s) + where + parent != %s and parent not like "old%%%%" and + fieldtype = 'Select' and options not like "link:%%%%" and + (options like "%%%%\\n%s%%%%" or options like "%%%%%s\\n%%%%")""" % \ + ('%s', '%s', '%s', old, old), (old, new, new), debug=debug) + + webnotes.conn.sql("""\ + update `tabProperty Setter` set value=replace(value, %s, %s) + where + doc_type != %s and field_name is not null and + property='options' and value not like "link%%%%" and + (value like "%%%%\\n%s%%%%" or value like "%%%%%s\\n%%%%")""" % \ + ('%s', '%s', '%s', old, old), (old, new, new), debug=debug) + +def update_parenttype_values(old, new, doclist, debug=0): + child_doctypes = (d.options for d in doclist + if d.doctype=='DocField' and d.fieldtype=='Table') + + for doctype in child_doctypes: + webnotes.conn.sql("""\ + update `tab%s` set parenttype=%s + where parenttype=%s""" % (doctype, '%s', '%s'), + (new, old), debug=debug) \ No newline at end of file