diff --git a/src/ui/recipe_selection/recipe_selection.py b/src/ui/recipe_selection/recipe_selection.py index d5663dc..ff3182e 100755 --- a/src/ui/recipe_selection/recipe_selection.py +++ b/src/ui/recipe_selection/recipe_selection.py @@ -9,13 +9,15 @@ from lib.db import Recipes, Users, db from PyQt5.QtCore import QTimer, pyqtSignal from PyQt5.QtGui import QKeySequence from PyQt5.QtWidgets import QFileDialog, QMessageBox, QShortcut - +import shutil from lib.helpers.step import Step from ui.crud import Crud, Json_External_Dialog_Editor_Cell_Widget from ui.helpers import replace_widget from ui.recipe_spec_and_step_editor import Recipe_Spec_And_Step_Editor from ui.widget import Widget +from datetime import datetime +from src.components import ArchiveSynchronizer class Noner: def __getitem__(self, key): @@ -284,7 +286,7 @@ class Recipe_Selection(Widget): options |= QFileDialog.DontUseNativeDialog csv_path, _ = QFileDialog.getOpenFileName( self, - "Esportazione ricette", + "Importazione ricette", "ricette.csv", "CSV data (*.csv);;All Files (*)", options=options, @@ -489,3 +491,41 @@ class Recipe_Selection(Widget): if ret == QMessageBox.Ok: Recipes.delete().execute() self.crud.refresh() + def backup_current_recipes(self): + # Define the backup directory and file name + backup_dir = os.path.join('config', 'csv_import', 'backup_csv') + timestamp = datetime.now().strftime("%d%m%y") + backup_file = f"backup_{timestamp}.csv" + backup_path = os.path.join(backup_dir, backup_file) + + # Ensure the backup directory exists + os.makedirs(backup_dir, exist_ok=True) + + # Export current recipes to backup file + self.export_recipes(csv_path=backup_path) + + def move_imported_csv(self, csv_path): + # Move the imported CSV to the 'imported_csv' directory + imported_dir = os.path.join('config', 'csv_import', 'imported_csv') + os.makedirs(imported_dir, exist_ok=True) + imported_path = os.path.join(imported_dir, os.path.basename(csv_path)) + shutil.move(csv_path, imported_path) + self.log.info(f"Imported CSV moved to {imported_path}") + return imported_path + + def check_and_import_auto_csv(self): + if self.archive_sync.machine_status ==" logged-in": + # Define the directory to check + auto_import_dir = os.path.join('config', 'csv_import', 'auto_csv_import') + + # Check if the directory exists and is not empty + if os.path.exists(auto_import_dir) and os.listdir(auto_import_dir): + # Perform backup + self.backup_current_recipes() + + # Move and import each CSV file in the directory + for csv_file in os.listdir(auto_import_dir): + csv_path = os.path.join(auto_import_dir, csv_file) + if os.path.isfile(csv_path) and csv_file.endswith(".csv"): + self.import_recipes(csv_path=csv_path) + self.move_imported_csv(csv_path)