Skip to content
Snippets Groups Projects
Commit aa66962e authored by Duncan Holmes's avatar Duncan Holmes
Browse files

finished ORM lab. we are forgiven

parent 78a04f6d
Branches
No related tags found
No related merge requests found
...@@ -19,6 +19,44 @@ class MoviesApp(App): ...@@ -19,6 +19,44 @@ class MoviesApp(App):
self.movie_database = MovieDatabase(url) self.movie_database = MovieDatabase(url)
self.session = self.movie_database.create_session() self.session = self.movie_database.create_session()
def get_selected_genres(self, action_selected, adventure_selected, sci_fi_selected, session):
selected_genres = []
if action_selected:
# Query for the existing "Action" genre in the database
action_genre = session.query(Genre).filter_by(name='Action').first()
if not action_genre: # If the genre doesn't exist, create it
action_genre = Genre(name='Action')
selected_genres.append(action_genre)
if adventure_selected:
adventure_genre = session.query(Genre).filter_by(name='Adventure').first()
if not adventure_genre:
adventure_genre = Genre(name='Adventure')
selected_genres.append(adventure_genre)
if sci_fi_selected:
sci_fi_genre = session.query(Genre).filter_by(name='SciFi').first()
if not sci_fi_genre:
sci_fi_genre = Genre(name='SciFi')
selected_genres.append(sci_fi_genre)
return selected_genres
def display_database(self, session):
# Fetch and print the entire database: Movies and their genres
print("\n---------- Entire Database: Movies and Genres ----------")
all_movies = session.query(Movie).all()
for movie in all_movies:
print(f"Movie ID: {movie.movie_id}, Title: {movie.title}")
print(f" Genres: {[genre.name for genre in movie.genres]}")
print("--------------------------------------------------------\n")
def update_text_below_rest_of_interface(self, text, *args):
self.root.ids.message.text = text
def clear_text_below_rest_of_interface(self, *args):
self.root.ids.message.text = ""
def create_movie(self, title, action_selected, adventure_selected, sci_fi_selected): def create_movie(self, title, action_selected, adventure_selected, sci_fi_selected):
if(title == "peepeepoopoo_1"): if(title == "peepeepoopoo_1"):
query = self.session.query(Genre) query = self.session.query(Genre)
...@@ -83,20 +121,6 @@ class MoviesApp(App): ...@@ -83,20 +121,6 @@ class MoviesApp(App):
self.update_text_below_rest_of_interface(text=f'{title} has been added to the database.') self.update_text_below_rest_of_interface(text=f'{title} has been added to the database.')
Clock.schedule_once(self.clear_text_below_rest_of_interface, 5) Clock.schedule_once(self.clear_text_below_rest_of_interface, 5)
def display_database(self, session):
# Fetch and print the entire database: Movies and their genres
print("\n---------- Entire Database: Movies and Genres ----------")
all_movies = session.query(Movie).all()
for movie in all_movies:
print(f"Movie ID: {movie.movie_id}, Title: {movie.title}")
print(f" Genres: {[genre.name for genre in movie.genres]}")
print("--------------------------------------------------------\n")
def update_text_below_rest_of_interface(self, text, *args):
self.root.ids.message.text = text
def clear_text_below_rest_of_interface(self, *args):
self.root.ids.message.text = ""
def update_movie(self, title, action_selected, adventure_selected, sci_fi_selected): def update_movie(self, title, action_selected, adventure_selected, sci_fi_selected):
session = self.movie_database.create_session() session = self.movie_database.create_session()
# Write code in update_movie to find the movie with the title entered by the user. # Write code in update_movie to find the movie with the title entered by the user.
...@@ -118,35 +142,43 @@ class MoviesApp(App): ...@@ -118,35 +142,43 @@ class MoviesApp(App):
# them to appear in the database.) # them to appear in the database.)
# i suppose .all returns a list. this poses a design question i will ignore. # i suppose .all returns a list. this poses a design question i will ignore.
# the design equesiton being if we're allowing duplicates. of that i will ignore # the design equesiton being if we're allowing duplicates. of that i will ignore
if session.query(Movie).filter_by(title=title).count() <= 0 :
self.update_text_below_rest_of_interface(text=f'{title} is not in the database.')
else:
session.query(Movie).filter_by(title=title).all()[0].genres = selected_genres session.query(Movie).filter_by(title=title).all()[0].genres = selected_genres
session.commit()
self.display_database(session) self.display_database(session)
self.update_text_below_rest_of_interface(text=f'{title} has been updated in the database.')
def get_selected_genres(self, action_selected, adventure_selected, sci_fi_selected, session): Clock.schedule_once(self.clear_text_below_rest_of_interface, 5)
selected_genres = []
if action_selected:
# Query for the existing "Action" genre in the database
action_genre = session.query(Genre).filter_by(name='Action').first()
if not action_genre: # If the genre doesn't exist, create it
action_genre = Genre(name='Action')
selected_genres.append(action_genre)
if adventure_selected:
adventure_genre = session.query(Genre).filter_by(name='Adventure').first()
if not adventure_genre:
adventure_genre = Genre(name='Adventure')
selected_genres.append(adventure_genre)
if sci_fi_selected: def delete_movie(self, title):
sci_fi_genre = session.query(Genre).filter_by(name='SciFi').first() #Queries have a method .count() that counts the matching rows;
if not sci_fi_genre: # it provides less information than the .one() method, but also has better
sci_fi_genre = Genre(name='SciFi') # performance. Using the count method, write code to throw a ValueError
selected_genres.append(sci_fi_genre) # if no movies have the title the user entered.
session = self.movie_database.create_session()
#rememebr this line it's the most badass line ever
movie = session.query(Movie).filter_by(title=title).first()
if not movie: #wack ass python conditional
self.update_text_below_rest_of_interface(text=f'{title} is not in the database.')
Clock.schedule_once(self.clear_text_below_rest_of_interface, 5)
return
#.clear() is a list method
movie.genres.clear()
# 0_0
session.delete(movie)
self.display_database(session)
self.update_text_below_rest_of_interface(text=f'{title} has been deleted. rest in pepperonis.')
return selected_genres
def delete_movie(self, title):
pass
def main(): def main():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment