Beispiel #1
0
def remove_category():
    option_remove_category = int(
        input(
            "Warning! Deleting a category will also delete all the products inside of it.\n1. Continue\n2. Go back\n"
        ))
    if option_remove_category == 1:
        category_to_remove = Category(
            input("Introduce the name of the category to be removed:\n"))
        try:
            categories = Categories.load_categories()
            if categories.count(category_to_remove) > 0:
                products = Products.load_products()
                for prod in products:
                    if prod.get_category_name() == category_to_remove.name:
                        Products.remove_product(prod)
                Categories.remove_category(category_to_remove)
                input(
                    "Category -" + str(category_to_remove) +
                    "- and all its products were removed successfully.\nPress enter key in order to continue\n"
                )
            else:
                category_option = int(
                    input(
                        "This category does not exist in the list. Input 1 to try entering another category or any other number to return to the store menu:\n"
                    ))
                if category_option == 1:
                    remove_category()
        except JSONDecodeError:
            input(
                "Error on retrieving the categories. Press enter key in order to continue\n"
            )
    elif option_remove_category == 2:
        print("Going back...\n")
    else:
        error_handler()
        remove_category()