|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +from nicegui import ui |
| 4 | + |
| 5 | +from app.analysis_context import AnalysisContext |
| 6 | +from components.select_analysis import present_timestamp |
| 7 | +from gui.base import GuiSession |
| 8 | + |
| 9 | + |
| 10 | +class ManageAnalysisDialog(ui.dialog): |
| 11 | + """ |
| 12 | + Dialog for managing analyses (view and delete). |
| 13 | +
|
| 14 | + Displays a list of analyses for the current project in a grid and allows |
| 15 | + users to select and delete one or more analyses. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__(self, session: GuiSession) -> None: |
| 19 | + """ |
| 20 | + Initialize the Manage Analysis dialog. |
| 21 | +
|
| 22 | + Args: |
| 23 | + session: GUI session containing app context and state |
| 24 | + """ |
| 25 | + super().__init__() |
| 26 | + |
| 27 | + self.session = session |
| 28 | + now = datetime.now() |
| 29 | + self.analysis_contexts: list[AnalysisContext] = ( |
| 30 | + session.current_project.list_analyses() |
| 31 | + if session.current_project is not None |
| 32 | + else [] |
| 33 | + ) |
| 34 | + # Track IDs of analyses deleted during this dialog session |
| 35 | + self.deleted_ids: set = set() |
| 36 | + |
| 37 | + # Build dialog UI |
| 38 | + with self, ui.card().classes("w-full"): |
| 39 | + # Dialog title |
| 40 | + ui.label("Manage Analyses").classes("text-h6 q-mb-md") |
| 41 | + |
| 42 | + # Check if there are analyses to display |
| 43 | + if not self.analysis_contexts: |
| 44 | + ui.label("No analyses found").classes("text-grey q-mb-md") |
| 45 | + else: |
| 46 | + # Analyses grid — multiRow selection enabled |
| 47 | + self.grid = ui.aggrid( |
| 48 | + { |
| 49 | + "columnDefs": [ |
| 50 | + {"headerName": "Analyzer Name", "field": "name"}, |
| 51 | + {"headerName": "Date Created", "field": "date"}, |
| 52 | + {"headerName": "ID", "field": "analysis_id", "hide": True}, |
| 53 | + ], |
| 54 | + "rowData": [ |
| 55 | + { |
| 56 | + "name": ctx.display_name, |
| 57 | + "date": ( |
| 58 | + present_timestamp(ctx.create_time, now) |
| 59 | + if ctx.create_time |
| 60 | + else "Unknown" |
| 61 | + ), |
| 62 | + "analysis_id": ctx.id, |
| 63 | + } |
| 64 | + for ctx in self.analysis_contexts |
| 65 | + ], |
| 66 | + "rowSelection": {"mode": "multiRow"}, |
| 67 | + }, |
| 68 | + theme="quartz", |
| 69 | + ).classes("w-full h-96") |
| 70 | + |
| 71 | + # Action buttons |
| 72 | + with ui.row().classes("w-full justify-end gap-2 mt-4"): |
| 73 | + ui.button( |
| 74 | + "Close", |
| 75 | + on_click=self._handle_close, |
| 76 | + color="secondary", |
| 77 | + ).props("outline") |
| 78 | + |
| 79 | + ui.button( |
| 80 | + "Delete Selected", on_click=self._handle_delete, color="negative" |
| 81 | + ) |
| 82 | + |
| 83 | + async def _handle_delete(self) -> None: |
| 84 | + """Handle delete button click — confirm then delete all selected analyses.""" |
| 85 | + selected_rows = await self.grid.get_selected_rows() |
| 86 | + |
| 87 | + if not selected_rows: |
| 88 | + ui.notify("Please select one or more analyses to delete", type="warning") |
| 89 | + return |
| 90 | + |
| 91 | + count = len(selected_rows) |
| 92 | + |
| 93 | + # Show a single confirmation for all selected rows |
| 94 | + confirmed = await self._show_delete_confirmation(count, selected_rows) |
| 95 | + if not confirmed: |
| 96 | + return |
| 97 | + |
| 98 | + errors: list[str] = [] |
| 99 | + newly_deleted: list[str] = [] |
| 100 | + |
| 101 | + for row in selected_rows: |
| 102 | + analysis_id = row["analysis_id"] |
| 103 | + analysis_name = row["name"] |
| 104 | + |
| 105 | + analysis_context = next( |
| 106 | + (a for a in self.analysis_contexts if a.id == analysis_id), None |
| 107 | + ) |
| 108 | + |
| 109 | + if not analysis_context: |
| 110 | + errors.append(f"'{analysis_name}' not found") |
| 111 | + continue |
| 112 | + |
| 113 | + try: |
| 114 | + analysis_context.delete() |
| 115 | + if analysis_context.is_deleted: |
| 116 | + self.deleted_ids.add(analysis_id) |
| 117 | + newly_deleted.append(analysis_id) |
| 118 | + except Exception as e: |
| 119 | + errors.append(f"'{analysis_name}': {e}") |
| 120 | + |
| 121 | + # Update the dialog grid in place — remove deleted rows |
| 122 | + if newly_deleted: |
| 123 | + self.grid.options["rowData"] = [ |
| 124 | + row |
| 125 | + for row in self.grid.options["rowData"] |
| 126 | + if row["analysis_id"] not in newly_deleted |
| 127 | + ] |
| 128 | + self.grid.update() |
| 129 | + |
| 130 | + if errors: |
| 131 | + ui.notify(f"Some deletions failed: {'; '.join(errors)}", type="negative") |
| 132 | + elif newly_deleted: |
| 133 | + label = "analysis" if len(newly_deleted) == 1 else "analyses" |
| 134 | + ui.notify( |
| 135 | + f"Deleted {len(newly_deleted)} {label} successfully.", type="positive" |
| 136 | + ) |
| 137 | + |
| 138 | + async def _show_delete_confirmation(self, count: int, rows: list[dict]) -> bool: |
| 139 | + """ |
| 140 | + Show confirmation dialog before deleting analyses. |
| 141 | +
|
| 142 | + Args: |
| 143 | + count: Number of analyses selected for deletion |
| 144 | + rows: Selected row data dicts |
| 145 | +
|
| 146 | + Returns: |
| 147 | + True if user confirmed deletion, False otherwise |
| 148 | + """ |
| 149 | + if count == 1: |
| 150 | + description = f"analysis '{rows[0]['name']}'" |
| 151 | + else: |
| 152 | + description = f"{count} analyses" |
| 153 | + |
| 154 | + with ui.dialog() as dialog, ui.card(): |
| 155 | + ui.label(f"Are you sure you want to delete {description}?").classes( |
| 156 | + "q-mb-md" |
| 157 | + ) |
| 158 | + ui.label("This action cannot be undone.").classes("text-warning q-mb-lg") |
| 159 | + |
| 160 | + with ui.row().classes("w-full justify-end gap-2"): |
| 161 | + ui.button( |
| 162 | + "Cancel", |
| 163 | + on_click=lambda: dialog.submit(False), |
| 164 | + color="secondary", |
| 165 | + ).props("outline") |
| 166 | + |
| 167 | + ui.button( |
| 168 | + "Delete", on_click=lambda: dialog.submit(True), color="negative" |
| 169 | + ) |
| 170 | + |
| 171 | + return await dialog |
| 172 | + |
| 173 | + def _handle_close(self) -> None: |
| 174 | + """Close the dialog, returning the set of deleted analysis IDs to the caller.""" |
| 175 | + self.submit(self.deleted_ids) |
0 commit comments