def run(self): selected_option = self.show(self.logged_user) returned_information = self.selection(selected_option, self.logged_user) while True: if isinstance(returned_information, dict) and 'command' in returned_information.keys(): command_is_string = isinstance(returned_information['command'], str) else: command_is_string = False if command_is_string and returned_information[ 'command'] == 'home_logged_perfil': return 'home_logged_perfil' elif command_is_string and returned_information[ 'command'] == 'outside_account': return 'outside_account' elif command_is_string and returned_information[ 'command'] == 'show_another_perfil': selected_option = self.show(returned_information['object']) returned_information = self.selection( selected_option, returned_information['object']) elif command_is_string and returned_information[ 'command'] == 'wrong_selection': selected_option = self.show( returned_information['object'], information_message=returned_information[ 'information message']) returned_information = self.selection( selected_option, returned_information['object']) elif command_is_string and returned_information[ 'command'] == 'access_user': selected_option = self.show(returned_information['object']) returned_information = self.selection( selected_option, returned_information['object']) elif command_is_string and returned_information[ 'command'] == 'access_post': PostView(self.logged_user, displayed_post=returned_information['object'] )._show_selected_post(returned_information['object']) selected_option = self.show( User.get_user_instance( returned_information['object'].author_id)) returned_information = self.selection( selected_option, User.get_user_instance( returned_information['object'].author_id)) else: selected_option = self.show(self.logged_user) returned_information = self.selection(selected_option, self.logged_user)
def login_user(self, user_name, password): cursor = self.connection.start_database_connection() try: user_name = str(user_name) password = str(password) cursor.execute("select * from perfil where nome_usuario=%s", [user_name]) user_as_list = cursor.fetchall() if user_as_list: user = User(user_as_list[0]) else: user = None # Se a senha do usuário for diferente da senha digitada no campo: if user and user.password != password: raise WrongPasswordException() self.connection.close_database_connection() return user except ValueError: self.connection.close_database_connection() raise ValueError except WrongPasswordException: self.connection.close_database_connection() raise WrongPasswordException except Exception: self.connection.close_database_connection() raise Exception
def search_users(self, user_name_fragment): cursor = self.connection.start_database_connection() try: user_name_fragment = str(user_name_fragment) search_parameter = '%' + user_name_fragment + '%' cursor.execute("select * from perfil where nome_usuario like %s", [search_parameter]) users_as_list = cursor.fetchall() users = [] for user_as_list in users_as_list: users.append(User(user_as_list)) self.connection.close_database_connection() return users except ValueError: self.connection.close_database_connection() raise ValueError except WrongPasswordException: self.connection.close_database_connection() raise WrongPasswordException except Exception: self.connection.close_database_connection() raise Exception
def register_user(self, user_name, complete_name, password): cursor = self.connection.start_database_connection() try: user = User(None) user.complete_name = str(complete_name) user.user_name = str(user_name) user.password = str(password) user.biography = "" user.privacy = False cursor.execute("select * from perfil where nome_usuario=%s", [user.user_name]) # Se existir algum usuário com o mesmo nome de usuário, lança exceção avisando isso if cursor.fetchall(): raise RepeatedPrimaryKeyException() cursor.execute( "insert into perfil(nome_usuario, nome_real, senha, biografia, privacidade) values (%s, %s, %s, %s, %s)", (user.user_name, user.complete_name, user.password, user.biography, user.privacy)) self.connection.close_database_connection() return user except RepeatedPrimaryKeyException: self.connection.close_database_connection() raise RepeatedPrimaryKeyException except Exception: self.connection.close_database_connection() raise Exception
def show_logged_menu(self, user=None, information_message=None): ViewPartition().border_logo() if not user: user = User(None) print("Usuário: " + str(user.user_name) + " \n\n" \ "O que gostaria de fazer? Escolha uma das opcoes abaixo:\n" \ " 1 - Visualizar Perfil \n" \ " 2 - Ver Usuários que Segue \n" \ " 3 - Ver Seguidores \n" \ " 4 - Buscar Usuários \n" \ " 5 - Sair da Conta \n" ) self._show_information_message(information_message)
def run(self): search_key = self.show() if super()._is_empty_field(search_key): return None users_list = User.search_users(search_key, order_by='followers number') wrong_selection_message = None while (True): selected_index = self._filter_selected_index( self.show(users_list=users_list, information_message=wrong_selection_message)) if self._is_empty_field(selected_index): return None elif not self._is_out_of_bounds(selected_index, len(users_list)): return users_list[selected_index] else: wrong_selection_message = 'Opção inválida'
def get_user(self, user_name): cursor = self.connection.start_database_connection() try: user_name = str(user_name) cursor.execute("select * from perfil where nome_usuario=%s", [user_name]) user_as_list = cursor.fetchall() self.connection.close_database_connection() return User(user_as_list) except ValueError: self.connection.close_database_connection() raise ValueError except WrongPasswordException: self.connection.close_database_connection() raise WrongPasswordException except Exception: self.connection.close_database_connection() raise Exception
def _show_selected_notification(self, notification, information_message=None): ViewPartition().border_logo() print(notification) ViewPartition().border_divisory() if notification.notification_type == 'follow': if self.logged_user.privacy: print("Você aceitará o pedido dele?: \n", "S - Para sim \n", "N - Para não ") choice = self._filter_selected_value(InputField().show('>>')) if choice not in ['S', 'N']: return None follow_relationship = Follow.get_follow_instance( notification.id_follow_follower, notification.id_follow_followed) follow_relationship.set_confirmation(True if choice == 'S' else False) Notification.delete_instance(notification.notification_id) return { 'command': 'access_user', 'object': User.get_user_instance(notification.id_follow_follower) } else: return { 'command': 'access_user', 'object': User.get_user_instance(notification.id_follow_follower) } elif notification.notification_type == 'follow confirmation': Follow.delete_instance( User.get_user_instance(notification.id_follow_follower), User.get_user_instance(notification.id_follow_followed)) Notification.delete_instance(notification.notification_id) elif notification.notification_type == 'post markup': return { 'command': 'access_post', 'object': Post.get_post_instance(notification.id_postmarkup_post) } elif notification.notification_type == 'commentary markup': commentary = Commentary.get_commentary_instance( notification.id_commentarymarkup_commentary) post = Post.get_post_instance(commentary.post_id) return {'command': 'access_post', 'object': post} else: return None