def user_profile_setname(token, name_first, name_last): helper.check_name(name_first, name_last) user_all = storage.load_user_all() u_id = helper.get_id(token, user_all) user_all[str(u_id)]['name_first'] = name_first user_all[str(u_id)]['name_last'] = name_last storage.save_user_all(user_all)
def add_tags(self, tags: tuple, item_id: str): unique_tags = set(tags) for tag_name in unique_tags: tag_id = helper.get_id() self.database_connector.execute( "INSERT INTO Tags (TagId, Tag, ItemId) VALUES (%s, %s, %s)", (tag_id, tag_name.lower(), item_id))
def test_get_id(self): # arrange & act id = helper.get_id() # assert self.assertEqual(type(id), str) self.assertEqual(len(id), 36)
def add_channel(token, channel_id, name, is_public): channel = {} owner = {} data = load_user_all() u_id = helper.get_id(token, data) owner['u_id'] = u_id owner['name_first'] = data[str(u_id)]['name_first'] owner['name_last'] = data[str(u_id)]['name_last'] owner['profile_img_url'] = data[str(u_id)]['profile_img_url'] channel_all = load_channel_all() channel['owner'] = [] channel['owner'].append(owner) channel['name'] = name channel['access'] = is_public channel['member'] = [] channel['messages'] = [] # for use in standup.py channel['standup'] = { # if standup['is_active'] == False, # then 'length' and 'time_finish' should never be accessed. 'is_active': False, 'length': 0, 'time_finish': 0, 'message_queue': '', } channel_all[channel_id] = channel save_channel_all(channel_all)
def standup_send(token, channel_id, message): ##### ALL ERROR CHECKING. channel_all = storage.load_channel_all() ### ERROR: channel_id is not a valid channel. if channel_id not in channel_all: raise InputError() channel = channel_all[channel_id] standup = channel['standup'] ### ERROR: an active standup is not currently running in this channel if standup['is_active'] == False: raise InputError() ### ERROR: message is more than 1000 characters. if len(message) > 1000: raise InputError() ### ERROR: user is not a member of the channel that the standup is occurring in. user_all_data = auth.get_data() u_id = helper.get_id(token, user_all_data) members_list = channel['member'] owners_list = channel['owner'] if u_id not in (members_list or owners_list): raise AccessError() ### ERROR: if because of all the above checks the standup becomes not active, dont send the message. if standup['is_active'] == False: raise InputError() ##### ACTUALLY SENDING THE MESSAGE TO THE QUEUE. user_data = user_all_data[u_id] handlestr = user_data['handlestr'] standup['message_queue'] += generate_standup_message(handlestr, message) storage.save_channel_all() return {}
def channel_leave(token,channel_id): channel_id = str(channel_id) channel_data = get_data() user_data = auth.get_data() u_id = helper.get_id(token,user_data) helper.check_channel(channel_id,channel_data) helper.check_access(u_id,channel_data,channel_id) storage.remove_member(u_id,channel_id)
def test_is_valid_id(self): # arrange id = helper.get_id() invalid_id = "1234" # act & assert self.assertTrue(helper.is_valid_id(id)) self.assertFalse(helper.is_valid_id(invalid_id))
def __init__(self, rider_name): if not rider_name: raise InvalidRiderNameException( "{} is not valid".format(rider_name)) self._id = helper.get_id() self.name = rider_name self._riding = False
def channel_join(token, channel_id): channel_id = str(channel_id) channel_data = get_data() user_data = auth.get_data() # if channel_data[channel_id]['access'] == "False" : print("yeet") helper.check_channel(channel_id,channel_data) helper.check_public_channel(channel_data,channel_id) u_id = helper.get_id(token,user_data) storage.add_member(u_id,channel_id)
def user_profile_setemail(token, email): data = storage.load_user_all() helper.check_email(email) helper.check_email_exist(email, data) user_all = storage.load_user_all() u_id = helper.get_id(token, data) user_all[str(u_id)]['email'] = email storage.save_user_all(user_all)
def channel_invite(token,channel_id,u_id): u_id = str(u_id) channel_id = str(channel_id) data_channel = get_data() data_user = auth.get_data() helper.check_channel(channel_id,data_channel) helper.check_user(u_id,data_user) if data_user[u_id] not in data_channel[channel_id]['member']: helper.check_access(helper.get_id(token,data_user), data_channel, channel_id) storage.add_member(u_id, channel_id)
def test_add_item(self): # arrange item_id = helper.get_id() # act self.item_manager.add_item(item_id, item_id, item_id, setup_test.IMG_BRIGHTNESS) # assert self.assertEqual(setup_test.count_rows( self.database_connector, 'Items'), 1)
def channel_detail(token, channel_id): channel_data = get_data() data = storage.load_user_all() helper.check_channel(channel_id, channel_data) u_id = helper.get_id(token,data) helper.check_access(u_id,channel_data, channel_id) return { "name":channel_data[channel_id]['name'], "owner":channel_data[channel_id]['owner'], "members": channel_data[channel_id]['member'] }
def user_remove(token, u_id): # Check valid user user_all = storage.load_user_all() helper.check_user(u_id, user_all) # The authorised user is not an owner of the slackr t_u_id = str(helper.get_id(token, user_all)) helper.check_user(t_u_id, user_all) delete = [i for i in user_all if i['u_id'] == u_id] user_all[str(u_id)].remove(delete[0]) storage.save_user_all(user_all)
def message_sendlater(token, channel_id, message, time_sent): data = storage.load_channel_all() user_data = auth.get_data() u_id = helper.get_id(token,user_data) helper.check_access(u_id,data,channel_id) u_id = helper.get_id(token, user_data) message_id = helper.channel_id() # Channel ID is not a valid channel if not helper.valid_channel_id(channel_id, data): raise InputError('Channel id is not a valid channel') # helper.check_access(u_id,data, channel_id) if len(message) > 1000: raise InputError("Message should be under 1000 characters.") now = datetime.now() current_time = datetime.timestamp(now) if time_sent < current_time: raise InputError('The time entered is a time in the past') message_data = { 'message_id': message_id, 'u_id':u_id, 'reacts': False, 'is_pinned': False, 'message_text': message, } time = time_sent - current_time timer = threading.Timer(time, storage.add_message(message_data, channel_id), \ [message_data, channel_id]) timer.start() return {'message_id': message_id}
def test_get_item_details(self): # arrange item_id = helper.get_id() self.item_manager.add_item(item_id, item_id, item_id, setup_test.IMG_BRIGHTNESS) # act item_details = self.item_manager.get_item_details(item_id) # assert self.assertEqual(len(item_details), 4) self.assertIn(item_id, item_details['VectorPath']) self.assertIn(item_id, item_details['PngPath']) self.assertEqual(setup_test.IMG_BRIGHTNESS, item_details['ImageBrightness'])
def test_create_arguments(): reset() channel_property = create_channel()['channel_property'] user_data = storage.load_user_all() channel_data = storage.load_channel_all() channel_name = list(channel_data.values())[0]['name'] is_public = list(channel_data.values())[0]['access'] u_id = list(user_data.values())[0]['u_id'] owner_uid = get_id(channel_property['token'],user_data) assert channel_name == channel_property['name'] assert owner_uid == u_id assert is_public == channel_property['is_public']
def message_unreact(token, message_id, react_id): if not helper.check_valid_id(react_id): raise InputError('react_id is not a valid React ID') user_data = auth.get_data() u_id = helper.get_id(token, user_data) channel_data = get_data() react = helper.react_struct(react_id,u_id) for channel_id in channel_data: for message in channel_data[channel_id]['messages']: if message_id == message['message_id']: storage.remove_react(channel_id, message_id,react) return {}
def user_profile_sethandle(token, handle_str): # InputError if length of handle_str invalid user_all = storage.load_user_all() if len(handle_str) < 2 or len(handle_str) > 20: raise error.InputError # InputError if handle is taken by another user u_id = str(helper.get_id(token, user_all)) for user in user_all: if handle_str == user_all[user]['handle']: raise error.InputError user_all = storage.load_user_all() user_all[u_id]['handle'] = handle_str storage.save_user_all(user_all)
def channels_list(token): channel_data = get_data() user_data = auth.get_data() u_id = helper.get_id(token, user_data) channel_list = [] for channel_id in channel_data: for member in channel_data[channel_id]['member']: if u_id == member['u_id']: channel_list.append({ 'channel_id': channel_id, 'name': channel_data[channel_id]['name'] }) return {'channels': channel_list}
def __init__(self, license_no, color="yellow", location=None): if not license_no: raise InvalidTaxiLicenseNumberException( "{} is not valid".format(license_no)) self._license_no = license_no if not color: raise InvalidTaxiColorException("{} is not valid".format(color)) self._color = color if color == "pink": self._category = "pink" else: self._category = "default" self.available = True self.location = location self._id = helper.get_id()
def channel_message(token,channel_id,start): channel_id = str(channel_id) user_data = auth.get_data() u_id = helper.get_id(token, user_data) channel_data = get_data() helper.check_channel(channel_id, channel_data) helper.check_access(u_id, channel_data, channel_id) index = start-1 message = channel_data[channel_id]['messages'] if start > len(message): raise error.InputError message_list = [i for i in message if message.index(i)>= index and message.index(i) <= 50+index] if len(message_list) < 50: end = -1 else: end = 50 return { 'messages': message_list, 'start': start, 'end': end }
def __init__(self, pickup_location, drop_location, rider): self._id = helper.get_id() if pickup_location is not None and type( pickup_location) is not Location: raise InvalidLocationException( "{} is not of Location type".format(pickup_location)) self._pickup_location = pickup_location if drop_location is not None and type(drop_location) is not Location: raise InvalidLocationException( "{} is not of Location type".format(drop_location)) self._drop_location = drop_location if type(rider) is not Rider: raise InvalidRiderException( "{} is not a valid Rider".format(rider)) self._rider = rider self._start_time = datetime.datetime.now() self._end_time = None self._taxi = None self._status = "processing" self._fare = None
def message_send(token,channel_id,message): data = storage.load_channel_all() user_data = auth.get_data() u_id = helper.get_id(token, user_data) helper.check_access(u_id,data,channel_id) message_id = helper.channel_id() # helper.check_access(u_id,data, channel_id) if len(message) > 1000: raise InputError("Message should be under 1000 characters.") time_created = helper.get_current_time_as_unix_timestamp() message_data = { 'message_id': message_id, 'u_id': u_id, 'reacts': [], 'is_pinned': False, 'message_text': message, 'time_created': time_created, } storage.add_message(message_data, channel_id) # channel['messages_list'].prepend(message_data) if message == "/hangman": return -1 return {'message_id': message_id}
def get_unique_id(self): item_id = helper.get_id() while self.item_manager.id_exists(item_id): item_id = helper.get_id() return item_id
def user_profiles_uploadphoto(token, img_url, x_start, y_start, x_end, y_end): # get file from img_url. response = requests.get(img_url, stream=True) if response.status_code != 200: raise error.InputError("Not 200 code!") # make a new file with filename: image12characters.jpg. filename = f'{get_random_alphaNumeric_string(12)}.jpg' # stored in filepath: /pictures. #filepath = f'/Users/uni/Documents/Uni/20T1/Comp1531/Assignments/iteration_1/W17A-17/src/static/pictures/{filename}' filepath = f'{os.getcwd()}/static/pictures/{filename}' with open(filepath, 'wb') as OUT_FILE: # copy data into file without checking its type. shutil.copyfileobj(response.raw, OUT_FILE) del response # check file is a jpg. try: with Image.open(filepath) as IMAGE_FILE: if IMAGE_FILE.format != 'JPEG': # remove the file we created. os.remove(filepath) raise InputError("Image given is not a jpeg.") except IOError: # remove the file we created. os.remove(filepath) raise InputError( "PIL failed to open the file. Likely file isn't a valid image.") x_start = int(x_start) x_end = int(x_end) y_start = int(y_start) y_end = int(y_end) # we now know filepath *is* a jpg image. # check the dimensions of the image. with Image.open(filepath) as IMAGE_FILE: # IMAGE_FILE.size returns tuple (width, height) of IMAGE_FILE. width, height = IMAGE_FILE.size if ((x_end - x_start) >= width) or ((y_end - y_start) >= height): # remove the file we created. os.remove(filepath) raise InputError( "You gave the wrong dimensions for cropping dummy.") # crop image. with Image.open(filepath) as IMAGE_FILE: box = (x_start, y_start, x_end, y_end) CROPPED_IMAGE_FILE = IMAGE_FILE.crop(box) # save cropped image into a new file. new_filename = 'cropped' + filename new_filepath = f'{os.getcwd()}/static/pictures/{new_filename}' CROPPED_IMAGE_FILE.save(new_filepath) # for debugging purposes, don't bother removing the IMAGE_FILE. ### hmmm now how to let front end know where the file is... user_all = storage.load_user_all() u_id = helper.get_id(token, user_all) profile_img_url = 'http://localhost:8050/static/pictures/' + new_filename user_all[str(u_id)]['profile_img_url'] = profile_img_url storage.save_user_all(user_all) # update profile_img_url for the user in all other channels. channel_all = storage.load_channel_all() # woops didn't know channel was the key, not the value. for channel_id in channel_all: channel = channel_all[channel_id] owner_list = channel['owner'] for owner in owner_list: if owner['u_id'] == u_id: owner['profile_img_url'] = profile_img_url member_list = channel['member'] for member in member_list: if member['u_id'] == u_id: member['profile_img_url'] = profile_img_url storage.save_channel_all(channel_all) return {}