popMenu PowerTable - pass parameter in function called

#1

Hi,
I try to create popup Menu and sub Menu in a “onPopupTrigger” script in a Power Table. I get dynamic user in my submenu depending on row seleted in my power table (a row = a project) and I would like to add a user to the selected project.
When I click on a user of my sub menu, I would like to pass the username in the function called (addUser).

Is it possible ?

Thanks

My code below :

import system
	
       def addUser(event,username,id):
		import system
		import sys
		#When selected in user list submenu the function have to get the username
		username= ...

		try:
			user_id=system.db.runPrepQuery("SELECT id FROM ignition_users WHERE username=?",[username],"base_administrateur")[0][0]
		except:
			sys.exit()
		query="INSERT INTO ignition_user_projects (user_id,project_id) VALUES(?,?)"
		system.db.runPrepUpdate(query,[user_id,id],"base_administrateur")
		
		
	
	#Get list of users not added to my project
	id=self.data.getValueAt(self.selectedRow,0) #ID of my project
	query="""SELECT DISTINCT u.username as User FROM ignition_projects p
	JOIN ignition_user_projects up ON p.id=up.project_id
	JOIN ignition_users u ON up.user_id=u.id
	WHERE p.id<>?"""
	ds=system.db.runPrepQuery(query,[id],"base_administrateur")
	users=[]
	usersDef=[]
	for row in ds:
		users.append(row['User'])
		usersDef.append(addUser)
		

	
	#List of usernames not added to my project yet and I would like to
	usersMenu=[users,usersDef]
	
	
	subMenu=[['Add'],[usersMenu]]
	menu = system.gui.createPopupMenu(['Users'],[subMenu])
	menu.show(event)
0 Likes

#2

I highly recommend functools.partial for function composition like this. Basically, leave addUser defined as you have it, then call partial to wrap the function with the required arguments for each instance you’re going to add to the popup menu:

	for row in ds:
		users.append(row['User'])
		usersDef.append(partial(addUser, username=row['User'])
1 Like