Delete Show and confirmation

click-textual
Maveno 1 year ago
parent 0cda6390cd
commit a46a2b421e

@ -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()

@ -8,7 +8,8 @@ from textual.containers import Grid, Horizontal
from ffx.model.show import Show
from .show_details_screen import ShowDetailsScreen
from .show_new_screen import ShowNewScreen
from .show_delete_screen import ShowDeleteScreen
from .help_screen import HelpScreen
@ -44,33 +45,56 @@ class ShowsScreen(Screen):
BINDINGS = [
("e", "edit_show", "Edit Show"),
("n", "new_show", "New Show"),
("d", "delete_show", "Delete Show"),
]
def action_new_show(self):
self.app.push_screen(ShowDetailsScreen(), self.handle_new_screen)
def handle_new_screen(self, screenResult):
show = (screenResult['id'], screenResult['name'], screenResult['year'])
self.table.add_row(*map(str, show))
def __init__(self):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
def action_edit_show(self):
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 = {}
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):
self.app.push_screen(ShowDetailsScreen(), self.handle_new_screen)
def handle_new_screen(self, screenResult):
show = (screenResult['id'], screenResult['name'], screenResult['year'])
self.table.add_row(*map(str, show))
def action_edit_show(self):
selectedShow = self.getSelectedShow()
if selectedShow:
self.app.push_screen(ShowDetailsScreen(show = selectedShow), self.handle_edit_screen)
def handle_edit_screen(self, screenResult):
row_key, col_key = self.table.coordinate_to_cell_key(self.table.cursor_coordinate)
@ -79,12 +103,17 @@ class ShowsScreen(Screen):
self.table.update_cell(row_key, self.column_key_year, screenResult['year'])
def __init__(self):
super().__init__()
def action_delete_show(self):
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):

Loading…
Cancel
Save