click-textual
Maveno 1 year ago
parent 37786f56b5
commit c963c2c675

@ -8,11 +8,7 @@ from textual.containers import Grid, Horizontal
from ffx.model.show import Show
# class IdInput(Input):
# def on_key(self, event: events.Key) -> None:
# if event == "enter":
# quit()
# Screen[dict[int, str, int]]
class ShowDetailsScreen(Screen):
CSS = """
@ -50,33 +46,33 @@ class ShowDetailsScreen(Screen):
}
"""
#BINDINGS = [
# #("q", "quit()", "Quit"),
# ("h", "switch_mode('help')", "Help")
#]
#def action_quit(self):
# quit()
def __init__(self, show_id = None):
def __init__(self, show = {}):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
self.show_id = show_id
self.show_obj = show
def action_submit(self):
quit()
def on_mount(self):
if self.show_obj:
self.query_one("#id_input", Input).value = self.show_obj['id']
self.query_one("#name_input", Input).value = self.show_obj['name']
self.query_one("#year_input", Input).value = self.show_obj['year']
def compose(self):
yield Header()
with Grid():
yield Static("New Show" if self.show_id is None else "Show", id="toplabel")
yield Static("New Show" if self.show_obj else "Show", id="toplabel")
yield Static("ID")
yield Input(type="text", id="id_input")
yield Static("Name")
@ -105,33 +101,6 @@ class ShowDetailsScreen(Screen):
return showId, showName, showYear
def on_input_submitted(self, event: Input.Submitted) -> None:
showId, showName, showYear = self.getValues()
#self.query_one("#output_static", Static).update(f"{showId} - {showName} ({showYear})")
# if event.input.id == "id_input":
# # Retrieve the entered text
# name = event.value
# # Display the result in the output label
# self.query_one("#output_static", Static).update(f"Hello, {name}!")
# self.set_focus(self.query_one("#name_input", Input))
#
# if event.input.id == "name_input":
# # Retrieve the entered text
# name = event.value
# # Display the result in the output label
# self.query_one("#output_static", Static).update(f"Yo, {name}!")
# self.set_focus(self.query_one("#year_input", Input))
#
# if event.input.id == "year_input":
# # Retrieve the entered text
# name = event.value
# # Display the result in the output label
# self.query_one("#output_static", Static).update(f"Ya, {name}!")
# self.set_focus(None)
# Event handler for button press
def on_button_pressed(self, event: Button.Pressed) -> None:
# Check if the button pressed is the one we are interested in
@ -140,11 +109,17 @@ class ShowDetailsScreen(Screen):
showId, showName, showYear = self.getValues()
self.query_one("#output_static", Static).update(f"{showId} - {showName} ({showYear})")
self.addShow(showId, showName, showYear)
#TODO: Validation
if self.addShow(showId, showName, showYear):
screenResult = {}
screenResult['id'] = showId
screenResult['name'] = showName
screenResult['year'] = showYear
# self.app.pop_screen()
self.dismiss(showId)
self.dismiss(screenResult)
else:
#TODO: Meldung
self.app.pop_screen()
if event.button.id == "cancel_button":
self.app.pop_screen()
@ -153,12 +128,18 @@ class ShowDetailsScreen(Screen):
try:
s = self.Session()
show = Show(id = show_id,
name = show_name,
year = show_year)
q = s.query(Show).filter(Show.id == show_id)
if not q.count():
show = Show(id = show_id,
name = show_name,
year = show_year)
s.add(show)
s.commit()
return True
else:
return False
s.add(show)
s.commit()
except Exception as ex:
click.ClickException(f"ShowDetailsScreen.addShow(): {repr(ex)}")
finally:

@ -42,29 +42,41 @@ class ShowsScreen(Screen):
"""
BINDINGS = [
#("q", "quit()", "Quit"),
("e", "switch_mode('show_details')", "Edit Show"),
("e", "edit_show", "Edit Show"),
("n", "new_show", "New Show"),
#("h", "switch_mode('help')", "Help")
]
# MODES = {
# "show_details": ShowDetailsScreen,
# "show_new": ShowNewScreen,
# "help": HelpScreen,
# }
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):
# 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)
if row_key is not None:
selected_row_data = self.table.get_row(row_key)
def handle_new_screen(self, showId):
#)[selected_row_index]
showList = self.loadShows()
selectedShow = {}
selectedShow['id'] = selected_row_data[0]
selectedShow['name'] = selected_row_data[1]
selectedShow['year'] = selected_row_data[2]
for show in showList:
if show[0] == showId:
self.table.add_row(*map(str, show))
self.app.push_screen(ShowDetailsScreen(show = selectedShow), self.handle_edit_screen)
def handle_edit_screen(self, screenResult):
show = (screenResult['id'], screenResult['name'], screenResult['year'])
self.table.add_row(*map(str, show))
@ -95,23 +107,6 @@ class ShowsScreen(Screen):
self.table.add_row(*map(str, show)) # Convert each element to a string before adding
#def on_resume(self) -> None:
# quit()
# showList = self.loadShows()
#
# tableIdSet = set({int(r[0]) for r in self.table.rows})
# databaseIdSet = set({int(s[0]) for s in showList})
#
# missingIdSet = databaseIdSet - tableIdSet
# removedIdSet = tableIdSet - databaseIdSet
#
# for show in showList:
# if show[0] in missingIdSet:
# self.table.add_row(*map(str, show))
def compose(self):

Loading…
Cancel
Save