Delete Show and confirmation

This commit is contained in:
Maveno
2024-09-26 12:59:07 +02:00
parent 0cda6390cd
commit a46a2b421e
2 changed files with 169 additions and 17 deletions

View File

@@ -0,0 +1,123 @@
import click
from textual import events
from textual.app import App, ComposeResult
from textual.screen import Screen
from textual.widgets import Header, Footer, Placeholder, Label, ListView, ListItem, Static, DataTable, Button, Input
from textual.containers import Grid, Horizontal
from ffx.model.show import Show
# Screen[dict[int, str, int]]
class ShowDeleteScreen(Screen):
CSS = """
Grid {
grid-size: 2;
grid-rows: 2 auto;
grid-columns: 30 auto;
height: 100%;
width: 100%;
padding: 1;
}
Input {
border: none;
}
Button {
border: none;
}
#toplabel {
height: 1;
column-span: 2;
}
#two {
column-span: 2;
row-span: 2;
tint: magenta 40%;
}
.box {
height: 100%;
border: solid green;
}
"""
def __init__(self, show = {}):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
self.show_obj = show
def on_mount(self):
if self.show_obj:
self.query_one("#showlabel", Static).update(f"{self.show_obj['id']} - {self.show_obj['name']} ({self.show_obj['year']})")
def compose(self):
yield Header()
with Grid():
yield Static("Are you sure to delete the following show?", id="toplabel")
yield Static("")
yield Static("")
yield Static("", id="showlabel")
yield Static("")
yield Static("")
yield Static("")
yield Static("")
yield Static("")
yield Button("Delete", id="delete_button")
yield Button("Cancel", id="cancel_button")
yield Footer()
# Event handler for button press
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "delete_button":
if self.deleteShow(self.show_obj['id']):
self.dismiss(self.show_obj['id'])
else:
#TODO: Meldung
self.app.pop_screen()
if event.button.id == "cancel_button":
self.app.pop_screen()
def deleteShow(self, show_id):
try:
s = self.Session()
q = s.query(Show).filter(Show.id == show_id)
if q.count():
q.delete()
s.commit()
return True
return False
except Exception as ex:
click.ClickException(f"ShowDetailsScreen.updateShow(): {repr(ex)}")
finally:
s.close()

View File

@@ -8,7 +8,8 @@ from textual.containers import Grid, Horizontal
from ffx.model.show import Show from ffx.model.show import Show
from .show_details_screen import ShowDetailsScreen from .show_details_screen import ShowDetailsScreen
from .show_new_screen import ShowNewScreen from .show_delete_screen import ShowDeleteScreen
from .help_screen import HelpScreen from .help_screen import HelpScreen
@@ -44,8 +45,39 @@ class ShowsScreen(Screen):
BINDINGS = [ BINDINGS = [
("e", "edit_show", "Edit Show"), ("e", "edit_show", "Edit Show"),
("n", "new_show", "New Show"), ("n", "new_show", "New Show"),
("d", "delete_show", "Delete Show"),
] ]
def __init__(self):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
def getSelectedShow(self):
# Fetch the currently selected row when 'Enter' is pressed
#selected_row_index = self.table.cursor_row
row_key, col_key = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
selectedShow = {}
if row_key is not None:
selected_row_data = self.table.get_row(row_key)
selectedShow['id'] = selected_row_data[0]
selectedShow['name'] = selected_row_data[1]
selectedShow['year'] = selected_row_data[2]
return selectedShow
def action_new_show(self): def action_new_show(self):
self.app.push_screen(ShowDetailsScreen(), self.handle_new_screen) self.app.push_screen(ShowDetailsScreen(), self.handle_new_screen)
@@ -57,19 +89,11 @@ class ShowsScreen(Screen):
def action_edit_show(self): def action_edit_show(self):
# Fetch the currently selected row when 'Enter' is pressed selectedShow = self.getSelectedShow()
#selected_row_index = self.table.cursor_row
row_key, col_key = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
if row_key is not None: if selectedShow:
selected_row_data = self.table.get_row(row_key) self.app.push_screen(ShowDetailsScreen(show = selectedShow), self.handle_edit_screen)
selectedShow = {}
selectedShow['id'] = selected_row_data[0]
selectedShow['name'] = selected_row_data[1]
selectedShow['year'] = selected_row_data[2]
self.app.push_screen(ShowDetailsScreen(show = selectedShow), self.handle_edit_screen)
def handle_edit_screen(self, screenResult): def handle_edit_screen(self, screenResult):
@@ -79,12 +103,17 @@ class ShowsScreen(Screen):
self.table.update_cell(row_key, self.column_key_year, screenResult['year']) self.table.update_cell(row_key, self.column_key_year, screenResult['year'])
def __init__(self): def action_delete_show(self):
super().__init__()
self.context = self.app.getContext() selectedShow = self.getSelectedShow()
self.Session = self.context['database_session'] # convenience if selectedShow:
self.app.push_screen(ShowDeleteScreen(show = selectedShow), self.handle_delete_screen)
def handle_delete_screen(self, screenResult):
row_key, col_key = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
self.table.remove_row(row_key)
def loadShows(self): def loadShows(self):