def testParseUsernames(self): # Form field was not present in post data. id_set = project_helpers.ParseUsernames(self.cnxn, self.services.user, None) self.assertEqual(set(), id_set) # Form field was present, but empty. id_set = project_helpers.ParseUsernames(self.cnxn, self.services.user, '') self.assertEqual(set(), id_set) # Parsing valid user names. id_set = project_helpers.ParseUsernames( self.cnxn, self.services.user, '[email protected], [email protected]') self.assertEqual({111L, 333L}, id_set)
def ProcessChangeOwnership(self, mr, post_data): new_owner_id_set = project_helpers.ParseUsernames( mr.cnxn, self.services.user, post_data.get('changeowners')) remain_as_editor = post_data.get('becomeeditor') == 'on' if len(new_owner_id_set) != 1: mr.errors.transfer_ownership = ( 'Please add one valid user email.') else: new_owner_id = new_owner_id_set.pop() if self.services.features.LookupHotlistIDs( mr.cnxn, [mr.hotlist.name], [new_owner_id]): mr.errors.transfer_ownership = ( 'This user already owns a hotlist with the same name') if mr.errors.AnyErrors(): self.PleaseCorrect( mr, initial_new_owner_username=post_data.get('changeowners'), open_dialog=ezt.boolean(True)) else: old_and_new_owner_ids = [new_owner_id] + mr.hotlist.owner_ids (_, editor_ids, follower_ids) = hotlist_helpers.MembersWithoutGivenIDs( mr.hotlist, old_and_new_owner_ids) if remain_as_editor and mr.hotlist.owner_ids: editor_ids.append(mr.hotlist.owner_ids[0]) self.services.features.UpdateHotlistRoles( mr.cnxn, mr.hotlist_id, [new_owner_id], editor_ids, follower_ids) hotlist = self.services.features.GetHotlist(mr.cnxn, mr.hotlist_id) hotlist_url = hotlist_helpers.GetURLOfHotlist( mr.cnxn, hotlist, self.services.user) return framework_helpers.FormatAbsoluteURL( mr,'%s%s' % (hotlist_url, urls.HOTLIST_PEOPLE), saved=1, ts=int(time.time()), include_project=False)
def ProcessAddMembers(self, mr, post_data): """Process the user's request to add members. Args: mr: common information parsed from the HTTP request. post_data: dictionary of form data. Returns: String URL to redirect the user to after processing. """ # 1. Gather data from the request. group_id = mr.viewed_user_auth.user_id add_members_str = post_data.get('addmembers') new_member_ids = project_helpers.ParseUsernames( mr.cnxn, self.services.user, add_members_str) role = post_data['role'] # 2. Call services layer to save changes. if not mr.errors.AnyErrors(): try: self.services.usergroup.UpdateMembers( mr.cnxn, group_id, new_member_ids, role) except usergroup_svc.CircularGroupException: mr.errors.addmembers = ( 'The members are already ancestors of current group.') # 3. Determine the next page in the UI flow. if mr.errors.AnyErrors(): self.PleaseCorrect( mr, initial_add_members=add_members_str, initially_expand_form=ezt.boolean(True)) else: return framework_helpers.FormatAbsoluteURL( mr, '/g/%s/' % mr.viewed_username, include_project=False, saved=1, ts=int(time.time()))
def ProcessAddMembers(self, mr, post_data): """Process the user's request to add members. Args: mr: common information parsed from the HTTP request. post_data: dictionary of form data. Returns: String URL to redirect the user to after processing. """ # 1. Parse and validate user input. new_member_ids = project_helpers.ParseUsernames( mr.cnxn, self.services.user, post_data.get('addmembers')) role = post_data['role'] (owner_ids, committer_ids, contributor_ids) = project_helpers.MembersWithGivenIDs( mr.project, new_member_ids, role) total_people = len(owner_ids) + len(committer_ids) + len( contributor_ids) if total_people > framework_constants.MAX_PROJECT_PEOPLE: mr.errors.addmembers = ( 'Too many project members. The combined limit is %d.' % framework_constants.MAX_PROJECT_PEOPLE) # 2. Call services layer to save changes. if not mr.errors.AnyErrors(): self.services.project.UpdateProjectRoles(mr.cnxn, mr.project.project_id, owner_ids, committer_ids, contributor_ids) # 3. Determine the next page in the UI flow. if mr.errors.AnyErrors(): add_members_str = post_data.get('addmembers', '') self.PleaseCorrect(mr, initial_add_members=add_members_str, initially_expand_form=True) else: return framework_helpers.FormatAbsoluteURL( mr, urls.PEOPLE_LIST, saved=1, ts=int(time.time()), new=','.join([str(u) for u in new_member_ids]))
def ProcessAddMembers(self, mr, post_data, hotlist_url): """Process the user's request to add members. Args: mr: common information parsed from the HTTP request. post_data: dictionary of form data hotlist_url: hotlist_url to return to after data has been processed. Returns: String URL to redirect the user to after processing """ # NOTE: using project_helpers function new_member_ids = project_helpers.ParseUsernames( mr.cnxn, self.services.user, post_data.get('addmembers')) if not new_member_ids or not post_data.get('addmembers'): mr.errors.incorrect_email_input = ( 'Please give full emails seperated by commas.') role = post_data['role'] (owner_ids, editor_ids, follower_ids) = hotlist_helpers.MembersWithGivenIDs( mr.hotlist, new_member_ids, role) # TODO(jojwang): implement MAX_HOTLIST_PEOPLE if not owner_ids: mr.errors.addmembers = ( 'Cannot have a hotlist without an owner; please leave at least one.' ) if mr.errors.AnyErrors(): add_members_str = post_data.get('addmembers', '') self.PleaseCorrect(mr, initial_add_members=add_members_str, initially_expand_form=True) else: self.services.features.UpdateHotlistRoles(mr.cnxn, mr.hotlist_id, owner_ids, editor_ids, follower_ids) return framework_helpers.FormatAbsoluteURL( mr, '%s%s' % (hotlist_url, urls.HOTLIST_PEOPLE), saved=1, ts=int(time.time()), new=','.join([str(u) for u in new_member_ids]), include_project=False)