Esempio n. 1
0
  def ovr_doctype(self, doclist, ovr, ignore, onupdate):
    doclist = [Document(fielddata = d) for d in doclist]
    doc = doclist[0]
    orig_modified = doc.modified
    cur_doc = Document('DocType',doc.name)
    added = 0
    prevfield = ''
    prevlabel = ''
    idx = 0
    fld_lst = ''
        
    # fields
    # ------
    for d in getlist(doclist, 'fields'):
      fld = ''
      # if exists
      if d.fieldname:
        fld = sql("select name from tabDocField where fieldname=%s and parent=%s", (d.fieldname, d.parent))
      elif d.label: # for buttons where there is no fieldname
        fld = sql("select name from tabDocField where label=%s and parent=%s", (d.label, d.parent))

      if (not fld) and d.label: # must have label
        if prevfield:
          idx = sql("select idx from tabDocField where fieldname = %s and parent = %s",(prevfield,d.parent))[0][0]
        elif prevlabel and not prevfield:
          idx = sql("select idx from tabDocField where label = %s and parent = %s",(prevlabel,d.parent))[0][0]
        sql("update tabDocField set idx = idx + 1 where parent=%s and idx > %s", (d.parent, cint(idx)))          

        # add field
        nd = Document(fielddata = d.fields)
        nd.oldfieldname, nd.oldfieldtype = '', ''
        nd.idx = cint(idx)+1
        nd.save(new = 1, ignore_fields = ignore, check_links = 0)
        fld_lst += "\n"+'Label : '+cstr(d.label)+'   ---   Fieldtype : '+cstr(d.fieldtype)+'   ---   Fieldname : '+cstr(d.fieldname)+'   ---   Options : '+cstr(d.options)
        added += 1
      if d.fieldname:
        prevfield = d.fieldname
        prevlabel = ''
      elif d.label:
        prevfield = ''
        prevlabel = d.label
        
    # Print Formats
    # ------
    for d in getlist(doclist, 'formats'):
      fld = ''
      # if exists
      if d.format:
        fld = sql("select name from `tabDocFormat` where format=%s and parent=%s", (d.format, d.parent))
            
      if (not fld) and d.format: # must have label
        # add field
        nd = Document(fielddata = d.fields)
        nd.oldfieldname, nd.oldfieldtype = '', ''
        nd.save(new = 1, ignore_fields = ignore, check_links = 0)
        fld_lst = fld_lst + "\n"+'Format : '+cstr(d.format)
        added += 1
          
    # code
    # ----
    
    cur_doc.server_code_core = cstr(doc.server_code_core)
    cur_doc.client_script_core = cstr(doc.client_script_core)
    
    cur_doc.save(ignore_fields = ignore, check_links = 0)

    if version=='v160':
      so = get_obj('DocType', doc.name, with_children = 1)
      if hasattr(so, 'on_update'):
        so.on_update()
    elif version=='v170':
      import webnotes.model.doctype
      try: 
        webnotes.model.doctype.update_doctype(so.doclist)
      except: 
        pass
    
    set(doc,'modified',orig_modified)
    
    if in_transaction: sql("COMMIT")
    
    if added == 0:
      added_fields = ''
    else:
      added_fields =  ' <div style="color : RED">Added Fields :- '+ cstr(fld_lst)+ '</div>'
      
    return doc.name + (' Upgraded: %s fields added' % added)+added_fields
Esempio n. 2
0
def merge_doctype(doc_list, ovr, ignore, onupdate):
	import webnotes
	from webnotes.model.doc import Document
	from webnotes.model import doclist
	from webnotes.utils import cint
	from webnotes.utils import cstr
	
	doc_list = [Document(fielddata = d) for d in doc_list]
	
	doc = doc_list[0]
	orig_modified = doc.modified
	cur_doc = Document('DocType',doc.name)
	added = 0
	prevfield = ''
	prevlabel = ''
	idx = 0
	fld_lst = ''
	
	sql = webnotes.conn.sql

	# fields
	# ------
	for d in doclist.getlist(doc_list, 'fields'):
		fld = ''
		# if exists
		if d.fieldname:
			fld = sql("select name from tabDocField where fieldname=%s and parent=%s", (d.fieldname, d.parent))
		elif d.label: # for buttons where there is no fieldname
			fld = sql("select name from tabDocField where label=%s and parent=%s", (d.label, d.parent))

		if (not fld) and d.label: # must have label
			if prevfield:
				idx = sql("select idx from tabDocField where fieldname = %s and parent = %s",(prevfield,d.parent))[0][0]
			elif prevlabel and not prevfield:
				idx = sql("select idx from tabDocField where label = %s and parent = %s",(prevlabel,d.parent))[0][0]
			sql("update tabDocField set idx = idx + 1 where parent=%s and idx > %s", (d.parent, cint(idx)))					

			# add field
			nd = Document(fielddata = d.fields)
			nd.oldfieldname, nd.oldfieldtype = '', ''
			nd.idx = cint(idx)+1
			nd.save(new = 1, ignore_fields = ignore, check_links = 0)
			fld_lst += 'Label : '+cstr(d.label)+'	 ---	 Fieldtype : '+cstr(d.fieldtype)+'	 ---	 Fieldname : '+cstr(d.fieldname)+'	 ---	 Options : '+cstr(d.options)
			added += 1
			
		# clean up
		if d.fieldname:
			prevfield = d.fieldname
			prevlabel = ''
		elif d.label:
			prevfield = ''
			prevlabel = d.label
			
	# Print Formats
	# -------------
	for d in doclist.getlist(doc_list, 'formats'):
		fld = ''
		# if exists
		if d.format:
			fld = sql("select name from `tabDocFormat` where format=%s and parent=%s", (d.format, d.parent))
					
		if (not fld) and d.format: # must have label
			# add field
			nd = Document(fielddata = d.fields)
			nd.oldfieldname, nd.oldfieldtype = '', ''
			nd.save(new = 1, ignore_fields = ignore, check_links = 0)
			fld_lst = fld_lst + '\n'+'Format : '+cstr(d.format)
			added += 1
				
	# code
	# ----
	
	cur_doc.server_code_core = cstr(doc.server_code_core)
	cur_doc.client_script_core = cstr(doc.client_script_core)
	cur_doc.module = doc.module
	cur_doc.save(ignore_fields = ignore, check_links = 0)	
	

	# update schema
	# -------------
	import webnotes.model
	try:
		dl = webnotes.model.doc.get('DocType', doc.name)
		webnotes.model.doctype.update_doctype(dl)
	except:
		pass
	
	webnotes.conn.set(doc,'modified',orig_modified)
	
	if webnotes.conn.in_transaction: sql("COMMIT")
	
	if added == 0:
		added_fields = ''
	else:
		added_fields =	' Added Fields :'+ cstr(fld_lst)
		
	return doc.name + (' Upgraded: %s fields added' % added)+added_fields