diff --git a/cli.py b/cli.py index 5860271..774fbf3 100644 --- a/cli.py +++ b/cli.py @@ -17,8 +17,8 @@ from os import system from db import Db from config import DB_REQUEST, CLI_MSG_DISCLAIMER, CLI_MSG_ASK_IDX, \ CLI_MSG_ASK_ERR, CLI_MSG_QUIT, CLI_MSG_CHOOSEN_CAT, CLI_MSG_PROD, \ - CLI_MSG_SUBST, CLI_MSG_NO_SUBST, CLI_MSG_CAT, CLI_MSG_CHOOSEN_PROD - + CLI_MSG_SUBST, CLI_MSG_NO_SUBST, CLI_MSG_CAT, CLI_MSG_CHOOSEN_PROD, \ + CLI_MSG_DETAILLED_SUB, CLI_MSG_CHOOSEN_SUBST cli_end_msg = str() product_asked = {'valid_item': False} @@ -185,19 +185,33 @@ if product_asked['valid_item']: product_asked['item'][2] ) - # Shows product in the same category with a lowest nutriscore + # Asks the user to select a substitute elif substitute_list['max_id'] > 0: - substitute_asked = ask_user( + substit_asked = ask_user( head_msg, substitute_list ) - if substitute_asked['valid_item']: - cli_end_msg += "category : «{}»\n".format(category_asked['item'][1]) - cli_end_msg += "product : «{}»\n".format(product_asked['item'][1]) - cli_end_msg += "substitut : «{}»".format(substitute_asked['item'][1]) + ########################## + # SHOW SUBTITUTE DETAILS # + ########################## + if substit_asked['valid_item']: + LOCAL_DB.execute(DB_REQUEST['select_substitute'].format( + substit_asked['item'][1] + )) - # Asks the user to select a substitute + head_msg = CLI_MSG_DISCLAIMER + head_msg += CLI_MSG_CHOOSEN_CAT.format(category_asked['item'][1]) + head_msg += CLI_MSG_CHOOSEN_PROD.format(product_asked['item'][1]) + head_msg += CLI_MSG_CHOOSEN_SUBST.format(substit_asked['item'][1]) + head_msg += CLI_MSG_DETAILLED_SUB.format( + code=LOCAL_DB.result[0]['code'], + nutri=LOCAL_DB.result[0]['nutrition_grades'], + url=LOCAL_DB.result[0]['url'] + ) + + system('clear') + print(head_msg) # Saves if user choose it diff --git a/config.py b/config.py index 522406d..37a089a 100644 --- a/config.py +++ b/config.py @@ -30,6 +30,7 @@ DB_REQUEST = { 'list_cat': "SELECT c.name, COUNT(*) AS 'option' FROM category AS c JOIN product AS p ON p.category_id = c.id GROUP BY c.name ORDER BY COUNT(*) DESC;", 'list_prod': "SELECT p.name, p.nutrition_grades AS 'option' FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.name = '{}';", 'list_substitute': "SELECT p.name, p.nutrition_grades AS 'option' FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE c.name = '{}' AND p.nutrition_grades < '{}'", + 'select_substitute': "SELECT p.*, c.name FROM product AS p LEFT JOIN category AS c ON p.category_id = c.id WHERE p.name = '{}'", 'save_substitute': "UPDATE product SET substitute_id={} WHERE id={}", } @@ -41,13 +42,16 @@ CLI_MSG_ASK_IDX = "Index choisi [0-{}] («Q»uitter) :" CLI_MSG_ASK_BAK = "Voulez vous sauvegarder «{}» en substitut du produit «{}»?"\ " [O/N]\n(«Q» pour quitter): " -CLI_MSG_ASK_ERR = "\nSaisie incorrecte : «{}»" -CLI_MSG_QUIT = "\nAu revoir!" CLI_MSG_CHOOSEN_CAT = "# # Categorie : [ {} ]\n" CLI_MSG_CHOOSEN_PROD = "# Produits : [ {} ]\n" -CLI_MSG_CHOOSEN_SUBST = "Substitut : [ {} ]\n" +CLI_MSG_CHOOSEN_SUBST = "Fiche complète du substitut : [ {} ]\n" +CLI_MSG_DETAILLED_SUB = "Nutriscore [ {nutri} ]\tCode [ {code} ]"\ + "\nURL:{url}" + CLI_MSG_NO_SUBST = "Pas de substitut trouvé pour le produit «{}» (nutriscore : «{}»)" -CLI_ITEM_MAX_LEN = 15 +CLI_MSG_ASK_ERR = "\nSaisie incorrecte : «{}»" +CLI_MSG_QUIT = "\nAu revoir!" +#CLI_ITEM_MAX_LEN = 15 # DATABASE DB_CONFIG = {