dev export only selected

This commit is contained in:
Eduardo 2025-09-10 16:09:19 +02:00
parent 43b62efe2f
commit 026a51f9a8
2 changed files with 113 additions and 44 deletions

View File

@ -186,10 +186,12 @@ class Recipe_Selection(Widget):
if session.is_admin:
self.import_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().import_recipes())
self.export_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().export_recipes())
self.export_selected_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().export_recipes(selected_only=True))
self.delete_all_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().delete_recipes())
else:
self.import_b.setVisible(False)
self.export_b.setVisible(False)
self.export_selected_b.setVisible(False)
self.delete_all_b.setVisible(False)
# TESTING
@ -240,6 +242,7 @@ class Recipe_Selection(Widget):
# Update button visibility
self.import_b.setVisible(has_admin)
self.export_b.setVisible(has_admin)
self.export_selected_b.setVisible(has_admin)
self.delete_all_b.setVisible(has_admin)
# Connect or disconnect button handlers
@ -253,6 +256,10 @@ class Recipe_Selection(Widget):
self.export_b.clicked.disconnect()
except:
pass
try:
self.export_selected_b.clicked.disconnect()
except:
pass
try:
self.delete_all_b.clicked.disconnect()
except:
@ -261,6 +268,7 @@ class Recipe_Selection(Widget):
# Connect handlers
self.import_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().import_recipes())
self.export_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().export_recipes())
self.export_selected_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().export_recipes(selected_only=True))
self.delete_all_b.clicked.connect(lambda checked, selfi=weakref.ref(self): selfi().delete_recipes())
# Update CRUD readonly status
@ -389,10 +397,34 @@ class Recipe_Selection(Widget):
self.crud.refresh()
# EXPORT RECIPES TABLE TO CSV FILE
def export_recipes(self, csv_path=None, base_dir=None):
def export_recipes(self, csv_path=None, base_dir=None, selected_only=False):
"""
Export recipes to a CSV file. Opens a dialog for saving if no path is provided.
Args:
csv_path (str, optional): Path to save the CSV file. If None, a dialog will open.
base_dir (str, optional): Base directory for saving the file. If None, a default is used.
selected_only (bool, optional): If True, export only selected recipes. Default is False.
"""
# Check if we should export only selected recipes
if selected_only:
# Get selected rows
selected_rows = self.crud.get_selected_rows()
if not selected_rows:
QMessageBox.information(self, "Esportazione ricette", "Nessuna ricetta selezionata.")
return
# Ask for confirmation
ret = QMessageBox.question(
self,
"Esportazione ricette selezionate",
f"Vuoi esportare solo le {len(selected_rows)} ricette selezionate?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.Yes
)
if ret == QMessageBox.No:
# User chose not to export only selected recipes
selected_only = False
base_dir = base_dir or self._get_base_dir() # Use base directory or default
if csv_path is None:
@ -478,7 +510,25 @@ class Recipe_Selection(Widget):
"labeltxt_4",
"labeltxt_5",
]
for recipe in list(Recipes.select()):
# Get the selected recipe IDs if we're exporting only selected recipes
selected_recipe_ids = []
if selected_only:
selected_rows = self.crud.get_selected_rows()
for row in selected_rows:
# Adjust row index to match data_index (row - 1 because row 0 is the filter row)
row_idx = row - 1
if 0 <= row_idx < len(self.crud.data_index):
recipe_id = self.crud.data_index[row_idx]
if recipe_id is not None:
selected_recipe_ids.append(recipe_id)
# Get all recipes or only selected ones
recipes_query = Recipes.select()
if selected_only and selected_recipe_ids:
recipes_query = recipes_query.where(Recipes.id.in_(selected_recipe_ids))
for recipe in list(recipes_query):
steps = recipe.get_steps_map()
exportable = {
recipe_name_field: recipe.name,
@ -533,6 +583,7 @@ class Recipe_Selection(Widget):
"printer_selection": steps.get("print",Step()).spec.get("printer_selection",""),
}
data.append(exportable)
if len(data):
self.log.info(f"recipes: exporting recipes to {csv_path}")
with open(csv_path, "w", newline="") as f:

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>985</width>
<height>201</height>
<height>198</height>
</rect>
</property>
<property name="minimumSize">
@ -32,7 +32,10 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="2" column="2">
<item row="0" column="0" colspan="4">
<widget class="QWidget" name="crud_w" native="true"/>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="delete_all_b">
<property name="font">
<font>
@ -44,46 +47,7 @@
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QWidget" name="crud_w" native="true"/>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="import_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Importa ricette da file CSV</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="export_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Esporta ricette su file CSV</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3" alignment="Qt::AlignHCenter">
<item row="1" column="0" colspan="4" alignment="Qt::AlignHCenter">
<widget class="QPushButton" name="select_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@ -119,6 +83,60 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="export_selected_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Esporta ricette selezionate</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="export_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Esporta tutte le ricette</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="import_b">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Importa ricette da file CSV</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>