Rework MVC

click-textual
Javanaut 1 year ago
parent e325aaa529
commit 7c899e32bb

@ -34,3 +34,8 @@ class Show(Base):
# v2.0
# patterns: Mapped[List["Pattern"]] = relationship(back_populates="show", cascade="all, delete")
index_season_digits = Column(Integer, default=2)
index_episode_digits = Column(Integer, default=2)
indicator_season_digits = Column(Integer, default=2)
indicator_episode_digits = Column(Integer, default=2)

@ -0,0 +1,92 @@
import click
from ffx.model.pattern import Pattern
class PatternController():
def __init__(self, context):
self.context = context
self.Session = self.context['database_session'] # convenience
def updatePattern(self, show_id, pattern):
try:
s = self.Session()
q = s.query(Pattern).filter(Pattern.show_id == int(show_id), Pattern.pattern == str(pattern))
if not q.count():
pattern = Pattern(show_id = int(show_id), pattern = str(pattern))
s.add(pattern)
s.commit()
return True
except Exception as ex:
raise click.ClickException(f"PatternController.updatePattern(): {repr(ex)}")
finally:
s.close()
def findPattern(self, showId, pattern):
try:
s = self.Session()
q = s.query(Pattern).filter(Pattern.show_id == int(showId), Pattern.pattern == str(pattern))
if q.count():
pattern = q.first()
return int(pattern.id)
else:
return None
except Exception as ex:
raise click.ClickException(f"PatternController.findPattern(): {repr(ex)}")
finally:
s.close()
def getPatternDescriptor(self, patternId):
try:
s = self.Session()
q = s.query(Pattern).filter(Pattern.id == int(patternId))
patternDescriptor = {}
if q.count():
pattern = q.first()
patternDescriptor['id'] = pattern.id
patternDescriptor['pattern'] = pattern.pattern
patternDescriptor['show_id'] = pattern.show_id
return patternDescriptor
except Exception as ex:
raise click.ClickException(f"PatternController.getPatternDescriptor(): {repr(ex)}")
finally:
s.close()
def deletePattern(self, patternId):
try:
s = self.Session()
q = s.query(Pattern).filter(Pattern.id == int(patternId))
if q.count():
#DAFUQ: https://stackoverflow.com/a/19245058
# q.delete()
pattern = q.first()
s.delete(pattern)
s.commit()
return True
return False
except Exception as ex:
raise click.ClickException(f"PatternController.deletePattern(): {repr(ex)}")
finally:
s.close()

@ -8,6 +8,10 @@ from textual.containers import Grid, Horizontal
from ffx.model.pattern import Pattern
from .show_controller import ShowController
from .pattern_controller import PatternController
# Screen[dict[int, str, int]]
class PatternDeleteScreen(Screen):
@ -30,14 +34,10 @@ class PatternDeleteScreen(Screen):
}
#toplabel {
height: 1;
column-span: 2;
}
#two {
.two {
column-span: 2;
row-span: 2;
tint: magenta 40%;
}
.box {
@ -46,14 +46,17 @@ class PatternDeleteScreen(Screen):
}
"""
def __init__(self, pattern = {}, show = {}):
def __init__(self, patternId = None, showId = None):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
self.pattern_obj = pattern
self.show_obj = show
self.__pc = PatternController(context = self.context)
self.__sc = ShowController(context = self.context)
self.pattern_obj = self.__pc.getPatternDescriptor(patternId) if patternId is not None else {}
self.show_obj = self.__sc.getShowDesciptor(showId) if showId is not None else {}
def on_mount(self):
@ -69,22 +72,19 @@ class PatternDeleteScreen(Screen):
with Grid():
yield Static("Are you sure to delete the following filename pattern?", id="toplabel")
yield Static("Are you sure to delete the following filename pattern?", id="toplabel", classes="two")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Static("Pattern")
yield Static("", id="patternlabel")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Static("from show")
yield Static("", id="showlabel")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Button("Delete", id="delete_button")
yield Button("Cancel", id="cancel_button")
@ -97,7 +97,7 @@ class PatternDeleteScreen(Screen):
if event.button.id == "delete_button":
if self.deletePattern(self.show_obj['id'], self.pattern_obj['pattern']):
if self.__pc.deletePattern(self.pattern_obj['id']):
screenResult = {}
screenResult['show_id'] = self.show_obj['id']
@ -112,23 +112,3 @@ class PatternDeleteScreen(Screen):
if event.button.id == "cancel_button":
self.app.pop_screen()
def deletePattern(self, show_id, pattern):
try:
s = self.Session()
q = s.query(Pattern).filter(Pattern.show_id == int(show_id), Pattern.pattern == str(pattern))
if q.count():
#DAFUQ: https://stackoverflow.com/a/19245058
# q.delete()
pattern = q.first()
s.delete(pattern)
s.commit()
return True
return False
except Exception as ex:
click.ClickException(f"deletePattern(): {repr(ex)}")
finally:
s.close()

@ -9,6 +9,9 @@ from textual.containers import Grid, Horizontal
from ffx.model.show import Show
from ffx.model.pattern import Pattern
from .pattern_controller import PatternController
from .show_controller import ShowController
# Screen[dict[int, str, int]]
class PatternDetailsScreen(Screen):
@ -36,14 +39,10 @@ class PatternDetailsScreen(Screen):
#toplabel {
height: 1;
column-span: 2;
}
#two {
.two {
column-span: 2;
row-span: 2;
tint: magenta 40%;
}
.box {
@ -52,14 +51,17 @@ class PatternDetailsScreen(Screen):
}
"""
def __init__(self, pattern = {}, show = {}):
def __init__(self, patternId = None, showId = None):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
self.pattern_obj = pattern
self.show_obj = show
self.__pc = PatternController(context = self.context)
self.__sc = ShowController(context = self.context)
self.pattern_obj = self.__pc.getPatternDescriptor(patternId) if patternId is not None else {}
self.show_obj = self.__sc.getShowDesciptor(showId) if showId is not None else {}
# def loadPatterns(self, show_id):
@ -79,10 +81,11 @@ class PatternDetailsScreen(Screen):
def on_mount(self):
if self.pattern_obj:
self.query_one("#show_id_static", Static).update(str(self.pattern_obj['show_id']))
self.query_one("#pattern_input", Input).value = str(self.pattern_obj['pattern'])
if self.show_obj:
self.query_one("#showlabel", Static).update(f"{self.show_obj['id']} - {self.show_obj['name']} ({self.show_obj['year']})")
# for pattern in self.loadPatterns(int(self.show_obj['id'])):
# row = (pattern['pattern'],)
# self.patternTable.add_row(*map(str, row))
@ -105,16 +108,15 @@ class PatternDetailsScreen(Screen):
with Grid():
yield Static("Filename pattern" if self.pattern_obj else "New filename pattern", id="toplabel")
yield Static("Filename pattern" if self.pattern_obj else "New filename pattern", id="toplabel", classes="two")
yield Static("Show")
yield Static("", id="show_id_static")
yield Static("from show")
yield Static("", id="showlabel")
yield Static("Pattern")
yield Input(type="text", id="pattern_input")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Button("Save", id="save_button")
yield Button("Cancel", id="cancel_button")
@ -122,13 +124,8 @@ class PatternDetailsScreen(Screen):
yield Footer()
def getValues(self):
showId = int(self.show_obj['id'])
pattern = str(self.query_one("#pattern_input", Input).value)
return showId, pattern
def getPatternFromInput(self):
return str(self.query_one("#pattern_input", Input).value)
# Event handler for button press
@ -136,12 +133,12 @@ class PatternDetailsScreen(Screen):
# Check if the button pressed is the one we are interested in
if event.button.id == "save_button":
showId, pattern = self.getValues()
pattern = self.getPatternFromInput()
if self.updatePattern(showId, pattern):
if self.__pc.updatePattern(self.show_obj['id'], pattern):
screenResult = {}
screenResult['show_id'] = showId
screenResult['show_id'] = self.show_obj['id']
screenResult['pattern'] = pattern
self.dismiss(screenResult)
@ -152,34 +149,3 @@ class PatternDetailsScreen(Screen):
if event.button.id == "cancel_button":
self.app.pop_screen()
def updatePattern(self, show_id, pattern):
try:
s = self.Session()
q = s.query(Pattern).filter(Pattern.show_id == int(show_id), Pattern.pattern == str(pattern))
if not q.count():
pattern = Pattern(show_id = int(show_id), pattern = str(pattern))
s.add(pattern)
s.commit()
return True
# else:
#
# currentShow = q.first()
#
# changed = False
# if currentShow.name != str(show_name):
# currentShow.name = str(show_name)
# changed = True
# if currentShow.year != int(show_year):
# currentShow.year = int(show_year)
# changed = True
# if changed:
# s.commit()
# return changed
except Exception as ex:
click.ClickException(f"ShowDetailsScreen.updateShow(): {repr(ex)}")
finally:
s.close()

@ -1,2 +1,115 @@
import click
from ffx.model.show import Show
class ShowController():
pass
def __init__(self, context):
self.context = context
self.Session = self.context['database_session'] # convenience
def getShowDesciptor(self, showId):
try:
s = self.Session()
q = s.query(Show).filter(Show.id == showId)
showDescriptor = {}
if q.count():
show = q.first()
showDescriptor['id'] = int(show.id)
showDescriptor['name'] = str(show.name)
showDescriptor['year'] = int(show.year)
showDescriptor['index_season_digits'] = int(show.index_season_digits)
showDescriptor['index_episode_digits'] = int(show.index_episode_digits)
showDescriptor['indicator_season_digits'] = int(show.indicator_season_digits)
showDescriptor['indicator_episode_digits'] = int(show.indicator_episode_digits)
return showDescriptor
except Exception as ex:
raise click.ClickException(f"ShowController.getShowDesciptor(): {repr(ex)}")
finally:
s.close()
def updateShow(self, showDescriptor):
try:
s = self.Session()
q = s.query(Show).filter(Show.id == showDescriptor['id'])
if not q.count():
show = Show(id = int(showDescriptor['id']),
name = str(showDescriptor['name']),
year = int(showDescriptor['year']),
index_season_digits = showDescriptor['index_season_digits'],
index_episode_digits = showDescriptor['index_episode_digits'],
indicator_season_digits = showDescriptor['indicator_season_digits'],
indicator_episode_digits = showDescriptor['indicator_episode_digits'])
s.add(show)
s.commit()
return True
else:
currentShow = q.first()
changed = False
if currentShow.name != str(showDescriptor['name']):
currentShow.name = str(showDescriptor['name'])
changed = True
if currentShow.year != int(showDescriptor['year']):
currentShow.year = int(showDescriptor['year'])
changed = True
if currentShow.index_season_digits != int(showDescriptor['index_season_digits']):
currentShow.index_season_digits = int(showDescriptor['index_season_digits'])
changed = True
if currentShow.index_episode_digits != int(showDescriptor['index_episode_digits']):
currentShow.index_episode_digits = int(showDescriptor['index_episode_digits'])
changed = True
if currentShow.indicator_season_digits != int(showDescriptor['indicator_season_digits']):
currentShow.indicator_season_digits = int(showDescriptor['indicator_season_digits'])
changed = True
if currentShow.indicator_episode_digits != int(showDescriptor['indicator_episode_digits']):
currentShow.indicator_episode_digits = int(showDescriptor['indicator_episode_digits'])
changed = True
if changed:
s.commit()
return changed
except Exception as ex:
raise click.ClickException(f"ShowController.updateShow(): {repr(ex)}")
finally:
s.close()
def deleteShow(self, show_id):
try:
s = self.Session()
q = s.query(Show).filter(Show.id == int(show_id))
if q.count():
#DAFUQ: https://stackoverflow.com/a/19245058
# q.delete()
show = q.first()
s.delete(show)
s.commit()
return True
return False
except Exception as ex:
raise click.ClickException(f"ShowController.deleteShow(): {repr(ex)}")
finally:
s.close()

@ -8,6 +8,8 @@ from textual.containers import Grid, Horizontal
from ffx.model.show import Show
from .show_controller import ShowController
# Screen[dict[int, str, int]]
class ShowDeleteScreen(Screen):
@ -29,15 +31,11 @@ class ShowDeleteScreen(Screen):
border: none;
}
#toplabel {
height: 1;
column-span: 2;
height: 1;
}
#two {
.two {
column-span: 2;
row-span: 2;
tint: magenta 40%;
}
.box {
@ -46,14 +44,14 @@ class ShowDeleteScreen(Screen):
}
"""
def __init__(self, show = {}):
def __init__(self, showId = None):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
self.show_obj = show
self.__sc = ShowController(context = self.context)
self.show_obj = self.__sc.getShowDesciptor(showId) if showId is not None else {}
def on_mount(self):
@ -69,19 +67,16 @@ class ShowDeleteScreen(Screen):
with Grid():
yield Static("Are you sure to delete the following show?", id="toplabel")
yield Static("Are you sure to delete the following show?", id="toplabel", classes="two")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Static("", id="showlabel")
yield Static("")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Static("")
yield Static("")
yield Static("", classes="two")
yield Button("Delete", id="delete_button")
yield Button("Cancel", id="cancel_button")
@ -95,7 +90,7 @@ class ShowDeleteScreen(Screen):
if event.button.id == "delete_button":
if self.deleteShow(self.show_obj['id']):
if self.__sc.deleteShow(self.show_obj['id']):
self.dismiss(self.show_obj['id'])
else:
@ -104,25 +99,3 @@ class ShowDeleteScreen(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 == int(show_id))
if q.count():
#DAFUQ: https://stackoverflow.com/a/19245058
# q.delete()
show = q.first()
s.delete(show)
s.commit()
return True
return False
except Exception as ex:
click.ClickException(f"ShowDetailsScreen.updateShow(): {repr(ex)}")
finally:
s.close()

@ -12,6 +12,9 @@ from ffx.model.pattern import Pattern
from .pattern_details_screen import PatternDetailsScreen
from .pattern_delete_screen import PatternDeleteScreen
from .show_controller import ShowController
from .pattern_controller import PatternController
# Screen[dict[int, str, int]]
class ShowDetailsScreen(Screen):
@ -39,14 +42,11 @@ class ShowDetailsScreen(Screen):
#toplabel {
height: 1;
column-span: 2;
}
#two {
.two {
column-span: 2;
row-span: 2;
tint: magenta 40%;
}
.box {
@ -61,14 +61,17 @@ class ShowDetailsScreen(Screen):
("r", "remove_pattern", "Remove Pattern"),
]
def __init__(self, show = {}):
def __init__(self, showId = None):
super().__init__()
self.context = self.app.getContext()
self.Session = self.context['database_session'] # convenience
self.__sc = ShowController(context = self.context)
self.__pc = PatternController(context = self.context)
self.show_obj = self.__sc.getShowDesciptor(showId) if showId is not None else {}
self.show_obj = show
def loadPatterns(self, show_id):
@ -92,10 +95,21 @@ class ShowDetailsScreen(Screen):
self.query_one("#name_input", Input).value = str(self.show_obj['name'])
self.query_one("#year_input", Input).value = str(self.show_obj['year'])
self.query_one("#index_season_digits_input", Input).value = str(self.show_obj['index_season_digits'])
self.query_one("#index_episode_digits_input", Input).value = str(self.show_obj['index_episode_digits'])
self.query_one("#indicator_season_digits_input", Input).value = str(self.show_obj['indicator_season_digits'])
self.query_one("#indicator_episode_digits_input", Input).value = str(self.show_obj['indicator_episode_digits'])
for pattern in self.loadPatterns(int(self.show_obj['id'])):
row = (pattern['pattern'],)
self.patternTable.add_row(*map(str, row))
else:
self.query_one("#index_season_digits_input", Input).value = "2"
self.query_one("#index_episode_digits_input", Input).value = "2"
self.query_one("#indicator_season_digits_input", Input).value = "2"
self.query_one("#indicator_episode_digits_input", Input).value = "2"
def getSelectedPattern(self):
@ -109,7 +123,7 @@ class ShowDetailsScreen(Screen):
if row_key is not None:
selected_row_data = self.patternTable.get_row(row_key)
selectedPattern['pattern'] = selected_row_data[0]
selectedPattern['pattern'] = str(selected_row_data[0])
#selectedPattern['pattern'] = selected_row_data[1]
return selectedPattern
@ -118,7 +132,7 @@ class ShowDetailsScreen(Screen):
def action_add_pattern(self):
if self.show_obj:
self.app.push_screen(PatternDetailsScreen(show = self.show_obj), self.handle_add_pattern)
self.app.push_screen(PatternDetailsScreen(showId = self.show_obj['id']), self.handle_add_pattern)
def handle_add_pattern(self, screenResult):
@ -132,7 +146,14 @@ class ShowDetailsScreen(Screen):
selectedPattern = self.getSelectedPattern()
if selectedPattern:
self.app.push_screen(PatternDeleteScreen(pattern = selectedPattern, show = self.show_obj), self.handle_remove_pattern)
selectedPatternId = self.__pc.findPattern(self.show_obj['id'], selectedPattern['pattern'])
if selectedPatternId is None:
raise click.ClickException(f"ShowDetailsScreen.action_remove_pattern(): Pattern to remove has no id")
self.app.push_screen(PatternDeleteScreen(patternId = selectedPatternId, showId = self.show_obj['id']), self.handle_remove_pattern)
def handle_remove_pattern(self, screenResult):
row_key, col_key = self.patternTable.coordinate_to_cell_key(self.patternTable.cursor_coordinate)
@ -142,10 +163,10 @@ class ShowDetailsScreen(Screen):
def compose(self):
# Create the DataTable widget
self.patternTable = DataTable()
self.patternTable = DataTable(classes="two")
# Define the columns with headers
self.column_key_id = self.patternTable.add_column("Pattern", width=60)
self.column_key_id = self.patternTable.add_column("Patterns", width=60)
#self.column_key_name = self.patternTable.add_column("Name", width=50)
#self.column_key_year = self.patternTable.add_column("Year", width=10)
@ -156,9 +177,9 @@ class ShowDetailsScreen(Screen):
with Grid():
yield Static("Show" if self.show_obj else "New Show", id="toplabel")
yield Static("ID")
yield Static("Show" if self.show_obj else "New Show", id="toplabel", classes="two")
yield Static("ID")
if self.show_obj:
yield Static("", id="id_wdg")
else:
@ -169,13 +190,22 @@ class ShowDetailsScreen(Screen):
yield Static("Year")
yield Input(type="integer", id="year_input")
yield Static("")
yield Static("")
yield Static(" ", classes="two")
yield Static("Index Season Digits")
yield Input(type="integer", id="index_season_digits_input")
yield Static("Index Episode Digits")
yield Input(type="integer", id="index_episode_digits_input")
yield Static("Indicator Season Digits")
yield Input(type="integer", id="indicator_season_digits_input")
yield Static("Indicator Edisode Digits")
yield Input(type="integer", id="indicator_episode_digits_input")
yield Static(" ", classes="two")
yield self.patternTable
yield Static("")
yield Static("")
yield Static(" ", classes="two")
yield Button("Save", id="save_button")
yield Button("Cancel", id="cancel_button")
@ -183,16 +213,25 @@ class ShowDetailsScreen(Screen):
yield Footer()
def getValues(self):
def getShowDescriptorFromInput(self):
showDescriptor = {}
if self.show_obj:
showId = int(self.show_obj['id'])
showDescriptor['id'] = int(self.show_obj['id'])
else:
showId = int(self.query_one("#id_wdg", Input).value)
showDescriptor['id'] = int(self.query_one("#id_wdg", Input).value)
showDescriptor['name'] = str(self.query_one("#name_input", Input).value)
showDescriptor['year'] = int(self.query_one("#year_input", Input).value)
showDescriptor['index_season_digits'] = int(self.query_one("#index_season_digits_input", Input).value)
showDescriptor['index_episode_digits'] = int(self.query_one("#index_episode_digits_input", Input).value)
showDescriptor['indicator_season_digits'] = int(self.query_one("#indicator_season_digits_input", Input).value)
showDescriptor['indicator_episode_digits'] = int(self.query_one("#indicator_episode_digits_input", Input).value)
showName = str(self.query_one("#name_input", Input).value)
showYear = int(self.query_one("#year_input", Input).value)
return showId, showName, showYear
return showDescriptor
# Event handler for button press
@ -200,16 +239,10 @@ class ShowDetailsScreen(Screen):
# Check if the button pressed is the one we are interested in
if event.button.id == "save_button":
showId, showName, showYear = self.getValues()
if self.updateShow(showId, showName, showYear):
screenResult = {}
screenResult['id'] = showId
screenResult['name'] = showName
screenResult['year'] = showYear
showDescriptor = self.getShowDescriptorFromInput()
self.dismiss(screenResult)
if self.__sc.updateShow(showDescriptor):
self.dismiss(showDescriptor)
else:
#TODO: Meldung
self.app.pop_screen()
@ -217,36 +250,3 @@ class ShowDetailsScreen(Screen):
if event.button.id == "cancel_button":
self.app.pop_screen()
def updateShow(self, show_id, show_name, show_year):
try:
s = self.Session()
q = s.query(Show).filter(Show.id == show_id)
if not q.count():
show = Show(id = int(show_id),
name = str(show_name),
year = int(show_year))
s.add(show)
s.commit()
return True
else:
currentShow = q.first()
changed = False
if currentShow.name != str(show_name):
currentShow.name = str(show_name)
changed = True
if currentShow.year != int(show_year):
currentShow.year = int(show_year)
changed = True
if changed:
s.commit()
return changed
except Exception as ex:
click.ClickException(f"ShowDetailsScreen.updateShow(): {repr(ex)}")
finally:
s.close()

@ -59,22 +59,18 @@ class ShowsScreen(Screen):
def getSelectedShow(self):
def getSelectedShowId(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 selected_row_data[0]
return selectedShow
return None
@ -89,10 +85,10 @@ class ShowsScreen(Screen):
def action_edit_show(self):
selectedShow = self.getSelectedShow()
selectedShowId = self.getSelectedShowId()
if selectedShow:
self.app.push_screen(ShowDetailsScreen(show = selectedShow), self.handle_edit_screen)
if selectedShowId is not None:
self.app.push_screen(ShowDetailsScreen(showId = selectedShowId), self.handle_edit_screen)
def handle_edit_screen(self, screenResult):
@ -105,10 +101,10 @@ class ShowsScreen(Screen):
def action_delete_show(self):
selectedShow = self.getSelectedShow()
selectedShowId = self.getSelectedShowId()
if selectedShow:
self.app.push_screen(ShowDeleteScreen(show = selectedShow), self.handle_delete_show)
if selectedShowId is not None:
self.app.push_screen(ShowDeleteScreen(showId = selectedShowId), self.handle_delete_show)
def handle_delete_show(self, screenResult):

Loading…
Cancel
Save