def get_data(): try: data = storage.load_channel_all() except Exception: storage.new_storage() data = storage.load_channel_all() return data
def standup_start(token, channel_id, length): 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 currently running in this channel. if standup['is_active'] == True: raise InputError() ### we ensure that is_active is set to true AFTER finding the rest of the information. standup['length'] = length # finding time_finish (a Unix timestamp). now = helper.get_current_time_as_datetime() now_plus_length = now + datetime.timedelta(seconds=length) time_finish = helper.convert_datetime_to_unix_timestamp(now_plus_length) standup['time_finish'] = time_finish standup['message_queue'] = '' ### start standup. standup['is_active'] = True ### sleep for length seconds (during this period standup_send() calls will be successful). time.sleep(length) ### stop standup. standup['is_active'] = False ### send the standup['message_queue'] to the channel. message_functions.message_send(token, channel_id, standup['message_queue']) storage.save_channel_all(channel_all) return { 'time_finish': time_finish, }
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 message_unpin(token,message_id): channels_all = storage.load_channel_all() message = helper.get_message_dictionary(message_id,channels_all) if message['is_pinned'] == False: raise InputError() message['is_pinned'] = False storage.save_channel_all(channels_all) return {}
def message_remove(token,message_id): channels_all = storage.load_channel_all() messages = helper.get_messages_list_containing_message(message_id,channels_all) message = helper.get_message_dictionary(message_id,channels_all) ### ERROR: message no longer exists. if message == {}: raise InputError() # remove message from the messages list. messages.remove(message) storage.save_channel_all(channels_all) return {}
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 test_channel_join(): reset() channel_data = create_channel() token = channel_data['owner_data']['token'] u_id = channel_data['owner_data']['u_id'] channel_id = channel_data['channel_id'] data = { 'token': token, 'channel_id': channel_id } requests.post(url = f"{BASE_URL}/channel/join",json = data).raise_for_status() data1 = storage.load_channel_all() # print(data1[str(channel_id)]['member'][0]["u_id"]) # print(u_id) assert data1[str(channel_id)]['member'][0]["u_id"] == u_id
def message_search(token, query_str): channels_all = storage.load_channel_all() u_id = helper.get_u_id_from_token(token) messages = [] for channel in channels_all: for message in channel['messages']: if message['u_id'] != u_id: continue # so message is a message the user has posted themself. if query_str in message['message_text']: messages.append(message) # sort messages list by the 'time_created' key. messages.sort(key=return_time_created) return { 'messages': messages, }
def standup_active(token, channel_id): channel_all = storage.load_channel_all() ### ERROR: channel_id is not a valid channel. if channel_id not in channel_all: raise InputError() ### finding is_active. channel = channel_all[channel_id] standup = channel['standup'] is_active = standup['is_active'] ### finding time_finish. if is_active == True: time_finish = standup['time_finish'] else: time_finish = None return { 'is_active': is_active, 'time_finish': time_finish, }
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_channel_invite(): reset() channel_data = create_channel() token = channel_data['owner_data']['token'] channel_id = channel_data['channel_id'] data = { "name_first":"elnaz", "name_last":"tavakol", "email":"*****@*****.**", "password":"******" } data_register = json.loads(requests.post(url = f"{BASE_URL}/auth/register",json = data).content) u_id = data_register['u_id'] data = { "token":token, "u_id":u_id, "channel_id":channel_id } requests.post(url = f"{BASE_URL}/channel/invite",json = data) data1 = storage.load_channel_all() # print(data1[str(channel_id)]['member'][0]["u_id"]) assert int(data1[str(channel_id)]['member'][0]["u_id"]) == int(u_id)
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 message_edit(token,message_id,message): channels_all = storage.load_channel_all() message_dict = helper.get_message_dictionary(message_id,channels_all) message_dict['message_text'] = message storage.save_channel_all(channels_all) return {}
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 {}