def __init__(self, arg_username=None, arg_password=None, arg_watched=None, arg_watched_codes="", arg_reviews=None, arg_review_codes="", arg_timewatching=0, arg_watchlist=None, arg_watchlist_code=None, arg_code=None): self.user_username = arg_username.strip().lower() if isinstance( arg_username, str) else None self.user_code = str(hash( self.user_username)) if arg_code is None else arg_code self.user_password = arg_password if isinstance(arg_password, str) else None self.user_watched = list() if arg_watched is None else arg_watched self.user_watched_codes = arg_watched_codes self.user_reviews = list() if arg_reviews is None else arg_reviews self.user_review_codes = arg_review_codes self.user_timewatching = arg_timewatching self.user_watchlist = Watchlist( self.user_code) if arg_watchlist is None else arg_watchlist self.user_watchlist_code = self.user_watchlist.code if arg_watchlist_code is None else arg_watchlist_code
def __init__(self, userName, userPassword): self.user_username = userName.strip().lower() if isinstance( userName, str) else None self.user_password = userPassword if isinstance(userPassword, str) else None self.user_watched = list() self.user_reviews = list() self.user_watchlist = Watchlist() self.user_timewatching = 0
def watchlist(user): watchlist = Watchlist(user.code) watchlist.add_movie(Movie("Moana", 2016)) watchlist.add_movie(Movie("Ice Age", 2002)) watchlist.add_movie(Movie("Guardians of the Galaxy", 2012)) return watchlist
class User: def __init__(self, arg_username=None, arg_password=None, arg_watched=None, arg_watched_codes="", arg_reviews=None, arg_review_codes="", arg_timewatching=0, arg_watchlist=None, arg_watchlist_code=None, arg_code=None): self.user_username = arg_username.strip().lower() if isinstance( arg_username, str) else None self.user_code = str(hash( self.user_username)) if arg_code is None else arg_code self.user_password = arg_password if isinstance(arg_password, str) else None self.user_watched = list() if arg_watched is None else arg_watched self.user_watched_codes = arg_watched_codes self.user_reviews = list() if arg_reviews is None else arg_reviews self.user_review_codes = arg_review_codes self.user_timewatching = arg_timewatching self.user_watchlist = Watchlist( self.user_code) if arg_watchlist is None else arg_watchlist self.user_watchlist_code = self.user_watchlist.code if arg_watchlist_code is None else arg_watchlist_code def __repr__(self): return f"<User {self.user_username}>" def __eq__(self, other): return self.user_username == other.username def __lt__(self, other): return self.user_username < other.username def __hash__(self): return hash(self.user_username) @property def username(self): return self.user_username @property def password(self): return self.user_password @property def watched_movies(self): return self.user_watched @property def watched_movie_codes(self): return self.user_watched_codes @property def reviews(self): return self.user_reviews @property def review_codes(self): return self.user_review_codes @property def time_spent_watching_movies_minutes(self): return self.user_timewatching @property def watchlist(self): return self.user_watchlist @property def watchlist_code(self): return self.user_watchlist_code @property def code(self): return self.user_code @username.setter def username(self, newName): if isinstance(newName, str): self.user_username = newName.strip().lower() @password.setter def password(self, newPassword): if isinstance(newPassword, str): self.user_password = newPassword @watched_movies.setter def watched_movies(self, newWatched): if isinstance(newWatched, list): self.user_watched = newWatched self.user_watched_codes = ",".join( [movie.code for movie in self.user_watched]) @watched_movie_codes.setter def watched_movie_codes(self, new): print("WARNING: Codes cannot be manually reassigned") @reviews.setter def reviews(self, newReviews): if isinstance(newReviews, list): self.user_reviews = newReviews @review_codes.setter def review_codes(self, new): print("WARNING: Codes cannot be manually reassigned") @time_spent_watching_movies_minutes.setter def time_spent_watching_movies_minutes(self, newTimeWatching): if isinstance(newTimeWatching, int): self.user_timewatching = newTimeWatching @watchlist.setter def watchlist(self, newWatchlist): self.user_watchlist = newWatchlist self.user_watchlist_code = self.user_watchlist.code @watchlist_code.setter def watchlist_code(self, new): print("WARNING: Codes cannot be manually reassigned") @code.setter def code(self, new): print("WARNING: Codes cannot be manually reassigned") def watch_movie(self, movie): if not movie in self.user_watched: self.user_watched.append(movie) self.user_watched_codes = ",".join( [movie.code for movie in self.user_watched]) self.user_timewatching += movie.runtime_minutes if movie in self.user_watchlist: self.user_watchlist.remove_movie(movie) def add_review(self, review): if review is not None: self.user_reviews.append(review) self.user_review_codes = ",".join( [review.code for review in self.user_reviews]) def remove_review(self, review): if review is not None: self.user_reviews.remove(review) self.user_review_codes = ",".join( [review.code for review in self.user_reviews])
def load(self, engine): print("LOADING DATABASE...") conn = engine.raw_connection() cursor = conn.cursor() # STEP ONE: Retrieve entries from database cursor.execute("""SELECT * FROM actors""") actors = cursor.fetchall() cursor.execute("""SELECT * FROM directors""") directors = cursor.fetchall() cursor.execute("""SELECT * FROM genres""") genres = cursor.fetchall() cursor.execute("""SELECT * FROM movies""") movies = cursor.fetchall() cursor.execute("""SELECT * FROM reviews""") reviews = cursor.fetchall() cursor.execute("""SELECT * FROM users""") users = cursor.fetchall() cursor.execute("""SELECT * FROM watchlists""") watchlists = cursor.fetchall() # STEP TWO: Convert entries into isolated object references for row in actors: actor = Actor(row[1], list(), row[2], list(), row[3], row[4]) self.repo_actors.add(actor) for row in directors: director = Director(row[1], list(), row[2], row[3]) self.repo_directors.add(director) for row in genres: genre = Genre(row[1], list(), row[2], row[3]) self.repo_genres.add(genre) for row in movies: movie = Movie(row[1], row[2], row[3], None, row[4], list(), row[5], list(), row[6], row[7], list(), row[8], row[9], float(row[10]), row[11], row[12]) self.repo_movies.append(movie) for row in reviews: review = Review(None, row[1], None, row[2], row[3], row[4], row[5], row[6], row[7]) self.repo_reviews.append(review) for row in users: user = User(row[1], row[2], list(), row[3], list(), row[4], row[5], None, row[6], row[7]) self.repo_users.append(user) for row in watchlists: watchlist = Watchlist(row[1], list(), row[2], row[3]) self.repo_watchlists.append(watchlist) # STEP THREE: Populate object relationships for actor in self.repo_actors: for code in actor.movie_codes.split(","): if code != "": actor.add_movie(self.find_movie(code)) for code in actor.colleague_codes.split(","): if code != "": actor.add_actor_colleague(self.find_actor(code)) for director in self.repo_directors: for code in director.movie_codes.split(","): if code != "": director.add_movie(self.find_movie(code)) for genre in self.repo_genres: for code in genre.movie_codes.split(","): if code != "": genre.add_movie(self.find_movie(code)) for movie in self.repo_movies: movie.director = self.find_director(movie.director_code) for code in movie.actor_codes.split(","): if code != "": movie.add_actor(self.find_actor(code)) for code in movie.genre_codes.split(","): if code != "": movie.add_genre(self.find_genre(code)) for code in movie.review_codes.split(","): if code != "": movie.add_review(self.find_review(code)) for review in self.repo_reviews: review.user = self.find_user(review.user_code) review.movie = self.find_movie(review.movie_code) for user in self.repo_users: for code in user.watched_movie_codes.split(","): if code != "": user.watched_movies.append(self.find_movie( code)) # Can't use watch_movie() for this for code in user.review_codes.split(","): if code != "": user.add_review(self.find_review(code)) user.watchlist = self.find_watchlist(user.watchlist_code) for watchlist in self.repo_watchlists: for code in watchlist.movie_codes.split(","): if code != "": watchlist.add_movie(self.find_movie(code)) conn.commit() conn.close() print("DATABASE LOADED")
class User: def __init__(self, userName, userPassword): self.user_username = userName.strip().lower() if isinstance( userName, str) else None self.user_password = userPassword if isinstance(userPassword, str) else None self.user_watched = list() self.user_reviews = list() self.user_watchlist = Watchlist() self.user_timewatching = 0 def __repr__(self): return f"<User {self.user_username}>" def __eq__(self, other): return self.user_username == other.username def __lt__(self, other): return self.user_username < other.username def __hash__(self): return hash(self.user_username) @property def username(self): return self.user_username @property def password(self): return self.user_password @property def watched_movies(self): return self.user_watched @property def reviews(self): return self.user_reviews @property def watchlist(self): return self.user_watchlist @property def time_spent_watching_movies_minutes(self): return self.user_timewatching @property def comments(self): return self.user_comments @username.setter def username(self, newName): if isinstance(newName, str): self.user_username = newName.strip().lower() @password.setter def password(self, newPassword): if isinstance(newPassword, str): self.user_password = newPassword @watched_movies.setter def watched_movies(self, newWatched): if isinstance(newWatched, list): self.user_watched = newWatched @reviews.setter def reviews(self, newReviews): if isinstance(newReviews, list): self.user_reviews = newReviews @watchlist.setter def watchlist(self, newWatchlist): self.user_watchlist = newWatchlist @comments.setter def comments(self, newComments): if isinstance(newComments, list): self.user_comments = newComments @time_spent_watching_movies_minutes.setter def time_spent_watching_movies_minutes(self, newTimeWatching): if isinstance(newTimeWatching, int): self.user_timewatching = newTimeWatching def watch_movie(self, movie): if not movie in self.user_watched: self.user_watched.append(movie) self.user_timewatching += movie.runtime_minutes if movie in self.user_watchlist: self.user_watchlist.remove_movie(movie) def add_review(self, review): self.user_reviews.append(review) def remove_review(self, review): self.user_reviews.remove(review)