from __future__ import unicode_literals
import webnotes


@webnotes.whitelist()
def get_children():
	ctype = webnotes.form_dict.get('ctype')
	webnotes.form_dict['parent_field'] = 'parent_' + ctype.lower().replace(' ', '_')
	if not webnotes.form_dict.get('parent'):
		webnotes.form_dict['parent'] = ''
		
	return webnotes.conn.sql("""select name as value, 
		if(is_group='Yes', 1, 0) as expandable
		from `tab%(ctype)s`
		where docstatus < 2
		and ifnull(%(parent_field)s,'') = "%(parent)s"
		order by name""" % webnotes.form_dict, as_dict=1)
		
@webnotes.whitelist()
def add_node():
	from webnotes.model.doc import Document
	ctype = webnotes.form_dict.get('ctype')
	parent_field = 'parent_' + ctype.lower().replace(' ', '_')

	d = Document(ctype)
	d.fields[ctype.lower().replace(' ', '_') + '_name'] = webnotes.form_dict['name_field']
	d.fields[parent_field] = webnotes.form_dict['parent']
	d.is_group = webnotes.form_dict['is_group']
	d.save()
@@ -577,16 +577,18 @@
 ode():%0A%09
+# 
 from web
@@ -619,16 +619,16 @@
 ocument%0A
-
 %09ctype =
@@ -724,40 +724,22 @@
 _')%0A
-%0A%09d = Document(ctype)%0A%09d.
+%09name_
 field
-s%5B
+ = 
 ctyp
@@ -775,19 +775,84 @@
  '_name'
-%5D =
+%0A%09%0A%09doclist = %5B%7B%0A%09%09%22doctype%22: ctype,%0A%09%09%22__islocal%22: 1,%0A%09%09name_field:
  webnote
@@ -880,19 +880,12 @@
 ld'%5D
+,
 %0A%09
-d.fields%5B
+%09
 pare
@@ -892,19 +892,17 @@
 nt_field
-%5D =
+:
  webnote
@@ -926,20 +926,21 @@
 nt'%5D
+,
 %0A%09
-d.
+%09%22
 is_group
  = w
@@ -935,18 +935,18 @@
 is_group
- =
+%22:
  webnote
@@ -970,16 +970,50 @@
 roup'%5D%0A%09
-d
+%7D%5D%0A%09webnotes.model_wrapper(doclist)
 .save()

create sales browser documents through wrapper