def product_choice(self): """This method offers a choice of 10 products to the user according to the category chosen previously. Products methods are called to retrieve the products. They are associated with a number in a dict and stored in a list. The user makes his choice and the product chosen and stored in a variable. Returns: int or str: this answer makes it possible to decide on the following of the program. """ print("*************************************") print(f"Voici la liste des produits non sains." f"Tapez le chiffre correspondant au produit de votre choix") for dictio in self.product: prCyan(f"\033[95m{dictio['choice']}" f".\033[0m {repr(dictio['product'])}") self.answer_product = self.input_answer(self.answer_product, len(self.product)) if self.answer_product not in ["b", "B"]: self.fat_product = [ x["product"] for x in self.product if x["choice"] == self.answer_product ][0] else: return self.answer_product
def display_favorites(self): """This method retrieves the favorite dictionary and displays all the favorites. The user therefore makes the choice to return to the menu or to leave. """ self.answer_home = 0 self.load() for f in self.favorites: prYellow("Le produit substitué est :") print(f["fat_product"]) prYellow("Le substitut est :") print(f'{f["healthy_product"]}' f" \033[94m Magasins où acheter:\033[0m " f'{f["healthy_product"].format_stores}') print(f"*****************************" f"************************************") print() prCyan(f"Souhaitez-vous quitter le programme" f"ou retourner au menu principal?") prCyan("[q] pour quitter ou [m] pour aller au menu") while True: self.answer_favorites = input("Votre choix: ") if self.answer_favorites.lower() == "q": sys.exit() elif self.answer_favorites.lower() == "m": break
def verify_db(): """This method is a security that avoids reinstalling the database more times and thus losing all its favorites. The choice is left to the user. """ session = Session() table_names = inspect(session.engine).get_table_names() answer = str() if table_names != []: while answer.lower not in ['y', 'n']: prRed("Vous vous apprêtez à réinitialiser la base de données") prRed(f"Attention la base de données contient déjà des éléments," f"notamment la table de vos favoris qui contient" f" {len(Favorites.select_favorites(session.session))}" f" éléments") prRed("Si vous continuez tous vos favoris seront supprimés.") prCyan("Voulez-vous continuer?[y] yes [n] no") answer = input("Votre choix: ") if answer.lower() == 'y': return elif answer.lower() == 'n': sys.exit() else: prYellow("Tapez [y] pour yes et [n] pour no") continue
def category_choice(self): """This method corresponds to the choice menu of the category. The query in Categories is used to retrieve all categories. The categories are stored in a dictionary associating the id of the category to the category. Then the dictionary is displayed in an orderly way and the user makes his choice. Returns: int or str: Corresponds to the choice of the user. """ self.answer_home = 0 self.answer_product = 0 print(f"Choisissez une catégorie parmi les suivantes" f" en tapant le chiffre correspondant:") c = Category() self.dict_category = { row.id: row.name for row in c.select_categories(self.session) } [ prCyan(f"{x}. {y.capitalize()}") for x, y in sorted(self.dict_category.items()) ] self.answer_category = self.input_answer(self.answer_category, len(self.dict_category)) self.product = [] self.gen_product() return self.answer_category
def subsitue(self): """This method allows to generate a substitute for the product previously selected thanks to the request contained in Product that looks for a product with a better Nutriscore. Then the user decides whether to save the result in the Favorites table, whether to leave the program or to return to the main menu. Returns: str: this answer makes it possible to decide on the following of the program. """ print("*************************************") prYellow(f"Vous avez sélectionné: {self.fat_product}") p = Product() prPurple("Voici un produit plus sain dans la même catégorie: ") self.healthy_product = p.find_healthy(self.session, self.fat_product, self.answer_category) print( repr(self.healthy_product) + f" \033[94m Magasins où acheter:\033[0m" f"{self.healthy_product.format_stores}") prCyan(f"Souhaitez-vous sauvegarder dans vos favoris" f" le substitut et le produit substitué ? [y] or [n]") while True: self.subsitue_answer = input("Votre choix : ") if self.subsitue_answer in ["q", "Q"]: sys.exit() elif self.subsitue_answer in ["b", "B"]: return self.subsitue_answer elif self.subsitue_answer.lower() == "y": self.save_products() prYellow(f"Produit sauvegardé dans la" f" base de données avec succès") prCyan("Voulez-vous retourner au menu principal [y] ou [n]") self.subsitue_answer = input("Vottre choix :") if self.subsitue_answer.lower() == "n": sys.exit() elif self.subsitue_answer.lower() == "y": break else: prRed("[y] pour quitter ou [n] pour aller au menu") elif self.subsitue_answer.lower() == "n": prCyan(f"Vous ne souhaitez pas enregistrer le substitut." f" Souhaitez-vous quitter le programme ou retourner" f" au menu principal?") prCyan("[q] pour quitter ou [m] pour aller au menu") self.subsitue_answer = input("Vottre choix :") if self.subsitue_answer.lower() == "q": sys.exit() elif self.subsitue_answer.lower() == "m": break else: print("[q] pour quitter ou [m] pour aller au menu") else: print("[y] ou [n]")
def home(self): """Corresponds to the main menu This method offers two options to the user. Depending on his answer he will access the requested menu. """ self.answer_home = None self.answer_category = None self.subsitue_answer = None self.answer_product = None self.favorites = [] f = Figlet(font='slant') prPurple(f.renderText('Welcome on PyHealthy')) print(f"Bienvenue sur PyHealthy, l'application qui a pour but de" f" mettre un peu plus de sains dans votre vie.") prRed(f"À tout moment dans le programme faites [q]" f" pour quitter ou [b] pour revenir au menu principal") print("Que souhaitez-vous faire ? ") prCyan("1. Voulez-vous substituer un produit ? ") prCyan("2. Voulez-vous accéder à vos aliments déjà substitués ?") self.answer_home = self.input_answer(self.answer_home, 2)