Ejemplo n.º 1
0
    def combine_databases(self, source_file, dest_file):
        source = RecipeBook(source_file)
        dest = RecipeBook(dest_file)
        source.cursor.execute("""
            SELECT *
            FROM Recipe""")
        results = source.cursor.fetchall()
        if results:
            force = None
            for r in results:
                result = source.cursor.execute(
                    """
                    SELECT ri.amount AS 'Amount',
                    mu.name AS 'Unit of Measure',
                    i.name AS 'Ingredient',
                    ri.order_num AS 'Order'
                    FROM Recipe r
                    JOIN RecipeIngredient ri on r.id = ri.recipe_id
                    JOIN Ingredient i on i.id = ri.ingredient_id
                    LEFT OUTER JOIN Measure mu on mu.id = measure_id
                    WHERE r.id = ?""", [r[0]])
                ingredients = []
                for i in sorted(result, key=lambda tup: tup[3]):
                    ingredients.append(i)

                dest.cursor.execute(
                    """
                    SELECT *
                    FROM Recipe r
                    WHERE r.name = ?""", [r[1]])
                result = dest.cursor.fetchone()
                if result:
                    if force == None:
                        d = ModalWindow(
                            self.my_gui, "Overwrite All",
                            "Warning: Duplicate recipe names exist.\nDo you want to overwrite all duplicates?\n(Choosing 'No' will result in a prompt for each duplicate case)"
                        )
                        self.my_gui.wait_window(d.modalWindow)
                        force = (d.choice == 'Yes')
                    if not force:
                        d = ModalWindow(
                            self.my_gui, "Overwrite Recipe",
                            "Warning: Recipe name already exists.\nDo you want to overwrite {}?"
                            .format(r[1]))
                        self.my_gui.wait_window(d.modalWindow)
                        if d.choice == 'Yes':
                            dest.add(r[1], r[2], r[3], r[4], r[5], r[6], r[7],
                                     ingredients, True)
                    else:
                        dest.add(r[1], r[2], r[3], r[4], r[5], r[6], r[7],
                                 ingredients, force)
                else:
                    dest.add(r[1], r[2], r[3], r[4], r[5], r[6], r[7],
                             ingredients, False)
        source.close()
        dest.renumber()
        dest.close(True)
Ejemplo n.º 2
0
    preference_file = "recipe_manager_preferences.json"
    current_dir = os.getcwd()
    show_tips = True
    if not os.path.isfile(os.path.join(current_dir, preference_file)):
        if __platform__ == "win32":
            database = os.path.join(current_dir, "recipe_data.db")
        else:
            database = os.path.join(os.path.expanduser("~"),
                                    "Documents/recipe_data.db")
    else:
        with open(os.path.join(current_dir, preference_file), "r") as f:
            recipe_format = json.load(f)
            database = recipe_format.get("database", None)
            show_tips = recipe_format.get("show_tips", True)
        if not database or not os.path.isfile(database):
            if __platform__ == "win32":
                database = os.path.join(current_dir, "recipe_data.db")
            else:
                database = os.path.join(os.path.expanduser("~"),
                                        "Documents/recipe_data.db")
            with open(os.path.join(current_dir, preference_file), "w") as f:
                recipe_format["database"] = database
                json.dump(recipe_format, f)

    book = RecipeBook(database)
    book.renumber()
    book.close(True)

    root = Tk()
    manager = RecipeManager(root, database, preference_file, show_tips)