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

Upload New File

parent d475f7e5
No related branches found
No related tags found
No related merge requests found
from sys import stderr
from kivy.app import App
from sqlalchemy.exc import SQLAlchemyError
from movies import MovieDatabase, Movie, Genre
from kivy.clock import Clock
from kivy.uix.label import Label
class MoviesApp(App):
def __init__(self, **kwargs):
super(MoviesApp, self).__init__(**kwargs)
url = (MovieDatabase.construct_mysql_url
('localhost',
3306,
'movies',
'root',
'FunNyMeMe123!%40%23'))
self.movie_database = MovieDatabase(url)
self.session = self.movie_database.create_session()
def create_movie(self, title, action_selected, adventure_selected, sci_fi_selected):
if(title == "peepeepoopoo_1"):
query = self.session.query(Genre)
print(query)
exit("I'M NOT RUNNING ANYTHING ELSE \n I HATE YOU")
# SELECT genres.genre_id AS genres_genre_id, genres.name AS genres_name
# FROM genres
elif( title == "peepeepoopoo_2"):
query = self.session.query(Genre).filter(Genre.name == 'Drama')
print(query)
exit("I'M NOT RUNNING ANYTHING ELSE \n I HATE YOU")
# SELECT genres.genre_id AS genres_genre_id, genres.name AS genres_name
# FROM genres
# WHERE genres.name = %(name_1)s
elif( title == "peepeepoopoo_3"):
query = self.session.query(Genre)
print(query)
query = self.session.query(Genre).filter(Genre.name == 'Drama')
print(query)
exit("I'M NOT RUNNING ANYTHING ELSE \n I HATE YOU")
"""
add a movie to the database
class Movie(Persisted):
movie_id = Column(Integer, primary_key=True)
title = Column(String(256), nullable=False)
budget = Column(Integer)
gross_revenue = Column(Integer)
opening_date = Column(Date)
genres = relationship('Genre', uselist=True, secondary='movie_genres', back_populates='movies')
reviews = relationship('Review', uselist=True, back_populates='movie')
"""
# Fetch session from the database
session = self.movie_database.create_session()
# Create the movie object
incoming_movie = Movie(title=title) # Add more attributes if required
# Fetch or create the required genres
selected_genres = self.get_selected_genres(action_selected=action_selected,
adventure_selected=adventure_selected,
sci_fi_selected=sci_fi_selected,
session=session) # A list to hold the fetched/linked genres
# Assign the genres to the movie
incoming_movie.genres = selected_genres
print("i'm printing everything")
print(f"\tincoming movie = {incoming_movie}")
print(f"\tincoming_movie.genres = {incoming_movie.genres}")
print(f"\tincoming_movie.tilte = {incoming_movie.title}")
# Add and commit the movie to the database
session.add(incoming_movie)
session.commit()
print(f"Movie '{incoming_movie.title}' added with genres: {[genre.name for genre in selected_genres]}")
self.display_database(session)
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.
# .all() seems to funcitoin the same as .first()
# not tested but i assume it gives all of the results with that name.
#print( session.query(Movie).filter_by( title=title).all() )
#Write code in update_movie to build a list containing the genres selected by the user and change the movie's genre list to that list.
selected_genres = self.get_selected_genres(action_selected=action_selected,
adventure_selected=adventure_selected,
sci_fi_selected=sci_fi_selected,
session=session)
#Write code to add and commit the changes to the database.
# (Just as with Git, a file must be added and committed in order
# to record changes that have been made to it; ORM objects in
# SQLAlchemy must be added and committed for changes made to
# 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)
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 delete_movie(self, title):
pass
def main():
try:
app = MoviesApp()
app.run()
except SQLAlchemyError as exception:
print('Database connection failed!', file=stderr)
print(f'Cause: {exception}', file=stderr)
exit(1)
if __name__ == '__main__':
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment