def join_users_to_groups(db, how_many_groups_per_user, print_progress=False): """ iterate through each user and join her to a bunch of random groups that were previously created. how_many_groups_per_user = the number of groups that each user should join.""" total_joins=0 user_num=1 total_users=len(db.user_db) progress_interval = calculate_progress_interval(total_users) for user_id, user in db.user_db.root.items(): if print_progress and (user_num % progress_interval == 0): print "Joining %s groups for user %s of %s" % (how_many_groups_per_user, user_num, total_users) user_num+=1 for x in range(how_many_groups_per_user): g = get_random_group(db) if g.can_join(user): api.group_join(g,user) # use instead of g.add_member(user) to make sure we add to UserGroup too get_transaction().commit() total_joins+=1 return total_joins
def join(group_db, child, parent): try: child_group = group_db[child] except KeyError: print '\nGroup "%s" not found' % (child) return try: parent_group = group_db[parent] except KeyError: print '\nGroup "%s" not found' % (parent) return print '\nJoining child "%s" to parent "%s"' % (child_group.display_name(), parent_group.display_name()) # don't do anything if relationship already exists # if child_group.is_member_of_group(parent_group): if parent_group in group_db.member_groups(child_group): print ' Already joined.' return # try joining without joining the owner first try: api.group_join(parent_group, child_group) print ' Group succesfully joined.' except NotEnoughPrivileges: # ok, none of the owners of the child is a member of the parent, # so let's join the first owner of the child group to the parent creator = child_group.owners[0] try: api.group_join(parent_group, creator) print ' Joined user "%s" to "%s"' % (creator.display_name(), parent_group.display_name()) api.group_join(parent_group, child_group) print ' Group succesfully joined.' except NotEnoughPrivileges: print ' Failed because could not join user "%s" to "%s"' % (creator.display_name(), parent_group.display_name()) return transaction_commit()