def return_to_parent_directory(update, context): """ Moving user back to parent directory of current directory """ current_directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) parent_directory = Directory.objects.get( contains_directories=current_directory, user_id=Directory.encrypt_user_id(update.effective_user.id)) def __handle_successfully_switched(): context.chat_data['current_directory'] = parent_directory.name update.message.reply_text( f"You now switched to directory {parent_directory.name}") def __handle_dir_with_no_parents(): if current_directory.name != ROOT_DIRECTORY: context.chat_data['current_directory'] = ROOT_DIRECTORY logger.error( f"Unexpected accident: directory {current_directory.name} " f"doesn't have a parent directory. User were redirected to root directory" ) update.message.reply_text(f"You now in the root directory") if parent_directory: __handle_successfully_switched() else: __handle_dir_with_no_parents()
def create_directory(update, context, name): """ Create new directory in the current one :param update: Telegram chat data :param context: Telegram chat data :param name: name of a directory to be created """ name = name[0] # check if directory exists if Directory.exists( name, Directory.encrypt_user_id(update.effective_user.id)): update.message.reply_text(f"The directory '{name}' already exists") else: # create new directory new_directory = Directory(name=name, user_id=update.effective_user.id) new_directory.save() # add new directory as a sub directory to current_directory current_directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) current_directory.update( add_to_set__contains_directories=new_directory) current_directory.save() update.message.reply_text( f"The directory '{name}' is successfully created and saved in {current_directory.name} directory" )
def process_keyboard(update, context): """ A callback for go_to_directory() and remove_directory() methods, that perform directory change to selected one """ query = update.callback_query action, directory = query.data.split(',') current_directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) def _goto_handler(directory_name): if current_directory.has_subdirectory( directory_name, Directory.encrypt_user_id(update.effective_user.id)): context.chat_data['current_directory'] = directory_name query.answer() query.edit_message_text( text=f"You now switched to directory {directory_name}") else: context.chat_data['current_directory'] = ROOT_DIRECTORY logger.error( f"Unexpected accident: <directory.has_subdirectory> returned False for directory " f"{directory_name}. current_directory={current_directory}, action={action}, " f"directory={directory_name}. User were redirected to root directory" ) query.edit_message_text( f"Wow, you've broke me somehow..\nSo i switched you to root directory" ) def _delete_handler(directory_name): if current_directory.has_subdirectory( directory_name, Directory.encrypt_user_id(update.effective_user.id)): subdirectory = Directory.objects.get( name=directory_name, user_id=Directory.encrypt_user_id( update.effective_user.id)) subdirectory.delete() query.edit_message_text( f"The directory '{directory_name}' and it's files are successfully deleted from current " f"{current_directory.name} directory") else: query.edit_message_text( f"Seems, the directory '{directory_name}' is already deleted" ) if directory == CANCEL_BUTTON: query.edit_message_text(text=f"Operation were canceled") else: action_map = { DIRECTORY_ACTIONS['goto']: _goto_handler, DIRECTORY_ACTIONS['delete']: _delete_handler, } handler = action_map[action] handler(directory)
def _delete_handler(directory_name): if current_directory.has_subdirectory( directory_name, Directory.encrypt_user_id(update.effective_user.id)): subdirectory = Directory.objects.get( name=directory_name, user_id=Directory.encrypt_user_id( update.effective_user.id)) subdirectory.delete() query.edit_message_text( f"The directory '{directory_name}' and it's files are successfully deleted from current " f"{current_directory.name} directory") else: query.edit_message_text( f"Seems, the directory '{directory_name}' is already deleted" )
def save_photo(update, context): """ Add given photo to current directory """ # create new record about the file in the DB photo = File(telegram_id=update.message.message_id) photo.save() # attach new file to current directory directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) directory.update(add_to_set__contains_files=photo) directory.save() update.message.reply_text( f'Your file now live in directory {directory.name}')
def start(update, context): logger.info('New user joined') root_directory = Directory.objects.get( name=ROOT_DIRECTORY, user_id=Directory.encrypt_user_id(update.effective_user.id)) if root_directory: pass else: Directory(name=ROOT_DIRECTORY, user_id=update.effective_user.id).save() update.message.reply_text( 'Welcome to Telegram Cloud bot. I will help you to store an manage your photos. ' 'Now you are in a root directory. ' 'You can upload your photos here, or create you on directories to ' 'store your files im more manageable way')
def go_to_directory(update, context): """ Create a buttons template. Buttons are names of subdirectories, that current directory contains. By clicking on one of buttons user will be redirected to selected directory """ current_directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) if current_directory.contains_directories: keyboard = FileSystemHandlers.__create_subdirectories_keyboard( current_directory, DIRECTORY_ACTIONS['goto']) update.message.reply_text( 'Choose the folder, that you want go to', reply_markup=InlineKeyboardMarkup(keyboard)) else: update.message.reply_text( f"There are no subdirectories in the directory {context.chat_data.get('current_directory')}" )
def _goto_handler(directory_name): if current_directory.has_subdirectory( directory_name, Directory.encrypt_user_id(update.effective_user.id)): context.chat_data['current_directory'] = directory_name query.answer() query.edit_message_text( text=f"You now switched to directory {directory_name}") else: context.chat_data['current_directory'] = ROOT_DIRECTORY logger.error( f"Unexpected accident: <directory.has_subdirectory> returned False for directory " f"{directory_name}. current_directory={current_directory}, action={action}, " f"directory={directory_name}. User were redirected to root directory" ) query.edit_message_text( f"Wow, you've broke me somehow..\nSo i switched you to root directory" )
def show_subdirectories(update, context): """ Show subdirectories of current directory :param update: Telegram chat data :param context: Telegram chat data """ current_directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) if current_directory.contains_directories: subdirectories = reduce( lambda res, directory: f"{res} {directory.name}", current_directory.contains_directories, "") update.message.reply_text( f"Current directory {context.chat_data.get('current_directory')} " f"contains the following subdirectories: {subdirectories}") else: update.message.reply_text( f"There are no subdirectories in the directory {context.chat_data.get('current_directory')}" )
def show_photo(update, context): """ Send all photos from current directory """ directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) if not directory.contains_files: update.message.reply_text( 'There are no photos stored in current directory') else: for photo in directory.contains_files: try: context.bot.forward_message( chat_id=update.effective_chat.id, from_chat_id=update.effective_chat.id, message_id=File.prepare_telegram_id(photo.telegram_id)) except BadRequest as bad_request: no_message = 'Message to forward not found' if str(bad_request) == no_message: continue else: raise
def remove_directory(update, context): """ Remove selected subdirectory :param update: Telegram chat data :param context: Telegram chat data """ current_directory = Directory.objects.get( name=context.chat_data.get('current_directory'), user_id=Directory.encrypt_user_id(update.effective_user.id)) if current_directory.contains_directories: keyboard = FileSystemHandlers.__create_subdirectories_keyboard( current_directory, DIRECTORY_ACTIONS['delete']) update.message.reply_text( 'Choose the folder, that you want to delete', reply_markup=InlineKeyboardMarkup(keyboard)) else: update.message.reply_text( f"There are no subdirectories in the directory {context.chat_data.get('current_directory')}" )