def get_user_input(input_for): try: global DEBUG if input_for == 1: global headings temp = enumerate(headings) for enum in temp: print("{}) {}".format((enum[0] + 1), enum[1])) #helper function takes care of the checking value = helper.get_user_data( question= "Please enter an integer based on one of the ennumeration EG 1 for Zip Code: ", validate_func=helper.validate_user_input, alert="Please enter a valid number betweeen 1 and 7", counter=0) # Just a design desion we dont ask the user to enter value from 0 # but start from 1 and we adjust it here value = value - 1 elif input_for == 2: value = helper.get_user_data( question="Please enter a floor value: ", validate_func=helper.validate_floor_value, alert="Please enter a valid floor value", counter=0) return value except Exception as e: print(str(e)) print("An error occured please re-enter a valid value") sys.exit(1)
def user_remove(): '''admin removes a user from slackr''' data = get_data() payload = request.get_json() token = payload['token'] u_id = int(payload['u_id']) check_token(token) user_data = get_user_data(u_id) if user_data == {}: raise InputError(description='No such user exists') person_u_id = get_user_from_token(token) person_data = get_user_data(person_u_id) if person_data['permission_id'] != 1: raise AccessError(description='The authorised person is not an owner of slackr') user_info = { 'u_id': u_id, 'name_first': user_data['name_first'], 'name_last': user_data['name_last'], } for channel in data['channels']: if user_info in channel['owner_members']: curr_channel_id = channel['channel_id'] data['channels'][curr_channel_id - 1]['owner_members'].remove(user_info) data['channels'][curr_channel_id - 1]['all_members'].remove(user_info) elif user_info in channel['all_members']: curr_channel_id = channel['channel_id'] data['channels'][curr_channel_id - 1]['all_members'].remove(user_info) dumps({})
def channel_leave(token, channel_id): ''' User leaves a channel. Parameters: token - channel_id - Returns: {} Errors: InputError: Invalid channel id. AccessError: User is not inside channel. ''' check_token(token) data = get_data() if not find_id_in_list(data['channels'], channel_id, 'channel_id'): raise InputError(description='Channel does not exist') current_channel = data['channels'][channel_id - 1] target_u_id = get_user_from_token(token) if find_id_in_list(current_channel['all_members'], target_u_id, 'u_id'): target = get_user_data(target_u_id) user_info = { 'u_id': target['u_id'], 'name_first': target['name_first'], 'name_last': target['name_last'], } data['channels'][channel_id - 1]['all_members'].remove(user_info) return {} # the authorised user is not a member of channel with channel_id raise AccessError( description='The authorised user is not a member of this channel')
def channel_join(token, channel_id): ''' User joins a channel. Parameters: token - channel_id - Returns: {} Errors: InputError: Invalid channel id. AccessError: Channel is private. ''' check_token(token) data = get_data() if not find_id_in_list(data['channels'], channel_id, 'channel_id'): # Channel ID is not a valid channel raise InputError(description='Channel does not exist') current_channel = data['channels'][channel_id - 1] target_u_id = get_user_from_token(token) target = get_user_data(target_u_id) if not current_channel['is_public'] and target['permission_id'] != 1: raise AccessError(description='Private channel') user_info = { 'u_id': target['u_id'], 'name_first': target['name_first'], 'name_last': target['name_last'], } data['channels'][channel_id - 1]['all_members'].append(user_info) return {}
def channel_addowner(token, channel_id, u_id): ''' Add a member of channel to owner list. Parameters: token - channel_id - u_id - Returns: {} Errors: InputError: Invalid channel id. User is already an owner. AccessError: The authorised person is not an owner of channel or owner of slackr. ''' check_token(token) data = get_data() if not find_id_in_list(data['channels'], channel_id, 'channel_id'): raise InputError(description='Channel does not exist') current_channel = data['channels'][channel_id - 1] if find_id_in_list(current_channel['owner_members'], u_id, 'u_id'): raise InputError( description='User is already an owner of this channel') person_u_id = get_user_from_token(token) person = get_user_data(person_u_id) person_is_owner = find_id_in_list(current_channel['owner_members'], person_u_id, 'u_id') person_in_channel = find_id_in_list(current_channel['all_members'], person_u_id, 'u_id') if person_is_owner or (person_in_channel and person['permission_id'] == 1): target = get_user_data(u_id) user_info = { 'u_id': target['u_id'], 'name_first': target['name_first'], 'name_last': target['name_last'], } data['channels'][channel_id - 1]['owner_members'].append(user_info) if not find_id_in_list(current_channel['all_members'], u_id, 'u_id'): data['channels'][channel_id - 1]['all_members'].append(user_info) return {} raise AccessError(description='The authorised person has no permission')
def userpermission_change(): '''admian change user's permission id''' data = get_data() payload = request.get_json() token = payload['token'] check_token(token) u_id = int(payload['u_id']) permission_id = int(payload['permission_id']) if u_id is None or permission_id is None: raise InputError(description='Empty input') if permission_id != 1 and permission_id != 2: raise InputError(description='Invalid permission id') if get_user_data(u_id) == {}: raise InputError(description='No such user exists') for user in data['users']: if user['u_id'] == u_id: user['permission_id'] = permission_id return dumps({})
def channel_invite(token, channel_id, u_id): ''' Invite someone to this channel. Parameters: token - channel_id - u_id - Returns: {} Errors: InputError: Invalid channel id, Invalid u_id. AccessError: The authorised person is not inside this channel. ''' check_token(token) data = get_data() if not find_id_in_list(data['channels'], channel_id, 'channel_id'): raise InputError(description='Channel does not exist') if u_id > get_max_u_id() or u_id <= 0: raise InputError(description='u_id does not refer to a valid user') current_channel = data['channels'][channel_id - 1] person_u_id = get_user_from_token(token) # Check if authorised person is in channel with channel_id for user in current_channel['all_members']: if user['u_id'] == person_u_id: target = get_user_data(u_id) user_info = { 'u_id': target['u_id'], 'name_first': target['name_first'], 'name_last': target['name_last'], } data['channels'][channel_id - 1]['all_members'].append(user_info) return {} # the authorised user is not already a member of the channel raise AccessError( description='The authorised user is not a member of this channel')
def channel_details(token, channel_id): ''' Show details to a user. Parameters: token - channel_id - Returns: a dict including channel name, owner member list, and member list. Errors: InputError: Invalid channel id. AccessError: The authorised person is not inside this channel. ''' check_token(token) data = get_data() if not find_id_in_list(data['channels'], channel_id, 'channel_id'): raise InputError(description='Channel does not exist') current_channel = data['channels'][channel_id - 1] person_u_id = get_user_from_token(token) for user in current_channel['all_members']: if user['u_id'] == person_u_id: for member in current_channel['all_members']: info = get_user_data(user['u_id']) member['profile_img_url'] = info['profile_img_url'] # Output { name, owner_members, all_members } details = { 'name': current_channel['name'], 'owner_members': current_channel['owner_members'], 'all_members': current_channel['all_members'], } return details raise AccessError( description='he authorised user is not a member of this channel')
def test_get_user_data_Bad(): userID = 1235 assert json.loads(json.dumps(helper.get_user_data(userID)))["error"] == 1
def test_get_user_data_Well(): userID = 1 assert json.loads(json.dumps(helper.get_user_data(userID)))["error"] == 0