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_ids = random.sample(range(1, movie_count), quantity) movies = repo.get_movies_by_id(random_ids) return movies_to_dict(movies)
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() movies = [] if quantity >= movie_count: quantity = movie_count - 1 random_ids = random.sample(range(1, movie_count), quantity) for id in random_ids: movie = repo.get_movie(id) movies.append(movie) return movies_to_dict(movies)
def get_movie(movie_id: int, repo: AbstractRepository): movie = repo.get_movie(movie_id) if movie is None: raise NonExistentArticleException return movie_to_dict(movie)
def add_review(movie_id: int, review_text: str, username: str, repo: AbstractRepository): # Check that the article exists. movie = repo.get_movie(movie_id) 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) # Update the repository. repo.add_review(review)
def get_reviews_for_movie(movie_id, repo: AbstractRepository): movie = repo.get_movie(movie_id) if movie is None: raise NonExistentArticleException return reviews_to_dict(movie.review)
def get_movies_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) movies = repo.get_movies_by_date(target_date=date) 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 Articles to dictionary form. movies_dto = movies_to_dict(movies) return movies_dto, prev_date, next_date
def allocate(line: OrderLine, repo: AbstractRepository, session) -> str: batches = repo.list() if not is_valid_sku(line.sku, batches): raise InvalidSku(f'Invalid sku {line.sku}') batchref = model.allocate(line, batches) session.commit() return batchref
def get_movie_by_id(id_list, repo: AbstractRepository): movies = repo.get_movies_by_id(id_list) # Convert Articles to dictionary form. movies_as_dict = movies_to_dict(movies) return movies_as_dict
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_movie_by_id(id_list, repo: AbstractRepository): movie_list = [] for id in id_list: movie = repo.get_movie(id) movie_list.append(movie) movies_as_dict = movies_to_dict(movie_list) return movies_as_dict
def add_review(movie_id: int, review_text: str, username: str, rating: int, repo: AbstractRepository): # Check that the movie exists. movie = repo.get_movie(movie_id) if movie is None: raise NonExistentArticleException user = repo.get_user(username) if user is None: raise UnknownUserException # Create review. review = Review(movie, review_text, rating) review.user = user review.user.add_review(review) movie.add_review(review) # Update the repository. repo.add_review(review)
def allocate( orderid: str, sku: str, qty: int, repo: AbstractRepository, session ) -> str: line = OrderLine(orderid, sku, qty) batches = repo.list() if not is_valid_sku(line.sku, batches): raise InvalidSku(f'Invalid sku {line.sku}') batchref = model.allocate(line, batches) session.commit() return batchref
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_genre_names(repo: AbstractRepository): genres = repo.get_genres() genre_names = [genre.genre for genre in genres] return genre_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_user(username: str, repo: AbstractRepository): user = repo.get_user(username) if user is None: raise UnknownUserException return user_to_dict(user)
def get_last_movie(repo: AbstractRepository): movie = repo.get_last_movie() return movie_to_dict(movie)
def add_batch( ref: str, sku: str, qty: int, eta: Optional[date], repo: AbstractRepository, session, ): repo.add(model.Batch(ref, sku, qty, eta)) session.commit()
def get_movie_by_genre(genre_name, repo: AbstractRepository): movies = repo.get_movie_by_genre(genre_name) movies_as_dict = movies_to_dict(movies) return movies_as_dict
def get_movie_ids_for_director(director_name, repo: AbstractRepository): movie_ids = repo.get_movie_ids_for_director(director_name) return movie_ids