def get_random_articles(quantity, repo: AbstractRepository): article_count = repo.get_number_of_movie() if quantity >= article_count: # Reduce the quantity of ids to generate if the repository has an insufficient number of articles. quantity = article_count - 1 # Pick distinct and random articles. random_ids = random.sample(range(1, article_count), quantity) articles = repo.get_movie_by_rank(random_ids) return articles_to_dict(articles)
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_movie_by_rank(rank, repo: AbstractRepository): movies = repo.get_movie_by_rank(target_rank=rank) movies_data = list() prev_rank = next_rank = None if len(movies) > 0: prev_rank = repo.get_rank_of_previous_movie(movies[0]) next_rank = repo.get_rank_of_next_movie(movies[0]) # Convert Articles to dictionary form. movies_data = movies_to_dict(movies) return movies_data, prev_rank, next_rank
def add_comment(movie_rank: int, comment_text: str, username: str, 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 comment. comment = make_comment(comment_text, user, movie) # Update the repository. repo.add_comment(comment)
def get_movies_by_rank(id_list, repo: AbstractRepository): articles = repo.get_movie_by_rank(id_list) # Convert Articles to dictionary form. articles_as_dict = articles_to_dict(articles) return articles_as_dict
def get_comments_for_article(article_id, repo: AbstractRepository): article = repo.get_movie(article_id) if article is None: raise NonExistentArticleException return reviews_to_dict(article.review)
def get_movie(article_id: int, repo: AbstractRepository): article = repo.get_movie(article_id) if article is None: raise NonExistentArticleException return article_to_dict(article)
def add_review(article_id: int, comment_text: str, username: str, repo: AbstractRepository): # Check that the article exists. article = repo.get_movie(article_id) if article is None: raise NonExistentArticleException user = repo.get_user(username) if user is None: raise UnknownUserException # Create comment. comment = make_comment(comment_text, user, article) # Update the repository. repo.add_review(comment)
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_movie(movie_rank: int, repo: AbstractRepository): movie = repo.get_movie(movie_rank) if movie is None: raise NonExistentMovieException return movie_to_dict(movie)
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_year(date, repo: AbstractRepository): # Returns articles for the target date (empty if no matches), the date of the previous article (might be null), the date of the next article (might be null) articles = repo.get_movie_by_year(target_date=date) articles_dto = list() prev_date = next_date = None if len(articles) > 0: # Convert Articles to dictionary form. articles_dto = articles_to_dict(articles) return articles_dto, prev_date, next_date
def get_movie_ranks_for_director(director_name, repo: AbstractRepository): movie_ids = repo.get_movie_ranks_for_director(director_name) return movie_ids
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_first_movie(repo: AbstractRepository): first_movie = repo.get_first_movie() return movie_to_dict(first_movie)
def get_genre_names(repo: AbstractRepository): tags = repo.get_genres() tag_names = [tag.genre_name for tag in tags] return tag_names
def get_last_movie(repo: AbstractRepository): last_movie = repo.get_last_movie() return movie_to_dict(last_movie)
def get_director_name(repo: AbstractRepository): director = repo.get_directors() return director
def get_actor_name(repo: AbstractRepository): actors = repo.get_actors() actor_names = [actor.actor_full_name for actor in actors] return actor_names
def get_genre_names(repo: AbstractRepository): genres = repo.get_genres() genre_names = [genre.genre_name for genre in genres] return genre_names
def get_last_movie(repo: AbstractRepository): article = repo.get_last_movie() return article_to_dict(article)
def get_movie_ranks_for_genre(tag_name, repo: AbstractRepository): article_ids = repo.get_movie_ranks_for_genre(tag_name) return article_ids