def add_image_link(movie_id: int, link: str, repo: AbstractRepository): movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException # Update the repository. repo.add_image_link(link, movie)
def add_user(username: str, password: str, repo: AbstractRepository): # Check that the given username is available. user = repo.get_user(username) if user is not None: raise NameNotUniqueException # Encrypt password so that the database doesn't store passwords 'in the clear'. password_hash = generate_password_hash(password) # Create and store the new User, with password encrypted. user = User(username, password_hash) repo.add_user(user)
def get_random_movies(quantity, repo: AbstractRepository): movie_count = repo.get_number_of_movies() if quantity >= movie_count: # Reduce the quantity of ids to generate if the repository has an insufficient number of movies. quantity = movie_count - 1 # Pick distinct and random movies. random_ids = random.sample(range(1, movie_count), quantity) movies = repo.get_movies_by_id(random_ids) return movies_to_dict(movies)
def add_rating(movie_id: int, rating: float, username: str, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException user = repo.get_user(username) if user is None: user = User("Guest account", "Abcd1234") # Update the repository. repo.add_rating(rating, user, movie)
def get_movie(movie_id: int, repo: AbstractRepository): movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException return movie_to_dict(movie, repo)
def get_comments_for_movie(movie_id, repo: AbstractRepository): movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException return comments_to_dict(movie.comments)
def get_movies_by_id(id_list, repo: AbstractRepository): movies = repo.get_movies_by_id(id_list) # Convert Movies to dictionary form. movies_as_dict = movies_to_dict(movies, repo) return movies_as_dict
def genre_to_dict(genre: Genre, repo: AbstractRepository): genre_dict = { 'name': genre, 'tagged_movies': [movie.id for movie in repo.get_movies() if genre in movie.genres] } return genre_dict
def add_comment(movie_id: int, comment_text: str, username: str, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException user = repo.get_user(username) if user is None: print("cant find user") user = User("Guest account", "Abcd1234") # Create comment. comment = make_comment(comment_text, user, movie) # Update the repository. repo.add_comment(comment)
def authenticate_user(username: str, password: str, repo: AbstractRepository): authenticated = False user = repo.get_user(username) if user is not None: authenticated = check_password_hash(user.password, password) if not authenticated: raise AuthenticationException
def get_movies_by_date(date, repo: AbstractRepository): # Returns movies for the target date (empty if no matches), the date of the previous movie (might be null), the date # of the next movie (might be null) ids = get_movie_ids_for_date(date, repo) ids.sort() movies = repo.get_movies_by_id(ids) movies_dto = list() prev_date = next_date = None if len(movies) > 0: prev_date = repo.get_date_of_previous_movie(movies[0]) next_date = repo.get_date_of_next_movie(movies[0]) # Convert Movies to dictionary form. movies_dto = movies_to_dict(movies, repo) return movies_dto, prev_date, next_date
def get_user(username: str, repo: AbstractRepository): user = repo.get_user(username) if user is None: raise UnknownUserException return user_to_dict(user)
def get_movie_ids_for_actor(actor_name, repo: AbstractRepository): movie_ids = repo.get_movie_ids_for_actor(actor_name) return movie_ids
def get_genre_names(repo: AbstractRepository): genres = repo.get_genres() genre_names = [genre.genre_name for genre in genres] return genre_names
def get_movie_ids_for_date(date, repo: AbstractRepository): movie_ids = repo.get_movie_ids_for_date(date) return movie_ids
def get_titles(repo: AbstractRepository): movies = repo.get_movies() titles = [movie.title for movie in movies] return titles
def get_last_movie(repo: AbstractRepository): movie = repo.get_last_movie() return movie_to_dict(movie, repo)
def get_dates(repo: AbstractRepository): dates = repo.get_dates() return dates
def get_actor_names(repo: AbstractRepository): actors = repo.get_actors() actor_names = [actor.actor_full_name for actor in actors] return actor_names
def get_director_names(repo: AbstractRepository): directors = repo.get_directors() director_names = [director.director_full_name for director in directors] return director_names
def get_movie_ids_for_genre(genre_name, repo: AbstractRepository): movie_ids = repo.get_movie_ids_for_genre(genre_name) return movie_ids
def get_movie_ids_for_title(title, repo: AbstractRepository): movie_ids = repo.get_movie_ids_for_title(title) return movie_ids