def add_reviews(movie_id: int, review_content: str, rank: int, username: str, repo: AbstractRepository): movie = repo.get_movie_by_id(movie_id) # create the review review = Review(movie, review_content, rank) # add reviews to repository repo.add_review(review)
def add_user(username: str, password: str, repo: AbstractRepository): if repo.get_user(username): raise DuplicatedUsernameException password_hash = generate_password_hash(password) new_user = User(username, password_hash) repo.add_user(new_user) if has_app_context(): _save_users_to_disk(current_app.config['USER_DATA_PATH'], repo)
def add_review(movie_id: int, review_text: str, user: User, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_id) rating = 0 review = Review(movie, review_text, rating, user) # Update the repository. repo.add_review(review, movie, user)
def add_review(repo: AbstractRepository, movie: Movie, review_text: str, rating: int, user: Union[User, None] = None): """ Adds a review to this repository. If a user is specified it is treated as being the review's creator, otherwise the review is considered to be posted anonymously. """ review = Review(movie, review_text, rating, user=user) repo.add_review(review, user) if user: user.add_review(review)
def get_movie_reviews(repo: AbstractRepository, movie: Movie, page_number: int, page_size: int = DEFAULT_PAGE_SIZE) -> SearchResults: """ Returns a page of the reviews for the specified movie. Page numbers start from zero. """ reviews = repo.get_reviews_for_movie(movie, page_number, page_size) hits = repo.get_number_of_reviews_for_movie(movie) pages = repo.get_number_of_review_pages_for_movie(movie, page_size) return SearchResults(reviews, hits, page_number, pages)
def get_user_movies(repo: AbstractRepository, user: User, page_number: int, page_size: int = DEFAULT_PAGE_SIZE) -> SearchResults: """ Returns a page of a user's watchlist and watched movies. Page numbers start from zero. """ movies = repo.get_movies_for_user(user, page_number, page_size) hits = repo.get_number_of_movies_for_user(user) pages = repo.get_number_of_movie_pages_for_user(user, page_size) return SearchResults(movies, hits, page_number, pages)
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 articles. 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)
def add_user(repo: AbstractRepository, username: str, password: str) -> None: """ Adds the given user to the given repository. """ # Check that the given username is available. if check_if_user_exists(repo, username): 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 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_articles(quantity, repo: AbstractRepository): article_count = repo.get_number_of_articles() 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_articles_by_id(random_ids) return articles_to_dict(articles)
def change_username(repo: AbstractRepository, user: User, username: str) -> None: """ Sets the given user's username to the given username. Raises: NameNotUniqueException: if the given username is already taken. """ # Check that the given username is available. if check_if_user_exists(repo, username): raise NameNotUniqueException repo.change_username(user, username)
def user_register(username: str, password: str, repo: AbstractRepository): user = repo.get_user(username) messages = {'state': False} if user is not None: messages['error'] = 'The user has been registered' return messages else: user = User(username, password) repo.add_user(user) messages['state'] = True messages['error'] = 'register successfully' return messages '''
def add_review(movie_id: int, review_text: str, user: User, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException if user is None: raise UnknownUserException # Create review. review = make_review(review_text, user, movie) # Update the repository. repo.add_review(review)
def get_movie_by_rank(rank, repo: AbstractRepository): movie = repo.get_movie_by_rank(target_rank=rank) movies_dto = list() prev_rank = next_rank = None if movie.rank > 0: prev_rank = repo.get_previous_movie_by_rank(movie) next_rank = repo.get_next_movie_by_rank(movie) # Convert Movies to dictionary form. movies_dto = movie_to_dict(movie) return movies_dto, prev_rank, next_rank
def get_articles_by_date(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_articles_by_date(target_date=date) articles_dto = list() prev_date = next_date = None if len(articles) > 0: prev_date = repo.get_date_of_previous_article(articles[0]) next_date = repo.get_date_of_next_article(articles[0]) # Convert Articles to dictionary form. articles_dto = articles_to_dict(articles) return articles_dto, prev_date, next_date
def authenticate_user(username: str, password: str, repo: AbstractRepository) -> None: authenticated = False user = repo.get_user(username) if user: authenticated = check_password_hash(user.password, password) if not authenticated: raise AuthenticationException
def get_movie(movie_rank: int, repo: AbstractRepository): movie = repo.get_movie(movie_rank) if movie is None: raise NonExistentArticleException return movie_to_dict(movie)
def get_comments_for_article(article_id, repo: AbstractRepository): article = repo.get_article(article_id) if article is None: raise NonExistentArticleException return comments_to_dict(article.comments)
def get_movies_by_rank(rank_list, repo: AbstractRepository): movies = repo.get_movies_by_rank(rank_list) # Convert Articles to dictionary form. movies_as_dict = movies_to_dict(movies) return movies_as_dict
def add_review(movie_rank: int, review_text: str, username: str, repo: AbstractRepository, review_int: int): # Check that the article exists. movie = repo.get_movie(movie_rank) if movie is None: raise NonExistentArticleException user = repo.get_user(username) if user is None: raise UnknownUserException # Create comment. review = make_review(review_text, user, movie, review_int) # Update the repository. repo.add_review(review)
def add_comment(article_id: int, comment_text: str, username: str, repo: AbstractRepository): # Check that the article exists. article = repo.get_article(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_comment(comment)
def check_if_user_exists(repo: AbstractRepository, username: str) -> bool: """ Returns True if a user with the given name exists in the given repository otherwise False. """ try: _ = repo.get_user(username) except ValueError: return False return True
def get_articles_by_id(id_list, repo: AbstractRepository): articles = repo.get_articles_by_id(id_list) # Convert Articles to dictionary form. articles_as_dict = articles_to_dict(articles) return articles_as_dict
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)
def get_article(article_id: int, repo: AbstractRepository): article = repo.get_article(article_id) if article is None: raise NonExistentArticleException return article_to_dict(article)
def get_reviews_for_movie(movie_id, repo: AbstractRepository): movie = repo.get_movie(movie_id) if movie is None: raise NonExistentMovieException return reviews_to_dict(movie.reviews)
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) return movies_as_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: raise UnknownUserException # Create comment. comment = make_review(comment_text, user, movie) # Update the repository. repo.add_review(comment)
def get_movies_per_actor(repo: AbstractRepository, actors: List[Actor]) -> Dict[Actor, int]: """ Returns a dict mapping Actors to the number of movies in the given repository with that actor. """ movies: Dict[Actor, int] = {} for actor in actors: movies[actor] = repo.get_number_of_movies(actors=[actor]) return movies
def add_review(movie_id: str, username: str, comment: str, rating: int, repo: AbstractRepository) -> None: movie = repo.get_movie_by_id(movie_id) if movie: review = Review(movie, username, comment, rating) movie.add_review(review) if has_app_context(): _save_reviews_to_disk(current_app.config['REVIEW_DATA_PATH'], review)