Пример #1
0
	def setUp(self):
		class test_old_class():
			"""
			Dummy Hack to test for get_server_obj
			"""
			pass
			
		self.dummy_class = test_old_class
		doc_obj = Document('DocType','file')
		self.server_obj = code.get_server_obj(doc_obj)
Пример #2
0
	def run_on_update(self):
		from webnotes.model.code import get_server_obj
		so = get_server_obj(self.doc, self.doclist)
		if hasattr(so, 'on_update'):
			so.on_update()
Пример #3
0
	def run_on_update(self):
		from webnotes.model.code import get_server_obj
		so = get_server_obj(self.doc, self.doclist)
		if hasattr(so, 'on_update'):
			so.on_update()
Пример #4
0
def savedocs():
	import webnotes.model.doclist

	from webnotes.model.code import get_server_obj
	from webnotes.model.code import run_server_obj
	import webnotes.utils

	from webnotes.utils import cint

	sql = webnotes.conn.sql
	form = webnotes.form

	# action
	action = form.getvalue('action')
	
	# get docs	
	doc, doclist = _get_doclist(webnotes.model.doclist.expand(form.getvalue('docs')))

	# get server object	
	server_obj = get_server_obj(doc, doclist)

	# check integrity
	if not check_integrity(doc):
		return
	
	if not doc.check_perm(verbose=1):
		webnotes.msgprint("Not enough permission to save %s" % doc.doctype)
		return
	
	# validate links
	ret = webnotes.model.doclist.validate_links_doclist([doc] + doclist)
	if ret:
		webnotes.msgprint("[Link Validation] Could not find the following values: %s. Please correct and resave. Document Not Saved." % ret)
		return

	# saving & post-saving
	try:
		# validate befor saving and submitting
		if action in ('Save', 'Submit') and server_obj:
			if hasattr(server_obj, 'validate'):	
				t = run_server_obj(server_obj, 'validate')
			if hasattr(server_obj, 'custom_validate'):
				t = run_server_obj(server_obj, 'custom_validate')
				
		# set owner and modified times
		is_new = cint(doc.fields.get('__islocal'))
		if is_new and not doc.owner:
			doc.owner = form.getvalue('user')
		
		doc.modified, doc.modified_by = webnotes.utils.now(), webnotes.session['user']
		
		# save main doc
		try:
			t = doc.save(is_new)
		except NameError, e:
			webnotes.msgprint('%s "%s" already exists' % (doc.doctype, doc.name))
			if webnotes.conn.sql("select docstatus from `tab%s` where name=%s" % (doc.doctype, '%s'), doc.name)[0][0]==2:
				webnotes.msgprint('[%s "%s" has also been trashed / cancelled]' % (doc.doctype, doc.name))
			webnotes.errprint(webnotes.utils.getTraceback())
			raise e
		
		# save child docs
		for d in doclist:
			deleted, local = d.fields.get('__deleted',0), d.fields.get('__islocal',0)
	
			if cint(local) and cint(deleted):
				pass
			elif d.fields.has_key('parent'):
				if d.parent and (not d.parent.startswith('old_parent:')):
					d.parent = doc.name # rename if reqd
					d.parenttype = doc.doctype
				d.modified, d.modified_by = webnotes.utils.now(), webnotes.session['user']
				d.save(new = cint(local))
	
		# on_update
		if action in ('Save','Submit') and server_obj:
			# special for DocType, DocType
			if doc.doctype == 'DocType':
				import webnotes.model.doctype
				webnotes.model.doctype.update_doctype([doc] + doclist)
				
			else:				
				if hasattr(server_obj, 'on_update'):
					t = run_server_obj(server_obj, 'on_update')
					if t: webnotes.msgprint(t)
	
				if hasattr(server_obj, 'custom_on_update'):
					t = run_server_obj(server_obj, 'custom_on_update')
					if t: webnotes.msgprint(t)
				
		# on_submit
		if action == 'Submit':
			_do_action(doc, doclist, server_obj, 'on_submit', 1)

		# on_submit
		if action == 'Update':
			_do_action(doc, doclist, server_obj, 'on_update_after_submit', 0)
				
		# on_cancel
		if action == 'Cancel':
			_do_action(doc, doclist, server_obj, 'on_cancel', 2)
		
		# On Trash
		if action == 'Trash':
			_do_action(doc, doclist, server_obj, 'on_trash', 2)
			# validate
			validate_trash_doc(doc, doclist)

		# update recent documents
		webnotes.user.update_recent(doc.doctype, doc.name)

		# send updated docs
		webnotes.response['saved'] = '1'
		webnotes.response['main_doc_name'] = doc.name
		webnotes.response['docname'] = doc.name
		webnotes.response['docs'] = [doc] + doclist
Пример #5
0
def set_doc(doclist, ovr=0, ignore=1, onupdate=1):
	import webnotes
	from webnotes.model.doc import Document
	from webnotes.model.code import get_obj
	from webnotes.model.code import get_server_obj
	from webnotes.model.meta import get_table_fields

	sql = webnotes.conn.sql

	if not doclist:
		return 'No Doclist'

	doc = Document(fielddata = doclist[0])
	orig_modified = doc.modified

	exists = webnotes.conn.exists(doc.doctype, doc.name)
	#print doc.doctype, doc.name
	if not webnotes.conn.in_transaction: 
		sql("START TRANSACTION")
	
	if exists:
		 
		if ovr:
			if doc.doctype == 'DocType':
				return merge_doctype(doclist, ovr, ignore, onupdate) 

			if doc.doctype == 'DocType Mapper':
				return merge_mapper(doclist, ovr, ignore, onupdate)

			if doc.doctype == 'Module Def':
				return merge_module_def(doclist, ovr, ignore, onupdate)
			
					
			# check modified timestamp
			# ------------------------
			ts = sql("select modified from `tab%s` where name=%s" % (doc.doctype, '%s'), doc.name)[0][0]
			if str(ts)==doc.modified:
				return doc.name + ": No update"

			# Replace the record
			# ------------------

			# remove main doc
			newname = '__overwritten:'+doc.name
			n_records = sql("SELECT COUNT(*) from `tab%s` WHERE name like '%s%%'" % (doc.doctype, newname))
			if n_records[0][0]:
				newname = newname + '-' + str(n_records[0][0])
				
			sql("UPDATE `tab%s` SET name='%s', docstatus=2 WHERE name = '%s' limit 1" % (doc.doctype, newname, doc.name))
			
			# remove child elements
			tf_list = get_table_fields(doc.doctype)
			for t in tf_list:
				sql("UPDATE `tab%s` SET parent='%s', docstatus=2 WHERE parent='%s' AND parentfield='%s'" % (t[0], 'oldparent:'+doc.name, doc.name, t[1]))
				
		else:
			if webnotes.conn.in_transaction: 
				sql("ROLLBACK")
			return doc.name + ": Exists / No change"

	# save main
	check_links = 1	
	if doc.doctype == 'Patch':
		doc.save(make_autoname = 0, new = 1, ignore_fields = ignore, check_links=0)
	else:
		doc.save(new = 1, ignore_fields = ignore, check_links=0)
	
	# save others
	dl = [doc]
	for df in doclist[1:]:
		try:
			d = Document(fielddata = df)
			d.save(new = 1, ignore_fields = ignore, check_links=0)
			dl.append(d)
		except:
			pass # ignore tables
	
	if onupdate:
		if doc.doctype=='DocType':
			import webnotes.model.doctype
			webnotes.model.doctype.update_doctype(dl)
		else:
			so = get_server_obj(doc, dl)
			if hasattr(so, 'on_update'):
				so.on_update()
			
		
	# reset modified
	webnotes.conn.set(doc, 'modified', orig_modified)

	if webnotes.conn.in_transaction: 
		sql("COMMIT")

	return doc.name + ': Completed'