Example #1
0
def _do_action(doc, doclist, so, method_name, docstatus=0):
	from webnotes.model.code import run_server_obj
	set = webnotes.conn.set

	if so and hasattr(so, method_name):
		errmethod = method_name
		run_server_obj(so, method_name)
		if hasattr(so, 'custom_'+method_name):
			run_server_obj(so, 'custom_'+method_name)
		errmethod = ''

	# set docstatus for all children records
	if docstatus:
		for d in [doc] + doclist:
			if int(d.docstatus or 0) != 2:
				set(d, 'docstatus', docstatus)
Example #2
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
Example #3
0
	def test_run_server_obj(self):
		func_list = dir(self.server_obj)
		print self.server_obj.doc.module
		assert(code.run_server_obj(self.server_obj,'onload'))