Merge branch 'master' of github.com:webnotes/wnframework

This commit is contained in:
Rushabh Mehta 2012-05-30 17:41:53 +05:30
commit 99ccd3fc2e
6 changed files with 333 additions and 81 deletions

View file

@ -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))

View file

@ -3,9 +3,9 @@
# These values are common in all dictionaries
{
'creation': '2012-04-14 17:04:30',
'creation': '2012-05-03 18:43:23',
'docstatus': 0,
'modified': '2012-04-15 08:57:32',
'modified': '2012-05-25 12:46:53',
'modified_by': u'Administrator',
'owner': u'Administrator'
},
@ -34,22 +34,17 @@
'name': '__common__',
'parent': u'Property Setter',
'parentfield': u'fields',
'parenttype': u'DocType',
'permlevel': 0
'parenttype': u'DocType'
},
# These values are common for all DocPerm
{
'cancel': 1,
'create': 1,
'doctype': u'DocPerm',
'name': '__common__',
'parent': u'Property Setter',
'parentfield': u'permissions',
'parenttype': u'DocType',
'permlevel': 0,
'read': 1,
'write': 1
'read': 1
},
# DocType, Property Setter
@ -60,13 +55,38 @@
# DocPerm
{
'cancel': 1,
'create': 1,
'doctype': u'DocPerm',
'role': u'Administrator'
'permlevel': 0,
'role': u'Administrator',
'write': 1
},
# DocPerm
{
'cancel': 1,
'create': 1,
'doctype': u'DocPerm',
'permlevel': 0,
'role': u'System Manager',
'write': 1
},
# DocPerm
{
'cancel': 0,
'create': 0,
'doctype': u'DocPerm',
'permlevel': 1,
'role': u'Administrator',
'write': 1
},
# DocPerm
{
'doctype': u'DocPerm',
'permlevel': 1,
'role': u'System Manager'
},
@ -79,6 +99,7 @@
'label': u'DocType or Field',
'no_column': 0,
'options': u'\nDocField\nDocType',
'permlevel': 0,
'reqd': 1
},
@ -90,7 +111,8 @@
'fieldname': u'select_doctype',
'fieldtype': u'Select',
'label': u'Select DocType',
'no_column': 0
'no_column': 0,
'permlevel': 0
},
# DocField
@ -102,6 +124,7 @@
'fieldtype': u'Select',
'label': u'Select Field',
'no_column': 0,
'permlevel': 0,
'reqd': 0
},
@ -113,7 +136,8 @@
'fieldname': u'select_property',
'fieldtype': u'Select',
'label': u'Select Property',
'no_column': 0
'no_column': 0,
'permlevel': 0
},
# DocField
@ -124,7 +148,8 @@
'doctype': u'DocField',
'fieldname': u'value',
'fieldtype': u'Text',
'label': u'Set Value'
'label': u'Set Value',
'permlevel': 0
},
# DocField
@ -132,6 +157,7 @@
'doctype': u'DocField',
'fieldname': u'column_break0',
'fieldtype': u'Column Break',
'permlevel': 0,
'search_index': 0
},
@ -140,10 +166,12 @@
'colour': u'White:FFF',
'doctype': u'DocField',
'fieldname': u'doc_type',
'fieldtype': u'Read Only',
'fieldtype': u'Link',
'in_filter': 0,
'label': u'DocType',
'no_column': 0,
'options': u'DocType',
'permlevel': 1,
'reqd': 0,
'search_index': 1
},
@ -158,6 +186,7 @@
'fieldtype': u'Read Only',
'in_filter': 0,
'label': u'Field Name',
'permlevel': 0,
'reqd': 0,
'search_index': 1
},
@ -171,6 +200,7 @@
'fieldtype': u'Read Only',
'in_filter': 0,
'label': u'Property',
'permlevel': 0,
'reqd': 1,
'search_index': 1
},
@ -183,7 +213,8 @@
'fieldname': u'property_type',
'fieldtype': u'Read Only',
'label': u'Property Type',
'no_column': 0
'no_column': 0,
'permlevel': 0
},
# DocField
@ -194,6 +225,7 @@
'fieldname': u'default_value',
'fieldtype': u'Read Only',
'label': u'Default Value',
'no_column': 0
'no_column': 0,
'permlevel': 0
}
]

View file

@ -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

View file

@ -136,70 +136,19 @@ def get_search_criteria(dt):
# Rename Doc
#=================================================================================
def rename(doctype, old, new, is_doctype=0, debug=1):
import webnotes.model.rename_doc
webnotes.model.rename_doc.rename_doc(doctype, old, new, is_doctype, debug)
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))
import webnotes.model.rename_doc
link_fields = webnotes.model.rename_doc.get_link_fields(dt)
link_fields = [[lf['parent'], lf['fieldname']] for lf in link_fields]
return link_fields
# 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():

View file

@ -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("""

View file

@ -0,0 +1,257 @@
import webnotes
def rename_doc(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)
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':
# 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")
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
dt != %s and dt 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, debug=0):
child_doctypes = webnotes.conn.sql("""\
select options, fieldname from `tabDocField`
where parent=%s and fieldtype='Table'""", new, as_dict=1, debug=debug)
custom_child_doctypes = webnotes.conn.sql("""\
select options, fieldname from `tabCustom Field`
where dt=%s and fieldtype='Table'""", new, as_dict=1, debug=debug)
child_doctypes += custom_child_doctypes
fields = [d['fieldname'] for d in child_doctypes]
property_setter_child_doctypes = webnotes.conn.sql("""\
select value as options from `tabProperty Setter`
where doc_type=%s and property='options' and
field_name in ("%s")""" % ('%s', '", "'.join(fields)),
new, debug=debug)
child_doctypes += property_setter_child_doctypes
child_doctypes = (d['options'] for d in child_doctypes)
for doctype in child_doctypes:
webnotes.conn.sql("""\
update `tab%s` set parenttype=%s
where parenttype=%s""" % (doctype, '%s', '%s'),
(new, old), debug=debug)