diff --git a/lab on ORM/main.py b/lab on ORM/main.py index 10ba203c922e38bc5c0a4a196f803b56438ae38c..1d82f2587ee0234967a5a359a58c47d272bbceb2 100644 --- a/lab on ORM/main.py +++ b/lab on ORM/main.py @@ -19,6 +19,44 @@ class MoviesApp(App): self.movie_database = MovieDatabase(url) 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): if(title == "peepeepoopoo_1"): query = self.session.query(Genre) @@ -83,20 +121,6 @@ class MoviesApp(App): 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) - 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): session = self.movie_database.create_session() # Write code in update_movie to find the movie with the title entered by the user. @@ -118,35 +142,43 @@ class MoviesApp(App): # them to appear in the database.) # 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 - session.query(Movie).filter_by(title=title).all()[0].genres = selected_genres - self.display_database(session) + 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.commit() + 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): - selected_genres = [] + Clock.schedule_once(self.clear_text_below_rest_of_interface, 5) - 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) + def delete_movie(self, title): + #Queries have a method .count() that counts the matching rows; + # it provides less information than the .one() method, but also has better + # performance. Using the count method, write code to throw a ValueError + # 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():