Hi,
I have a df with cl1 and cl2. cl1 displays the group name and cl2 the users which should be added to the group in cl1.
I am trying to loop through the df but got issues when the users should be added to the group.
Also when I leave out the index at groups.add_users(), it gives me the error:
'list' object has no attribute 'add_users'
for i in df['c1']:
groups = gis.groups.search('title: "'+i+'"', '')
print(groups)
for row in df['c2']:
for i in groups:
groups[i].add_users(row)
e.g.:
cl1 | cl2 |
group_xy | user_1, user_2 |
group_b | user_3, user_4, user_5 |
Am I forgetting something? Would be very grateful if anyone know of a solution or what I am doing wrong.
Thank you!
Solved! Go to Solution.
Hi @mylktea
This will add the users in cl2 to the groups in cl1
group_lst = df["cl1"].tolist()
## for each group name in the list
for group in group_lst:
## get the string content of cl2 cell
users = df.loc[df['cl1'] == group, 'cl2'].iloc[0]
## remove any spaces (this is to remove the space after each
## comma in your cl2) omit the .replace if your usernames contain spaces
## easier to make sure the usernames in cl2 follow
## User_1,User_2,User_3 with no spaces in the text
## the .split creates the list to add users
users = users.replace(" ", "").split(",")
add2group = portal.groups.search(group)[0]
## by printing you will given a dictionary of any failures to add
print(add2group.add_users(users))
Hi @mylktea
This will add the users in cl2 to the groups in cl1
group_lst = df["cl1"].tolist()
## for each group name in the list
for group in group_lst:
## get the string content of cl2 cell
users = df.loc[df['cl1'] == group, 'cl2'].iloc[0]
## remove any spaces (this is to remove the space after each
## comma in your cl2) omit the .replace if your usernames contain spaces
## easier to make sure the usernames in cl2 follow
## User_1,User_2,User_3 with no spaces in the text
## the .split creates the list to add users
users = users.replace(" ", "").split(",")
add2group = portal.groups.search(group)[0]
## by printing you will given a dictionary of any failures to add
print(add2group.add_users(users))
Thank you - it worked!