예제 #1
0
def get_user_watchlist(username: str, repo: AbstractRepository):
    """
    """
    watchlist = repo.get_watchlists_for_user(repo.get_user(username=username))
    if watchlist and watchlist.size() != 0:
        return [movie_to_dict(movie) for movie in watchlist]
    else:
        return []
예제 #2
0
def get_movies_by_director(director: str, repo: AbstractRepository):
    """
    """
    director = repo.get_director_by_name(director)
    if director is None:
        raise BrowseException
    movies = repo.get_movies_by_director(director)
    return [utils.movie_to_dict(movie) for movie in movies]
예제 #3
0
def watch_movie(username: str, title: str, year: str,
                repo: AbstractRepository):
    """
    """
    movie = repo.get_movie(title, int(year))
    if movie:
        user = repo.get_user(username)
        if user:
            user.watch_movie(movie)
예제 #4
0
def remove_from_watchlist(username: str, title: str, year: str,
                          repo: AbstractRepository):
    """
    """
    user = repo.get_user(username)
    if user:
        movie = repo.get_movie(title, int(year))
        if movie:
            repo.remove_movie_from_watchlist(user, movie)
예제 #5
0
def get_movies_by_genre(genre: str, repo: AbstractRepository):
    """
    """
    genre = repo.get_genre(genre)
    if genre is None:
        raise UnknownGenreException
    movies = repo.get_movies_by_genre(genre)

    return [utils.movie_to_dict(movie) for movie in movies]
예제 #6
0
def add_user(username: str, password: str, repo: AbstractRepository):
    """
    """
    user = repo.get_user(username)
    if user:
        raise NameNotUnqiueException

    password_hash = generate_password_hash(password)

    repo.add_user(User(username, password_hash))
예제 #7
0
def add_to_watchlist(movie_rank: int, repo: AbstractRepository):
    # Check that the movie exists
    movie = repo.get_movie(movie_rank)
    if movie is None:
        raise NonExistentMovieException

    # Update the repository
    repo.add_to_watchlist(movie)

    add = repo.check_if_added(movie_rank)
예제 #8
0
def get_movies_by_actor(actor, repo: AbstractRepository):
    """
    """
    actor = repo.get_actor(actor)
    if actor is None:
        raise PersonException('Actor does not exist')
    movies = repo.get_movies_by_actor(actor)
    return [
        utils.movie_to_dict(movie)
        for movie in sorted(movies, key=lambda x: x.rank)
    ], get_collegues(actor)
예제 #9
0
def get_movies_by_director(director: str, repo: AbstractRepository):
    """
    """
    director = repo.get_director_by_name(director)
    if director is None:
        raise PersonException
    movies = repo.get_movies_by_director(director)
    return [
        utils.movie_to_dict(movie)
        for movie in sorted(movies, key=lambda x: x.rank)
    ]
예제 #10
0
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 articles.
    random_ranks = random.sample(range(1, movie_count), quantity)
    movies = repo.get_movies_by_rank(random_ranks)

    return movies_to_dict(movies)
예제 #11
0
def add_user(user_name: str, password: str, repo: AbstractRepository):
    # Check that the given username is available.
    user = repo.get_user(user_name)
    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(user_name, password_hash)
    repo.add_user(user)
예제 #12
0
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)

    movies = repo.get_movies_by_date(int(date))

    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])

    return movies, prev_date, next_date
예제 #13
0
def add_review(title: str, year: str, review_text: str, username: str,
               rating: int, repo: AbstractRepository):
    """
    """
    movie = repo.get_movie(title, int(year))
    if movie is None:
        raise UnknownMovieException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    review = make_review(user, movie, review_text, rating)
    repo.add_review(review)
예제 #14
0
def add_review(movie_rank: int, review_text: str, username: str, rating, repo: AbstractRepository):
    # Check that the movie exists.
    movie = repo.get_movie(movie_rank)
    if movie is None:
        raise NonExistentMovieException

    user = repo.get_user(username)
    if user is None:
        raise UnknownUserException

    # Create review.
    review = make_review(review_text, user, movie, rating)

    # Update the repository.
    repo.add_review(review)
예제 #15
0
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, id_list, repo)

    return movies_as_dict
예제 #16
0
def get_movie_by_director(director_name, repo: AbstractRepository):
    movies = repo.get_movie_by_director(director_name)
    movies_dto = list()
    if len(movies) > 0:
        movies_dto = movies_to_dict(movies)

    return movies_dto
예제 #17
0
def get_genre(genre, repo: AbstractRepository):
    genre = repo.get_genre()

    if genre is None:
        raise NonExistentMovieException

    return genre
예제 #18
0
def tag_to_dict(tag: Tag, movie_id: int, repo: AbstractRepository):
    tag_dict = {
        'name': tag.tag_name,
        'tagged_movies':
        [repo.movie_index(movie) for movie in tag.tagged_movies]
    }
    return tag_dict
예제 #19
0
def get_movies_by_type(rank_list: list, repo: AbstractRepository):
    movies = repo.get_movies_by_rank(rank_list)

    # Convert Movies to dictionary form.
    movies_as_dict = movies_to_dict(movies)

    return movies_as_dict
예제 #20
0
def get_director_for_movie(movie_id, repo: AbstractRepository):
    movie = repo.get_movie_by_id(movie_id)

    if movie is None:
        raise NonExistentMovieException

    return director_to_dict(movie.director)
예제 #21
0
def get_movies_by_genre(movie: str, repo: AbstractRepository):
    movie = repo.get_movie(movie.__repr__())

    if movie is None:
        raise NonExistentMovieException

    return movie_to_dict(movie)
예제 #22
0
def get_movie(movie_rank: int, repo: AbstractRepository):
    movie = repo.get_movie(movie_rank)

    if movie is None:
        raise NonExistentMovieException

    return movie_to_dict(movie)
예제 #23
0
def get_all_movies(repo: AbstractRepository):
    movies = repo.get_all_movies()
    movies_dto = list()
    if len(movies) > 0:
        movies_dto = movies_to_dict(movies)

    return movies_dto
예제 #24
0
def get_reviews_for_movie(movie_rank, repo: AbstractRepository):
    movie = repo.get_movie(movie_rank)

    if movie is None:
        raise NonExistentMovieException

    return reviews_to_dict(movie.reviews)
예제 #25
0
def get_movie_for_genre(genre_name, repo: AbstractRepository):
    movies = repo.get_movie_for_genre(genre_name)
    movies_dto = list()
    if len(movies) > 0:
        movies_dto = movies_to_dict(movies)

    return movies_dto
예제 #26
0
def get_genres(repo: AbstractRepository):
    """
    """
    genres = []
    for genre in sorted(repo.dataset_of_genres):
        movies = repo.get_movies_by_genre(genre)
        genres.append(dict(name=genre.genre_name, movies=[utils.movie_to_dict(movie) for movie in movies]))
    return genres
예제 #27
0
def get_user(username, repo: AbstractRepository):
    """
    """
    user = repo.get_user(username)
    if user:
        return user_to_dict(user)
    else:
        return None
예제 #28
0
def get_actors(repo: AbstractRepository):
    """
    """
    actors = []
    for actor in sorted(repo.dataset_of_actors):
        movies = repo.get_movies_by_actor(actor)
        actors.append(dict(name=actor.actor_full_name, movies=[utils.movie_to_dict(movie) for movie in movies]))
    return actors
예제 #29
0
def authenticate_user(user_name: str, password: str, repo: AbstractRepository):
    authenticated = False

    user = repo.get_user(user_name)
    if user is not None:
        authenticated = check_password_hash(user.password, password)
    if not authenticated:
        raise AuthenticationException
예제 #30
0
def get_directors(repo: AbstractRepository):
    """
    """
    directors = []
    for director in sorted(repo.dataset_of_directors):
        movies = repo.get_movies_by_director(director)
        directors.append(dict(movies=movies, name=director.director_full_name))
    return directors